4
public class Test {

    public static void main(String[] args) {    
        int sum=0;
        for(int i=1;i<10;i++)
            sum = sum+i*i*i*i*i*i*i*i*i*i;
        System.out.println(sum);                

    }    
}

OUTPUT:619374629

    for(int i=1;i<10;i++)
        sum = sum+i*i*i*i*i*i*i*i*i*i*i;
    System.out.println(sum);        

       OUTPUT:
        -585353335

In the second output i thought the integer range crossed.But why it is giving -ve number.It need to give me an error.What is the reason for this behaviour?

Thanks in advance...

PSR
  • 39,804
  • 41
  • 111
  • 151
  • Why downvote.give me the reason who down voted this – PSR Jun 03 '13 at 04:03
  • 3
    with out saying any reason giving the downvote is not correct – PSR Jun 03 '13 at 04:04
  • 3
    I didn't downvote, but I understand why they might: this isn't a real question about solving a real problem. It's just a basic misunderstanding of programming 101. Integer overflow doesn't throw exceptions in Java. – Lee Daniel Crocker Jun 03 '13 at 04:11
  • @LeeDanielCrocker basically most of questions here are based on a "misunderstanding" at some point... Making a comment to point to a duplicate is Ok (as there are tons on questions like this one, and a simple search would yields results - that could deserve -1). But some people need to be taught that *int* (Java) has only 32 bits, and doesn't throw exceptions when overflowing - while div by zero does... – Déjà vu Jun 03 '13 at 04:13
  • If that misunderstanding were in code that actually does something, I'd agree. But this isn't real code. – Lee Daniel Crocker Jun 03 '13 at 04:17
  • Questions which have been answers many times before and tend to get down voted. You should always try to search for the answer first. Personally, I don't like down voting and prefer to comment. – Peter Lawrey Jun 03 '13 at 07:05

4 Answers4

11

You have overflowed the size of a 32 bit integer.

Consider what happens when i is equal to 10:

sum = sum + 100000000000 //1 with 11 zeroes

But the maximum positive number that can be stored in a 32 bit integer is only about 2 billion (2 with nine zeroes).

In fact, it gets even worse! The intermediate calculations will be performed with limited precision, and as soon as the multiplication of 10*10*10*10... overflows, then the 10s will be being multiplied with a weird negative number, and be already wrong.

So the number you end up with is not seeming to follow any rules of arithmetic, but in fact it makes perfect sense once you know that primitive integers have limited storage.

The solution is to use 64-bit long and hope you don't overflow THAT too, if you do then you need BigInteger.

Patashu
  • 21,443
  • 3
  • 45
  • 53
6

Java defines integer math as signed 2s-complement mod 2^32 (for int) and 2^64 (for long). So any time that the result of an int multiply is 2^31 or higher, it wraps around and becomes a negative number. That's just the way integer math works in java.

Java spec

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

You are using the primitive type. So, when an integer overflows, it will only print out the bits contained in it which is negative. If you want error, try Integer.

yeh
  • 1,496
  • 14
  • 35
1

As you predicted, you passed the integer range, though causing infinite values (as the sign [the highest bit] gets touched).

Mordechai
  • 15,437
  • 2
  • 41
  • 82