
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the contract between equals() and hashCode() methods in Java?\\n
Every Java object has two very important methods, equals() and hashCode(), and these methods are designed to be overridden according to their specific general contract.
Since the Object class is the parent class of every class, the default implementation of the equals() and hashCode() methods is already present in each class. However, we need to override these methods based on the requirement.
Let's discuss the contract between equals() and hashCode() methods in Java. But before that, we need to understand these methods.
The hashCode() Method
The hashCode() method returns an integer value, which is referred to as the hash code value of an object. Every Object, at the time of creation assigned with a unique 32-bit, signed int value. This value is the hash code value of that object.
Syntax
The syntax of hashCode() method is as follows:
public int hashCode()
The equals() Method
The equals() method of the Object class accepts another object as a parameter, and verifies whether it is equal to the current object. It returns TRUE if both objects are equal and FALSE if they are not.
The default implementation of this method (provided by the Object class) determines that two objects are equal if both of them reference the same object. It does not check the value or state of the objects. But we can override this method to provide our own implementation to compare the state or value of the objects.
Syntax
The syntax of equals() method is as follows:
public boolean equals(Object obj)
Contract Associated with hashCode() Method
Following is the list of contracts associated with the Java hashCode() method:
- The hashCode() method should return the same integer value for the same object for each call of this method unless the value stored in the object is modified.
- If two objects are equal according to the equals() method, then the hashCode() method should return the same integer value for both objects.
- But, it is not necessary that the hashCode() method will return a distinct result for the objects that are not equal according to the equals() method.
Contract Associated with equals() Method
For any non-null reference variables a, b, and c, the contract associated with the equals() method is given below:
- 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 also return TRUE.
- Multiple calls of a.equals(b) should consistently return TRUE or consistently return FALSE, if the value of the object is not modified for either object.
- a.equals(null) should return FALSE.
So, it is necessary to override the hashCode() method of the Object class if we are overriding the equals() method.
Example: hashCode() and equals() Method
The following Java program demonstrates the use of hashCode() and equals() methods.
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; } }
Output
Output of the above code is given below:
Object One and Object Two are equal Object Three and Object Four are not equal