Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Programmer Certification (OCPJP)
Mock Question
tanu dua
Ranch Hand
Posts: 145
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi all !
Can anyone plz explain the output of the following program :
public class exp { public static void main(String args[]) { double d1=1.0; double d2=0.0; byte b=1; d1=d1/d2; System.out.println(d1); b=(byte)d1; System.out.println(b); } }
Output :
INFINITY
-1
Problem:
How infinity is converted to -1 while casting.
Thanx
Tybon Wu
Ranch Hand
Posts: 84
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 145
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanx mate !!
You have cleared my doubts ...
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Casting Related Doubt
narrowing double to byte question.
why the code is giving the answer as -1
casting problem
Mock question from Jaworski
More...