1. Concept library
  2. Java & Spring Boot
  3. Module 3: JVM internals

Concept

Heap and stack

Where the JVM keeps local variables and where it keeps objects, and what a reference really is.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what lives on the stack and what lives on the heap
  2. Explain what a reference is and what a variable of an object type holds
  3. Predict what a method sees when it changes an object passed to it

In practice

A few lines that show Heap and stack doing its job.

Java
void example() {
    int count = 3;                        // primitive, lives in this frame
    var sb = new StringBuilder("hi");     // object on the heap; sb is a reference
    var alias = sb;                       // second reference, same object
    alias.append("!");
    System.out.println(sb);               // hi!
}   // the frame goes; the object stays until unreachable

The common mistake

Watch for

Thinking a variable of an object type "contains" the object. It holds a reference, so passing it to a method or assigning it lets that code change the same object.

What to know first

Your roadmap puts these before Heap and stack, unless you already know them.

How it stays learned

Once a session shows you understand Heap and stack, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Heap and stack.