Forums Register Login

Q'tion from Majji's paper

+Pie Number of slices to send: Send

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thanks a lot bill for nice explation.
regards
Ashwini.
If you're gonna buy things, buy this thing and I get a fat kickback:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 759 times.
Similar Threads
can't understand this..!!
Double.NaN ???
about == equals
NaN reference
Double.Nan question.plz help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:34:11.