JVM architecture
- Java course
- Expert
- Lesson 1/46
- 5 min
Learn the JVM's class loader, runtime and execution engine.
JVM architecture
The JVM (Java Virtual Machine) is an abstract machine that runs compiled Java code. Source code is first compiled to bytecode with javac; the JVM then runs this bytecode by interpreting or compiling it.
The JVM has three main components: the class loader loads classes into memory, the runtime data areas hold data, and the execution engine runs the bytecode. Java 21 LTS uses this structure.
This architecture lets Java code run independently of the operating system. The same bytecode runs the same way on Windows, Linux or macOS.
Compile and run flow
Java's flow has two stages. First javac turns the .java source file into a .class bytecode file. Then the java command runs the bytecode inside the JVM.
javac Main.java # produces Main.class
java Main # JVM loads and runs Main.classBytecode is OS-independent; it runs on any JVM. This is the basis of the 'write once, run anywhere' principle.
Practice Print the Java version
Define a Main class. In the main method, print the value of Runtime.version().feature() with System.out.println. This value is the Java LTS major version number (21 for Java 21).
Starter code
public class Main {
public static void main(String[] args) {
// Print the Java major version number
}
}Sample solution
public class Main {
public static void main(String[] args) {
System.out.println(Runtime.version().feature());
}
}Expected output
21
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the JVM do?
- It compiles source code
- It runs bytecode Correct answer
- It manages the operating system
- It deletes classes
Explanation: The JVM runs compiled bytecode.
2 Complete the javac input
javac Main.____ # produces Main.class
javac Main.java # produces Main.class
Explanation: The javac Main.java command produces Main.class.
3 Match the JVM components
| Term | Meaning |
|---|---|
Class loader |
Loads classes |
Runtime data areas |
Holds data |
Execution engine |
Runs bytecode |
javac |
Compiles source |