When you add 1 to Integer.MAX_VALUE , it overflows and wraps up to Integer.MIN_VALUE .
This is because Java uses two additions to represent integers. Example in 4 bits:
0000 : 0 0001 : 1 ... 0111 : 7 (max value) 1000 : -8 (min value) ... 1110 : -2 1111 : -1
So, when you add 1 to 0111 (max), it goes into 1000 , and the minimum. Expand this idea to 32 bits and it works the same.
As for why your long also shows the wrong result, this is because it performs the addition on int , and then implicitly converts to long . You need to do:
long l = (long) Integer.MAX_VALUE + 1 System.out.println(l);
source share