GeeCON Prague 2014*
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


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals and hashcode" Watch "Equals and hashcode" New topic
Author

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);
System.out.println(bl);
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());


Output :


false
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);

System.out.println(bl);
System.out.println(b2);
System.out.println(in1.hashCode());
System.out.println(in2.hashCode());
System.out.println(str.hashCode());
System.out.println(str1.hashCode());


Output :

true
true
1
1
-1052563509
-1052563509


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

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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.

Henry


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: 14190
    
  20

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 8 API documentation
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,
Suhas
http://www.xplore-java.blogspot.com/
Hetal Vora
Greenhorn

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%
 
GeeCON Prague 2014
 
subject: Equals and hashcode