Articles
Stack and heap: a simple guide to program memory
From an integer variable to a string reference: a simple explanation of stack, heap, LIFO and data lifetime using the JVM teaching model.

We often hear about the stack and heap when discussing program memory. Where do values go, and how do these areas differ? The useful starting point is not a split between “fixed-size” and “variable-size” data, but how data is organised and how long it is needed.
We will use Java and the Java Virtual Machine (JVM) as a teaching model for the two examples below. Languages and runtimes differ, so this model should not be applied to every program.
The stack
Start with a local variable inside a method:
int x = 5;In the JVM model, calling a method creates a frame containing its local variables and working data. Here, the value of x belongs to the current frame. That frame is removed when the call finishes. Frames and local variables
An int does not belong on the stack just because it is primitive or fixed-size: it can be a field within an object on the heap. “Local” describes where a variable is declared; “primitive” describes its type. Kinds of variables in Java
A stack follows “last in, first out” (LIFO). Think of books placed on top of one another: the newest goes on top and is taken off first. Similarly, the most recent method call returns before execution continues in its caller.
The heap
Now take the same text example, inside a method:
String name = "yahea";Here, name is a local variable holding a reference to a String object, not the text itself. The model distinguishes the local reference from the object it refers to. The JVM heap supplies storage for objects and arrays; text length alone does not determine storage location. The heap
Local-variable portion of the frame
- x
- 5Integer value
- name
- Local reference, not the text itself
A conceptual model, not a physical-address map. Other frame components and implementation optimisations are not shown.
The two examples share a simplified teaching frame: x holds 5, while name holds a reference to a String object in the heap.
Concept reference: JVM specification — heap, frames and local variablesA String object's value cannot change after it is created. The variable can refer to another string object, but that does not resize the original object. The String type
The heap does not follow the same LIFO order, and it is not unlimited memory. In Java, a garbage collector reclaims storage for objects that are no longer needed rather than requiring the programmer to delete them manually.
A short comparison
| Aspect | Stack in the JVM model | Heap in the JVM model |
|---|---|---|
| Role | Method-call frames and their working data | Objects and arrays |
| Organisation | The newest frame is added and removed first | No required LIFO order |
| Lifetime | A method's frame belongs to its call | An object may outlive a method when references to it remain |
This is a model for understanding, not a guarantee of every value's physical location after JIT optimisations. Some object allocations can be eliminated while preserving program behaviour. HotSpot enhancements
In short, first ask whether you mean a local variable, a reference or an object. Then consider the runtime and the data's lifetime, rather than treating size or type alone as a rule for stack and heap.

