Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

did the multiplication rules change!?

Thread view: 
mark.oliveira@gmail.com - 10 Jan 2006 16:49 GMT
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
shakah - 10 Jan 2006 16:54 GMT
mark.olive...@gmail.com wrote:
> Hi all,
>
[quoted text clipped - 24 lines]
>
> Mark

You're just seeing int overflow in the product2 line, try:
 long product2 = (1000L*60L*60L*24L*30L*3L);
mark.oliveira@gmail.com - 10 Jan 2006 16:59 GMT
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.
shakah - 10 Jan 2006 17:13 GMT
mark.olive...@gmail.com wrote:
> 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.
Chris Smith - 10 Jan 2006 23:58 GMT
> mark.olive...@gmail.com wrote:
> > Thanks!  That seems to do the trick.  So, basically, those numbers are
[quoted text clipped - 6 lines]
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Mike Schilling - 11 Jan 2006 02:24 GMT
> mark.olive...@gmail.com wrote:
>> Thanks!  That seems to do the trick.  So, basically, those numbers are
[quoted text clipped - 6 lines]
> 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.
Roedy Green - 11 Jan 2006 04:11 GMT
>Can anyone please explain this behavior??

see http://mindprod.com/jgloss/primitives.html
you have overflowed the range of int.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.