From this question I learned Double.NaN is not equal to itself.
I was verifying this for myself and noticed this is not the case if you wrap Double.NaN in a Double instance. For example:
public class DoubleNaNTest {
public static void main(String[] args) {
double primitive = Double.NaN;
Double object = new Double(primitive);
// test 1 - is the primitive is equal to itself?
boolean test1 = primitive == primitive;
// test 2 - is the object equal to itself?
boolean test2 = object.equals(object);
// test 3 - is the double value of the object equal to itself?
boolean test3 = object.doubleValue() == object.doubleValue();
System.out.println("Test 1 = " + test1);
System.out.println("Test 2 = " + test2);
System.out.println("Test 3 = " + test3);
}
}
Outputs:
Test 1 = false
Test 2 = true
Test 3 = false
It seems to me that all three tests should evaluate to false as all three operations are equivalent (as they are if you use something other then Double.NaN).
Could someone explain what's going on here?