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"); The answer is False True. I don't understand why the second case is true. It is given that Double.NaN is never equal to Double.NaN. Can anybody explain why is it so!!!. Thanx
I think the primitives behave according to IEEE-754 standard, but the Wrapper has to behave according to the contract of equals method in Object class. This is even clearer in: Integer iw = new Integer(10); Float fw = new Float(10.0f); if (iw.equals(fw)) System.out.println("Equal"); The equals could have returned true. But the contract of equals doesn't allow it. If you check, if(a.doubleValue() == b.doubleValue()) System.out.println("True doubleValue"); else System.out.println("False doubleValue"); it returns false.