Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Automatic conversion

 
ravee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {
static public void main(String s[]){
float f = 10;
System.out.println(f/0);
}
}
this program throws ArithmeticException and if I change the denominator to 0.0 works fine.
why ?
thanks
ravee.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works fine as it is(ie. (f/0)). It prints "infinity". Both float and double datatype take care of the divide by zero.
Here in the following 2 lines ,implicit conversion takes place ie
gets converted to float type.
float f = 10;
System.out.println(f/0);
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravee, your observations don't agree with what should happen and which did happened when I tested. Do you know why? Are you sure of what you said?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic