aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting!!!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting!!!!!!!!" Watch "Casting!!!!!!!!" New topic
Author

Casting!!!!!!!!

balu gates
Greenhorn

Joined: Jan 05, 2001
Posts: 16
Hi
Explain this..........
class Infinite {
public static void main(String args[]) {
double d1=1.0;
double d2=0.0;
int b=1;
d1=d1/d2;
b=(byte)d1;
System.out.println(b);

}
}
The answer is -1.
Explain.
Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
Floating point division never results in ArithmaticException, instead the value, here in this case is infinity. And if you cast it to byte yields to the max value (all bits set) FOR BYTE. Hence you see -1. Check out JLS 4.3.
Hope this helps.
Originally posted by balu gates:
Hi
Explain this..........
class Infinite {
public static void main(String args[]) {
double d1=1.0;
double d2=0.0;
int b=1;
d1=d1/d2;
b=(byte)d1;
System.out.println(b);

}
}
The answer is -1.
Explain.

 
jQuery in Action, 2nd edition
 
subject: Casting!!!!!!!!
 
Similar Threads
Mock question from Jaworski
Mock Question
explain me (double)
why the code is giving the answer as -1
A mock question