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, 2018Posted byLithmee

Key Difference – equalsvs hashCode inJava

The equals is similar to the == operator, which is to test for object identity rather than object equality. ThehashCode is a method by which a class implicitly or explicitly break down the data stored inan instance of the class into a single hash value, which is a 32 bit signed integer. Thekey differencebetween equals and hashCode inJavais thatthe equals is used to compare twoobjects在使用hashCode决定wh的散列ich 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

关键区别equals和hashCode 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 asHashMap.

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 inan 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 – equalsvs hashCode inJava

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:ProgrammingTagged 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 ReplyCancel 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 Isopoly and Heteropoly Acids

Difference Between Isopoly and Heteropoly Acids

Difference Between Samsung Galaxy S II(2) (GT-i9100) and LG Optimus 3D

Difference Between Sudan and South Sudan

Difference Between Time Domain and Frequency Domain

Difference Between Impressionism and Post-Impressionism

Difference Between Impressionism and Post-Impressionism

Latest Posts

  • What is the Difference Between Functional and Performance Ingredients
  • What is the Difference Between Intracerebral Hemorrhage and Subarachnoid Hemorrhage
  • What is the Difference Between Behentrimonium Chloride and Behentrimonium Methosulfate
  • What is the Difference Between Systolic and Diastolic Dysfunction
  • What is the Difference Between Care and Concern
  • What is the Difference Between Ehlers Danlos and Marfan Syndrome
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

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