简体   繁体   English

为什么Integer.MAX_VALUE + 1比Integer.MAX_VALUE小?

[英]Why is Integer.MAX_VALUE+1 smaller than Integer.MAX_VALUE?

I am simply trying to add numbers from 1 to Integer.MAX_VALUE, but in output I'm getting nothing. 我只是想将数字从1添加到Integer.MAX_VALUE,但是在输出中却什么也没得到。 The program is struck in between. 该程序介于两者之间。 Below is the class that I created. 下面是我创建的类。

 public class Test {
    public static void main(String args[]) {
        long sum = 0;
        int start_value = 1;
        long end_value = Integer.MAX_VALUE;
        while(start_value <= end_value){
            sum += start_value;
            start_value++;
        }
        System.out.println(sum);
    }
 }

Do anybody have any idea why this is hanging. 有人知道为什么这挂了吗? This program is never completed. 该程序永远不会完成。

How to solve this type of problem? 如何解决这类问题?

It should never complete as you have an infinite loop. 它永远不会完成,因为您有无限循环。

Your loop is effectively 您的循环有效

while(start_value <= Integer.MAX_VALUE) {

but the Integer.MAX_VALUE is the largest by definition so it is effective 但根据定义,Integer.MAX_VALUE最大,因此有效

while (true) {

You can change the loop to do what you need 您可以更改循环以执行所需的操作

int start_value = 0;
while(start_value < end_value) {
    start_value++;
    sum += startValue;
}

This way you can catch the problem before it fails. 这样,您就可以在问题失败之前及时发现问题。

An obtuse solution might be 一个钝的解决方案可能是

for (int i = 1; i > 0; i++)
    sum += i;

This would stop when i overflows. i溢出时,这将停止。

It's because of something called integer Overflow. 这是由于所谓的整数溢出。 When you add 1 to the MAX_VALUE , you get MIN_VALUE if you are using signed integers, or 0 if using unsigned integers. 当您将1加到MAX_VALUE ,如果您使用带符号整数,则将获得MIN_VALUE 0如果使用无符号整数,则将获得0

Briefly explained, when you add 1 to 99 for example, you have to carry the 1 twice to end up on the third digit: 100 . 简要说明一下,例如,当您将1加到99 ,您必须两次携带1才能出现在第三位数字: 100 But if you only had a maximum of 2 digits allowed, then you carry the one twice, and end up with 00 . 但是,如果您最多只允许输入2位数字,那么请携带两位数,最后以00结束。 In computers, there is a limited numbers of bits (binary digits) allowed, usually 32 or 64. 在计算机中,允许的位数(二进制数字)有限,通常为32或64。

You can read more about it here: 你可以在这里读更多关于它的内容:
Wiki Integer Overflow Wiki整数溢出
Signed vs Unsigned Integers 有符号与无符号整数

In addition to the other answers where you're stuck in an infinite loop because the the max integer value is never reached, you should maybe add a print inside the loop so you can see progress: 除了由于从未达到最大整数值而使您陷入无限循环的其他答案之外,您可能还应该在循环内添加打印,以便可以看到进度:

public class Test {
public static void main(String args[]) {
    long sum = 0;
    int start_value = 1;
    int end_value = Integer.MAX_VALUE - 1;//Just in case
    while(start_value <= end_value){
        sum += start_value;
        start_value++;

        //Print every 100 loops, change this if it prints too often
        if (start_value % 100 == 0){
           System.out.println("integer at: " + start_value + ", sum: " + sum);
        }
    }
    System.out.println(sum + Integer.MAX_VALUE);//Since we only went to Integer.MAX_VALUE -1

    }
}

For the reasons indicated in the other answers, it can be quite tricky to iterate over intervals bounded by maximum or minimum values for a primitive type. 由于其他答案中指出的原因,在原始类型的最大值或最小值所限制的时间间隔内进行迭代可能非常棘手。

Java 8 allows a new solution to this, because IntStream.rangeClosed and LongStream.rangeClosed can be used. Java 8允许对此提出新的解决方案,因为可以使用IntStream.rangeClosedLongStream.rangeClosed

In your case, you can do 就您而言,您可以

IntStream.rangeClosed(1, Integer.MAX_VALUE).mapToLong(i -> i).sum();

or just 要不就

LongStream.rangeClosed(1, Integer.MAX_VALUE).sum();

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

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