JVM vs JDK vs JRE Explained: How Java Code Actually Runs
Understand the difference between JDK, JRE and JVM, how javac turns source into bytecode, how class loading and the JIT compiler work, and how JVM memory is organised into heap and stack.
"Write once, run anywhere" is Java's famous promise, and the JVM is how it keeps it. Beginners often mix up the JDK, JRE and JVM, but they are simply three nested layers: each one contains the next. This guide follows a single .java file all the way to a running program.
From source code to running program#
- Write source code in
Hello.java. - Compile it with
javac, which producesHello.class. - The
.classfile contains bytecode: instructions for an imaginary CPU, not for Intel or ARM. - Load: the JVM's class loader reads the bytecode and verifies it is safe.
- Execute: the JVM interprets the bytecode and compiles hot parts into native machine code.
// Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, JVM!");
}
}javac Hello.java # creates Hello.class (bytecode)
java Hello # starts a JVM that runs it
javap -c Hello # disassemble and view the bytecodeBecause every operating system has its own JVM, the same Hello.class runs on Windows, macOS and Linux without recompiling.
JDK vs JRE vs JVM#
| Layer | What it is | Contains | Who needs it |
|---|---|---|---|
| JVM | Java Virtual Machine: the engine that runs bytecode | Class loader, bytecode verifier, interpreter, JIT compiler, garbage collector | Every Java program, at runtime |
| JRE | Java Runtime Environment | JVM + core libraries (java.lang, java.util, …) | Anyone who only runs Java apps |
| JDK | Java Development Kit | JRE + tools: javac, jar, javadoc, jshell, jdb | Developers |
The simple formula: JDK = JRE + development tools, and JRE = JVM + standard libraries.
Since Java 11, Oracle no longer ships a separate JRE download. You install a JDK (for example Eclipse Temurin or Oracle JDK), and for deployment you can build a slim custom runtime with jlink.
Class loading#
The JVM doesn't load every class at startup. Classes are loaded lazily, the first time they're used, by a chain of class loaders:
- Bootstrap class loader: loads core classes like
java.lang.String. - Platform class loader: loads other JDK modules.
- Application class loader: loads your classes from the classpath.
They follow parent delegation: a loader first asks its parent to load a class, and only loads it itself if the parent can't. That's why you can't replace java.lang.String with your own fake version.
After loading, a class goes through linking (verification, preparing static fields, resolving references) and initialization (running static blocks).
Interpreter and JIT compiler#
At first the JVM interprets bytecode one instruction at a time, which starts quickly but runs slowly. While doing so, it counts how often each method runs. Methods that get "hot" are handed to the Just-In-Time (JIT) compiler, which turns them into optimised native machine code.
- C1 compiles quickly with light optimisations.
- C2 spends more time and applies heavy optimisations like inlining and loop unrolling.
This is called tiered compilation, and it's why Java apps get faster after a "warm-up" period. It's also why micro-benchmarks need tools like JMH: the first runs measure the interpreter, not the optimised code.
JVM memory areas#
| Area | Stores | Shared? |
|---|---|---|
| Heap | All objects and arrays | Shared by all threads |
| Stack | Method frames: local variables, parameters, return addresses | One per thread |
| Metaspace | Class metadata, method bytecode | Shared |
| PC register | Address of the current instruction | One per thread |
| Native method stack | Frames for native (C/C++) code | One per thread |
void createUser() {
int age = 25; // primitive: lives on the stack
User user = new User("Asha", age); // reference on the stack, object on the heap
}When the method returns, its stack frame disappears. The User object stays on the heap until nothing references it, and then the garbage collector reclaims it.
StackOverflowError usually means infinite recursion filled a thread's stack. OutOfMemoryError: Java heap space means live objects filled the heap, often from a memory leak like an ever-growing static collection.
Garbage collection#
Java frees memory automatically. Most objects die young, so the heap is split into generations: new objects go into the young generation, which is collected often and cheaply; objects that survive several collections are promoted to the old generation.
| Collector | Best for |
|---|---|
| G1 (default) | Balanced throughput and pause times for most apps |
| ZGC | Very large heaps with pauses under a millisecond |
| Parallel | Batch jobs where throughput matters more than pauses |
| Serial | Small heaps and single-core containers |
java -Xms512m -Xmx2g -XX:+UseZGC -jar app.jar
# start heap max heap choose a collectorUseful JDK tools#
jshell: try Java snippets interactively.jps: list running JVMs.jstack <pid>: thread dump, great for finding deadlocks.jcmd <pid> GC.heap_info: heap usage.jconsole/ VisualVM / JDK Mission Control: live monitoring.
Thread stacks and the shared heap are the heart of concurrency. Continue with our Java multithreading guide, or revisit Java exception handling to understand errors like OutOfMemoryError.
Key takeaways#
- JDK = JRE + tools; JRE = JVM + libraries; the JVM runs bytecode.
javaccompiles source to platform-independent bytecode, which is why Java runs anywhere a JVM exists.- Class loaders load classes lazily using parent delegation.
- The JIT compiler turns hot methods into native code, so Java speeds up after warm-up.
- Objects live on the shared heap; local variables live on per-thread stacks; the GC frees unreachable objects.