Guide
How does the JVM run a Java program?
Follow one Java program from source code through compilation, class loading, memory, execution and garbage collection, step by step, with real javac and javap output.
From source code to output, step by step
Your first Java program
What is a Java program, before anything runs?
This is : instructions written by a person, in the Java language. People can read it, but the processor inside your computer cannot run these words directly.
So before a Java program runs, its source code is translated. The next chapters follow that translation, and then the machine that runs the result.
In one sentence: Source code is text written by people, and it is translated before it runs.
Try chapter 1The compiler
How does Hello.java become something the JVM can run?
The compiler is a program called . It reads your source code, checks it for mistakes, and writes a new file:
Hello.class.A is not text you write. It holds : a compact list of instructions meant for the Java Virtual Machine, not for your processor. That is why the same class file can run on Windows, macOS or Linux, on any computer with a recent enough JVM (Java 25 or newer for this file).
In one sentence: The compiler, javac, turns source code into a class file full of bytecode: instructions for the JVM.
Try chapter 2The JVM
What is the JVM, and why does Java need one?
The (JVM) is the software machine that runs Java programs. Think of it as a machine that understands bytecode, the instructions in class files.
Your processor does not understand bytecode, but the JVM is itself an ordinary program for your computer. So the same class file runs anywhere a JVM is installed, and the JVM also looks after memory for your program.
In one sentence: The JVM is a software machine that runs bytecode; its parts load classes, manage memory, run code and clean up.
Try chapter 3Loading classes
How does the JVM get Hello.class ready to run?
Before any of your code runs, the JVM has to find your class and get it ready. The finds
Hello.classand reads it. Then the JVM it: it checks that the bytecode is safe and prepares space for the class's data. Finally it the class by running its setup code. Only then doesmainstart.In one sentence: Before a class runs, it is found, loaded, linked (checked and prepared) and initialized.
Try chapter 4Memory
When your program creates an object, where does it go?
Two places matter most. Each thread has a with one for every method that is running; the frame holds that method's local variables, such as
person. The is shared memory where objects live.new Person("Mia")creates the object in the heap. The variablepersondoes not contain the object: it holds a , a way for the JVM to find the object. Two variables can refer to the same object.In one sentence: Objects live in the heap; each method call gets a frame on its thread’s stack; a variable such as person holds a reference to its object, not the object itself.
Try chapter 5Execution
How does the JVM actually run the bytecode?
Your processor (the CPU) only understands : instructions for its own kind of chip. Bytecode is not machine code, so the JVM bridges the gap in two ways.
First, the reads bytecode instructions one at a time and carries each one out. That starts instantly. Then, when a method runs often, the translates it into machine code while the program keeps running, and from then on the CPU runs that machine code directly, much faster.
In one sentence: The interpreter runs bytecode at once; the JIT compiler turns busy code into machine code for the CPU.
Try chapter 6Garbage collection
What happens to objects your program no longer uses?
Java cleans up memory for you. The looks for objects your program can no longer reach: no variable leads to them, directly or through other objects.
Three moments matter. An object is reachable while some path of references leads to it. When the last path disappears it becomes unreachable, and so eligible for collection, but it is still in memory. Only when the garbage collector runs is its memory actually reclaimed.
In one sentence: Objects nothing can reach become eligible for collection; the garbage collector reclaims them later.
Try chapter 7
Execution engine
JVM specificationTurns bytecode into work on the CPU: an interpreter first, then JIT-compiled machine code for hot code.
Simply put
The part of the JVM that actually runs your code. It starts by interpreting bytecode one instruction at a time. Code that runs often is compiled to native machine code while the program runs, so it gets much faster.
Deep dive: the specification and HotSpot in detail
The JVM Specification defines what each instruction does, not how to execute it, and explicitly allows translating bytecode to native code at load time or during execution (JVMS §2.13). HotSpot's engine is "mixed mode": a template , JIT compilation with C1 and C2, a for compiled code, runtime profiling that steers the compilers, and to fall back when speculative assumptions break.
The specification requires
Instruction semantics only. Interpretation, compilation and their mix are implementation choices.
HotSpot (JDK 25) does
HotSpot JDK 25: template interpreter, tiered C1/C2 JIT, segmented code cache, deoptimization.
$ java -versionVerified with OpenJDK 25.0.4 · Linux x64
Common misconceptions
Java is interpreted, so it is slow.
Hot code runs as optimized machine code produced by C2, often using run-time profile information that an ahead-of-time compiler does not have.
Sources
- SpecJVMS §2.13 Public Design, Private Implementation(opens in a new tab)
Implementations may translate bytecode to native code and choose their own internals. · Java SE 25
- SpecJVMS §2.11 Instruction Set Summary(opens in a new tab)
Opcodes, operands and the inner loop of a JVM interpreter. · Java SE 25
- SourcecompilationPolicy.hpp(opens in a new tab)
Tiered compilation levels 0–4 and the thresholds that move methods between them. · JDK 25 GA (tag jdk-25-ga)