top 5 facts about java hashcode and equals method

Sanjay Y
3 min readFeb 17, 2018

Java there are the two basic fundamental methods present in the Object class. Most collection classes use method to check object equality.Java.lang.Object has methods called hasCode() and equals(). These methods play a crucial role in an application.

java.lang.object has hashCode() method , which returns hashcode of an object .

If we look at source code of the object class we can see the code as following

java hashcode

This is native method, because it has to interact with the machine. Here machine dependent code is written in the C language, which is not coming with the source package or in rt.jar of the lib location of the Java Runtime Environment (JRE).

The hashCode method returns an integer value dependent on the internal representation of a pointer to an object on the heap.

This method converts object internal address to an integer , as internal address is not available via Java SDK hence it is native. Lets discuss what are equals and hashcode methods.

This method does the object comparison in java between two different objects .Let’s look at objects code for equals method

Now , above code returns true if and only if both variables refer to the same object, if their references are one and the same .

Why Do we Need to override Equals and hashCode :-

It is generally necessary to override the hashCode method whenever equals method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

1.Override the Only HashCode:-

We are overriding the hashcode method only.

Once we are declaring two employee objects with same value like below

Employee e=new Employee("Frugalis", 1);
Employee e2=new Employee("Frugalis", 1);

If we override only hashcode and try to put it inside map by calling map.put(e, "Frugalis") , it takes e calculates its hashcode and allocates a particular memory location or a bucket.

Now again if we try to put in map by calling map.put(e2, "Frugalis") , logically if we think in a business scenario perspective or hashmap perspective , it should replace first with second as both are same and equal . But it inserts both of them in a map .Why So ?

So the problem here is equal was not overriden , so once map hashes second object e2 and iterates through bucket to check for any object that is equal to e2, it calls equals method .

As we do not override equals method here , it calls object class equals method and returns false , as object class default implementation of equals checks the reference of two objects and we have both references are different in this case .

2. Overriding Only equals method:-

We are overriding the equals method only.

Output:-

Explanation:-

If only equals is overriden, then when you call map.put(e1,"Frugalis") e1 will hash to some bucket and when you call map.put(e2,"minds") it will hash to some other bucket (as they have a different hashCode returned).

So, although they are equal, as they don't hash to the same bucket, the map can't realize it and both of them stay in the map , which is again not relevant as per a real time business logic.

As you can see in both scenarios implementing any one of hashcode and equals doesnt gives you a correct result .By defining equals() and hashCode() consistently, you can improve the usability of your classes as keys in hash-based collections .

So when you need to use your object in the hashing based collection, must override both equals() and hashCode()

I hope i have made it fully clear and if you guys have any question , please comment and Subscribe .

Suggest me the Topics You Want to know More In Comments Below .

Originally published at http://frugalisminds.com on February 17, 2018.

--

--