Video in progress
What is the difference between an interpreter and JIT compilation, and why does the JVM use both?
The interpreter runs bytecode right away but slowly; the JIT spends time on compilation, but afterward the code runs much faster. The JVM interprets rarely used code and compiles only the hot code.
📖 Answer
Each approach has its own cost:
| Interpreter | JIT | |
|---|---|---|
| Starting | immediate | needs compilation time |
| Execution speed | slow | close to native |
| Memory | little | needs a code cache |
| Optimization | none | based on runtime statistics |
Compiling all the code immediately would slow startup badly: much of an application (configuration, one-time initialization) runs only once or twice. Compiling it is pointless.
Another strength of the JIT: it optimizes based on data from the actual run. For example, if only one class ever calls an interface method, the JIT inlines that class's method directly. An AOT compiler can't know this ahead of time.
That is why Java runs in "mixed mode": the output of java -version says "mixed mode" too.
Sign up to mark studied questions and save your progress.