The equals is similar to the == operator, which is to test for object identity rather than object equality. The hashCode is a method by which a class implicitly or explicitly break down the data stored in an instance of the class into a single hash value, which is a 32 bit signed integer. The key difference between equals and hashCode in Java is that the equals is used to compare two objects while the hashCode is used in hashing to decide which group an object should be categorized into.
CONTENTS
1. Overview and Key Difference
2. What is equals in Java
3. What is hashCode in Java
4. Side by Side Comparison – equals vs hashCode in Java in Tabular Form
5. Summary
What is equals in Java?
The equals method is used to compare two objects. The default equals method is defined in the object class. That implementation is similar to the == operator. The two object references are equal only if they are pointing to the same object. It is possible to override the equals method.
The statement System.out.println(s1.equals(s2)) will give the answer false because s1 and s2 are referring to two different objects. It was similar to the statement, System.out.println(s1 == s2);
The statement System.out.println(s1.equals(s3)) will give the answer true because s1 and s3 are referring to the same object. It was similar to the statement, System.out.println(s1 == s3);
There is no equals method in the Student class. Therefore, the equals in the Object class is called. True is displayed only if the object reference is pointing to the same object.
According to the above program, the equals method is overridden. An object is passed to the method, and it is type casted to Student. Then, the id values are checked. If the id values are similar, it will return true. If not, it will return false. The ids of s1 and s2 are similar. So, it will print true. The ids of s1 and s3 are also similar, so it will print true.
What is hashCode in Java?
The hashCode is used in hashing to decide to which group an object should be categorized into. A group of objects can share the same hashCode. A correct hashing function can evenly distribute objects into different groups.
A correct hashCode can have properties as follows. Assume that there are two objects as obj1 and obj2. If obj1.equals(obj2) is true, then the obj1.hashCode() is equal to obj2.hashCode(). If obj1.equals(obj2) is false, it is not necessary that obj1.hashCode() is not equal to obj2.hashCode(). The two unequal object might also have the same hashCode.
The Student class contains the equals and hashCode methods. The equals method in the Student class will receive an object. If the object is null, it will return false. If the classes of the objects are not the same, it will return false. The id values are checked in both objects. If they are similar, it will return true. Else it will return false.
In the main program, objects s1 and s2 are created. When calling s1.equals(s2) will give true because the equals method is overridden and it checks the id values of the two objects. Even though they are referring to two objects, the answer is true because the id values of s1 and s2 are the same. As the s1.equals(s2) is true, the hashCode of s1 and s2 should be equal. Printing the hashCode of s1 and s2 gives the same value. The hashCode method can be used with Collections such as HashMap.
What is the Difference Between equals and hashCode in Java?
equals vs hashCode in Java |
|
equals is a method in Java that acts similar to the == operator, which is to test for object identity rather than object equality. | hashCode is a method by which a class implicitly or explicitly break down the data stored in an instance of the class into a single hash value. |
Usage | |
The method equals is used to compare two objects. | The method is used in hashing to decide which group an object should be placed into. |
Summary – equals vs hashCode in Java
The difference in equals and hashCode in Java is that the equals is used to compare two objects while the hashCode is used in hashing to decide which group an object should be categorized into.
Reference:
1.“Java HashCode().” Wikipedia, Wikimedia Foundation, 17 Feb. 2018. Available here
2.Harold, Elliotte Rusty. “The Equals() Method.” The Equals() Method, 2 Nov. 2001. Available here