• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

hashCode() problem

 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I know that every object has hashcode in integer form as a key to identify that object, and according to documentation of hashCode(), it returns hashCode value of invoking object.Now question is that in my program hashCode() is return the value of instance variable value of obj1 and obj2. those are 2 and 12. Where is the hashcode of objects ?? i am feeling very irritated because i don't want value of variable if i want variable value then simply i can make a get() method and return the variable value then why to use this nonsense hashCode().

it gives hashCode value when i dont override hashCode() but it ought to override if equals() is overridden and why it gives different hashcode of two equal objects ?.
and what to do of these hashcode value now what is the use of these ?






 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hashCode() method in Object is based on the internal memory address of the object. So if you have two different objects, they will almost certainly have different hash codes if you haven't overridden it. Regardless of whether you've overridden equals() or not - it doesn't care about that. Which is why you should provide your own hashCode() method. The first example you've given is fine.

And the reason you do this, as I said in another thread, is that there is a "contract" between equals() and hashCode() (see the documentation for those methods) which says that if two objects are equal, they should have the same hash code (but not the other way around - you're allowed to have two unequal objects with the same hash code). If you break the contract, you break any code that relies on it. Which includes the HashMap and HashSet classes.
 
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Nandwana wrote:I know that every object has hashcode in integer form as a key to identify that object, and according to documentation of hashCode(), it returns hashCode value of invoking object.Now question is that in my program hashCode() is return the value of instance variable value of obj1 and obj2. those are 2 and 12. Where is the hashcode of objects ?? i am feeling very irritated because i don't want value of variable if i want variable value then simply i can make a get() method and return the variable value then why to use this nonsense hashCode(),I need hashcode please help me to find it .


In your first code snippet, the hashCode() function is returning exactly what you have programmed it to return. And that snippet also fulfills the equals/hashcode contract, since every two equal InstEqual objects will have equal hashcodes. so it works. If you would still like to use more complicated-looking hashcode values, you can try this function:



Nitesh Nandwana wrote:it gives hashCode value when i dont override hashCode() but it ought to override if equals() is overridden and why it gives different hashcode of two equal objects ?.


To override the hashCode function, you must actually write the code to override it, as you did in the first example. In this case you are just inheriting the hashCode() function of Object, which does not fulfill the contract for your new equals() function. This makes sense, since the code in the Object class knows nothing about your new class or any overriding functions.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eli Wood wrote: since every two equal InstEqual objects will have equal hashcodes.



In my last example, obj1 and obj2 are equal then why their hash code values are not same as you said above.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Nandwana wrote:In my last example, obj1 and obj2 are equal then why their hash code values are not same as you said above.


Because you've commented out the bit of code that makes that happen. If too objects are equal then they should have the same hash code, but it doesn't happen by magic. If you override equals(), it's up to you to override hashCode() as well in a consistent way.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Nandwana wrote:

Eli Wood wrote: since every two equal InstEqual objects will have equal hashcodes.



In my last example, obj1 and obj2 are equal then why their hash code values are not same as you said above.



Matthew Brown explained it.
 
Eli Wood
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Nandwana wrote:

Eli Wood wrote: since every two equal InstEqual objects will have equal hashcodes.



In my last example, obj1 and obj2 are equal then why their hash code values are not same as you said above.


I was referring to your first code snippet when I typed that.

When you comment out your hashCode() function, as in your second snippet, the code will use the hashCode() function from the Object class, which only fulfills the equals/hashCode contract with respect to the equals() method defined in Object.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


thank you matthew
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eli Wood wrote:




thank you Eli for detailed explanation and for this cool trick
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to get hashcode with overridden hashcode() because i want to check whether they are same or not.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eli Wood wrote:



.



According to hashCode() documentation, it is invoked with object, here in above code how did you invoked and why did you so i am not getting at all.
 
Eli Wood
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Nandwana wrote:

Eli Wood wrote:



.



According to hashCode() documentation, it is invoked with object, here in above code how did you invoked and why did you so i am not getting at all.

This code creates a new Integer using the "value" of this.value, calls the toString() method to return a string representation of the Integer, then calls the String class's hashCode() function(which overrides Object's hashCode() function) on that String to create a hash code.

But that code may be too dense for just learning and is really not important for this concept. If you don't understand, I would just forget it for now.

Your first code snippet, that just returns this.value, works perfectly well.



There is no rule against having a hash code with the same value of one of your class's members (although it may not be a best practice). What, exactly, do you feel is wrong with it?
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Eli i think i'll get it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic