Re: did the multiplication rules change!?



mark.olive...@xxxxxxxxx wrote:
> Hi all,
>
> I am dumbfounded... I have the following code:
>
> long product = 1000;
> product = product*60;
> product = product*60;
> product = product*24;
> product = product*30;
> product = product*3;
>
> long product2 = (1000*60*60*24*30*3);
>
> System.out.println("product: " + product);
> System.out.println("product2: " + product2);
>
> which produces the following result:
>
> product: 7776000000
> product2: -813934592
>
> Can anyone please explain this behavior?? Am I wrong to expect the same
> value for both product and product2? If so I would really like to know
> why. Any insight will be very much appreciated.
>
> Thanks,
>
> Mark

You're just seeing int overflow in the product2 line, try:
long product2 = (1000L*60L*60L*24L*30L*3L);

.



Relevant Pages