File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java.lang.ArithmeticException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java.lang.ArithmeticException" Watch "java.lang.ArithmeticException" New topic


lalitha kaparapu

Joined: Nov 05, 2007
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
double doubleY = intX / 0 ; // line 4
System . out . println ( doubleX == doubleY ) ; // line 5
Chandu Sree

Joined: Oct 04, 2007
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

Joined: Aug 29, 2006
Posts: 132
here integer stores -127 to 127 bits, so it gives exception. this is not the case with double and float.

Perform for today. Adapt for tomorrow.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
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".

SCJP 5.0
I agree. Here's the link:
subject: java.lang.ArithmeticException
It's not a secret anymore!