Video in progress

What is the JIT compiler, and why does it make Java faster?

JIT (Just-In-Time) is the part of the JVM that compiles frequently executed bytecode into native machine code while the program is running. After that, that code runs directly on the processor with no interpretation.

📖 Answer

The JVM first interprets the bytecode, and translating every instruction one by one is slow. While doing that, it counts which methods and loops are called a lot. When it finds "hot" code, the JIT turns it into native code.

HotSpot has two compilers. C1 compiles fast, but with fewer optimizations. C2 is slower but optimizes aggressively: inlining, loop unrolling, removing unnecessary checks. Tiered compilation (the default) uses both in sequence: first C1, and if the code gets even hotter, C2.

The practical result is "warm-up": the application is slower in the first minutes after startup, then speeds up. That is why measuring a benchmark right away is wrong; tools like JMH go through a warm-up phase first.

To see the JIT at work: -XX:+PrintCompilation.