File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals and Hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals and Hashcode" Watch "Equals and Hashcode" New topic

Equals and Hashcode

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Hi all,

If equals method is overridden, we must override the hashcode method right? is it true the other way round too?


Be Humble... Be Nice.
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
If you override either equals() or hashcode(), then you have to override the other one too.

SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Thanks for the reply Vidhya.
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
No its not mandatory to override if you do override one.
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

No its not mandatory to override if you do override one.

well, if we don't override we will not get any compile time error or run time exception, but it won't work as desired.

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

There is a contract between Equals and Hashcode method.

If 2 objects are equal according to Equals method, then their Hashcodes should be same.

As long as this contract is fulfilled, the program will work as required.

Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hello Arjun,

really concept is

if equal() true -then Hashcode () must true => must overriding
if equal() false -then Hashcode() not dependen=> not necessary
if Hashcode() false- Then equal() must false=> must overriding
if Hashcode() true-> Then only request to equal() true=> might be overriding .

it base of my besat knowledege.
if any one no more about this, welcome to him/her.

Best Regards,<br />Vipin<br />MCA, SCJP5, SCWCD in progress
I agree. Here's the link:
subject: Equals and Hashcode
It's not a secret anymore!