*
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
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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting!!!!!!!!
 
Similar Threads
explain me (double)
Mock Question
why the code is giving the answer as -1
Mock question from Jaworski
A mock question