Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting!!!!!!!!

 
balu gates
Greenhorn
Posts: 16
  • 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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic