aspose file tools*
The moose likes Java in General and the fly likes getting object location in memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "getting object location in memory?" Watch "getting object location in memory?" New topic
Author

getting object location in memory?

Ankit Yadav
Greenhorn

Joined: Jul 07, 2009
Posts: 12
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

You need to have a reference to that object to access it.


Mohamed Sanaulla | My Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

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++.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Even the value returned by System.identityHashCode does not return the memory location. That's hidden internally in the JVM and cannot be retrieved.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ankit Yadav
Greenhorn

Joined: Jul 07, 2009
Posts: 12
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Mar 17, 2011
Posts: 8016
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
jQuery in Action, 2nd edition
 
subject: getting object location in memory?