2

I was wondering if it was possible to get some String value of an Object to access that Object on the same machine (same RAM) or the same VM via that particular String.

e.g.

Object objA1 = new Object();
System.out.print(objA1.adress); => output: d146a6581ed9e
Object objExt = Object.buildFromMemoryAdress("d146a6581ed9e");

I hope you understand what I'm trying to understand.

EDIT: I found in

http://javapapers.com/core-java/address-of-a-java-object/#&slider1=1

a Class that allows me to get the String of the logical address of an instance on the (VM?) memory: sun.misc.Unsafe

I think I can also use Unsafe to retrieve an Object from the (restricted to the VM?) memory.

If not possible like this, how would I do it, and since it's out of curiosity are there any other languages (especially high end) that allow direct memory access like this?

sinekonata
  • 364
  • 3
  • 11

3 Answers3

5

It is incorrect to assume that the number that you see in the toString() result is the memory address.

It is, in fact, the object's hash code. If the object isn't modified, its hash code remains constant. However, its memory address can change at any time: a compacting garbage collector can decide to move the object in memory whenever it feels like it.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • +1: Multiple objects can have the same hash code. Even for `System.identityHashCode(Object)` – Peter Lawrey Jan 18 '12 at 09:02
  • Thanks I did not assume it was the address. I wanted to know if some hashcode could be unique and if yes, retrievable from another separate project. You answered and discarded that possibility but I still want to know how to do so. I edited my question a lot since I had a lot of answers focusing on toString. – sinekonata Jan 18 '12 at 10:04
3

Absolutely not. In fact, it's clearly impossible, given that you can obviously have two different objects whose toString() methods return the same string. As a simple example:

Integer a = new Integer(10);
Integer b = new Integer(10);

Object x = Object.buildFromToString("10");

What should x refer to? The same object that a refers to, or the same object that b refers to?

toString() is not meant to return an object identifier - it's just meant to return some sort of textual representation of an object. Just because the default implementation returns something which looks a bit like an identifier shouldn't be taken as an indication that it should be used as an identifier.

If you want to store some way of accessing an object at some other point in time, I suggest you just store a reference to it as an Object variable.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Then what would be an Object identifier? HashCode is not unique either right? I edited my question a lot since I had a lot of answers focusing on toString. – sinekonata Jan 18 '12 at 10:05
  • @sinekonata: The answers focused on `toString()` because the question did :) I don't know of any way to get a useful object identifier in some textual representation - the simplest approach is just to keep a reference to the object within the code. You could potentially create a mapping from *your own* object identifier to object references (possibly via `WeakReference`) if you wanted to... but it's not clear what you're trying to achieve. – Jon Skeet Jan 18 '12 at 10:09
  • I simply want to do what the machine can do i.e. reading the object's address and tell the machine to construct another object using the given address. it is just out of curiosity as stated. The class sun.misc.Unsafe seems to be able to do hardware manipulations, I wondered if there was a simpler way than accessing the memory directly. – sinekonata Jan 18 '12 at 10:15
  • @sinekonata: You're not *meant* to do that in Java - it's a managed language. The VM is able to move objects around in memory and do all kinds of things for you precisely *because* you don't get to mess with the internals yourself. – Jon Skeet Jan 18 '12 at 10:30
2

No, this is not possible. Java objects are only accessible if you have a reference to those objects. What you can do is store your objects in a Map<String, Object> under a given name, and get back the reference of the object from its name, using the map.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255