• 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

Is hashcode same for equal objects ?

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic