What happens when you increment an integer beyond its max value?

From the Java Language Specification section on integer operations:

The built-in integer operators do not
indicate overflow or underflow in any
way.

The results are specified by the language and independent of the JVM version: Integer.MAX_VALUE + 1 == Integer.MIN_VALUE and Integer.MIN_VALUE - 1 == Integer.MAX_VALUE. The same goes for the other integer types.

The atomic integer objects (AtomicInteger, AtomicLong, etc.) use the normal integer operators internally, so getAndDecrement(), etc. behave this way as well.

Leave a Comment