File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting!!!!!!!!

 
balu gates
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic