This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q Watch "Q New topic

Q'tion from Majji's paper

ashwini srinivasan

Joined: Nov 07, 2000
Posts: 26

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 given is c) (False and True).
can anyone explain me how c) is the correct answer.
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.
ashwini srinivasan

Joined: Nov 07, 2000
Posts: 26
Thanks a lot bill for nice explation.
I agree. Here's the link:
subject: Q'tion from Majji's paper
It's not a secret anymore!