Memory Address of Objects in Java

This commit is contained in:
Ali Dehghani 2020-07-06 12:06:26 +04:30
parent 960f1a578b
commit 15f10601bc
1 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.memaddress;
import org.junit.Test;
import org.openjdk.jol.vm.VM;
public class MemoryAddressUnitTest {
@Test
public void printTheMemoryAddress() {
String answer = "42";
System.out.println("The memory address is " + VM.current().addressOf(answer));
}
@Test
public void identityHashCodeAndMemoryAddress() {
Object obj = new Object();
System.out.println("Memory address: " + VM.current().addressOf(obj));
System.out.println("hashCode: " + obj.hashCode());
System.out.println("hashCode: " + System.identityHashCode(obj));
System.out.println("toString: " + obj);
}
}