| Author |
Getting an Object based on hashcode
|
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Hi ,
I have written a java class to generate the hashcode for an Object.
The hashcode value is 19770577
I am getting the hashcode generated by the JVM for a newly Object created as shown .
Now as i know the location of the address .
Is it possible to get that Object beased on this value .
For example i want to get the Object that is stored in 19770577.
Thank you .
(Sorry for asking queries on Christmas day )
|
Save India From Corruption - Anna Hazare.
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
I don't really understand what you try to achieve here.
What you do is call the hashCode method inherited from Object, which always returns a unique number for an object reference.
What exactly is your question ?
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Ravi Kiran V wrote:
Now as i know the location of the address .
The identity hashcode is *not* the location/address of the object. It may have been originally generated using the address. But even if you can calculate the address from the identity hashcode, objects are moved around by the garbage collector (while the identity hashcode doesn't change). If there is a relationship between the memory address and the identity hashcode of the object, it won't remain for very long.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Venu Chakravorty
Ranch Hand
Joined: Aug 19, 2009
Posts: 46
|
|
|
I think what Ravi is trying to say is that: 'suppose we have N objects all having a hashcode value of 10, then how do we obtain references to those N objects provided we know the hashcode value of any one object.'
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
venu chakravorty wrote:I think what Ravi is trying to say is that: 'suppose we have N objects all having a hashcode value of 10, then how do we obtain references to those N objects provided we know the hashcode value of any one object.'
Short answer. Can't be done.
Longer answer. In theory, you can probably use JVMTI, which is an interface used by profilers to walk the heap, to find all reachable objects, and call the hashCode() method to find all matching objects.
And considering that this is the "beginning java" forum, the short answer is probably the better answer...
Henry
|
 |
Venu Chakravorty
Ranch Hand
Joined: Aug 19, 2009
Posts: 46
|
|
|
Thanks Henry. The short was was the better answer for me. :-)
|
 |
 |
|
|
subject: Getting an Object based on hashcode
|
|
|