JDK, JRE, JVM & Internal Working of Java
Master the exact differences between JDK, JRE, and JVM, plus how ClassLoader, Bytecode Verifier, Interpreter, and JIT Compiler execute your program.
- Bootstrap ClassLoader: Loads core JDK classes (
java.base). - Platform ClassLoader: Loads standard extension modules.
- Application ClassLoader: Loads your application classes from the classpath/modulepath.
- Verify: Ensures bytecode does not forge pointers, overflow operand stacks, or violate access modifiers.
Introduction
Understanding how Java executes under the hood is essential for debugging classloading issues, tuning production latency, and acing technical interviews. Every Java program passes through two distinct phases: compile-time bytecode generation and JVM runtime execution.
Learning Objectives
- Distinguish clearly between JDK, JRE, and JVM.
- Trace the complete lifecycle of a Java program from
.javasource to native CPU instructions. - Understand the ClassLoader subsystem (Loading, Linking, Initialization), Bytecode Verifier, and JIT Compiler.
JDK vs JRE vs JVM
| Component | Full Form | Contains | Purpose |
|---|---|---|---|
| JVM | Java Virtual Machine | ClassLoader, Execution Engine, Runtime Data Areas | Executes bytecode on the host OS |
| JRE | Java Runtime Environment | JVM + Core Class Libraries (java.lang, java.util, java.io) | Runs compiled Java applications |
| JDK | Java Development Kit | JRE + Development Tools (javac, javadoc, jdb, jar, javap) | Develops, compiles, packages, and debugs Java apps |
Internal Working Step-by-Step
- 1Write Source: Save code in
Main.java. - 2Compile: Run
javac Main.javato produceMain.classcontaining JVM bytecode. - 3Class Loading: The ClassLoader locates and loads
Main.classusing a delegation hierarchy:
- Bootstrap ClassLoader: Loads core JDK classes (
java.base). - Platform ClassLoader: Loads standard extension modules.
- Application ClassLoader: Loads your application classes from the classpath/modulepath.
- 4Linking (Verify, Prepare, Resolve):
- Verify: Ensures bytecode does not forge pointers, overflow operand stacks, or violate access modifiers.
- Prepare: Allocates memory for
staticfields and sets them to default values (0,false,null). - Resolve: Replaces symbolic references with direct memory references.
- 5Initialization: Executes
staticvariable initializers andstatic { ... }blocks. - 6Execution Engine:
- Interpreter: Reads bytecode line-by-line for fast startup.
- JIT (Just-In-Time) Compiler: Profiles "hot" methods/loops and compiles them directly into optimized native machine code (inlining, dead-code elimination, escape analysis).
Real-World Application
When a Spring Boot service starts up, the Interpreter handles initialization immediately, and within seconds the C1/C2 JIT compilers optimize your hot REST endpoints to native CPU speed.
Common Mistakes
- Trying to compile
.javafiles on a server that only has a JRE installed (nojavac). - Naming a
public classdifferently from the filename, which fails compilation immediately.
Interview Questions & FAQs
- Q: Is the JVM platform-independent?
- A: No! Bytecode is platform-independent, but the JVM itself is platform-dependent (Windows, Linux, and macOS each have their own native JVM binary).
- Q: What is the `javap` tool?
- A:
javap -c Maindisassembles.classfiles so you can inspect the exact JVM bytecode instructions generated byjavac.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)