Memory address of variables in Java

uzay95 picture uzay95 · Dec 25, 2009 · Viewed 156.2k times · Source

Please take a look at the picture below. When we create an object in java with the new keyword, we are getting a memory address from the OS.

When we write out.println(objName) we can see a "special" string as output. My questions are:

  1. What is this output?
  2. If it is memory address which given by OS to us:

    a) How can I convert this string to binary?

    b) How can I get one integer variables address?

alt text

Answer

Brian Agnew picture Brian Agnew · Dec 25, 2009

That is the class name and System.identityHashCode() separated by the '@' character. What the identity hash code represents is implementation-specific. It often is the initial memory address of the object, but the object can be moved in memory by the VM over time. So (briefly) you can't rely on it being anything.

Getting the memory addresses of variables is meaningless within Java, since the JVM is at liberty to implement objects and move them as it seems fit (your objects may/will move around during garbage collection etc.)

Integer.toBinaryString() will give you an integer in binary form.