Sub topic 6
Frames, Locals, and the Operand Stack
This chapter assumes that you can compile and run Java programs and understand methods, static members, and exceptions. You will use that knowledge to follow one method call inside the Java Virtual Machine (JVM).
A frame belongs to one method call
A stack frame is the JVM's working area for one method invocation. When a thread calls a method, the JVM creates a frame and places it on that thread's JVM stack. When the method returns, the JVM removes the frame.
A frame contains:
- A local-variable array
- An operand stack
- A reference to the runtime constant pool for the current class
The local-variable array stores method parameters and variables. The operand stack stores temporary values while instructions run. The constant-pool reference helps the JVM resolve symbolic names such as method and field references.
Each thread has its own stack, so frames for one thread are not directly shared with another thread. A frame's size is determined when the class is compiled.
Local variables use numbered slots
The local-variable array uses zero-based indexes called slots. For an instance method, slot 0 normally contains this, the reference to the current object. For a static method, slot 0 contains the first parameter.
Some values use two slots. A long or double occupies two consecutive local-variable slots. Most other primitive values and object references use one slot.
Consider this class:
public class MathOps {
public static int add(int a, int b) {
return a + b;
}
}Because add is static, slot 0 contains a and slot 1 contains b. The method does not need a slot for this.
The operand stack evaluates expressions
JVM bytecode uses a stack-based execution model. Instructions push values onto the operand stack and take values from it. An instruction such as iadd removes two integers, adds them, and pushes the result.
Compile the class and inspect its bytecode:
$ javac MathOps.java
$ javap -c MathOps
...
public static int add(int, int);
Code:
0: iload_0
1: iload_1
2: iadd
3: ireturnTrace the instructions:
iload_0pushes the integer in local slot 0. The stack is[a].iload_1pushes the integer in local slot 1. The stack is[a, b].iaddpopsaandb, then pushesa + b. The stack is[a + b].ireturnpops the result and returns it to the caller.
The JVM verifies that each instruction receives the correct kind and number of stack values. A malformed sequence, such as iadd with only one value available, cannot pass bytecode verification.
Method calls connect frames
A method invocation instruction creates a new frame. The caller supplies arguments, and the callee receives them in its local-variable slots. When the callee returns, its result becomes available to the caller, usually on the caller's operand stack.
The main invocation instructions are:
| Instruction | Typical use |
|---|---|
| `invokestatic` | Calls a static method |
| `invokevirtual` | Calls an ordinary instance method with dynamic dispatch |
| `invokeinterface` | Calls a method through an interface reference |
| `invokespecial` | Calls constructors, private methods, or selected superclass methods |
Understanding frames makes bytecode easier to read: track local-slot indexes, write down the operand stack after each instruction, and remember that a return removes the current frame.
Exercise
In static int multiply(int x, int y), which local slots contain x and y, and what does iload_0; iload_1; imul; ireturn return?
Show answer
x is in slot 0 and y is in slot 1. The instructions push both values, multiply them, and return x * y.