*
The moose likes Java in General and the fly likes Question to Authors : What would the output be? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question to Authors : What would the output be?" Watch "Question to Authors : What would the output be?" New topic
Author

Question to Authors : What would the output be?

Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hi Authors,

Please clarify regarding the code snippet below.



Also, the reason was expected. Can you please help me out, in this regard?

Cheers,
Ram
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Because the constants on the right-hand-sides of these assignments are integers, not longs, the multiplication is done with integers, and the final int result is then converted to a long. "a" is less than the maximum value of an int by about a factor of 25, but "b" is way over, so "b" overflows and you end up with an unexpected value -- i.e., you get (int) (24*60*60*1000*1000L), which, just by happenstance, is about 5 times as large as "a", rather than 1000 times.

If you change the declaration of "b" to be

public static final long b =24*60*60*1000*1000L;

(note that "L" on the end) then the multiplies are done using longs, and you'll get the answer you expect.


[Jess in Action][AskingGoodQuestions]
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
Ernest,

Yep. We discuss this one in detail in Puzzle 3 ("Long Division"). Note that a better fix is:



By making the first factor in the sequence of multiplications a long, we don't have to worry about where the overflow occurs: the entire computation is done using longs.

Regards,

Josh and Neal

Originally posted by Ernest Friedman-Hill:
Because the constants on the right-hand-sides of these assignments are integers, not longs, the multiplication is done with integers, and the final int result is then converted to a long. "a" is less than the maximum value of an int by about a factor of 25, but "b" is way over, so "b" overflows and you end up with an unexpected value -- i.e., you get (int) (24*60*60*1000*1000L), which, just by happenstance, is about 5 times as large as "a", rather than 1000 times.

If you change the declaration of "b" to be

public static final long b =24*60*60*1000*1000L;

(note that "L" on the end) then the multiplies are done using longs, and you'll get the answer you expect.


Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hi Ernets, Josh and Neal


Thanks a lot for the explanation. This is making things lucid.

Thanks Again

Cheers,
Ram.
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
Ram,

Happy to help; that's what we're here for.

Josh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question to Authors : What would the output be?
 
Similar Threads
Regarding int operations out of range
Incrementing and Decrementing Variables
Please explain the behavior
Working with Date and Time
How to get Yesterday's date