aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode() problem" Watch "hashCode() problem" New topic
Author

hashCode() problem

Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115




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 ?






Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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.
Eli Wood
Ranch Hand

Joined: Sep 04, 2008
Posts: 37

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

Joined: Jun 07, 2011
Posts: 115

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

Joined: Apr 06, 2010
Posts: 4422
    
    8

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.
Minh Nam
Ranch Hand

Joined: Sep 10, 2011
Posts: 57
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.


Advanced java topics
Eli Wood
Ranch Hand

Joined: Sep 04, 2008
Posts: 37

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

Joined: Jun 07, 2011
Posts: 115



thank you matthew
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Eli Wood wrote:




thank you Eli for detailed explanation and for this cool trick
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

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

Joined: Jun 07, 2011
Posts: 115

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

Joined: Sep 04, 2008
Posts: 37

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

Joined: Jun 07, 2011
Posts: 115

thank you Eli i think i'll get it.
 
 
subject: hashCode() problem