/
Lesson_01_Stack_Vs_Heap.java
80 lines (70 loc) · 3.41 KB
/
Lesson_01_Stack_Vs_Heap.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hello World with Dr. Dan - A Complete Introduction to Programming from Java to C++ (Code and Course © Dan Grissom)
//
// Additional Lesson Resources from Dr. Dan:
// High-Quality Video Tutorials: www.helloDrDan.com
// Free Commented Code: https://github.com/DanGrissom/hello-world-dr-dan-java
//
// In this lesson you will learn:
// 1) Memory Hierarchy & Computer Organization
// 2) Memory Structure and Storing Data (binary, decimal, hexadecimal)
// 3) How the Stack and Heap are used to store variables and data
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.ArrayList;
public class Lesson_01_Stack_Vs_Heap {
///////////////////////////////////////////////////////////////
// MAIN - Entry point where code will begin execution for file
///////////////////////////////////////////////////////////////
public static void main(String[] args) { // Main method, which makes a call to methodA
// Simple welcome statements printed to screen
System.out.println("Program Objective: Learn the difference between the Stack and the Heap.");
System.out.println("===========================================================================");
// Create some variables
ArrayList<Integer> mainList = new ArrayList<Integer>(); // An object
int mainInt = 10; // A primitive
mainList.add(mainInt); // Add the integer to the arrayList
// Call methodA, passing in mainInt (by value BC primitive) and mainList (by reference BC object)
methodA(mainInt, mainList);
// Print the integer and list after ALL method calls to see which variables
// were impacted by the method calls; if a variable changed, it means it was
// passed by reference and was in the heap; if a variable did not change, it
// means it was passed by value and a copy was added to the stack frame of the
// method being called
System.out.println("Printing mainInt after all method calls: " + mainInt);
System.out.println("Printing all integers in mainList after all method calls:");
for (int i : mainList)
System.out.println("\t" + i);
}
////////////////////////////////////////////////////////////////////////////////
// This method takes in an integer (by value) and a list (by reference) and
// makes updates
// Parameters:
// aVar - An int, passed by value
// aList - An ArrayList of ints, passed by reference
// Returns:
// void (nothing)
////////////////////////////////////////////////////////////////////////////////
public static void methodA(int aVar, ArrayList<Integer> aList) {
// Modify the parameters
int methAInt = 20;
aVar++;
aList.add(aVar);
// Call methodB, passing in aVar (by value BC primitive) and aList (by reference BC object)
methodB(aVar, aList);
}
////////////////////////////////////////////////////////////////////////////////
// This method takes in an integer (by value) and a list (by reference) and
// makes updates
// Parameters:
// aVar - An int, passed by value
// aList - An ArrayList of ints, passed by reference
// Returns:
// void (nothing)
////////////////////////////////////////////////////////////////////////////////
public static void methodB(int bVar, ArrayList<Integer> bList) {
// Modify the parameters
int methBInt = 30;
bVar++;
bList.add(bVar);
}
}