posted 24 years ago
The equals method is defined in the Object class, so it is included in all classes. It is defined in the Object Class as Object obj2.equals(Object obj1) will be true if and only if obj1 is the same object as obj2. This is the minimum requirement for the equals method.
However, classes override the equals method to give it a better meaning for thier class. For Double, equals is overridden so it will return true if the value of the doubles are == to each other with the exception of NAN where == will be false, but equals will return true.
So for the above question, Double.NaN.equals(Double.NaN) will return true as per the overridden method in Double. But for MyClass, if they don't override equals then it is going to return false unless the objects refer to the same objec, which in the case above they don't.
Hope that makes sense.
Bill