aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NaN reference " Watch "NaN reference " New topic
Author

NaN reference

lashkar
Greenhorn

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);
3:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
8:
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");
A) True
True
B) True
False
C) False
True
D) False
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.
Thanx


Swati Kale
SCJP
SCWCD
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
lashkar,
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.
lashkar
Greenhorn

Joined: Jul 14, 2001
Posts: 18
Thank you ALL .
Got it .
ML
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

------------------
azaman


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

Joined: Jul 09, 2001
Posts: 74
Hi Azaman,
Here's from JDK1.3 API docs
quote:
================================================================
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
Hi,
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.

Vanitha.
[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
 
Don't get me started about those stupid light bulbs.
 
subject: NaN reference