wood burning stoves*
The moose likes Beginning Java and the fly likes Equals and hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals and hashcode" Watch "Equals and hashcode" New topic

Equals and hashcode

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
when do we override equals and hashcode nmethods ? What happens if we dont override them in our class ? Can we overload them ? Whats the relationship between the two ie. equals and hashcode.

Program :

Test10 t1= new Test10("abc");
Test10 t2= new Test10("abc
boolean bl = t1.equals(t2);

Output :

2025617596 (some random value)
2036496738 (some random value)

whereas here the program output is different :

String str=new String("naveen");
String str1="naveen";
Integer in1 = new Integer(1);
Integer in2 = new Integer(1);
boolean bl = str1.equals(str);
boolean b2 = in1.equals(in2);


Output :


Why does the equals method different in both cases. I have used userdefined class in first case and String,Integer class in second one.

Henry Wong

Joined: Sep 28, 2004
Posts: 18507

jose chiramal wrote:when do we override equals and hashcode nmethods ? What happens if we dont override them in our class ? Can we overload them ? Whats the relationship between the two ie. equals and hashcode.

You override the equals() and hashCode() methods, if you intend to use your objects in any hashing collection. If you don't intend to use them, in a hashing collection, then I guess nothng will happen.

See the JavaDoc for the java.lang.Object class for some details about the hashcode/equals contract.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

Here's a good article by Java guru Brian Goetz that explains the equals() and hashCode() methods:

Java theory and practice: Hashing it out - Defining hashCode() and equals() effectively and correctly

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Hi jose,

In addition to what Henry said if you override hashcode and equals, you can define the equality constraint of the fields of the object and hence it will be possible to compare two objects directly.
As shown

Thanks and Regards,
Hetal Vora

Joined: Feb 11, 2009
Posts: 12
Just to add on, if two objects are equal as per equals(), they need to have the same hashcode values so that they can be retrieved from collections. So if you override equals(), you also need to override hashcode().
However, if two objects are not equal as per equals(), their hashcodes may or may not be the same. This totally depends on the hashing algorithm you are using.

Thanks and Regards, Hetal
SCJP 1.5 100%
I agree. Here's the link: http://aspose.com/file-tools
subject: Equals and hashcode
Similar Threads
The way to compare HashCodes == or equals?
equals hashcode doubt sjcp 1.4
Doubt in overriding hashcode ...
String literal and String object comparison
Please Explain (Hashcode)