This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in overriding hashcode ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in overriding hashcode ..." Watch "Doubt in overriding hashcode ..." New topic
Author

Doubt in overriding hashcode ...

ram chandra
Greenhorn

Joined: Mar 14, 2008
Posts: 4


Check the outputs ::
WeirdStringFixed@3039
WeirdStringFixed@3039
true
false


My doubt is why t1==t2 returning false , evenif I override the hashcode class?? I am bit confused about this fact the when I override the hashcode , and created 2 new object and print out its giving same address (WeirdStringFixed@3039) , but when i try t1==t2, its returning false . Why ?


Edit by mw: Added Code Tags.
[ May 26, 2008: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ram chandra:
...its giving same address (WeirdStringFixed@3039) , but when i try t1==t2, its returning false . Why ?

Welcome to JavaRanch!

Are you certain this represents an "address"? Why?

(If hashCode were not overridden, could you be certain this represents an address? Hint: Check the API documentation for hashCode in java.lang.Object.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100
Hi Ram,
== operator looks if two references refer to same object. equals(...) and hashcode() doesn't affect it. Since you have two different objects, each created with new operator, this comparison will always return false.


<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
ram chandra
Greenhorn

Joined: Mar 14, 2008
Posts: 4
Hi,

Thanks for the reply ..

Let me clear out the things ..

1. So when you create a new object ( other than default classes like String, Integer ) , there's no way 2 objects will become equal using == method . They can be made equal using "equals" method .

2. Is it true when you override equals method you have to override hashcode ? If so why ? In the code posted above , if you remove the hashcode override , then also it gives "true' for "equals" method
ram chandra
Greenhorn

Joined: Mar 14, 2008
Posts: 4
Ivan Ivanic
ranch hand
Member # 159041
posted Today 11:23 AM
--------------------------------------------------------------------------------
Hi Ram,
== operator looks if two references refer to same object. equals(...) and hashcode() doesn't affect it. Since you have two different objects, each created with new operator, this comparison will always return false.
--------------------

Use Real Words !!!Use Code Tags!!! Say Thanks
scjp6


Hi Ivan ...
You told that operator looks if two references refer to same object. equals(...) and hashcode() doesn't affect it . But if I dont override the hashcode

System.out.println(t1) and
System.out.println(t2) gives different values . After I override the hashcodes , then only t1 and t2 gives same values (WeirdStringFixed@3039
).
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100
But if I dont override the hashcode

System.out.println(t1) and
System.out.println(t2) gives different values . After I override the hashcodes , then only t1 and t2 gives same values (WeirdStringFixed@3039
).[/QB]

This is from API:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


So, original method hashcode defined in Object class returns int depending on internal address of the object.
When you override hashcode you are just changing what it will return. Underlying internal address is not affected with your override. So it doesn't affect result of ==.
And just to help you == is operator not method.


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ram chandra:
...Is it true when you override equals method you have to override hashcode ? If so why ? ...

If 2 objects are equal according to their equals method, then they should return the same hashCode. This ensures that instances will behave correctly when stored in a collection that uses hashing, like HashSet or HashMap.

For more detail about hashCode and equals, see the API documentation for the hashCode method of java.lang.Object.

By the way, you might note that your hashCode method is overridden to return a constant, 12345. In hex, this value is 3039, which is why you're seeing "WeirdStringFixed@3039."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in overriding hashcode ...
 
Similar Threads
doubt in Ch7 of K & B book
equals hashcode doubt sjcp 1.4
doubt in hashCode and Equals method
equals and hashCode??
Please Explain (Hashcode)