• 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

Getting an Object based on hashcode

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. The short was was the better answer for me. :-)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic