The question is published on by Tutorial Guruji team.
lets say i have this variable :
... Somekindofobject var = new Somekindofobject(); ...
and i want to know where var is located on the heap ( by address , like 0x08 and so on),and to print the address out .
is it possible?
i am working on a program that gets as input java program , and instruments code that prints out to file information about variable access. The only way that i can determine between two fields of objects of the same class is by thier address on heap. Therefore, i need the address on heap
You can use
System.identityHashCode to get a notion of sameness.
It’s not perfect, but it’s pretty good.
If you do get the heap address of an object, remember that the JVM is allowed to move objects around (and frequently does when a generational garbage collector promotes a long lived object to an older generation) so the heap location of an object is not a good proxy for identity in all circumstances.