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

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 class Object? So what is this? How will equal default values ​​be used? How will hashCode work by default and what will be returned? == will just check to see if they reference the same object, so it's easy, but what about the equals () and hashCode () methods?

+84
java equality object equals hashcode
Nov 14 '10 at 18:40
source share
6 answers

Yes, the default implementation is Object (generally speaking, if you inherit a class that overrides equals and / or hashCode, then you will use this implementation instead).

From the documentation:

equals

The equals method for the Object class implements the most varied possible equivalence relation for objects; that is, for any non-empty reference values ​​x and y, this method returns true if and only if x and y refer to the same object (x == y is true).

hashCode

As reasonably practical, the hashCode method defined by the Object class returns different integers for different objects. (This is usually done by converting the internal address of the object to an integer, but this implementation method is not required by the JavaTM programming language.)

+74
Nov 14 '10 at 18:44
source share

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 simply compares the memory addresses of these objects.

+39
Nov 14 '10 at 18:46
source share

The object has default implementations equals() and hashCode() in Object. If you do not provide your own implementation, they will be used. For equals() this means comparing == : objects will be equal if they are exactly the same object. For hashCode() , Javadoc has a good explanation.

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

+9
Nov 14 '10 at 18:48
source share

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

+1
Nov 14 '10 at 18:43
source share

If you do not provide your own implementation, one of the Object will be used. Everything is fine if you do not plan to introduce instances of the class in ie HashSet (any collection that actually uses hashCode ()), or something that is necessary to verify the equality of objects (i.e. the HashSet contains () method). Otherwise, it will not work correctly if that is what you are asking for.

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

+1
Nov 14 '10 at 19:06
source share

IBM developerworks says:

In this implementation, by default, two links are equal only if they refer to the same object. Similarly, the default implementation of hashCode () provided by Object is obtained by matching the memory address of the object to an integer value.

However, to be sure of the exact implementation details of a particular version of the Java provider, it is best to look at the source (if available)

0
Nov 14 2018-10-18
source share



All Articles