Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

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.

Sumit KumarUpdated Oct 11, 20264 min readJava
JVM vs JDK vs JRE Explained: How Java Code Actually Runs

"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#

From Java source to running program: write, compile with javac, bytecode, class loading and execution by the JVM
  1. Write source code in Hello.java.
  2. Compile it with javac, which produces Hello.class.
  3. The .class file contains bytecode: instructions for an imaginary CPU, not for Intel or ARM.
  4. Load: the JVM's class loader reads the bytecode and verifies it is safe.
  5. Execute: the JVM interprets the bytecode and compiles hot parts into native machine code.
Java
// Hello.java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, JVM!");
    }
}
Bash
javac Hello.java      # creates Hello.class (bytecode)
java Hello            # starts a JVM that runs it
javap -c Hello        # disassemble and view the bytecode

Because every operating system has its own JVM, the same Hello.class runs on Windows, macOS and Linux without recompiling.

JDK vs JRE vs JVM#

LayerWhat it isContainsWho needs it
JVMJava Virtual Machine: the engine that runs bytecodeClass loader, bytecode verifier, interpreter, JIT compiler, garbage collectorEvery Java program, at runtime
JREJava Runtime EnvironmentJVM + core libraries (java.lang, java.util, …)Anyone who only runs Java apps
JDKJava Development KitJRE + tools: javac, jar, javadoc, jshell, jdbDevelopers

The simple formula: JDK = JRE + development tools, and JRE = JVM + standard libraries.

Note

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#

AreaStoresShared?
HeapAll objects and arraysShared by all threads
StackMethod frames: local variables, parameters, return addressesOne per thread
MetaspaceClass metadata, method bytecodeShared
PC registerAddress of the current instructionOne per thread
Native method stackFrames for native (C/C++) codeOne per thread
Java
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.

Common errors

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.

CollectorBest for
G1 (default)Balanced throughput and pause times for most apps
ZGCVery large heaps with pauses under a millisecond
ParallelBatch jobs where throughput matters more than pauses
SerialSmall heaps and single-core containers
Bash
java -Xms512m -Xmx2g -XX:+UseZGC -jar app.jar
#     start heap  max heap   choose a collector

Useful 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.
  • javac compiles 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.