Java Integers Min_Value 负数然后比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12535095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:17:34  来源:igfitidea点击:

Java Integers Min_Value negative then compare

javavariablesintegernegative-number

提问by Quinma

I have a test tomorrow and I cant understand my books explanation, I appreciate the help:

我明天有考试,我无法理解我的书籍解释,我很感激帮助:

public class TestClass{
      public static void main(String[] args) throws Exception{
            int a = Integer.MIN_VALUE;
            int b = -a;
            System.out.println( a+ "   "+b);
      }
}

Output: -2147483648 -2147483648

输出: -2147483648 -2147483648

Why does this print 2 negative numbers of the same magnitude and not a positive and negative?

为什么这会打印 2 个大小相同的负数而不是正数和负数?

回答by assylias

Because of silent integer overflow: Integer.MIN_VALUEis -2^31and Integer.MAX_VALUEis 2^31-1, so -Integer.MIN_VALUEis 2^31, which is Integer.MAX_VALUE + 1, which by definition is too large for an integer. So it overflows and becomes Integer.MIN_VALUE...

由于无声整数溢出:Integer.MIN_VALUEis-2^31Integer.MAX_VALUEis 2^31-1,所以-Integer.MIN_VALUEis 2^31, which is Integer.MAX_VALUE + 1,根据定义,对于整数来说太大了。所以它溢出并变成Integer.MIN_VALUE......

You can also check that:

您还可以检查:

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

prints the same thing.

打印同样的东西。

More technically, the result is defined by the Java Language Specification #15.18.2:

从技术上讲,结果由Java 语言规范 #15.18.2 定义

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.

如果整数加法溢出,则结果是数学和的低位,以某种足够大的二进制补码格式表示。如果发生溢出,则结果的符号与两个操作数值的数学和的符号不同。

回答by Jo?o Mendes

Basically, because Integer.MAX_VALUEis actually only 2147483647, so -Integer.MIN_VALUE, which would be +2147483648, actually overflows the capacity of the internal binary representation of integers. Thus, the result "loops around" back to Integer.MIN_VALUE, or -2147483648.

基本上,因为Integer.MAX_VALUE实际上只有 2147483647,所以-Integer.MIN_VALUE,即 +2147483648,实际上溢出了整数内部二进制表示的容量。因此,结果“循环”回到Integer.MIN_VALUE, 或 -2147483648。

If you did long b = -((long)a);instead, you would get the expected result.

如果你这样做了long b = -((long)a);,你会得到预期的结果。

回答by user2332921

To show this even more clearly:

为了更清楚地表明这一点:

<br>
 Integer.MIN_VALUE is -2^31 = -2147483648<br>
 Integer.MAX_VALUE is 2^31-1 = 2147483647 
 /*notice this is 1 less than the negative value above*/
 <br>

1Integer.MAX_VALUEcan not take 2147483648. This is too large number for Integer by exactly 1. This causes the number to go back on the scale from max value back to starting poing which is the min value.

1Integer.MAX_VALUE不能拿2147483648。这对于整数来说太大了,正好是 1。这会导致数字从最大值回到起始点,这是最小值。