Learn Java
import com.learnjava.gettingstarted.JvmBytecode;

The JVM: Compilation & Bytecode in Java

@Concept

There are two separate steps between writing a .java file and watching a program run. First, javac compiles source into bytecode β€” a compact, platform-neutral instruction set stored in .class files, which is not the same thing as real CPU instructions for any specific machine. Second, the JVM (Java Virtual Machine) loads that bytecode and actually executes it, using a Just-In-Time (JIT) compiler to turn frequently-run code paths into real machine code on the fly for speed.

This two-step split is exactly how "write once, run anywhere" works in practice: bytecode itself is completely platform-independent, so only the JVM implementation needs to be platform-specific. The same unmodified .class file runs on Windows, macOS, or Linux as long as each machine has a compatible JVM installed.

@HowItWorks
1

Source compilation

javac parses .java files, type-checks them, and emits .class files containing bytecode β€” a portable instruction set, not real machine code for any specific CPU.

2

Class loading

Running java Main tells the JVM's class loader to locate and load the needed .class files into memory.

3

Bytecode verification

Before executing anything, the JVM verifies the loaded bytecode is structurally valid and doesn't violate safety rules β€” a background safety step, not something you interact with directly.

4

Interpretation

The JVM starts out interpreting bytecode instruction by instruction, translating each one to real machine instructions as it goes.

5

JIT compilation

For code that runs repeatedly β€” hot loops, frequently-called methods β€” the JVM's Just-In-Time compiler compiles that bytecode directly into optimized native machine code, so it stops needing to be re-interpreted every single time.

@Example
public class Add {
public static void main(String[] args) {
int result = add(4, 7);
System.out.println("Result: " + result);
}
Β 
static int add(int a, int b) {
return a + b;
}
}
Output
Result: 11
1javac Add.java turns this whole file into Add.class, made of bytecode instructions like "load a local variable" and "invoke a static method" β€” not x86 or ARM instructions.
2java Add starts a JVM, which loads Add.class and locates main as the entry point.
3Calling add(4, 7) is itself a bytecode instruction that jumps execution into the add method's own bytecode.
4The single println call produces the one line of output, and the JVM shuts down once main returns.
@UseCases
Understanding why the exact same .class file runs unmodified on Windows, macOS, and Linux
Explaining startup-time versus steady-state performance differences between interpreted and JIT-compiled code
Diagnosing an UnsupportedClassVersionError caused by a class compiled with a newer JDK than the JVM running it
Reasoning about why Java gets called both a 'compiled' and an 'interpreted' language depending on who you ask

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself β€” building beats watching.