Java hashCode() and equals() method complete understanding

Hello Readers, In this article we are going to learn about the Java hashCode() and equals() method.

These methods exists in the Object class. As you know, Object class is the parent class of all classes in java. Object class automatically gets extended in each class. There are total 9 methods are present in this Object class. We are only going to learn below mentioned two methods:

  • hashCode()
  • equals()

As Object class is parent class of every class in Java, the default implementation of these two methods is already present in each class, However , we can override these methods basis on our requirement.

hashCode() :

publicΒ intΒ hashCode()

This method return an integer value, which is referred as hash code value of the object. Every Object , at the time of creation ,Β  is assigned with aΒ  unique 32-bit signed int value .This value is the hash code value of that object.

There is one general contract associated with hashCode() method, which states :
  • The hashCode method should return same integer value for same object for each calling of this method. Unless the value stored in the object is modified.
  • If two objects are equal (according to equals(Object) method) then the hashCode() method should return same integer value for both the objects.
  • But, it is not necessary that hashCode() method will return distinct result for Objects that are not equal (according to equals(Object) method).

equals() :

publicΒ booleanΒ equals(ObjectΒ obj)

The equals() method of Object class checks the equality of the objects and accordingly it return true or false.

The default implementation, as provided by Object class,Β  checks the equality of the objects on the basis if both references refer to same object. It does not checks the value or state of the objects. But we can override this method to provide own implementation to compare the state or value of the objects.

There is one general contract associated with equals() method, which states :

As per Oracle, the equals() method is reflexive, symmetric , transitive and consistent in nature i.e.

For any non-null reference variables a, b and c :

  • a.equals(a) should always return true.
  • a.equals(b)Β should returnΒ trueΒ if and only if b.equals(a)Β returnsΒ true.
  • If a.equals(b)Β returnsΒ trueΒ and b.equals(c)Β returnsΒ true, then a.equals(c)Β should returnΒ true.
  • Multiple calling of a.equals(b)Β should consistently returnΒ trueΒ or consistently returnΒ false, If value of the object is not modified for either object.
  • a.equals(null)Β should returnΒ false.
Important points related to overriding of equals() and hashCode() method:

Oracle says, it is necessary to maintain the contract between equals() and hashCode() methods when we are providing own implementation of the equals() method.

As stated earlier, the default implementation of the equal() method does not compare the value / state of the objects so we can override the equals method and provide our own implementation, which can compare the states of the object and return true or false accordingly.

But it is important that we do not violate the genera contract of hashCode() and equals() method, which is :

If two Objects are equal , according to equals() method,Β then these objects must have equal hash codes.

So it is necessary to override the hashCode() method of Object class, if we are overriding the equals() method.

package com.tbs.examples;

/**
*
* @author techblogstation.com
* How to Override equals() method in Java?
* How to Override hasCode() method in Java?
*/

public class MainClass {

public static void main(String[] args) {

MainClass mainClass = new MainClass();

TestClass obj1 = new TestClass(1);
TestClass obj2 = new TestClass(1);

mainClass.test1(obj1, obj2);

TestClass obj3 = new TestClass(1);
TestClass obj4 = new TestClass(2);

mainClass.test2(obj3, obj4);
}

public void test1(TestClass obj1, TestClass obj2) {
if (obj1.equals(obj2)) {
System.out.println(“Object One and Object Two are equal”);
} else {
System.out.println(“Object One and Object Two are not equal”);
}
}

public void test2(TestClass obj3, TestClass obj4) {
if (obj3.equals(obj4)) {
System.out.println(“Object Three and Object Four are equal”);
} else {
System.out.println(“Object Three and Object Four are not equal”);
}
}
}

class TestClass {
private int val;

TestClass(int val) {
this.val = val;
}

public int getValue() {
return val;
}

@Override
public boolean equals(Object o) {

// null check
if (o == null) {
return false;
}

// this instance check
if (this == o) {
return true;
}

// instanceof Check and actual value check
if ((o instanceof TestClass) && (((TestClass) o).getValue() == this.val)) {
return true;
} else {
return false;
}
}

@Override
public int hashCode() {
int result = 0;
result = (int) (val / 11);
return result;
}
}

Thanks for reading this article. I hope , you like it,

For any suggestions / feedback / question / clarification, Kindly post your comments in the below comment box.

Please subscribe our news letter and connect with social media accounts and don’t miss any articles.

Happy Reading!!!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply