➗ Operators & Expressions · Beginner

Arithmetic operators in Java

+ - * / % on numbers.

🧩 The mysteryWhat's 7 + 3 * 2? If you said 20, Java disagrees. Let's learn the rules Java uses to do math.

The five operators

Java has five arithmetic operators: + add, - subtract, * multiply, / divide and % remainder. They work on every numeric type.

int a = 10, b = 4;
a + b   // 14
a - b   // 6
a * b   // 40
a / b   // 2 (more on this soon!)
a % b   // 2

% is the remainder

% gives what's left over after division. 10 % 3: 3 fits into 10 three times (9), leaving 1. 10 % 4 leaves 2. It's perfect for even/odd checks and wrapping around.

* / % before + -

Just like school math, *, / and % bind tighter than + and -. Operators on the same level go left to right. Parentheses override everything.

7 + 3 * 2       // 13
(7 + 3) * 2     // 20
1 + 2 * 3 % 4   // 1 + ((2*3) % 4) = 3
🔮 Predict it

Your turn

What does this print?

System.out.println(2 + 4 * 3 % 5);
  1. 4
  2. 14
  3. 2
Show the answer

* and % share a level and go left to right: 4 * 3 = 12, then 12 % 5 = 2. Finally 2 + 2 = 4.

🔮 Predict it

Your turn

Can you take a remainder of a decimal?

System.out.println(9.5 % 3);
  1. 0.5
  2. 0
  3. Compile error
Show the answer

Yes! % works on doubles too. 3 fits into 9.5 three times (9), leaving 0.5.

Say what you mean

✗ Wrong total
int cost = price + shipping * 2;

* goes first, so only the shipping is doubled.

✓ Right total
int cost = (price + shipping) * 2;

Parentheses add first, then double the whole order.

💼 In the real world

In real projects

Precedence slips hide in pricing, discounts and game physics, and they compile perfectly. Professional code adds parentheses whenever a reader might hesitate, because they cost nothing and prevent misreadings.

Key takeaways

  1. + - * / % work on all numeric types
  2. * / % before + -, then left to right
  3. % is remainder: 10 % 3 is 1
  4. Parentheses override precedence
🤯 Did you know?

% runs clocks and calendars: (hour + 5) % 12 wraps around a clock face, and day % 7 cycles through the days of the week.

Practice questions

What does this print?

System.out.println(7 + 3 * 2);
  1. 20
  2. 16
  3. 12
  4. 13
Check your answer

13. * binds tighter than +, so 3 * 2 = 6 is computed first, then 7 + 6 = 13.

What does this print?

System.out.println(7.5 % 2);
  1. 1
  2. 3.75
  3. Compile error
  4. 1.5
Check your answer

1.5. % also works on doubles: 2 fits into 7.5 three times (6), leaving 1.5.

Next: in Java, 7 / 2 is 3. Not 3.5. And -7 % 2 is -1. Integer division plays by its own rules.