Skip to content

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

  1. 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 1
  2. The 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 2
  3. The 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 3
  4. Loading 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.class and 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 does main start.

    In one sentence: Before a class runs, it is found, loaded, linked (checked and prepared) and initialized.

    Try chapter 4
  5. Memory

    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 variable person does 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 5
  6. Execution

    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 6
  7. Garbage 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 specification

Turns 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.

Mixed mode, as reported by the JVMShell
$ java -version

Verified 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

JVM Atlas

Explore the machine behind Java programs. A free, open-source atlas of the Java Virtual Machine. Specification baseline: Java SE 25. Implementation details: OpenJDK HotSpot, JDK 25.

MIT licensed · no accounts, no tracking, no backend · your progress stays in your browser.

JVM Atlas is an independent educational project and is not affiliated with or endorsed by Oracle. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.