in Khalid's book he overrides the equals() method like this (basically):
public boolean equals(Object obj){
if(obj == this)
... }
I don't understand one thing though, wouldn't the above check to see if they refer to the same object instead of checking for their meaningful value? The equals() method in the Object class already does this though?
Yes exactly. The equals() Object method is for checking the same object and not for checking the contents of the object. Although in the special case of <String> it overrides this method for checking the actual content of the string.
if you don't override the equals() you are going to compare the objects reference, ==, as it was implemented in Object class, but you can override equals() an compare the object in the way you want. For example with the most important field...
Correct me if I am wrong, but isn't equals(Object) method supposed to use hasCode() method. If the hash codes are same then the equals method returns true?
No. If the hashCode for two given objects are the same, they do not have to be equal. But, if the two objects ARE equal (acording to the equals() method), then they SHOULD have identical hashCode values (acording to the contract).
SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA
Live life to an interface, not an implementation!
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, the permaculture playing cards