125

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?

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
alexeypro
  • 3,633
  • 7
  • 36
  • 49

6 Answers6

113

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • 2
    Mind that contrary to `hashCode`'s documentation, HotSpot [returns a random number per default](https://stackoverflow.com/questions/2237720/what-is-an-objects-hash-code-if-hashcode-is-not-overridden/32454673#32454673) as the hash. See also [this blog entry](https://srvaroa.github.io/jvm/java/openjdk/biased-locking/2017/01/30/hashCode.html). – Matthias Braun Sep 28 '20 at 14:50
61

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
  • 7
    What version of JDK it from? In `v6u23 ea`: `public native int hashCode();` – khachik Nov 14 '10 at 18:52
  • @kha - You're right, I think I tracked down one of the native implementations to see what it actually did – Brad Mace Jul 19 '11 at 14:23
  • "In both cases it's just comparing the memory addresses of the objects in question.": HotSpot [returns a random number per default](https://stackoverflow.com/questions/2237720/what-is-an-objects-hash-code-if-hashcode-is-not-overridden/32454673#32454673) as the hash. See also [this blog entry](https://srvaroa.github.io/jvm/java/openjdk/biased-locking/2017/01/30/hashCode.html). – Matthias Braun Sep 28 '20 at 14:52
12

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.

Jorn
  • 20,612
  • 18
  • 79
  • 126
1

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.

khachik
  • 28,112
  • 9
  • 59
  • 94
  • 2
    It is a pointer to the object located in memory, but it's not a memory address of the object. The GC can move the object around in memory and the hash code will remain the same. – Jeremy Nov 14 '10 at 18:49
  • @Jeremy Thanks. http://stackoverflow.com/questions/2427631/how-is-hashcode-calculated-in-java may be interesting. – khachik Nov 14 '10 at 18:57
1

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.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79
  • (a) Why do you say the Object class’ default implementation of ‘equals’ will not work correctly with HashSet? That contradicts the other answers on this page. (b) Thanks for the Commons Lang links. – Basil Bourque Nov 06 '11 at 21:07
  • 1
    @Basil: I don't think that contradicts. Of course default implementation would work... somehow, but not the way you expect. That is, since equals() uses reference equality, two otherwise identical objects would be "different" in the eyes of default implementation. As the result, you might end up having two different instances of the exactly same thing in your Set. And rather typical use of Sets is when you want to eliminate duplicates... – Paweł Dyda Nov 07 '11 at 07:57
  • @PawełDyda: The default behavior is generally correct for mutable types. If `Foo` and `Bar` are references to two different instances of a mutable type, and there exists a method (e.g. `SomeMutatingMethod`) such that `Foo.SomeMutatingMethod()` does not affect `Bar` the same way it does `Foo`, that difference should be sufficient to regard the objects as unequal. – supercat Dec 04 '12 at 18:06
0

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)

brabster
  • 42,504
  • 27
  • 146
  • 186