Skip to content

Commit 6d085ce

Browse files
authored
Merge pull request eugenp#9646 from alimate/BAEL-4115
BAEL-4115: Memory Address of Objects in Java
2 parents 5718aa6 + 010bee0 commit 6d085ce

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.memaddress;
2+
3+
import org.junit.Test;
4+
import org.openjdk.jol.vm.VM;
5+
6+
public class MemoryAddressUnitTest {
7+
8+
@Test
9+
public void printTheMemoryAddress() {
10+
String answer = "42";
11+
12+
System.out.println("The memory address is " + VM.current().addressOf(answer));
13+
}
14+
15+
@Test
16+
public void identityHashCodeAndMemoryAddress() {
17+
Object obj = new Object();
18+
19+
System.out.println("Memory address: " + VM.current().addressOf(obj));
20+
System.out.println("hashCode: " + obj.hashCode());
21+
System.out.println("hashCode: " + System.identityHashCode(obj));
22+
System.out.println("toString: " + obj);
23+
}
24+
}

0 commit comments

Comments
 (0)