Java hashCode() and equals() method complete understanding

hashcode and equals

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!!!

Leave a Reply

Your email address will not be published. Required fields are marked *