-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackHeapTest.java
46 lines (38 loc) · 1.82 KB
/
StackHeapTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.nighthawk.hacks;
public class StackHeapTest {
public int n = 5; // primative datatype on the heap
public static void changeInt(int nValue, int nRefn, StackHeapTest nRef) {
System.out.println("BEFORE:");
System.out.println("nValue : " + nValue);
System.out.println("nRefn : " + nRefn);
System.out.println("nRef.n : " + nRef.n);
System.out.println("HashCode : " + System.identityHashCode(nRef));
nValue += 10;
nRefn += 10;
nRef.n += 10;
System.out.println("AFTER:");
System.out.println("nValue : " + nValue);
System.out.println("nRefn : " + nRefn);
System.out.println("nRef.n : " + nRef.n);
System.out.println("HashCode : " + System.identityHashCode(nRef));
}
public static void main(String[] args) {
int n = 5; // primitive datatype on the stack
StackHeapTest nRef = new StackHeapTest();
System.out.println("BEFORE:");
System.out.println("n : " + n);
System.out.println("nRef : " + nRef);
System.out.println("nRef.n : " + nRef.n);
System.out.println("HashCode n : " + System.identityHashCode(n));
System.out.println("HashCode nRef.n : " + System.identityHashCode(nRef.n));
System.out.println("HashCode nRef : " + System.identityHashCode(nRef));
changeInt(n, nRef.n, nRef); // stack by value, heat by value, heat by reference
System.out.println("AFTER:");
System.out.println("n : " + n);
System.out.println("nRef : " + nRef);
System.out.println("nRef.n : " + nRef.n);
System.out.println("HashCode n : " + System.identityHashCode(n));
System.out.println("HashCode nRef.n : " + System.identityHashCode(nRef.n));
System.out.println("HashCode : " + System.identityHashCode(nRef));
}
}