← All lessons
Java basics · Lesson 3 of 10

Numbers and maths

Java does maths with the usual signs: + and -, * for times, / for divide. Brackets work as in school: what's inside them is done first, and otherwise * and / come before + and -.

Java
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(10 - 4 - 3);
System.out.println(1.5 * 2);
Prints
14
20
3
3.0

Whole-number division

Here's the big surprise for beginners. When both numbers are int, the answer is an int too, so the part after the decimal point is dropped, not rounded. 7 / 2 is 3. If either number is a double, you get the decimal answer.

Java
System.out.println(7 / 2);
System.out.println(7 / 2.0);
System.out.println(7.0 / 2);
System.out.println(1 / 3);
System.out.println(9 / 10);
Prints
3
3.5
3.5
0
0

The % sign gives the remainder after dividing (it's called "mod"). 7 % 2 is 1, because 7 is three 2s with 1 left over. It's more useful than it looks: a number is even when n % 2 is 0, and n % 10 is its last digit.

Java
System.out.println(7 % 2);
System.out.println(10 % 2);
System.out.println(1234 % 10);
int minutes = 135;
System.out.println(minutes / 60 + " h " + minutes % 60 + " min");
Prints
1
0
4
2 h 15 min

Shortcuts

Changing a variable by some amount is so common that it has shortcuts. x += 5 means x = x + 5, and -=, *= and /= work the same way. x++ adds one and x-- takes one away.

Java
int coins = 10;
coins += 5;
coins *= 2;
coins--;
System.out.println(coins);
Prints
29

Converting and rounding

Putting an int into a double is always safe, so Java does it for you. The other way would lose the fraction, so you must ask for it with a cast: (int) 3.99 gives 3, cutting off the fraction. To round to the nearest whole number, use Math.round.

Java
double price = 3.99;
int whole = (int) price;
System.out.println(whole);
System.out.println(Math.round(price));
int total = 17, people = 4;
System.out.println((double) total / people);
Prints
3
4
4.25

Math has the other tools you'd expect: Math.max(a, b) and Math.min(a, b) pick the bigger or smaller, Math.abs(x) drops a minus sign, Math.sqrt(x) is the square root and Math.pow(a, b) is a to the power b.

Java
System.out.println(Math.max(4, 9));
System.out.println(Math.abs(-12));
System.out.println(Math.sqrt(49));
System.out.println(Math.pow(2, 10));
Prints
9
12
7.0
1024.0

Decimals are stored in binary, so some can't be exact: 0.1 + 0.2 prints 0.30000000000000004. For everyday maths that's fine; for money, real programs count in cents with whole numbers (or use BigDecimal, in a later course).

Practice

What does this print?

System.out.println(10 / 4);
More info

Quick reference

+ - * /Add, subtract, multiply, divide. * and / come before + and -.
7 / 23: dividing two ints drops the fraction.
7 % 21: the remainder.
x += 5; x++;x = x + 5; x = x + 1.
(int) 3.93: a cast cuts off the fraction.
Math.round(x)Rounds to the nearest whole number.
Math.max / min / absThe bigger, the smaller, without the minus sign.
Math.sqrt / powSquare root; a to the power b (both give doubles).

Related courses