INTERACTIVE JAVA LAB Stack, Heap & References
Step 1 / 1

Java Source Code

System.out Console

JVM Memory Model — Stack on left, Heap on right

Call Stack (LIFO)
Heap Memory (Objects)
Primitive value Reference (@addr) null GC eligible
Ready. Step through the code with Next ▶
Stack vs. Heap — Core JVM Rules
ConceptStack MemoryHeap Memory
What lives here?Method call frames, local primitive variables, and object reference variablesAll 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 referencesDoes not copy the object! Both references point to the same Heap object
Method callsJava is strictly pass-by-value: parameter receives a copy of the argument's stack slotMethods can mutate an object's fields via the copied reference, but cannot rebind the caller's variable
LifetimeAutomatically popped when the method returnsReclaimed by the Garbage Collector once zero live references point to it
Classroom Teaching Guide
1. Trace the arrows: Emphasize that a variable like 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.