• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is hashcode same for equal objects ?  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
This I read from following link http://www.jusfortechies.com/java/core-java/hashcode.php.

I have read similar kins of statement before also. This is very confusing for me. If there are two objects on the heap then each one of them has unique HashCode. If both these objects are equal, then values of their fields are equal, which are compared by overriding equals(). But still they are different objects and have different hashcode values. So what does this statement mean that "hashCode method on each of the two objects must produce the same integer result." ?

I asked similar question before also, but did not get satisfactory replies.

My simple understanding is "for two objects on heap their hash code values are unique, irrespective of their field values. If two objects have same hash code value then that means both references are pointing to same object on heap"

Please correct me if I am wrong.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37384
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Just because two objects are "physically" different doesn't meant their hash code is different. A hash code isn't an address in memory. As an example, run this code:



It prints false, true, true. The two objects are different. But they have the same equals and hash code because they reflect the same value. And String defines objects as equal if they contain the same literal characters in the same order.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:A hash code isn't an address in memory.


Quoted For Emphasis.

And to elaborate a bit: Hashcodes were never intended to be unique. They're intended to be a "summary" of the object. The idea is that unequal objects (by equals()) will have unequal hashcodes. It's a way to quickly find the object we're looking for, or quickly determine that it's not present. If our hashing function is well-designed, then unequal objects will be statistically unlikely to have equal hashcodes--but it won't be impossible.

Study up on hash fucntions for more details.
 
neha chaukar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the example you show above the third condition will return true only if we override hashcode method .If we do not over ride then hashcode method Object class will be called whilch will return integer value of memory address of both the objects.M i right in this ???
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16043
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hashCode() method is overridden in class String, so that version is used here, not Object.hashCode().

neha chaukar wrote:... whilch will return integer value of memory address of both the objects.

Forget about memory addresses. Hash codes do not have (in principle) anything to do with memory addresses. Yes, on the Oracle JVM, Object.hashCode() will return a hash code that's based on the memory address of the object, but that's an unimportant implementation detail, that confuses a lot of people (they think that a hash code is a memory address).
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neha chaukar wrote:In the example you show above the third condition will return true only if we override hashcode method .If we do not over ride then hashcode method Object class ...


Note, also though, that the above sample also relies on the .equals() method being overriden so the two different Strings with the same value return true. The default behavior in Object is that .equals() called on two different Objects returns false. Read the API document for equals and hashcode. There is a contract that if you override one then you should override the other so they perform consistently. If you override equals you are supposed to override hashCode to make sure instances that are equal() produce the same hashCode(). It is something you have to make happen not something that happens automatically.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!