Equals() and hashCode() method in Java

Object Equality

When it comes to Java ,there are two types of Object equality ,reference equality and object equality.

Reference Equality

Two references that refer to the same object on the heap are considered equal.If you want to check if two references are referring to the same object using the ‘==’ operator. The ‘==’ operator compare the bits in the variable.If both refers to the same object the bits will be equal.

Example:- We have a student class and create an object and two references which refer to the same object.

The two references sRef1 and sRef2 refer to s1 so the comparison operator return true.This is Reference equality.

Object Equality

Object equality is used to check if two references referring to two different objects are meaningfully equivalent.If you want to treat two different objects as equal you must override hashCode() and equals() method.Both the method have a default implementation in the class Object.The default behaviour of hashcode method is that each object will get a unique number (most versions of Java assign a hashcode based on the object’s memory address on the heap so now two objects will have the same hash code).The default implementation of equals method returns true if two references refer to the same object .It uses ‘==’ operator to check for equality.

Example:- In the Student class we have overridden hashCode() and equals() method and created our custom implementation for object equality.

Two student objects will be considered equal when they have the same first name, last name and student id and the hash code of the two objects are the same.

You need to override the equals method to create your implementation of of  object equality.

In the given example all the student objects s1,s2 and s3 are different as they have different firstname, lastname, department and different hashcode.

Now,I add a new constructor to send studentId as parameter to show object equality.

The two student objects s1 and s2 are created , which are meaning fully equivalent because as per the hashcode and equals method two objects are equal if they have same first name, last name and student Id and they have the same hashcode

Rules for hashCode() and equals() Method

  • If two objects are equal ,they must have matching hash codes.
  • If two objects are equal, calling equals on both the objects should return true.That is if a.equals(b) is true then b.equals(a) is also true
  • If two objects have the same hashcode they are not required to be equal.But if they are equal, they must have the same hashcode value.That is  a.equals(b) is true then a.hashCode() must be equals to b.hashCode() but not vice versa.
  • If you override equals()  ,you must override hashcode() method and vice versa
  • The default implementation of hashCode method is to generate a unique integer for each object on the heap. So, if you don’t override hashCode() method ,no two objects can ever be equal.
  • The default behaviour of equals method checks if the two references refers to the same object on the heap.If you don’t override this method no two objects will ever be considered equal since references to two different objects will always have a different bit pattern

I hope you liked this article.Happy Coding everyone 🙂

Published by Anna

Just a simple girl who loves to code. .I am primarily a Java developer by profession and I am here to share my love for coding with you all I hope you enjoy reading my blog. Happy Coding Guys :-)

Leave a comment

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