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.


If you do something like this:

int x = 2147483647;
x++;

If you now print out x, it will have the value -2147483648.

Tags:

Java

Math