tags:

views:

136

answers:

6

codes:

public class Main{
    public static void main(String[] a){
        long t=24*1000*3600;
        System.out.println(t*25);
        System.out.println(24*1000*3600*25);
    }
}

THe print out is :

2160000000

-2134967296

Why?


Thanks for all the replays.

That's to say, the only way is using L after the number?

I have tried the (long)24*1000*3600*25, it is also negative.

I got it.

+6  A: 

In the first case you are printing a long but in the second, you are printing it as int.

And int has a range from: -2^31 to 2^31 - 1 which is just below what you are calculating (int max: 2147483647 you: 2160000000) so you overflow the int to the negative range.

You can force the second one to use long as well:

System.out.println(24L*1000*3600*25);
lasseespeholt
+8  A: 

You reached the max of the int type which is Integer.MAX_VALUE or 2^31-1. It wrapped because of this, thus showing you a negative number.

For an instant explanation of this, see this comic:

alt text

Alberto Zaccagni
+1 for xkcd....
The Elite Gentleman
Funny,however I can not understand this comic..:(. But I maybe know the meaning,it show the Data overflow? Isn't it?
hguser
Yes, it shows the sheeps count going up until 32,767, then when the overflow occurs the count goes on, but starting from the lower limit. To me the metaphore of the circle helps a lot in understanding this.
Alberto Zaccagni
You need to compile in 16 bit mode for that comic to work. Or use shorts.
JeremyP
+2  A: 

Integral literals are treated as type int by default. 24*1000*3600*25 is greater than Integer.MAX_VALUE so overflows and evaluates to -2134967296. You need to explicitly make one of them a long using the L suffix to get the right result:

System.out.println(24L*1000*3600*25);
dogbane
+5  A: 

To explain it clearly,

System.out.println(24*1000*3600*25);

In the above statement are actually int literals. To make treat them as a long literal you need to suffix those with L.

System.out.println(24L*1000L*3600L*25L);

Caveat, a small l will suffice too, but that looks like capital I or 1, sometimes. Capital I doesn't make much sense here, but reading that as 1 can really give hard time. Furthermore, Even sufficing a single value with L will make the result long.

Adeel Ansari
a small `l` looks more like the digit `1`.
dogbane
@dogbane: Oh! Yeah, in most programming editors, its more similar to `1`. But I am not that wrong in suggesting that, I suppose. It happens when your font is `Arial`. Try that out, both would be ditto of each other.
Adeel Ansari
note that only one numbers needs to be `long`; 24L*1000*36000*25 will also work because the final result will be the length of the maximum equation member's length, in this case it's `long`. This is also true in almost every programming languages. Note that sometimes you'll need have a double so you don't loose any bit, ex: (long) (1d * someLong * someLong / someLong)
Yanick Rochon
+3  A: 

You should suffix the numbers with 'l'. Check the snippet below:

   public static void main(String[] a){
        long t=24*1000*3600;
        System.out.println(t*25);
        System.out.println(24l*1000l*3600l*25l);
    }
Sagar V
A: 

If you want to do mathematical operations with large numerical values without over flowing, try the BigDecimal class.

Let's say I want to multiply

200,000,000 * 2,000,000,000,000,000,000L * 20,000,000

int testValue = 200000000;
System.out.println("After Standard Multiplication = " +
                                                       testValue * 
                                                       2000000000000000000L * 
                                                       20000000);

The value of the operation will be -4176287866323730432, which is incorrect.

By using the BigDecimal class you can eliminate the dropped bits and get the correct result.

int testValue = 200000000;        
System.out.println("After BigDecimal Multiplication = " +
                              decimalValue.multiply(
                              BigDecimal.valueOf(2000000000000000000L).multiply(
                              BigDecimal.valueOf(testValue))));

After using the BigDecimal, the multiplication returns the correct result which is

80000000000000000000000000000000000

dinidu