*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q'tion from Majji's paper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q Watch "Q New topic
Author

Q'tion from Majji's paper

ashwini srinivasan
Greenhorn

Joined: Nov 07, 2000
Posts: 26

code//
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 given is c) (False and True).
can anyone explain me how c) is the correct answer.
Thanx.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
From the API:

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.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true. This allows hashtables to operate properly.

So this is a special case. Also pay attention to 0.0 and -0.0 as this is the opposite of NaN.
Bill
ashwini srinivasan
Greenhorn

Joined: Nov 07, 2000
Posts: 26
Thanks a lot bill for nice explation.
regards
Ashwini.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q'tion from Majji's paper
 
Similar Threads
about == equals
Double.NaN ???
can't understand this..!!
Double.Nan question.plz help
NaN reference