How default .equals and .hashCode will work for my classes?

JavaObjectEqualsHashcodeEquality

Java Problem Overview


Say I have my own class

public class MyObj { /* ... */ }

It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode.

Once we call equals and hashCode, what are the default implementations? From Object class? And what are they? How the default equals will work? How the default hashCode will work and what will return? == will just check if they reference to the same object, so it's easy, but what about equals() and hashCode() methods?

Java Solutions


Solution 1 - Java

Yes, the default implementation is Object's (generally speaking; if you inherit from a class that redefined equals and/or hashCode, then you'll use that implementation instead).

From the documentation:

equals > 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).

hashCode > As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Solution 2 - Java

From Object in one of the JVM implementations:

public boolean equals(Object object) {
    return this == object;
}

public int hashCode() {
    return VMMemoryManager.getIdentityHashCode(this);
}

In both cases it's just comparing the memory addresses of the objects in question.

Solution 3 - Java

There are default implementations of equals() and hashCode() in Object. If you don't provide your own implementation, those will be used. For equals(), this means an == comparison: the objects will only be equal if they are exactly the same object. For hashCode(), the Javadoc has a good explanation.

For more information, see Effective Java, Chapter 3 (pdf), item 8.

Solution 4 - Java

Yes, from Object class since your class extends Object implicitly. equals simply returns this == obj. hashCode implementation is native. Just a guess - it returns the pointer to the object.

Solution 5 - Java

If you do not provide your own implementation, one derived from Object would be used. It is OK, unless you plan to put your class instances into i.e. HashSet (any collection that actually use hashCode() ), or something that need to check object's equality (i.e. HashSet's contains() method). Otherwise it will work incorrectly, if that's what you are asking for.

It is quite easy to provide your own implementation of these methods thanks to HashCodeBuilder and EqualsBuilder from Apache Commons Lang.

Solution 6 - Java

IBM's developerworks says:

> Under this default implementation, two > references are equal only if they > refer to the exact same object. > Similarly, the default implementation > of hashCode() provided by Object is > derived by mapping the memory address > of the object to an integer value.

However, to be sure of the exact implementation details for a particular vendor's Java version it's probably best to look as the source (if it's available)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexeyproView Question on Stackoverflow
Solution 1 - JavaEtienne de MartelView Answer on Stackoverflow
Solution 2 - JavaBrad MaceView Answer on Stackoverflow
Solution 3 - JavaJornView Answer on Stackoverflow
Solution 4 - JavakhachikView Answer on Stackoverflow
Solution 5 - JavaPaweł DydaView Answer on Stackoverflow
Solution 6 - JavabrabsterView Answer on Stackoverflow