java.lang.object has two very important methods defined in it. They are -
* public boolean equals(Object obj)
* public int hashCode()
It is advisable that all the new classes you define implement both these methods.
public boolean equals - As the name indicates it is used to test equality in classes. This is done in the following steps
(i) Shallow comparison - checks to see whether both the objects refer to the same reference.
(ii) Deep comparision - to compare the relevant data structures.
Java 1.4 has the following needs -
a) reflexive - x.equals(x) should return true
b) symmentric - x.equals(y) should return true iff y.equals(x) returns true
c) transitive
d) consistent
e) for any non-null reference value x, x.equals(null) should return false.
public int hashCode() -
For two objects to be equal the hashCode should stay the same and not the vice-versa. Also, it is important to note that hashCode are needed while implementing collections such as hashMap, hashTables etc.