Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Variables, Data Types & Operators

What is integer overflow in Java, why is it silent, and how do you guard against it in production?

Level III · MidVariables, Data Types & Operators
Short answer

Java integer arithmetic wraps around modulo 2^32 (or 2^64) with no exception, so Integer.MAX_VALUE + 1 silently becomes Integer.MIN_VALUE.

Explanation

It is silent because the JLS defines two's-complement wraparound rather than a trap. In code that handles untrusted sizes, durations, or financial sums, use Math.addExact / multiplyExact (which throw on overflow) or widen to long, and watch for the midpoint bug (low+high)/2 in binary search.

Common mistakes
  • Assuming overflow throws an exception — it silently wraps around.
  • Not using Math.addExact / multiplyExact or long when values can grow.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Variables, Data Types & Operators.

Practise free

More on Variables, Data Types & Operators