Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# java.lang.ArithmeticException

lalitha kaparapu
Greenhorn
Posts: 16
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
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
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
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".