The main method prints the values/hash values of each of the variables.
public static void main(String[] args) {
int n = 5; // primitive data type on the stack
StackHeapTest nRef = new StackHeapTest(); // no-argument constructor
// initial values (Stack)
System.out.println("\n\nInitial Values:");
System.out.println("\tn:\n\t\tvalue = " + n + "\n\t\t" + "hash = " + System.identityHashCode(n));
System.out.println("\tnRef.n:\n\t\tvalue = " + nRef.n + "\n\t\t" + "hash = " + System.identityHashCode(nRef.n));
System.out.println("\tnRef:\n\t\tvalue = " + nRef + "\n\t\t" + "hash = " + System.identityHashCode(nRef));
// running the static method changeInt
nRef.changeInt(n, nRef.n, nRef); // stack by value, heap by value, heap by reference
// final values (Stack)
System.out.println("\n\nFinal values:");
System.out.println("\tn:\n\t\tvalue = " + n + "\n\t\t" + "hash = " + System.identityHashCode(n));
System.out.println("\tnRef.n:\n\t\tvalue = " + nRef.n + "\n\t\t" + "hash = " + System.identityHashCode(nRef.n));
System.out.println("\tnRef:\n\t\tvalue = " + nRef + "\n\t\t" + "hash = " + System.identityHashCode(nRef));
}
Output
This is the output for the main method:
Initial Values:
n:
value = 5
hash = 1407343478
nRef.n:
value = 5
hash = 1407343478
nRef:
value = com.nighthawk.hacks.StackHeapTest@77468bd9
hash = 2001112025
Before Changes:
n:
value = 5
hash = 1407343478
nRefN:
value = 5
hash = 1407343478
nRef.n:
value = 5
hash = 1407343478
nRef:
value = com.nighthawk.hacks.StackHeapTest@77468bd9
hash = 2001112025
After Changes:
n:
value = 15
hash = 1627800613
nRefN:
value = 15
hash = 1627800613
nRef.n:
value = 15
hash = 1627800613
nRef:
value = com.nighthawk.hacks.StackHeapTest@77468bd9
hash = 2001112025
Final values:
n:
value = 5
hash = 1407343478
nRef.n:
value = 15
hash = 1627800613
nRef:
value = com.nighthawk.hacks.StackHeapTest@77468bd9
hash = 2001112025
Reflection
We can see that the values of each of the variables starts off the same, and the nRef object reference is @77468bd9.
After calling the method, the variable values also remain the same, including the object reference @77468bd9, as their values are copied into the method.
After changes are made and the method is finished, only the value of the nRef.n has been effectively changed (from 5 to 15) because the object is referenced from the copied reference (which points to the same object) and its value is changed in the heap.
Code Setup and Discussion
Location
I created a new class at this location in the
hacks
folder ofspring_portfolio
.changeInt Static Method
The
changeInt
method used to modify the values in the test, plus print lines to show the status of values, is shown below:Main Method
The main method prints the values/hash values of each of the variables.
Output
This is the output for the main method:
Reflection
We can see that the values of each of the variables starts off the same, and the nRef object reference is @77468bd9.
After calling the method, the variable values also remain the same, including the object reference @77468bd9, as their values are copied into the method.
After changes are made and the method is finished, only the value of the nRef.n has been effectively changed (from 5 to 15) because the object is referenced from the copied reference (which points to the same object) and its value is changed in the heap.
Additions of String and double
Modified Code
New Output