Compiler, interpreter, runtime

Three words people use interchangeably and should not — with the error each one produces.

9 min read🧭 Programming Foundations

Three words people use as if they were the same thing. They are not, they do different jobs at different times, and knowing which one is talking to you is the fastest way to read an error message.

How programs execute: one Java program from source to output. The whole journey in a minute: javac turns Main.java into bytecode, the JVM loads it and interprets it, the JIT compiles the parts that run often into machine code, the CPU runs that, and the operating system holds the process around it. The rest of this lesson is that journey, one stage at a time.

A compiler translates, ahead of time

A compiler reads your source and produces something else — a different file, in a different language, that a machine can deal with more directly. It does this before the program runs, and it does it once.

javac Hello.java produces Hello.class, and the two files are nothing alike. The source began with readable text. Here is how the output begins:

the first 8 bytes of Hello.classplaintext
ca fe ba be 00 00 00 34

cafebabe is a joke from 1994 that every Java class file still carries: a marker saying "this is a class file". The 34 is hexadecimal for 52, which is the class-file version Java 8 emits. Not text, not for you.

The compiler's other job is refusing. Before it translates anything it checks that what you wrote is legal, and if it is not, nothing is produced:

plaintext
Broken.java:1: error: illegal start of type
public class Broken { void x( }

That is a compile-time error. The program does not exist yet, so it cannot have run. Every error of this kind is one the compiler caught for free, before a user saw it — which is the argument for languages that check a lot, and Java checks a lot.

An interpreter executes, as it goes

An interpreter does not produce a file. It reads instructions and performs them, one at a time, as it goes. There is no separate translation step and no output artifact — the reading is the running.

The trade is the obvious one. A compiler can spend time optimising because it runs once; an interpreter cannot, because it is running now. But an interpreter can start instantly, and it can do things a compiler cannot, because it knows what is actually happening rather than what might.

Java does both, and that is the point

This is where the three words get tangled, and where most explanations of Java go wrong.

plaintext
Hello.java  ──javac──▶  Hello.class  ──JVM──▶  running program
             compiler                  interpreter, then compiler again

javac compiles your source to bytecode — not to instructions for your CPU, but for an imaginary machine. Then the JVM reads that bytecode and interprets it. Then, for the parts that run often, it compiles those to real machine code while the program is running.

So "is Java compiled or interpreted?" has a precise answer: both, at different stages. It is compiled ahead of time to bytecode, interpreted at first, and compiled again at run time where it pays. You will meet the third step properly in the Java course; it is the reason a Java service is slow for its first few seconds and fast afterwards.

you writejavac checksjavac emitsJVM loadsJVM runs
on diskHello.javacan run?noerrors reach you asnothing yet

you write. Hello.java exists on disk and is text — 111 bytes you could read in any editor. Nothing can run it yet; it is a description.

1 / 5

The runtime is what is there while it runs

A runtime is the machinery your program needs present in order to work: memory management, the standard library, the mechanism that turns bytecode into action. For Java that is the JVM plus the class library.

It is a thing that must be installed, which is why "it works on my machine" is so often about the runtime rather than the code. The source compiled fine; the machine that has to run it does not have what it needs, or has a different version of it.

Reading an error by its stage

Three failures, three stages, three shapes — and the shape tells you where to look:

What you seeStageWhat it means
error: illegal start of typecompilethe source is not legal Java; nothing was produced
Error: Could not find or load main class NoSuchClassstartupthe source was fine; the runtime cannot find what you asked it to run
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5runit compiled, it loaded, it ran, and your logic was wrong

That third one is the interesting category, because the compiler could not have helped. x[5] on a two-element array is perfectly legal Java — the size is not known until the program runs. A whole class of bugs lives in that gap, and most of what "writing careful code" means is shrinking it.

Under the hood: how the JIT decides what is worth compiling

"For the parts that run often, it compiles those to real machine code while the program is running" is doing a lot of work in one sentence. The mechanism behind it is called tiered compilation, and it exists because interpreting and compiling are solving different problems.

The interpreter starts instantly (no compilation delay) but re-does the work of decoding each bytecode instruction every single time it runs, which is slow for code executed millions of times. A compiler produces fast machine code but takes real time to produce it — time that is wasted if the method it compiled only ever runs twice. HotSpot's answer is to run everything interpreted at first, count how often each method is called and each loop iterates, and only compile the ones that cross a threshold — hence "HotSpot": it is finding the hot spots and leaving the cold code alone.

There are two JIT compilers behind that decision, doing different amounts of work: C1 (the client compiler) compiles quickly with modest optimisation, good for code that is warm but not yet proven hot; C2 (the server compiler) takes longer but produces more aggressively optimised code, worthwhile once a method has run often enough to justify the extra compile time. Tiered compilation runs code through both stages — interpreted, then C1, then C2 — rather than jumping straight to the most expensive compiler, because most methods never run often enough to earn that investment, and compiling everything at C2's level would make every program's startup far slower for a benefit most of the code never uses.

This directly explains the observation the video and this lesson both make: a JVM is slower in its first seconds than it will be a minute later, because the code that matters has not been identified and compiled yet — it is still being interpreted while the JVM watches to see what is worth optimising. It is also why a benchmark that starts timing immediately and runs a method only once is measuring the interpreter, not the JIT-compiled code a real long-running service would actually spend most of its life executing — a well-known trap for anyone writing a Java microbenchmark by hand instead of using a tool built to warm up first (JMH).

Try it yourself

Work out why "compiled once" does not describe Java

A colleague says: "Java can't be that fast — it has to interpret bytecode, and interpreting is always slower than a real compiled language like C." Using only what this lesson has said, what is wrong with the premise of that sentence, without looking anything up?

Where the premise breaks

The sentence treats "Java interprets bytecode" as the whole story, but it is only the first stage. A long-running Java program does not stay interpreted — the JIT compiles the methods that actually matter to real machine code once they prove they are worth the investment, and that machine code is what a CPU-bound service spends the overwhelming majority of its running time executing, not the interpreter. The comparison "interpreting is slower than compiling" is true of a single execution strategy held fixed for the whole run; it says nothing about a system that starts by interpreting and then replaces the hot code with compiled code while running, which is what tiered compilation actually is. A short-lived script does pay the interpreted cost with little JIT benefit — which is a real and separate reason Java is a poor fit for a five-millisecond CLI tool — but that is a different claim from "Java is slow because it interprets."

Misconceptions

  • "Compiled languages are fast, interpreted ones are slow." Java is both and is fast. The distinction stopped predicting performance about twenty-five years ago.
  • "The compiler finds my bugs." It finds a category — the ones that are about form. It cannot know that you meant + and typed -, and it will not know your list is empty.
  • "Bytecode is machine code." It is instructions for a machine that does not exist in hardware. That indirection is exactly what lets the same .class file run on a laptop and a server with different CPUs.

Sources

  • Oracle, "HotSpot Virtual Machine" documentation — tiered compilation, C1 and C2.
  • Paleczny, Vick, Click, "The Java HotSpot Server Compiler" (USENIX JVM 2001) — the original description of adaptive optimisation and hot-spot detection.
  • Shipilev, A. et al., the JMH (Java Microbenchmark Harness) project documentation — why warm-up matters when measuring JIT-compiled code.
Progress is saved on this device and to your account when signed in.