how to get address of object in java

Solutions on MaxInterview for how to get address of object in java by the best coders in the world

showing results for - "how to get address of object in java"
Ricardo
02 Jan 2019
1    public static void printAddresses(String label, Object... objects) {
2    System.out.print(label + ": 0x");
3    long last = 0;
4    int offset = unsafe.arrayBaseOffset(objects.getClass());
5    int scale = unsafe.arrayIndexScale(objects.getClass());
6    switch (scale) {
7    case 4:
8        long factor = is64bit ? 8 : 1;
9        final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;
10        System.out.print(Long.toHexString(i1));
11        last = i1;
12        for (int i = 1; i < objects.length; i++) {
13            final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;
14            if (i2 > last)
15                System.out.print(", +" + Long.toHexString(i2 - last));
16            else
17                System.out.print(", -" + Long.toHexString( last - i2));
18            last = i2;
19        }
20        break;
21    case 8:
22        throw new AssertionError("Not supported");
23    }
24    System.out.println();
25}
26