• 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

can hashCode of two different object be same?

 
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!,
i am curious to know that can it be possible ,two different object have same hashcode?.. i am confused that how hashCode() method works.
and how the jvm allocates unique value to object. let me explain my understanding about hashCode:
as i know hashCode is unique no which is generated by jvm during object creation based on the address of an object to provide uniqueness.
and hashCode() is a non final method so we can override and can give our implementation so if once i have overridden the haashCode() then what will happen about uniqueness .
please correct me if i am wrong in above statement and please help me to clear my doubts by posting your reasonable answer.
thanks for your time to given for this post.

_____________________________________________________________________________ -vivek
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

can it be possible ,two different object have same hashcode?


Depends on which object you are talking about and what is the implementation of hashcode() method for that object.

as i know hashCode is unique no which is generated by jvm during object creation based on the address of an object to provide uniqueness.


Yes, that's correct. But that is about the jvm implementation for the java's Object class. It can always be overriden and its upto the overriding class to make sure that its implementation is unique. Look at the jdk code of java's String class to see one such override. And, yes , you can always just do a "return 1" in the overriden hashcode() method in your class. But that violates the basic principle of having a unique hashcode to differentiate between objects. Hope this clarifies.

~Lokesh.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it isn't. There is nothing unique about hash codes at all. Look at this recent thread where that sort of thing is discussed.
 
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

viki Bhardwaj wrote:as i know hashCode is unique no



The hashCode() method returns an int. There are 4,294,967,296 different possible int values.

There is an object called Long, which has 18,446,744,073,709,551,616 different possible values.

Based on this alone, the pigeonhole principle tells us that hashCode() values cannot possibly be unique. When you start to bring in Strings and more complex object graphs, it becomes even more obvious.
 
Jeff Verdegan
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

lokesh sree wrote:

as i know hashCode is unique no which is generated by jvm during object creation based on the address of an object to provide uniqueness.


Yes, that's correct.



No.

That is very, very much not correct.
 
lokesh sree
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ok, when I said hashCode will be unique, I was only talking about the implementation of the java's Object class. To quote, from the javadocs

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.



I will redirect the OP to the javadoc for more clarity.. http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()
 
Jeff Verdegan
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

lokesh sree wrote:
Ok, when I said hashCode will be unique, I was only talking about the implementation of the java's Object class.



There are 4,294,967,296 possible hashCode values. If we create 4,294,967,297 Objects, then, again, the pigeonhole principle tells us that no, it won't be unique.

To quote, from the javadocs

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.



Right. "As much as is reaonsably practical." So, in other words, "Don't assume it's unique, as it's not required to be."

 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

viki Bhardwaj wrote:as i know hashCode is unique no



The hashCode() method returns an int. There are 4,294,967,296 different possible int values.

There is an object called Long, which has 18,446,744,073,709,551,616 different possible values.

Based on this alone, the pigeonhole principle tells us that hashCode() values cannot possibly be unique. When you start to bring in Strings and more complex object graphs, it becomes even more obvious.



Thanks Jeff, now i understood as you explained but here i have one more doubt .....as i know after object creation object goes to bucket based on their hashcode and for each bucket there is a unique hashcode, a bucket can hold more than one object means two objects can be hold with the same hashcode value so here want to know that:

how does java make every object unique?
 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No it isn't. There is nothing unique about hash codes at all. Look at this recent thread where that sort of thing is discussed.


lokesh sree wrote:
I will redirect the OP to the javadoc for more clarity.. http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()



thanks Campbell and lokesh for such a link it was nice to have a more understanding.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

viki Bhardwaj wrote:.as i know after object creation object goes to bucket based on their hashcode and for each bucket there is a unique hashcode, a bucket can hold more than one object means two objects can be hold with the same hashcode value so here want to know that:

how does java make every object unique?


Each object is stored in a different location in memory - that's how they are distinguished.

Note that hash codes are only relevant to collections (or similar) that use hashing algorithms, like HashSet. A hash code might be related to (but not equal to) the location in memory but it doesn't have to be. But object creation is an entirely different thing.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You use the hash code to decide which bucket to put the object into in a hash based data structure. If you go through this thread, you will (I hope) find out how it is used.
 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You use the hash code to decide which bucket to put the object into in a hash based data structure. If you go through this thread, you will (I hope) find out how it is used.


thank you so much Campbell , for such link it was much enough talking about the point.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic