drewreed2005 / dre2.0

dre2.0
Apache License 2.0
0 stars 0 forks source link

3/25 Warm-Up Hack (Stack Heap Test) - Drew Reed, Period 3 #9

Open drewreed2005 opened 7 months ago

drewreed2005 commented 7 months ago

Code Setup and Discussion

Location

Screen Shot 2024-03-25 at 11 48 31 AM

I created a new class at this location in the hacks folder of spring_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:

public static void changeInt(int nValue, int nRefN, StackHeapTest nRef) {
    // before section
    System.out.println("\n\nBefore Changes:");
    System.out.println("\tn:\n\t\tvalue = " + nValue + "\n\t\t" + "hash = " + System.identityHashCode(nValue));
    System.out.println("\tnRefN:\n\t\tvalue = " + nRefN + "\n\t\t" + "hash = " + System.identityHashCode(nRefN));
    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));

    // add 10 to all three
    nValue += 10;
    nRefN += 10;
    nRef.n += 10;

    // after section
    System.out.println("\n\nAfter Changes:");
    System.out.println("\tn:\n\t\tvalue = " + nValue + "\n\t\t" + "hash = " + System.identityHashCode(nValue));
    System.out.println("\tnRefN:\n\t\tvalue = " + nRefN + "\n\t\t" + "hash = " + System.identityHashCode(nRefN));
    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));
}

Main Method

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.

Additions of String and double

Modified Code

public class StackHeapTest {
    public int n = 5; // primitive data type on the heap
    public String s = "Hello"; // reference type, reference from the heap
    public double d = 5.0; // primitive data type on the heap

    public static void changeInt(int nValue, int nRefN, StackHeapTest nRef, String sValue, double dValue) {
        // before section
        System.out.println("\n\nBefore Changes:");
        System.out.println("\tn:\n\t\tvalue = " + nValue + "\n\t\t" + "hash = " + System.identityHashCode(nValue));
        System.out.println("\tnRefN:\n\t\tvalue = " + nRefN + "\n\t\t" + "hash = " + System.identityHashCode(nRefN));
        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));
        System.out.println("\ts:\n\t\tvalue = " + sValue + "\n\t\t" + "hash = " + System.identityHashCode(sValue));
        System.out.println("\tnRef.s:\n\t\tvalue = " + nRef.s + "\n\t\t" + "hash = " + System.identityHashCode(nRef.s));
        System.out.println("\td:\n\t\tvalue = " + dValue + "\n\t\t" + "hash = " + System.identityHashCode(dValue));
        System.out.println("\tnRef.d:\n\t\tvalue = " + nRef.d + "\n\t\t" + "hash = " + System.identityHashCode(nRef.d));

        // add 10 to all three
        nValue += 10;
        nRefN += 10;
        nRef.n += 10;
        sValue += " World!";
        nRef.s += " World!";
        dValue += 5.0;
        nRef.d += 5.0;

        // after section
        System.out.println("\n\nAfter Changes:");
        System.out.println("\tn:\n\t\tvalue = " + nValue + "\n\t\t" + "hash = " + System.identityHashCode(nValue));
        System.out.println("\tnRefN:\n\t\tvalue = " + nRefN + "\n\t\t" + "hash = " + System.identityHashCode(nRefN));
        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));
        System.out.println("\ts:\n\t\tvalue = " + sValue + "\n\t\t" + "hash = " + System.identityHashCode(sValue));
        System.out.println("\tnRef.s:\n\t\tvalue = " + nRef.s + "\n\t\t" + "hash = " + System.identityHashCode(nRef.s));
        System.out.println("\td:\n\t\tvalue = " + dValue + "\n\t\t" + "hash = " + System.identityHashCode(dValue));
        System.out.println("\tnRef.d:\n\t\tvalue = " + nRef.d + "\n\t\t" + "hash = " + System.identityHashCode(nRef.d));
    }

    public static void main(String[] args) {
        int n = 5; // primitive data type on the stack
        String s = "Hello"; // object reference on the stack
        double d = 5.0; // 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));
        System.out.println("\ts:\n\t\tvalue = " + s + "\n\t\t" + "hash = " + System.identityHashCode(s));
        System.out.println("\tnRef.s:\n\t\tvalue = " + nRef.s + "\n\t\t" + "hash = " + System.identityHashCode(nRef.s));
        System.out.println("\td:\n\t\tvalue = " + d + "\n\t\t" + "hash = " + System.identityHashCode(d));
        System.out.println("\tnRef.d:\n\t\tvalue = " + nRef.d + "\n\t\t" + "hash = " + System.identityHashCode(nRef.d));

        // running the static method changeInt
        nRef.changeInt(n, nRef.n, nRef, s, d); // 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));
        System.out.println("\ts:\n\t\tvalue = " + s + "\n\t\t" + "hash = " + System.identityHashCode(s));
        System.out.println("\tnRef.s:\n\t\tvalue = " + nRef.s + "\n\t\t" + "hash = " + System.identityHashCode(nRef.s));
        System.out.println("\td:\n\t\tvalue = " + d + "\n\t\t" + "hash = " + System.identityHashCode(d));
        System.out.println("\tnRef.d:\n\t\tvalue = " + nRef.d + "\n\t\t" + "hash = " + System.identityHashCode(nRef.d));
    }
}

New Output

Initial Values:
        n:
                value = 5
                hash = 1407343478
        nRef.n:
                value = 5
                hash = 1407343478
        nRef:
                value = com.nighthawk.hacks.StackHeapTest@77468bd9
                hash = 2001112025
        s:
                value = Hello
                hash = 1627800613
        nRef.s:
                value = Hello
                hash = 1627800613
        d:
                value = 5.0
                hash = 2065530879
        nRef.d:
                value = 5.0
                hash = 1254526270

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
        s:
                value = Hello
                hash = 1627800613
        nRef.s:
                value = Hello
                hash = 1627800613
        d:
                value = 5.0
                hash = 662441761
        nRef.d:
                value = 5.0
                hash = 1618212626

After Changes:
        n:
                value = 15
                hash = 1023714065
        nRefN:
                value = 15
                hash = 1023714065
        nRef.n:
                value = 15
                hash = 1023714065
        nRef:
                value = com.nighthawk.hacks.StackHeapTest@77468bd9
                hash = 2001112025
        s:
                value = Hello World!
                hash = 2051450519
        nRef.s:
                value = Hello World!
                hash = 99747242
        d:
                value = 10.0
                hash = 1837543557
        nRef.d:
                value = 10.0
                hash = 1971489295

Final values:
        n:
                value = 5
                hash = 1407343478
        nRef.n:
                value = 15
                hash = 1023714065
        nRef:
                value = com.nighthawk.hacks.StackHeapTest@77468bd9
                hash = 2001112025
        s:
                value = Hello
                hash = 1627800613
        nRef.s:
                value = Hello World!
                hash = 99747242
        d:
                value = 5.0
                hash = 985655350
        nRef.d:
                value = 10.0
                hash = 804611486