This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Mock Question" Watch "Mock Question" New topic
Author

Mock Question

tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
Hi all !

Can anyone plz explain the output of the following program :



Output :
INFINITY
-1
Problem:
How infinity is converted to -1 while casting.

Thanx
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
When casting double to byte, the value of the double is converted to an int first, and then from int to byte.

So, when you convert infinity to int, you get 0x7fffffff. When you convert 0x7fffffff to byte, you get 0xff, which is -1.


SCJP2
tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
Thanx mate !!

You have cleared my doubts ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Question
 
Similar Threads
Mock question from Jaworski
casting problem
why the code is giving the answer as -1
narrowing double to byte question.
Casting Related Doubt