×

java · level 1 · unit 3 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Java is a calculator that never gets tired.

Each does one job: + adds, - takes away, * multiplies and / divides. Brackets go first, then * and /, then + and -, as in maths lessons.

Watch out with /. When both numbers are int, Java gives a whole number and drops the rest, so 7 / 2 is 3. Make one of them a double, like 7.0 / 2, to get 3.5.

% gives the , what is left over after sharing out. Share 7 sweets between 2 friends: each gets 3, and 7 % 2 is the 1 sweet left over.

public class Main {
    public static void main(String[] args) {
        System.out.println(2 + 3 * 4);
        System.out.println((2 + 3) * 4);
        System.out.println(7 / 2);
        System.out.println(7.0 / 2);
        System.out.println(7 % 2);
    }
}

Whole-number division never rounds up, so 9 / 10 is 0.