Java Source Code
System.out Console
JVM Memory Model — Stack on left, Heap on right
Primitive value Reference (@addr) null GC eligible
Ready. Step through the code with Next ▶
Stack vs. Heap — Core JVM Rules
| Concept | Stack Memory | Heap Memory |
|---|---|---|
| What lives here? | Method call frames, local primitive variables, and object reference variables | All objects created with new (and their instance fields) |
Assignment (b = a) | Copies the bits inside the slot: primitive value for primitives, or memory address for references | Does not copy the object! Both references point to the same Heap object |
| Method calls | Java is strictly pass-by-value: parameter receives a copy of the argument's stack slot | Methods can mutate an object's fields via the copied reference, but cannot rebind the caller's variable |
| Lifetime | Automatically popped when the method returns | Reclaimed by the Garbage Collector once zero live references point to it |
Classroom Teaching Guide
1. Trace the arrows: Emphasize that a variable like
2. Pass-by-value clarity: In Scenario ③, watch how reassigning
3. Controls: Use ← / → keys on desktop or swipe left/right on mobile phones.
Box b1 is not the box—it is a remote control holding an address like @4a2f.2. Pass-by-value clarity: In Scenario ③, watch how reassigning
b = new Box(99) inside modify() only moves the local parameter's arrow, leaving main's arrow untouched.3. Controls: Use ← / → keys on desktop or swipe left/right on mobile phones.