|
![]() |
can it be possible ,two different object have same 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.
Lokesh
( SCBCD 5, CCENT, SCJP 5 )
viki Bhardwaj wrote:as i know hashCode is unique no
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.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.
Lokesh
( SCBCD 5, CCENT, SCJP 5 )
lokesh sree wrote:
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.
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.
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()
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?
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.
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
|