For the Object class' equals method, the API says :
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
This means that until we override this method in our classes, this method would return true only if the both the references to our class are pointing to the same object in memory. Even if the two references point to 2 different objects that are exactly same, this method would return false. Am I right ?
About the interface Comparator's equals, the API says that
Note that it is always safe not to override Object.equals(Object).
but also says that
The ordering imposed by a Comparator c on a set of elements S is said to be consistent with equals if and only if (compare((Object)e1, (Object)e2)==0) has the same boolean value as e1.equals((Object)e2) for every e1 and e2 in S.
Now, if we only implement compare(), the object's equals method will always return false, even if the two references point to areas in the memory having a exact copy of each other's fields. So, it can never be consistent with equals.
Also, if the compare function serves the test for equality too, why do we need the equals method in the Comparator interface ?
Edited 6 Years Ago by daudiam: n/a