-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryDiagrams-Java.tex
More file actions
476 lines (383 loc) · 11.9 KB
/
Copy pathMemoryDiagrams-Java.tex
File metadata and controls
476 lines (383 loc) · 11.9 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
\DocumentMetadata{
lang=en,
pdfversion=2.0,
testphase={phase-III}
}
\documentclass{article}
\usepackage{fullpage}
\usepackage{float}
\usepackage{graphicx}
\setkeys{Gin}{width=\textwidth,keepaspectratio}%,draft}
\usepackage[hidelinks]{hyperref}
\usepackage{bookmark}
\hypersetup{
pdftitle={Reference Guide for Stack Tracing in Java},
pdfauthor={},
pdflang={en},
pdfdisplaydoctitle=true,
bookmarksopen=true,
bookmarksnumbered=true,
}
\begin{document}
\title{Reference Guide for Stack Tracing in Java}
\author{}
\date{2026-03-28}
\maketitle
\section{Basic Java}
\begin{verbatim}
public static void main(String[] args) {
int anInt = 10;
double aDouble = 5.8;
boolean aBoolean = true;
String aString = "6.3";
anInt = 20;
}
\end{verbatim}
\begin{itemize}
\item variable changes result in previous values being crossed out and
new ones written in so that progression can be seen
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for main: stack frame with local variables anInt, aDouble, aBoolean,
and aString. anInt shows reassignment from 10 to
20 with the previous value crossed out.}]{1-variables.png}
\end{figure}
\pagebreak
\section{Method calls}
\subsection{Return value}
\begin{verbatim}
public static double multiplyByTwo(double input) {
double x = input * 2;
return x;
}
public static void main (String[] args) {
double x = 7.0;
double result = multiplyByTwo(x);
result = multiplyByTwo(result);
System.out.println(result);
int y = 3;
}
\end{verbatim}
\begin{itemize}
\item each function call is put in its own stack frame
\item variables created after the function call appear further down the stack
\item IO stands for input/output and is where any command line user input or
outputs in terms of print statements appear
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for multiplyByTwo and main: separate stack frames for
each call. parameters and local variables including x and result. return values flowing back to
the caller. references from stack variables to heap as needed. IO area showing println
output.}]{functionReturn.png}
\end{figure}
\pagebreak
\subsection{No return value}
\begin{verbatim}
public static void printValue(int n) {
int temp = n+2;
System.out.println("temp == " + temp);
}
public static void main (String[] args) {
printValue(4);
}
\end{verbatim}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for printValue and main. separate stack frames for each call.
local temp computed from the argument. println output in the IO area. no
return value passed back to main.}]{2_2-functionNoReturn.png}
\end{figure}
\pagebreak
\section{For loops}
\subsection{Basic}
\begin{verbatim}
public static void main (String[] args) {
int x = 4;
for (int i = 1; i < 5; i++) {
System.out.println("i == " + i);
}
}
\end{verbatim}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for a basic for loop. main frame with x and loop index i. progression
of i across iterations. println output for each i in the IO area.}]{forBasic.png}
\end{figure}
\subsection{Scoped Variable}
\begin{verbatim}
public static void main (String[] args) {
int x = 4;
for (int i = 1; i < 5; i++) {
int temp = i + 2;
}
}
\end{verbatim}
\begin{itemize}
\item the dashed lines indicates the variables that are scoped within the loop are crossed out after the loop completes
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for a for loop with scoped variable temp: temp exists only
inside the loop body. dashed or crossed-out notation after the loop ends to show temp is out of scope.}]{forScoped.png}
\end{figure}
\pagebreak
\section{ArrayLists}
\begin{verbatim}
public static void main (String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x = 0; x < 4; x++) {
arr1.add(x);
}
System.out.println(arr1);
ArrayList<Integer> arr2 = arr1;
System.out.println(arr2);
}
\end{verbatim}
\begin{itemize}
\item note that the assignment statement for arr2 assigns the memory address
and does not do a deep copy
\item memory addresses all start with 0x to indicate that they are
hexadecimal numbers.
\begin{itemize}
\item heap addresses are given 3 digit numbers in the tracing tool
\item numbers effectively random. The values are not important as long it is consistent across the stack and heap
\end{itemize}
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for two ArrayList variables: arr1 on the stack pointing to
an ArrayList object on the heap with elements added in the loop, and arr2 assigned from arr1 so
both references share the same heap object. addresses labeled consistently on stack and heap.}]{arrayLists.png}
\end{figure}
\pagebreak
\subsection{Passed to functions}
\begin{verbatim}
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
for (int x = 0; x < arrIn.size(); x++) {
out += arrIn.get(x);
}
return out;
}
public static void main (String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x = 0; x < 4; x++) {
arr1.add(x);
}
System.out.println(arr1);
ArrayList<Integer> arr2 = arr1;
System.out.println(arr2);
int total = sum(arr1);
System.out.println("total: " + total);
}
\end{verbatim}
\begin{itemize}
\item when passing variables to functions as arguments the value on the
stack associated with the variable name is the argument to the function
that sets the parameter, even when that value is a reference to an object on the heap
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for sum and main: main holds arr1 and arr2
referencing the same heap ArrayList. sum frame with parameter arrIn referencing that
same ArrayList. loop index and accumulator out while summing via get. return value
total on the stack.}]{arrayListsFunc.png}
\end{figure}
\pagebreak
\section{HashMaps}
\begin{verbatim}
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>();
bills.put("Allen", 17);
bills.put("Knox", 88);
for (String key : bills.keySet()) {
System.out.println(key);
}
}
\end{verbatim}
\begin{itemize}
\item HashMaps are like dictionaries in python
\item we loop through them in a manner more similar to loops in python
\item note that the loop still has scoped variables like the previous for loop
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for a HashMap bills: map object on the heap with
string keys and integer values. main frame. for-each over keySet with a scoped loop variable.
println keys in the IO area.}]{5-hashMaps.png}
\end{figure}
\pagebreak
\section{Recursion}
\subsection{Standard Recursion}
\begin{verbatim}
public static int computeGeometricSum(int n) {
if (n > 0) {
int result = computeGeometricSum(n - 1);
result += n;
return result;
} else {
return 0;
}
}
public static void main (String[] args) {
int result = computeGeometricSum(3);
}
\end{verbatim}
\begin{itemize}
\item each new call of computeGeometricSum is performed in a new color
\begin{itemize}
\item returned values are kept in the color of the method that returns it
\end{itemize}
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for standard recursion computeGeometricSum:
stack frames for each recursive call. local result
and return arrows showing values passed back. base case at n equals 0.}]{recursionStandard.png}
\end{figure}
\pagebreak
\subsection{Tail Recursion}
\begin{verbatim}
public static int computeGeometricSumTail(int n, int total) {
if (n > 0) {
return computeGeometricSumTail(n - 1, total + n);
} else {
return total;
}
}
public static int cGSTHelper(int n) {
return computeGeometricSumTail(n, 0);
}
public static void main (String[] args) {
int result = cGSTHelper(3);
}
\end{verbatim}
\begin{itemize}
\item Note that all the return arrows point to the original return variable when using tail recursion. It is also acceptable to omit
the return arrows entirely since the recursive calls do not return direectly to a variable
\end{itemize}
\textit{(Solution on next page)}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for tail recursion computeGeometricSumTail and helper:
frames with n and total. return arrows pointing through the chain to the original
caller result variable rather than to intermediate locals.}]{recursionTail.png}
\end{figure}
\pagebreak
\section{Classes}
\begin{verbatim}
public class Player {
private double xLoc;
private double yLoc;
private int maxHP;
private int HP;
private int damageDealt;
public Player(double xLoc, double yLoc, int maxHP) {
this.xLoc = xLoc;
this.yLoc = yLoc;
this.maxHP = maxHP;
this.HP = maxHP;
this.damageDealt = 4;
}
public int getHP() {
return this.HP;
}
public void takeDamage(int damage) {
this.HP -= damage;
}
public void attack(Player otherPlayer) {
otherPlayer.takeDamage(this.damageDealt);
}
public void move(double dx, double dy) {
this.xLoc += dx;
this.yLoc += dy;
}
public static void main(String[] args) {
Player player1 = new Player(0.0, 0.0, 10);
Player player2 = new Player(7.0, -4.0, 10);
player2.move(-6.5, 3.4);
player2.attack(player1);
}
}
\end{verbatim}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for Player objects: two Player instances on the heap with
fields xLoc, yLoc, maxHP, HP, and damageDealt. main with player1 and player2 references. effects
of move and attack on instance variables.}]{7-classes.png}
\end{figure}
\pagebreak
\section{Inheritance}
\begin{verbatim}
public class GameItem {
private double xLoc;
private double yLoc;
public GameItem(double xLoc, double yLoc) {
this.xLoc = xLoc;
this.yLoc = yLoc;
}
public void move(double dx, double dy) {
this.xLoc += dx;
this.yLoc += dy;
}
}
public class Teleporter extends GameItem {
private double dx;
private double dy;
public Teleporter(double xLoc, double yLoc, double dx, double dy) {
super(xLoc, yLoc);
this.dx = dx;
this.dy = dy;
}
public static void main(String[] args) {
Teleporter t = new Teleporter(2, 2, 3, 3);
t.move(2, 3);
}
}
\end{verbatim}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for GameItem and Teleporter: Teleporter object on the heap
with subclass fields dx and dy plus inherited xLoc and yLoc. constructor calling super constructor. stack
frame for main and reference t. move updating locations.}]{8-inheritance.png}
\end{figure}
\pagebreak
\section{Polymorphism}
\begin{verbatim}
public class A {
protected int a;
public A(int a) {
this.a = a;
}
}
public class B extends A{
private int b;
public B(int b) {
super(b);
this.b = b*2;
}
}
public class C extends A{
private int c;
public C(int a, int c) {
super(a);
this.c = c;
}
}
public class RunABC {
public static void main(String[] args) {
A a = new A(1);
A b = new B(2);
A c = new C(3, 4);
}
}
\end{verbatim}
\begin{figure}[H]
\centering
\includegraphics[alt={Memory diagram for polymorphism with classes A, B, and C: variables
of type A in main holding references to an A instance, a B instance, and a C instance on
the heap. each object shows its actual instance variables including a, b, or c.}]{9-polymorphism.png}
\end{figure}
\end{document}