Sub topic 1
Starting out: the bytecode interpreter
HotSpot usually starts a Java application by interpreting its bytecode. The interpreter is a JVM component that reads bytecode instructions and performs their actions at run time. It provides a quick way to begin execution before the JVM has compiled frequently used code into native machine code.
Prerequisites
This chapter assumes that you can write basic Java classes, methods, loops, and interfaces. You should also know that javac converts Java source code into bytecode stored in .class files, and that the java command starts the JVM.
From source code to execution
Consider this small program:
public class InterpreterStart {
static int add(int left, int right) {
return left + right;
}
public static void main(String[] args) {
int result = add(2, 3);
System.out.println(result);
}
}Compile it with:
javac InterpreterStart.java
java InterpreterStartThe process has several stages:
javactranslates the source into JVM bytecode.- The JVM starts and loads the
InterpreterStartclass. - The JVM verifies that the bytecode follows JVM rules.
- The JVM prepares the
mainmethod for execution. - The interpreter reads and executes the bytecode instructions.
- The program prints
5.
Bytecode is not CPU-specific machine code. An instruction such as “load an integer” or “invoke a method” has a defined meaning for the JVM. This lets the same .class file run on different processors and operating systems that provide a compatible JVM.
How interpretation works
The interpreter maintains a frame for each active method call. A frame contains information such as:
- Local variables, including method parameters.
- An operand stack used by bytecode instructions.
- The current bytecode position.
- References needed to return to the caller.
For add(2, 3), bytecode places the two integer arguments on the operand stack. The interpreter executes the addition instruction, places the result on the stack, and returns it to main.
A method call creates a new frame. A return removes that frame and resumes the caller's frame. This model supports recursion, exceptions, and normal method calls without requiring every method to be compiled immediately.
Why start with interpretation?
Compiling every method at startup would increase startup time and consume memory for code that might run only once. Many applications contain setup code, error paths, or rarely used features. Compiling those methods immediately would provide little benefit.
Interpretation also gives HotSpot time to observe the program. While methods run, the JVM can record which methods and loops execute often. Later chapters explain how those measurements identify hot code for just-in-time compilation.
Interpretation is usually slower than optimized native code because the JVM must repeatedly fetch, decode, and dispatch bytecode instructions. The cost is worthwhile during startup and for cold code. HotSpot can then replace selected interpreted execution with compiled code while the application continues running.
Exercise
Why does HotSpot usually interpret bytecode before compiling a method?
Show answer
Interpretation reduces startup work and lets HotSpot identify code that runs often enough to justify compilation.