Integer ranges & overflow in Java
Fixed ranges, silent wrap-around, Math.addExact for checked arithmetic.
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 quintillionThe 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.
Your turn
What does this print?
int max = Integer.MAX_VALUE;
System.out.println(max + 1);2147483648-2147483648Throws 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)
// ArithmeticExceptionYour turn
100,000 × 100,000 is ten billion. What happens?
System.out.println(
Math.multiplyExact(100_000, 100_000));100000000001410065408Throws 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
long ms = 24 * 60 * 60 * 1000 * 1000;
// 500654080All the numbers are ints, so the math is done in int and overflows before the result is stored in the long.
long ms = 24L * 60 * 60 * 1000 * 1000;
// 8640000000024L is a long, so the whole calculation is done in long arithmetic.
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
- int: −2,147,483,648 to 2,147,483,647
- Overflow wraps around silently
- Math.addExact / multiplyExact throw on overflow
- Arithmetic on ints stays int, even if stored in a long
💡 Like a car odometer rolling over from 999999 to 000000.
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);- 2147483648
- -2147483648
- 2147483647
- 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));- Throws ArithmeticException
- -2147483648
- 2147483648
- 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.