⚛
Object Orientation Context
AP Computer Science A · Primitives vs References
⛶ Fullscreen
Stack vs Heap
Understanding Java memory is critical for tracking object state and aliasing bugs.
The Stack
Holds local variables (primitives) and
reference variables
(pointers to objects).
The Heap
Dynamically allocates memory using the
new
keyword. Contains the actual Object data structures.
Aliasing
Copying a reference (
a = b;
) does NOT copy the object! They both point to the same Heap address.
Tags
Stack
Heap
References
Primitives
Stack Memory
Local variables & Parameters
Heap Memory
Dynamic Objects (new keyword)
↻ Reset Environment
1. Object Instantiation & Aliasing
Robot
a
=
new
Robot(
"R2D2"
);
Robot
b
= a;
b.setName(
"BB8"
);
2. Primitive Copied By Value
int
x =
5
;
int
y = x;
y =
10
;
Console Output
System.out...