why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?

后端 未结 8 1851
鱼传尺愫
鱼传尺愫 2020-11-27 06:57

System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);

is true.

I understand that integer in Java is 32 bit and can\'t go above 23

相关标签:
8条回答
  • 2020-11-27 07:38

    Because the integer overflows. When it overflows, the next value is Integer.MIN_VALUE. Relevant JLS

    If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.

    0 讨论(0)
  • 2020-11-27 07:38

    The same reason why the date changes when you cross the international date line: there's a discontinuity there. It's built into the nature of binary addition.

    0 讨论(0)
提交回复
热议问题