Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.ArithmeticException

 
lalitha kaparapu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I tried the code below.Inspite of runtime exception at line 3 ,it is showing the runtime exception at line 4.Why the float value when divided by zero is returning infinity inspite of java.lang.ArithmetiException.
class Test{

public static void main ( String args [ ] ) {
int intX = 10 ; // line 1
float floatX = 10 ; // line 2
double doubleX = floatX / 0 ; // line 3
System.out.println(doubleX);
double doubleY = intX / 0 ; // line 4
System . out . println ( doubleX == doubleY ) ; // line 5
}
};
 
Chandu Sree
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Lalitha,

In java.lang.Float,java.lang.Double class,there are predefined constant values for divide by zero (ie Infinity).So, we are not getting any exception when you divide float or double values by zero (you get the answer as Infinity)

But in case of integer you will get the runtime exception (Arithmetic exception.),as there are no predefined constant values to represent in Integer class.


Hope this would help you.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here integer stores -127 to 127 bits, so it gives exception. this is not the case with double and float.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add on to Chandu's reply: when you divide a float or double by zero, you'll get Float.NaN or Double.NaN, respectively. "NaN" stands for "Not a number".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic