• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in overriding hashcode ...

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ram chandra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic