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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting!!!!!!!!