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 NaN reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NaN reference " Watch "NaN reference " New topic

NaN reference


Joined: Jul 14, 2001
Posts: 18

The following code will print
1: Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");
A) True
B) True
C) False
D) False

The answer being , C , I am Confused .
Any light ? Thanks.

M Lashkar
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
I guess, its similar like when u comapre two String objects. a and b being two different objects ( created using new) points to different locations so gives false in the first case (==) .
and in the second case (equals), vlaues are being compared so yields True.
Correct me if i am wrong.

Swati Kale
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
java.lang.Double has the following meaning for equals:
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Double object that represents a double that has the identical bit pattern to the bit pattern of the double represented by this object.
Also as swati said it overrides the equals of the java.lang.Object and hence acts similar to java.lang.String.
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Lashkar, the first comparison concerns the static values for Double.NaN (not the a & b instances you've created). I believe the API states that Double.Nan != Double.NaN. Two values which are not numbers don't necessarily equal each other, which is why Double.NaN == Double.NaN will always return false.

Joined: Jul 14, 2001
Posts: 18
Thank you ALL .
Got it .
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Would u clarify why a & b equals true when both has the value
Doulbe.NaN and we know that Doulbe.NaN != Doulbe.NaN


Ashik Uzzaman
Senior Member of Technical Staff,, San Francisco, CA, USA.
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Azaman,
Here's from JDK1.3 API docs
Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if
d1.doubleValue() == d2.doubleValue()

also has the value true. However, there are two exceptions:
If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.
If d1 represents .0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though .0==-0.0 has the value true. This allows hashtables to operate properly.
- eric
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
This is from JLS. I think this will be help for this discussion

NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (�15.20.1). The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN (�15.21.1). In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN.

[This message has been edited by Vanitha Sugumaran (edited July 15, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi lashkar,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: NaN reference
Similar Threads
about == equals
Double.NaN question
Double.Nan question.plz help
Mock Q
Q'tion from Majji's paper