Course contentsShow
← Java
Lesson 95 of 2,8703. Operators and ExpressionsFree lesson

Arithmetic Overflow and Wraparound Behavior

Learn how exceeding Integer.MAX_VALUE wraps to negative values without exceptions or warnings.

Arithmetic Overflow and Wraparound Behavior

You'll learn what happens when integer arithmetic exceeds Java's built-in limits—and why it fails silently.

The Silent Limit

When you perform arithmetic with int or other integer types in Java, the values are constrained to a fixed range. For int, that's roughly -2.1 billion to +2.1 billion (Integer.MIN_VALUE to Integer.MAX_VALUE). But here's the surprising part: Java doesn't stop you or warn you when you exceed these limits. Instead, the value "wraps around" to the opposite end of the range.

Think of it like an old-style odometer that only has room for so many digits. When a car hits 999,999 miles and you drive one more mile, it rolls over to 000,000. Java's integers work the same way.

How Wraparound Works

When you add 1 to Integer.MAX_VALUE (2,147,483,647), you don't get 2,147,483,648. You get Integer.MIN_VALUE (-2,147,483,648). The value wraps from the maximum positive value straight to the maximum negative value.

int max = 2147483647;  // Integer.MAX_VALUE
int overflow = max + 1;
// overflow is now -2147483648 (Integer.MIN_VALUE)

Similarly, subtracting from Integer.MIN_VALUE wraps to the positive side:

int min = -2147483648;  // Integer.MIN_VALUE
int underflow = min - 1;
// underflow is now 2147483647 (Integer.MAX_VALUE)

Why This Matters

Java provides no exceptions, no errors, and no warnings when overflow occurs. Your program continues running with incorrect values, which can lead to serious bugs—especially in financial calculations, array indexing, or loop counters.

Key Takeaway: Integer overflow in Java causes values to wrap around silently from max to min (or vice versa) without any runtime error, making it critical to validate your calculations stay within safe bounds.