aspose file tools*
The moose likes Java in General and the fly likes can hashCode of two different object be same? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "can hashCode of two different object be same?" Watch "can hashCode of two different object be same?" New topic
Author

can hashCode of two different object be same?

viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

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
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 100

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.


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 27, 2009
Posts: 100


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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 07, 2011
Posts: 74

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

Joined: Nov 07, 2011
Posts: 74

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

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
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
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

Joined: Nov 07, 2011
Posts: 74

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.
 
 
subject: can hashCode of two different object be same?