# Query on NaN Number

ramya ray
Ranch Hand
Posts: 101
In JLS it is mentioned that :
" x!=x is true if and only if x is NaN,"
and "The equality operator == returns false if either operand is NaN"
But while compiling the below code,it returns just opposite

class val{
int i=7;

public static void main (String args[]){

Float f = 0.0f/0.0f;
System.out.println(f);
System.out.println(f == f);//output true
System.out.println(f != f);//output false
}
}

Deepak Bala
Bartender
Posts: 6663
5
I dont think NaN is covered under SCJP. You wont find such questions in SCJP anyway

Keith Lynn
Ranch Hand
Posts: 2409
Originally posted by ramya ramya:
In JLS it is mentioned that :
" x!=x is true if and only if x is NaN,"
and "The equality operator == returns false if either operand is NaN"
But while compiling the below code,it returns just opposite

class val{
int i=7;

public static void main (String args[]){

Float f = 0.0f/0.0f;
System.out.println(f);
System.out.println(f == f);//output true
System.out.println(f != f);//output false
}
}

Note that you're boxing the output of 0.0f/0.0f into a Float instead of storing it in a float.

If you change the type of f to float or compare f against Float.NaN, you will see the output that you are expecting to see from the conditional operators.
[ September 01, 2006: Message edited by: Keith Lynn ]

ramya ray
Ranch Hand
Posts: 101
Thanks a lot for reply ,You are right ,If i replaced it to float f ,It works fine but why it is not working for Float f ,means making an object of Float.
Is there any object related concept in this .

Is it not covered in SCJP as it is floating point types,Please suggest.

wise owen
Ranch Hand
Posts: 2023

x!=x is true if and only if x is NaN

Float.NaN != Float.NaN

The equality operator == returns false if either operand is NaN

f == Float.NaN (f is Float object with value NaN)

 Consider Paul's rocket mass heater.