Strange java behaviour

P

priyom

Please got through the following code snippet.

class NanTest {

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
}
}

It throws exception on line 4 but not on line 3.
Could anyone please explain this.
 
M

M.J. Dance

priyom said:
Please got through the following code snippet.

class NanTest {

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
}
}

It throws exception on line 4 but not on line 3.
Could anyone please explain this.

http://java.sun.com/developer/TechTips/1998/tt0722.html#tip2
 
P

Patricia Shanahan

priyom said:
Please got through the following code snippet.

class NanTest {

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
}
}

It throws exception on line 4 but not on line 3.
Could anyone please explain this.

Line 4 is an int division by zero, and there is no way to represent the
result as an int, so it throws an exception.

Line 3 is a float division by zero, and there is a value,
Float.POSITIVE_INFINITY, that represents the result of dividing a
positive float by a zero float.

Patricia
 
L

Lew

priyom said:
class NanTest {

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
}
}

It throws exception on line 4 but not on line 3.
Could anyone please explain this.

Patricia said:
Line 4 is an int division by zero, and there is no way to represent the
result as an int, so it throws an exception.

Line 3 is a float division by zero, and there is a value,
Float.POSITIVE_INFINITY, that represents the result of dividing a
positive float by a zero float.

http://docs-pdf.sun.com/800-7895/800-7895.pdf
and
http://docs.sun.com/source/806-3568/ncg_goldberg.html

The latter is the HTML form of the former.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top