Reputation: 35
I am simply trying to add numbers from 1 to Integer.MAX_VALUE, but in output I'm getting nothing. 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?
Upvotes: 0
Views: 391
Reputation: 37645
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.
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();
Upvotes: 0
Reputation: 2950
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
}
}
Upvotes: 0
Reputation: 6274
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.
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
. But if you only had a maximum of 2 digits allowed, then you carry the one twice, and end up with 00
. In computers, there is a limited numbers of bits (binary digits) allowed, usually 32 or 64.
You can read more about it here:
Wiki Integer Overflow
Signed vs Unsigned Integers
Upvotes: 2
Reputation: 533492
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
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.
Upvotes: 3