File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Query on NaN Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Query on NaN Number" Watch "Query on NaN Number" New topic
Author

Query on NaN Number

ramya ray
Ranch Hand

Joined: Aug 11, 2006
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
}
}

Please explain ,
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

I dont think NaN is covered under SCJP. You wont find such questions in SCJP anyway


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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
}
}

Please explain ,


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

Joined: Aug 11, 2006
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

Joined: Feb 02, 2006
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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Query on NaN Number
 
Similar Threads
doubt reg NaN (from JLS)
NAN Question,Please help.
query regarding "=="
NaN reference
static variables