Note: To run the file, I just pressed 'run' in the top-right, then clicked on a random parent class (I think I used Alphabet?)
package com.nighthawk.hacks;
public class StackHeapTest {
public int n = 5; // primitive data type on the heap
public static void changeInt(int nValue, int nRefN, StackHeapTest nRef, Integer number) {
// Before changes
System.out.println("\n\n\nBEFORE CHANGES: (Inside Object):");
System.out.println("\nn:\nvalue = " + nValue + "\n" + "hash = " + System.identityHashCode(nValue));
System.out.println("\nnRefN:\nvalue = " + nRefN + "\n" + "hash = " + System.identityHashCode(nRefN));
System.out.println("\nInteger:\nvalue = " + number + "\n" + "hash = " + System.identityHashCode(number));
// Add 10 to all parameters
nValue += 10;
nRefN += 10;
nRef.n += 10;
number += 10;
// After changes
System.out.println("\n\n\nAFTER CHANGES: (Inside Object)");
System.out.println("\nn:\nvalue = " + nValue + "\n" + "hash = " + System.identityHashCode(nValue)); // Changes
System.out.println("\nnRefN:\nvalue = " + nRefN + "\n" + "hash = " + System.identityHashCode(nRefN)); // Changes
System.out.println("\nInteger:\nvalue = " + number + "\n" + "hash = " + System.identityHashCode(number)); // Changes
}
public static void main(String[] args) {
// primitive data types on the stack
int n = 5;
int nRefN = 5;
Integer number = 5;
StackHeapTest nRef = new StackHeapTest();
// Initial Values (Stack)
System.out.println("\nINITIAL VALUES: (Outside Object)");
System.out.println("\nn:\nvalue = " + n + "\n" + "hash = " + System.identityHashCode(n));
System.out.println("\nnRefN:\nvalue = " + nRefN + "\n" + "hash = " + System.identityHashCode(nRefN));
System.out.println("\nnRef.n:\nvalue = " + nRef.n + "\n" + "hash = " + System.identityHashCode(nRef.n));
System.out.println("\nInteger:\nvalue = " + number + "\n" + "hash = " + System.identityHashCode(number));
// Run method
nRef.changeInt(n, nRefN, nRef, number);
// Final Values (Stack)
System.out.println("\n\n\nFINAL VALUES: (Outside Object)");
System.out.println("\nn:\nvalue = " + n + "\n" + "hash = " + System.identityHashCode(n)); // No change
System.out.println("\nnRefN:\nvalue = " + nRefN + "\n" + "hash = " + System.identityHashCode(nRefN)); // No change
System.out.println("\nnRef.n:\nvalue = " + nRef.n + "\n" + "hash = " + System.identityHashCode(nRef.n)); // Change
System.out.println("\nInteger:\nvalue = " + number + "\n" + "hash = " + System.identityHashCode(number)); // No change
}
}
Output
Mr. Mortensen recommended one line per value + hash, but this looks nicer on an issue (i.e. n: value = 5, hash = 245565335)
INITIAL VALUES: (Outside Object)
n:
value = 5
hash = 245565335
nRefN:
value = 5
hash = 245565335
nRef.n:
value = 5
hash = 245565335
Integer:
value = 5
hash = 245565335
BEFORE CHANGES: (Inside Object):
n:
value = 5
hash = 245565335
nRefN:
value = 5
hash = 245565335
Integer:
value = 5
hash = 245565335
AFTER CHANGES: (Inside Object)
n:
value = 15
hash = 2065530879
nRefN:
value = 15
hash = 2065530879
Integer:
value = 15
hash = 2065530879
FINAL VALUES: (Outside Object)
n:
value = 5
hash = 245565335
nRefN:
value = 5
hash = 245565335
nRef.n:
value = 15
hash = 2065530879
Integer:
value = 5
hash = 245565335
## Reflection
This was a helpful exercise for better understanding and visually seeing how stacks and heaps work. You can see that within the object, each changed value is stored in the stack and thus can only be referenced within the method/object. In the main method though, each value is stored in the heap and thus does not output the values changed within the object, except for ``nRef.n`` which *is* part of the object, thus accessing the stack.
camelCase title "StackHeapTest"
StackHeapTest
ObjectOutput
n: value = 5 hash = 245565335
nRefN: value = 5 hash = 245565335
nRef.n: value = 5 hash = 245565335
Integer: value = 5 hash = 245565335
BEFORE CHANGES: (Inside Object):
n: value = 5 hash = 245565335
nRefN: value = 5 hash = 245565335
Integer: value = 5 hash = 245565335
AFTER CHANGES: (Inside Object)
n: value = 15 hash = 2065530879
nRefN: value = 15 hash = 2065530879
Integer: value = 15 hash = 2065530879
FINAL VALUES: (Outside Object)
n: value = 5 hash = 245565335
nRefN: value = 5 hash = 245565335
nRef.n: value = 15 hash = 2065530879
Integer: value = 5 hash = 245565335