Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between equals and hashCode in Java

Difference Between equals and hashCode in Java

April 4, 2018 Posted by Lithmee

Key Difference – equals vs hashCode in Java
 

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.

Difference Between equals and hashCode in Java

Figure 01: Java Program with equals

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.

Difference Between equals and hashCode in Java_Figure 02

Figure 02: Java Program with Overridden equals

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.

Difference Between equals and hashCode in Java_Figure 03

Figure 03: Student class with equals and hashCode

Key Difference Between equals and hashCode in Java

Figure 04: Main Program

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

Related posts:

Difference between Java and J2EE Difference Between Arrays and Linked Lists Difference Between ER Diagram and Class Diagram kEY Difference Between Xcode and SwiftDifference Between Xcode and Swift Difference Between Static and Dynamic Memory AllocationDifference Between Static and Dynamic Memory Allocation

Filed Under: Programming Tagged With: equals and hashCode Differences, equals Definition, equals in Java, Function of equals in Java, Function of hashCode in Java, hashCode Definition, hashCode in Java, Usage of equals in Java, Usage of hashCode in Java

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

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

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Android 2.3 and Android 2.4

Difference Between Fraud and Embezzlement

Difference Between Fraud and Embezzlement

Difference Between Formic Acid and Acetic Acid

Difference Between Formic Acid and Acetic Acid

Difference Between They and Them in English Grammar

Difference Between Treasury Bills and Bonds

Latest Posts

  • What is the Difference Between Omphalocele and Gastroschisis
  • What is the Difference Between Autonomic and Somatic Reflexes
  • What is the Difference Between Plagiocephaly and Craniosynostosis
  • What is the Difference Between Coconut Oil and Virgin Coconut Oil
  • What is the Difference Between Wintrobe and Westergren Tube
  • What is the Difference Between Coarctation and Hypoplastic Aortic Arch
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.