📦 Variables & Types · Beginner

Integer ranges & overflow in Java

Fixed ranges, silent wrap-around, Math.addExact for checked arithmetic.

🧩 The mysteryAdd 1 to the biggest possible int and you get... a huge negative number. No error, no warning. Welcome to overflow.

Every box has limits

Each integer type has a fixed range. For a signed type with n bits, the range is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. Remember: int is about ±2.1 billion, long about ±9.2 quintillion.

byte  -128 .. 127
short -32,768 .. 32,767
int   -2,147,483,648 .. 2,147,483,647
long  about ±9.2 quintillion

The odometer effect

Like a car odometer rolling over from 999999 to 000000, an int that goes past its maximum wraps around to its minimum. Java does this silently: no exception, no warning.

🔮 Predict it

Your turn

What does this print?

int max = Integer.MAX_VALUE;
System.out.println(max + 1);
  1. 2147483648
  2. -2147483648
  3. Throws ArithmeticException
Show the answer

One past the largest int wraps to the smallest int, Integer.MIN_VALUE. No exception. 2147483648 simply doesn't exist as an int.

Make overflow loud

Math.addExact, Math.subtractExact and Math.multiplyExact do the same arithmetic but throw ArithmeticException ("integer overflow") instead of wrapping. Use them when a wrong number would be worse than a crash.

Math.addExact(2, 3)        // 5
Math.addExact(Integer.MAX_VALUE, 1)
// ArithmeticException
🔮 Predict it

Your turn

100,000 × 100,000 is ten billion. What happens?

System.out.println(
    Math.multiplyExact(100_000, 100_000));
  1. 10000000000
  2. 1410065408
  3. Throws ArithmeticException
Show the answer

Ten billion doesn't fit in an int, so multiplyExact throws instead of quietly wrapping to the wrong number (which would have been 1410065408).

The long that didn't help

✗ Overflows
long ms = 24 * 60 * 60 * 1000 * 1000;
// 500654080

All the numbers are ints, so the math is done in int and overflows before the result is stored in the long.

✓ Correct
long ms = 24L * 60 * 60 * 1000 * 1000;
// 86400000000

24L is a long, so the whole calculation is done in long arithmetic.

💼 In the real world

In real projects

YouTube's view counter was a 32-bit signed int. In 2014 "Gangnam Style" approached 2,147,483,647 views and YouTube upgraded to 64-bit. Money totals, file sizes and millisecond timestamps belong in long.

Key takeaways

  1. int: −2,147,483,648 to 2,147,483,647
  2. Overflow wraps around silently
  3. Math.addExact / multiplyExact throw on overflow
  4. Arithmetic on ints stays int, even if stored in a long

💡 Like a car odometer rolling over from 999999 to 000000.

🤯 Did you know?

In 2015 the FAA ordered Boeing 787s to be power-cycled regularly: a counter in the generator control units could overflow after 248 days of continuous power and shut them down.

Practice questions

What does this print?

int big = Integer.MAX_VALUE;
big = big + 1;
System.out.println(big);
  1. 2147483648
  2. -2147483648
  3. 2147483647
  4. Throws ArithmeticException
Check your answer

-2147483648. One past the largest int wraps to the smallest int. No exception is thrown.

What does this print?

int max = Integer.MAX_VALUE;
System.out.println(Math.addExact(max, 1));
  1. Throws ArithmeticException
  2. -2147483648
  3. 2147483648
  4. Compile error
Check your answer

Throws ArithmeticException. Math.addExact performs the same addition but checks for overflow and throws ArithmeticException ("integer overflow") instead of wrapping.

Next: 017 isn't seventeen. Literals have secret prefixes and suffixes that change everything.