Matthew Brown wrote:
Arhaan Singhania wrote:The '==' implementation you are using is inherited from the Object class as you have not ovveriden
the method.
That's slightly misleading. The == operator is not inherited from Object, and cannot be overridden. It always checks if the two references refer to the same object.
The equals() method is inherited from Object, and in Object is defined to use ==, which is where the confusion has arisen, I think. The equals() method can be overridden.
Exactly!!! what d and e are storing(bitwise) is not the value 1 but the reference where two Double objects are stored.It does not reflect anything about the contents of the objects.
And as Matthew has rightly pointed, when you display the two objects, the toSring() method is called, which has been inherited and overriden by the Double class, to display its contents.
so although your print statement prints 1.0 and 1.0,
the if(d==e) fails.
And again, the equals() method is similarly inherited and overriden by the Double class to check whether the content of two Double objects being compared is the same!!! so the if() condition returns true!!!
so if you had something like this
Hope it clears your doubt!!!