• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting object location in memory?

 
Ankit Yadav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just out of curiosity and the required in some problem, i want to know from the experts is there any way we can refer any particular object if if have its hashcode?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to have a reference to that object to access it.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Ankit Yadav wrote:is there any way we can refer any particular object if if have its hashcode?

If all you have is the hash code, then no, it is not possible to find that object in memory.

The hash code is not a memory location, nor does it contain any implicit reference to the object. Suppose you wrote a class like this, which is perfectly legal:

All instances of class Example would return the value 4 for the hash code. You can see that there's clearly no way to get to the Example object if you only know its hash code.

Besides this, Java does not allow you to access memory directly via memory addresses; Java does not have pointers, like you have in C or C++.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the value returned by System.identityHashCode does not return the memory location. That's hidden internally in the JVM and cannot be retrieved.
 
Ankit Yadav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of thanks for memorizing that..... actually this question came up as i was trying to build a linkedlist, apart from having stored the references as pointer to next node is there any other way we can achieve that?

Jesper de Jong wrote:
Mr. Ankit Yadav wrote:is there any way we can refer any particular object if if have its hashcode?

If all you have is the hash code, then no, it is not possible to find that object in memory.

The hash code is not a memory location, nor does it contain any implicit reference to the object. Suppose you wrote a class like this, which is perfectly legal:

All instances of class Example would return the value 4 for the hash code. You can see that there's clearly no way to get to the Example object if you only know its hash code.

Besides this, Java does not allow you to access memory directly via memory addresses; Java does not have pointers, like you have in C or C++.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no concept of pointers in Java. All you have are references pointing to your objects in the heap memory. (though references are somewhat (to some extent) like pointers in the sense they give you access to something in the memory, but unlike pointers you cannot do reference++ to get to the next memory location.)
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Ankit Yadav wrote:of thanks for memorizing that..... actually this question came up as i was trying to build a linkedlist, apart from having stored the references as pointer to next node is there any other way we can achieve that?

Why would you want to? With a class like:you have the basis for a forward-LinkedList. What makes you think you need to know where the Node is in memory?

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mr. Ankit Yadav wrote:of thanks for memorizing that..... actually this question came up as i was trying to build a linkedlist, apart from having stored the references as pointer to next node is there any other way we can achieve that?

Why would you want to? With a class like:you have the basis for a forward-LinkedList. What makes you think you need to know where the Node is in memory?

Winston


And, for that matter, once you populate the "next" variable, its value does tell the JVM where to find that Node in memory. We programmers cannot see that value, and we can only use it to access that node and compare to other values for equality, but that's all we need to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic