Lesson · Java Roadmap & Environment
The journey of a .java file
13 min readJava Program Lifecycle
Source → javac → bytecode → class loader → verifier → interpreter + JIT → machine code. Two very different worlds — compile time and runtime — and knowing which one a question lives in.
The pipeline at a glance
A Java program's life has two halves. Compile time turns your text into platform-independent bytecode. Runtime — once per JVM start — loads that bytecode, verifies it, and executes it; hot spots get compiled to native machine code *while the program runs*.
Step through the pipeline below. Every stage names what it owns — and later chapters (class loading, JIT, bytecode) are deep-dives into individual boxes of this diagram.
One file · two worlds
Compile timeonce, on your machine
Hello.java
the text you author
javac
parses + type-checks
Hello.class
portable instructions
Runtimeevery JVM start, on any machine
class loader
finds + links the class
verifier
checks bytecode safety
interpreter
runs it immediately
JIT (C1/C2)
compiles the hot paths
machine code
your CPU, full speed
One file, two lives: compiled once by javac, executed (and re-optimized) by every JVM that runs it. Step through what happens between writing the text and machine code executing.
Compile time: javac's job
javac parses your source, type-checks it against the JDK and your classpath, and emits a .class file — instructions for an imaginary machine plus the metadata (constant pool, method signatures, annotations) needed to run them. It is a pure transformation: source in, bytecode out, nothing executed.
This is why compile errors stop everything. A missing type, a wrong argument, an unreachable statement — the compiler refuses to produce the artifact at all. Java moves a large class of bugs to the earliest possible moment, and this is the moment.
Runtime: load, verify, execute
java MyApp boots a JVM, and the class loader finds MyApp.class (from the classpath or module path), brings it in, and links it. Bytecode from the filesystem is not trusted yet — the verifier checks it: stack shapes balance, types line up, jumps land on real instructions. Only then does your code run.
Execution starts interpreted: the JVM evaluates bytecode directly, which starts instantly and runs modestly. Meanwhile it profiles the program — call counts, loop iterations — and hands the hot methods to the JIT compiler, which turns them into optimized native machine code. That's why a long-running Java service is dramatically faster a minute after boot than it was in the first second.
Why this design wins — and what it costs
The split buys three things at once. Portability: the bytecode is the artifact; JVMs exist for every platform, so you never recompile. Safety: verification means a malformed or hostile .class is rejected before it executes — a property native binaries simply do not have. Adaptive performance: the JIT optimizes for how the program *actually* behaves (inlining the right calls, eliminating the right checks), not for a guess made at build time.
The cost is startup: interpretation and warm-up make the first seconds of a JVM slower than a native binary. That's the trade AOT compilers like GraalVM native-image attack from the other side — and why both models coexist. For the JVM to run at all, the last step is always the same: bytecode in, native code out.
Bytecode is not machine code
.class files contain JVM instructions — portable, verifiable, and decided at runtime. Machine code appears only when the JIT compiles a hot method, differently on each platform. Saying 'javac compiles to machine code' marks the answer as memorized, not understood.
Interview angles
Walk through what happens from source to running program.+
javac type-checks the source and emits bytecode (.class). At launch the JVM's class loader finds and loads that class, the verifier checks the bytecode, the interpreter starts executing, and hot methods get JIT-compiled to native code. Compile once; load, verify and optimize per run.
Is Java compiled or interpreted?+
Both, in sequence: javac compiles ahead of time to bytecode, then the JVM interprets that bytecode and JIT-compiles hot paths to machine code at runtime. It's a hybrid model — the standard answer includes the runtime half.
Why does Java start slower but catch up?+
Startup: loading, verification and interpretation with an unwarmed JIT. Afterwards: the JIT has profiles of the real execution, so hot paths run as optimized native code, often near or matching native binaries. Benchmarks must warm up for exactly this reason.
Where does the class loader get .class files from?+
The classpath (directories and jars) or the module path — an ordered list resolved by name. Parent delegation means core classes resolve from the JDK first, which is how java.lang.String stays trustworthy. Deep-dive: the Class Loading topic.