How does Java handle integer underflows and overflows and how would you check for it?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Java integer arithmetic uses fixed-width two's-complement values for byte, short, int, and long. When a calculation exceeds the representable range, Java does not raise an exception by default for the basic arithmetic operators; it wraps around.
That behavior is predictable, but it is also easy to miss in business logic, counters, pricing code, and ID generation. If overflow matters, you need to check for it explicitly.
What Overflow and Underflow Look Like
For int, the maximum value is 2147483647 and the minimum is -2147483648. Adding one to the maximum wraps to the minimum, and subtracting one from the minimum wraps to the maximum.
Output:
Java is not “detecting and fixing” the overflow here. It is simply discarding the bits that no longer fit.
Use Math.addExact and Friends When Overflow Is an Error
The standard library provides checked arithmetic helpers. They throw ArithmeticException when the result does not fit.
Related methods include Math.subtractExact, Math.multiplyExact, Math.incrementExact, Math.decrementExact, and Math.toIntExact.
These methods are the best default when overflow is logically invalid and you want the code to fail loudly.
Manual Checks Before the Operation
You can also check bounds before doing the arithmetic. That is useful when you want to return a fallback value or a custom error instead of throwing.
This approach keeps control in your code, but it is also easier to get wrong than Math.addExact if you repeat it everywhere.
Narrowing Conversions Need Separate Attention
Overflow does not only happen in arithmetic. It also happens when you cast from a wider type to a narrower one.
The cast compiles, but the value is truncated. If that narrowing step matters, use Math.toIntExact instead of a plain cast.
When to Use BigInteger
If the domain allows values beyond long, then overflow checks are not enough. You need a different numeric type.
BigInteger stores arbitrarily large integers and is the right tool for cryptography, combinatorics, large counters, or any calculation where wraparound is unacceptable and range is unknown in advance.
It is slower and more verbose than primitive arithmetic, so use it when the requirements justify it.
Common Pitfalls
- Assuming Java throws an exception automatically on primitive integer overflow. Normal
+,-, and*do not. - Testing only positive overflow and forgetting negative underflow cases.
- Replacing a checked operation with a cast that silently truncates the value.
- Mixing
intandlongcarelessly and assuming the wider result is always preserved through later assignments. - Using manual guard logic everywhere instead of centralizing the check or using
Math.addExactstyle helpers.
Summary
- Java primitive integer arithmetic wraps on overflow and underflow.
- Use
Math.addExact,subtractExact, and related methods when overflow should be treated as an error. - Manual pre-checks are possible, but they require careful boundary logic.
- Narrowing casts can also lose data even when no arithmetic operator is involved.
- Use
BigIntegerwhen valid values may exceed the primitive numeric range.
Related reading
- How does Java implement hash tables?
- How does Java makes use of multiple cores?
- How does Junit Rule work?
- How does Keep-alive work with ThreadPoolExecutor?
- How does lombok work?
- How does Spring achieve IOC with autowiring?
- How does Spring Kafka BATCH ack mode work with non-batch listener?
- How does spring.kafka.consumer.auto-offset-reset works in spring-kafka

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.