did the multiplication rules change!?

M

mark.oliveira

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
 
S

shakah

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);
 
M

mark.oliveira

Thanks! That seems to do the trick. So, basically, those numbers are
being multiplied as ints (as opposed to the longs I was expecting)? I
assume the Ls tell the JVM to treat them as longs... is that accurate?

Thanks again for the help.
 
S

shakah

Thanks! That seems to do the trick. So, basically, those numbers are
being multiplied as ints (as opposed to the longs I was expecting)? I
assume the Ls tell the JVM to treat them as longs... is that accurate?

Thanks again for the help.

You have it right as far as I'm concerned ("bare" numbers are indeed
ints), though I'm sure some Usenet/Java zealot will seize this
opportunity to explain why this simplistic view isn't correct.
 
C

Chris Smith

shakah said:
You have it right as far as I'm concerned ("bare" numbers are indeed
ints), though I'm sure some Usenet/Java zealot will seize this
opportunity to explain why this simplistic view isn't correct.

No, you're exactly right.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mike Schilling

shakah said:
You have it right as far as I'm concerned ("bare" numbers are indeed
ints), though I'm sure some Usenet/Java zealot will seize this
opportunity to explain why this simplistic view isn't correct.

He'd have to be a zealot/fruitcake, because that's 100% correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top