简体   繁体   English

为什么循环迭代超出 Integer.MAX_VALUE?

[英]Why does the loop iterate beyond Integer.MAX_VALUE?

Can someone explain why this code is an infinite loop on Java 7,8?有人可以解释为什么这段代码是 Java 7,8 上的无限循环吗? The loop should stop when i = Integer.MAX_VALUE + 1 but it seems to deny the limit.i = Integer.MAX_VALUE + 1时循环应该停止,但它似乎拒绝限制。 Why does this happen?为什么会发生这种情况?

public static void main(String[] args) {
    for (int i = Integer.MAX_VALUE - 100; i <= Integer.MAX_VALUE; i++);
}
  • The value Integer.MAX_VALUE changes from 2147483647 to -2147483648 ie Integer.MIN_VALUE when it overflows. Integer.MAX_VALUE值在溢出时从2147483647变为-2147483648Integer.MIN_VALUE
  • Integer overflow occurs when the arithmetic operation tries to create value that is beyond reach of data type value range.当算术运算尝试创建超出数据类型值范围的值时,会发生整数溢出
  • In java integers have range from -2147483648 to 2147483647 .在 java 中,整数的范围从-21474836482147483647
  • In for loop when i becomes 2147483647 then i++ operation will try to increment its value by 1 which is not possible because range is only till 2147483647 .在 for 循环中,当 i 变为2147483647 i++ 操作将尝试将其值增加 1,这是不可能的,因为范围仅到2147483647
  • So instead of having its value as 2147483648 it will go back to starting value ie -2147483648因此,它的值不是2147483648而是返回到起始值,即-2147483648
  • Hence i <= Integer.MAX_VALUE always becomes true .因此i <= Integer.MAX_VALUE总是变为true
  • As condition never becomes false , loop will continue till infinity.由于条件永远不会变为false ,循环将一直持续到无穷大。

You can check value overflow by simply writing your code as below.您可以通过简单地编写如下代码来检查值溢出。

for (int i = Integer.MAX_VALUE - 100; i <= Integer.MAX_VALUE; i++){
        System.out.println(i);
}

You will see below output.您将看到以下输出。

2147483645
2147483646
2147483647
-2147483648
-2147483647
-2147483646

Look at看着

 i <= Integer.MAX_VALUE

This is always true.这总是正确的。 If i = Integer.MAX_VALUE+1 then it will overflow, and become negative.如果 i = Integer.MAX_VALUE+1 那么它就会溢出,变成负数。

Do this:做这个:

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

Because when i reaches being equal to Integer.MAX_VALUE then it becomes Integer.MAX_VALUE + 1 which becomes negative因为当i达到等于Integer.MAX_VALUE然后它变成Integer.MAX_VALUE + 1变成负数

Better would be更好的是

for (long i = Integer.MAX_VALUE - 100; i <= Integer.MAX_VALUE; i++)

You expect the loop to run till i <= Integer.MAX_VALUE and stop as soon as it becomes Integer.MAX_VALUE + 1 , ie, 2147483648 .您希望循环运行到i <= Integer.MAX_VALUE并在它变为Integer.MAX_VALUE + 1立即停止,即2147483648 However, THAT DOES NOT HAPPEN because whenever you add something to Integer.MAX_VALUE, say x , the calculation takes the form:但是,这不会发生,因为每当您向 Integer.MAX_VALUE 添加某些内容时,例如x ,计算采用以下形式:

Integer.MIN_VALUE + x - 1;

Since int cannot store beyond 2147483647 (and of course beyond -2147483648 ), this thing is done to make sure that there's no overflow.由于int不能存储超过2147483647 (当然也超过-2147483648 ),所以这样做是为了确保没有溢出。 So, after 2147483647 , i becomes -2147483648 , which is obviously less than +2147483647 and thus, it is an infinite loop .因此,在2147483647之后, i 变为-2147483648 ,这显然小于+2147483647 ,因此,它是一个无限循环

If you want the loop to iterate in that range (ie, you just wanna use the values), you can make use of a type cast between long and int like this:如果您希望循环在该范围内迭代(即,您只想使用这些值),您可以使用longint之间的类型转换,如下所示:

for(long i = (long)Integer.MAX_VALUE - 100; i<= (long)Integer.MAX_VALUE; i++){
    //to use i as an int, type cast from (long) to (int)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM