Memory address of variables in Java Memory address of variables in Java java java

Memory address of variables in Java


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.


It is possible using sun.misc.Unsafe : see this great answer from @Peter Lawrey -> Is there a way to get a reference address?

Using its code for printAddresses() :

    public static void printAddresses(String label, Object... objects) {    System.out.print(label + ": 0x");    long last = 0;    int offset = unsafe.arrayBaseOffset(objects.getClass());    int scale = unsafe.arrayIndexScale(objects.getClass());    switch (scale) {    case 4:        long factor = is64bit ? 8 : 1;        final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;        System.out.print(Long.toHexString(i1));        last = i1;        for (int i = 1; i < objects.length; i++) {            final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;            if (i2 > last)                System.out.print(", +" + Long.toHexString(i2 - last));            else                System.out.print(", -" + Long.toHexString( last - i2));            last = i2;        }        break;    case 8:        throw new AssertionError("Not supported");    }    System.out.println();}

I set up this test :

    //hashcode    System.out.println("Hashcode :       "+myObject.hashCode());    System.out.println("Hashcode :       "+System.identityHashCode(myObject));    System.out.println("Hashcode (HEX) : "+Integer.toHexString(myObject.hashCode()));    //toString    System.out.println("toString :       "+String.valueOf(myObject));    printAddresses("Address", myObject);

Here is the output :

Hashcode :       125665513Hashcode :       125665513Hashcode (HEX) : 77d80e9toString :       java.lang.Object@77d80e9Address: 0x7aae62270

Conclusion :

  • hashcode != address
  • toString = class@HEX(hashcode)


That is the output of Object's "toString()" implementation. If your class overrides toString(), it will print something entirely different.