➗ Operators & Expressions · Beginner

The Math class in Java

Math.max/min/abs/pow/sqrt/round/floor/ceil and Math.floorMod.

🧩 The mysteryMath.abs should never return a negative number, right? Yet Math.abs(Integer.MIN_VALUE) does. Let's tour Java's math toolbox, traps included.

A static toolbox

java.lang.Math is full of static helpers: call them on the class name, no object needed. max, min and abs keep their argument type (int here). pow and sqrt always return double, even for whole results.

Math.max(3, 7)    // 7
Math.min(3, 7)    // 3
Math.abs(-4)      // 4
Math.pow(2, 3)    // 8.0
Math.sqrt(9)      // 3.0
🔮 Predict it

Your turn

What does this print?

System.out.println(Math.sqrt(25));
System.out.println(Math.max(-2, -9));
  1. 5 -2
  2. 5.0 -2
  3. 5.0 -9
Show the answer

sqrt returns a double: 5.0. max of two ints is an int, and -2 is larger than -9: -2.

floor, ceil, round

floor goes down, toward negative infinity. ceil goes up, toward positive infinity. Both return a double. round goes to the nearest whole number and returns a long (for a double argument).

Math.floor(-1.5)   // -2.0
Math.ceil(-1.5)    // -1.0
Math.round(2.4)    // 2
🔮 Predict it

Your turn

Halfway cases. What does this print?

System.out.println(Math.round(3.5));
System.out.println(Math.round(-3.5));
  1. 4 -4
  2. 4 -3
  3. 3 -3
Show the answer

Ties round toward positive infinity: 3.5 → 4, but -3.5 → -3, because -3 is the larger neighbor. It's not "away from zero", and it's not banker's rounding.

floorMod: never negative

% takes the sign of the dividend: -7 % 3 is -1. Math.floorMod(-7, 3) takes the sign of the divisor: 2. Perfect for wrapping around, like stepping back through the days of the week.

int today = 1;            // Tuesday
(today - 3) % 7           // -2 (oops)
Math.floorMod(today - 3, 7) // 5: Sat
⚠️ The trap

abs can be negative

+2,147,483,648 doesn't fit in an int, so negating Integer.MIN_VALUE overflows right back to itself. Math.absExact (Java 15+) throws ArithmeticException instead.

Math.abs(Integer.MIN_VALUE)
// -2147483648 !
Math.absExact(Integer.MIN_VALUE)
// ArithmeticException
💼 In the real world

In real projects

A classic production bug: bucket = Math.abs(hash) % n returns a negative index when the hash is MIN_VALUE, crashing once in billions of keys. Professionals write Math.floorMod(hash, n).

Key takeaways

  1. pow and sqrt return double: Math.pow(2, 3) is 8.0
  2. floor ↓, ceil ↑ (both return double)
  3. round(2.5) is 3, round(-2.5) is -2
  4. floorMod(-7, 3) is 2, while -7 % 3 is -1
🤯 Did you know?

In Java 6, Math.round(0.49999999999999994) returned 1 instead of 0, because adding 0.5 internally rounded up. The bug was fixed in Java 7.

Practice questions

What does this print?

System.out.println(Math.floor(-1.5));
System.out.println(Math.ceil(-1.5));
  1. -1.0 -2.0
  2. -2 -1
  3. -1.0 -1.0
  4. -2.0 -1.0
Check your answer

-2.0 -1.0. floor goes toward negative infinity (-2.0) and ceil toward positive infinity (-1.0). Both return doubles.

What does this print?

System.out.println(Math.round(2.5));
System.out.println(Math.round(-2.5));
  1. 3 -3
  2. 3 -2
  3. 2 -2
  4. 2 -3
Check your answer

3 -2. Math.round rounds halves toward positive infinity: 2.5 becomes 3, but -2.5 becomes -2.

World 4: Control Flow! Your programs learn to make decisions and repeat themselves with if, switch and loops. And yes, there's a bug called the "dangling else".