LearnContact
Lesson 345 min read

JDK, JRE & JVM

Understand the complete Java platform architecture. Learn what JDK, JRE, and JVM are, how Java source code is compiled into bytecode, how the JVM executes programs, and why Java is platform independent.

Introduction

When you begin learning Java, three terms appear almost immediately: JDK, JRE, and JVM. These three components form the foundation of the Java development and execution environment.

A Java developer writes source code, compiles that source code into bytecode, and then runs the bytecode through the Java Virtual Machine. Different parts of this process are handled by the JDK, JRE, and JVM.

Understanding these components is essential because they explain how Java programs are developed, compiled, executed, managed in memory, and made portable across different operating systems.

Core Java Platform Components
JAVA DEVELOPMENT

        │
        ▼

JDK
Java Development Kit

        │
        ▼

JRE
Java Runtime Environment

        │
        ▼

JVM
Java Virtual Machine

        │
        ▼

OPERATING SYSTEM

        │
        ▼

HARDWARE
What You Will Learn
  • What the Java platform is.
  • What the JDK is.
  • What the JDK contains.
  • What important JDK tools do.
  • What javac does.
  • What the java command does.
  • What the JRE is.
  • What the JRE contains.
  • The difference between JDK and JRE.
  • What the JVM is.
  • Why the JVM exists.
  • How Java source code becomes an executing program.
  • What bytecode is.
  • What a .class file contains.
  • How bytecode is executed.
  • Why Java is called platform independent.
  • Why the JVM itself is platform dependent.
  • How JDK, JRE, and JVM are related.
  • The internal architecture of the JVM.
  • How the Class Loader Subsystem works.
  • The loading, linking, and initialization phases.
  • The different built-in class loaders.
  • How parent delegation works.
  • The major JVM runtime memory areas.
  • What the Method Area stores.
  • What Heap Memory stores.
  • How Java Stacks work.
  • What stack frames contain.
  • What the PC Register does.
  • What the Native Method Stack does.
  • The difference between Heap and Stack.
  • How the Execution Engine works.
  • The role of the Interpreter.
  • The role of the JIT Compiler.
  • How HotSpot optimization works.
  • How Garbage Collection works.
  • What automatic memory management means.
  • When objects become eligible for garbage collection.
  • What JNI is.
  • How native libraries interact with Java.
  • The complete JVM execution flow.
  • How to compile and run a Java program.
  • Why multiple JVM implementations exist.
  • What JDK distributions are.
  • How modern Java handles JRE deployment.
  • Common JDK, JRE, and JVM mistakes.

The Java Platform

Java is more than a programming language. It is also a complete software platform consisting of development tools, runtime components, standard libraries, specifications, and a virtual machine.

Java Language

The programming language developers use to write Java source code.

Development Tools

Tools compile, document, debug, package, inspect, and manage Java programs.

JVM

The virtual machine executes Java bytecode.

Standard Libraries

Java provides APIs for collections, files, networking, concurrency, databases, and more.

Java Platform
JAVA PLATFORM

├── Java Programming Language
│
├── Development Tools
│   ├── javac
│   ├── java
│   ├── jar
│   ├── javadoc
│   └── Other Tools
│
├── Standard Libraries
│
├── Runtime Environment
│
└── Java Virtual Machine

The Big Picture

Before studying each component separately, understand the complete relationship between them.

JDK, JRE and JVM
┌─────────────────────────────────────────┐
│                  JDK                    │
│        Java Development Kit             │
│                                         │
│   Development Tools                     │
│   ┌─────────────────────────────────┐   │
│   │             JRE                 │   │
│   │    Java Runtime Environment     │   │
│   │                                 │   │
│   │   Java Libraries                │   │
│   │   ┌─────────────────────────┐   │   │
│   │   │          JVM            │   │   │
│   │   │ Java Virtual Machine    │   │   │
│   │   └─────────────────────────┘   │   │
│   └─────────────────────────────────┘   │
└─────────────────────────────────────────┘
Simple Relationship
  • JDK is used to develop and run Java programs.
  • JRE provides the environment required to run Java programs.
  • JVM executes Java bytecode.
  • Conceptually, the JDK includes runtime capabilities.
  • The JVM is the core execution engine of Java.

What is JDK?

JDK stands for Java Development Kit. It is the complete toolkit used by Java developers to create, compile, run, debug, document, package, and manage Java applications.

If you want to write and compile Java programs, you need a JDK.

JDK Meaning
JDK

Java Development Kit

        │
        ▼

USED FOR

Writing Java Programs
Compiling Java Programs
Running Java Programs
Debugging Java Programs
Packaging Java Programs
Documenting Java Programs
JDK in One Sentence
  • The JDK is the complete development environment for Java.

What Does JDK Contain?

The JDK contains the tools and runtime components required for Java development.

Inside the JDK
JDK

├── Java Compiler
│   └── javac
│
├── Java Launcher
│   └── java
│
├── Documentation Tool
│   └── javadoc
│
├── Archive Tool
│   └── jar
│
├── Debugging Tools
│
├── Monitoring Tools
│
├── Inspection Tools
│
├── Runtime Components
│
├── Standard Libraries
│
└── JVM
  • Java compiler.
  • Java application launcher.
  • Archive and packaging tools.
  • Documentation generator.
  • Debugging tools.
  • Monitoring tools.
  • Class inspection tools.
  • Module tools.
  • Runtime components.
  • Standard Java libraries.
  • Java Virtual Machine.

Important JDK Tools

The JDK provides many command-line tools. Some are used every day, while others are mainly used for debugging, monitoring, packaging, documentation, or advanced development.

javac

Compiles Java source code into bytecode.

java

Launches a Java application.

jar

Creates and manages Java archive files.

javadoc

Generates documentation from Java source code.

jdb

Provides command-line debugging capabilities.

javap

Displays information about compiled class files.

jshell

Provides an interactive environment for experimenting with Java code.

keytool

Manages keys and certificates.

javac Compiler

The javac command is the Java compiler. It reads Java source code from .java files and translates it into Java bytecode stored in .class files.

Compilation Process
Hello.java

Java Source Code

        │
        ▼

javac Hello.java

        │
        ▼

JAVA COMPILER

        │
        ▼

Hello.class

Java Bytecode
Compile a Java Program
javac Hello.java
Important
  • javac compiles source code.
  • The input is normally a .java file.
  • The output is normally one or more .class files.
  • The .class file contains bytecode.
  • javac does not directly create a Windows .exe file.

java Launcher

The java command launches a Java application. It starts the Java runtime, loads the required classes, initializes the JVM environment, and begins program execution.

Run a Java Program
java Hello
java Command
java Hello

    │
    ▼

Start Java Runtime

    │
    ▼

Load Hello Class

    │
    ▼

Find main() Method

    │
    ▼

Execute Bytecode

    │
    ▼

Program Output
Do Not Add .class
  • Correct: java Hello
  • Incorrect: java Hello.class
  • The java command expects a class name, not the class filename.

Other JDK Tools

The JDK contains many additional tools for different stages of development and application management.

  • jar creates and manages JAR archives.
  • javadoc generates API documentation.
  • javap inspects compiled class information.
  • jdb provides command-line debugging.
  • jshell provides interactive Java experimentation.
  • jdeps analyzes dependencies.
  • jlink creates custom runtime images.
  • jpackage creates application packages.
  • jcmd sends diagnostic commands to JVM processes.
  • jconsole provides monitoring and management capabilities.
  • jstack displays Java thread stack information.
  • jmap provides memory-related information.
  • keytool manages keys and certificates.

What is JRE?

JRE stands for Java Runtime Environment. Conceptually, it provides the environment required to run Java applications.

The JRE includes the JVM together with runtime libraries and supporting components required during program execution.

JRE Meaning
JRE

Java Runtime Environment

        │
        ▼

PROVIDES

Environment for Running
Java Applications

        │
        ▼

CONTAINS

JVM
+
Runtime Libraries
+
Supporting Components
JRE in One Sentence
  • The JRE provides the environment in which Java applications run.

What Does JRE Contain?

Inside the JRE
JRE

├── JVM
│
├── Runtime Libraries
│
├── Supporting Files
│
├── Security Components
│
├── Networking Components
│
└── Other Runtime Resources

The JRE is focused on execution rather than development. Traditional standalone JRE distributions generally did not include development tools such as the Java compiler.

JDK vs JRE

JDK vs JRE
JDK

Purpose:
Develop + Run

Contains:
Development Tools
Runtime Components
JVM

Used By:
Java Developers


JRE

Purpose:
Run

Contains:
Runtime Components
JVM

Used By:
Application Users

JDK

Used for developing, compiling, debugging, packaging, and running Java programs.

JRE

Conceptually provides the environment required to run Java programs.

Easy Rule
  • Want to develop Java applications? Use a JDK.
  • Want only the runtime concept? Think of the JRE.
  • Modern Java deployment may use a full JDK or a custom runtime image.

What is JVM?

JVM stands for Java Virtual Machine. It is the abstract execution environment responsible for running Java bytecode.

The JVM loads classes, verifies bytecode, manages runtime memory, executes instructions, optimizes frequently executed code, manages threads, and supports automatic garbage collection.

JVM Responsibilities
JVM

├── Loads Classes
├── Verifies Bytecode
├── Manages Memory
├── Executes Bytecode
├── Optimizes Code
├── Manages Threads
├── Performs Garbage Collection
└── Interacts with Native Systems
JVM in One Sentence
  • The JVM is the engine that executes Java bytecode.

Why JVM Exists

Traditional native programs are commonly compiled for a specific processor and operating system. Java uses a different approach.

Java source code is compiled into platform-neutral bytecode. A platform-specific JVM then executes that bytecode on the local machine.

Traditional Native Model
SOURCE CODE

    │
    ▼

PLATFORM-SPECIFIC COMPILER

    │
    ▼

NATIVE MACHINE CODE

    │
    ▼

SPECIFIC OPERATING SYSTEM
Java Model
JAVA SOURCE CODE

        │
        ▼

JAVA COMPILER

        │
        ▼

BYTECODE

        │
        ▼

PLATFORM-SPECIFIC JVM

        │
        ▼

OPERATING SYSTEM

        │
        ▼

HARDWARE

Source Code to Execution

A Java program passes through multiple stages before its instructions execute on the processor.

Complete Java Flow
STEP 1

Developer Writes

Hello.java

        │
        ▼

STEP 2

javac Compiler

        │
        ▼

STEP 3

Hello.class

Bytecode

        │
        ▼

STEP 4

Class Loader

        │
        ▼

STEP 5

Bytecode Verification

        │
        ▼

STEP 6

JVM Runtime Memory

        │
        ▼

STEP 7

Execution Engine

        │
        ▼

STEP 8

Native Machine Instructions

        │
        ▼

STEP 9

Program Output

Java Source Code

Java source code is human-readable program text written according to Java language syntax. Java source files normally use the .java extension.

Hello.java
public class Hello {

    public static void main(String[] args) {

        System.out.println("Hello, Java!");

    }

}
Source File
  • Written by the developer.
  • Human readable.
  • Uses Java syntax.
  • Normally uses the .java extension.
  • Must be compiled before traditional bytecode execution.

Compilation

Compilation is the process of translating Java source code into Java bytecode.

Compile
javac Hello.java
After Compilation
BEFORE

Hello.java


COMMAND

javac Hello.java


AFTER

Hello.java
Hello.class

If the source code contains compilation errors, the compiler reports them and the expected class file may not be generated successfully.

What is Bytecode?

Bytecode is the intermediate instruction format produced by the Java compiler. It is designed for execution by a Java Virtual Machine rather than directly by a particular physical processor.

Bytecode Position
HIGH LEVEL

Java Source Code

        │
        ▼

INTERMEDIATE LEVEL

Java Bytecode

        │
        ▼

LOW LEVEL

Native Machine Instructions
Why Bytecode Matters
  • It separates Java source code from specific hardware.
  • The same compatible bytecode can run through different JVM implementations.
  • It enables the Java portability model.
  • It can be verified before execution.
  • It can be interpreted or compiled at runtime.

The .class File

A .class file contains compiled Java bytecode and related class information. It is not a normal text file intended for manual editing.

File Types
Hello.java

Human-Readable
Java Source Code

        │
        ▼

javac

        │
        ▼

Hello.class

Compiled
Java Bytecode

A single Java source file can sometimes produce multiple .class files, depending on the classes and other constructs declared in the source.

Bytecode Execution

The JVM does not simply read the entire class file and immediately execute it without preparation. Classes are loaded, linked, initialized, and then executed through the JVM runtime system.

Bytecode Execution Process
.class FILE

    │
    ▼

CLASS LOADER

    │
    ▼

BYTECODE VERIFICATION

    │
    ▼

RUNTIME MEMORY

    │
    ▼

EXECUTION ENGINE

    │
    ├── Interpreter
    │
    └── JIT Compiler

    │
    ▼

NATIVE EXECUTION

Platform Independence

Platform independence is one of the most important ideas associated with Java. A compatible Java class file can run on different platforms when each platform provides a suitable JVM implementation.

One Bytecode, Multiple Platforms
Hello.class

SAME BYTECODE

    │
    ├──────────────┬──────────────┐
    ▼              ▼              ▼

Windows JVM     Linux JVM      macOS JVM

    │              │              │
    ▼              ▼              ▼

Windows          Linux          macOS
The Key Idea
  • The bytecode can remain the same.
  • The JVM implementation changes for the platform.
  • The JVM acts as an abstraction between bytecode and the operating system.

WORA Principle

WORA stands for Write Once, Run Anywhere. It describes the goal of writing and compiling Java code once and then running the resulting bytecode on different compatible Java platforms.

Write Once, Run Anywhere
WRITE ONCE

Hello.java

        │
        ▼

COMPILE ONCE

Hello.class

        │
        ▼

RUN ANYWHERE

Windows
Linux
macOS
Other Supported Platforms
WORA Does Not Mean Zero Compatibility Concerns
  • The target environment still needs a compatible Java runtime.
  • Native libraries can introduce platform dependencies.
  • Operating-system-specific code can reduce portability.
  • External files and environment configurations may differ.
  • Java version compatibility still matters.

Why Java is Platform Independent

Java is called platform independent because Java source code is normally compiled into bytecode rather than directly into one operating system’s native machine code.

Platform Independence
JAVA CODE

    │
    ▼

BYTECODE

    │
    ▼

NOT DIRECTLY TIED TO

Windows
Linux
macOS

    │
    ▼

EXECUTED THROUGH

Appropriate JVM

Why JVM is Platform Dependent

The JVM must communicate with the actual operating system and hardware. Therefore, JVM implementations are built for specific platforms.

Platform-Specific JVMs
SAME BYTECODE

        │
        ├───────────────┬───────────────┐
        ▼               ▼               ▼

Windows JVM         Linux JVM        macOS JVM

Different           Different        Different
Implementation      Implementation   Implementation
Remember
  • Java bytecode is platform independent.
  • The JVM implementation is platform dependent.
  • The JVM creates the portability layer.

Complete Relationship

JDK, JRE and JVM Relationship
DEVELOPER

    │
    ▼

JAVA SOURCE CODE

    │
    ▼

JDK TOOL: javac

    │
    ▼

BYTECODE

    │
    ▼

RUNTIME ENVIRONMENT

    │
    ▼

JVM

    │
    ▼

OPERATING SYSTEM

    │
    ▼

HARDWARE
Simple Formula
CONCEPTUAL MODEL

JDK
=
Development Tools
+
Runtime Capabilities


JRE
=
JVM
+
Runtime Libraries
+
Supporting Components


JVM
=
Bytecode Execution Engine
+
Runtime Memory Management
+
Execution Services

JVM Architecture

The JVM contains multiple subsystems that work together to load classes, manage memory, execute bytecode, optimize code, and interact with native systems.

JVM Architecture
                    .class FILES
                         │
                         ▼
              ┌─────────────────────┐
              │    CLASS LOADER     │
              │      SUBSYSTEM      │
              └─────────────────────┘
                         │
                         ▼
              ┌─────────────────────┐
              │   RUNTIME MEMORY    │
              │                     │
              │  ├── Method Area    │
              │  ├── Heap           │
              │  ├── Java Stacks    │
              │  ├── PC Registers   │
              │  └── Native Stacks  │
              └─────────────────────┘
                         │
                         ▼
              ┌─────────────────────┐
              │  EXECUTION ENGINE   │
              │                     │
              │  ├── Interpreter    │
              │  ├── JIT Compiler   │
              │  └── Garbage        │
              │      Collector      │
              └─────────────────────┘
                         │
                         ▼
              ┌─────────────────────┐
              │   NATIVE INTERFACE  │
              │   NATIVE LIBRARIES  │
              └─────────────────────┘

Class Loader Subsystem

The Class Loader Subsystem is responsible for finding and loading class definitions required by a Java application.

Class Loading Process
CLASS REQUIRED

    │
    ▼

CLASS LOADER SUBSYSTEM

    │
    ├── Loading
    │
    ├── Linking
    │
    └── Initialization

    │
    ▼

CLASS READY FOR USE

Loading Phase

During loading, the JVM finds the binary representation of a class and creates the corresponding runtime representation.

Loading
.class FILE

    │
    ▼

READ CLASS DATA

    │
    ▼

CREATE RUNTIME CLASS REPRESENTATION

    │
    ▼

CLASS LOADED
  • Find the required class.
  • Read the class binary data.
  • Create the runtime representation of the class.
  • Make class information available to the JVM.

Linking Phase

After loading, a class passes through linking. Linking is commonly described through verification, preparation, and resolution.

Linking
LINKING

├── Verification
│   └── Check class structure and bytecode
│
├── Preparation
│   └── Prepare memory for class-level data
│
└── Resolution
    └── Resolve symbolic references

Verification

Checks whether the loaded class follows required structural and bytecode rules.

Preparation

Prepares memory and default values for relevant class-level data.

Resolution

Transforms symbolic references into more direct runtime references when required.

Initialization Phase

Initialization executes class initialization logic at the appropriate time. This includes initialization of static fields and execution of static initialization blocks according to Java rules.

Static Initialization
class Example {

    static int value = 100;

    static {
        System.out.println(
            "Class initialized"
        );
    }

}

Class Loaders

A typical modern Java runtime uses a hierarchy of built-in class loaders for different categories of classes.

Class Loader Hierarchy
BOOTSTRAP CLASS LOADER

        │
        ▼

PLATFORM CLASS LOADER

        │
        ▼

APPLICATION CLASS LOADER

        │
        ▼

APPLICATION CLASSES

Bootstrap Class Loader

The Bootstrap Class Loader is responsible for loading fundamental Java runtime classes required by the platform.

Bootstrap Loader
BOOTSTRAP CLASS LOADER

        │
        ▼

LOADS FUNDAMENTAL CLASSES

Examples:

java.lang.Object
java.lang.String
java.lang.System

Platform Class Loader

The Platform Class Loader loads platform classes that are not loaded directly by the Bootstrap Class Loader.

Platform Loader
PLATFORM CLASS LOADER

        │
        ▼

LOADS PLATFORM CLASSES

        │
        ▼

DELEGATES UPWARD FIRST

        │
        ▼

BOOTSTRAP CLASS LOADER

Application Class Loader

The Application Class Loader normally loads classes from the application class path and is responsible for many classes written by application developers.

Application Loader
APPLICATION CLASS LOADER

        │
        ▼

LOADS

Your Classes
Application Dependencies
Classpath Resources

Parent Delegation Model

Class loading commonly follows a parent delegation model. Before a class loader attempts to load a class itself, it first asks its parent to load the class.

Parent Delegation
APPLICATION NEEDS CLASS

        │
        ▼

APPLICATION LOADER

        │
        ▼

ASK PARENT

PLATFORM LOADER

        │
        ▼

ASK PARENT

BOOTSTRAP LOADER

        │
        ▼

CAN LOAD?

YES ───────────────► RETURN CLASS

NO
 │
 ▼

PLATFORM TRIES

NO
 │
 ▼

APPLICATION TRIES
Why Parent Delegation Matters
  • Helps protect core Java classes.
  • Reduces unnecessary duplicate class loading.
  • Creates a predictable loading hierarchy.
  • Improves consistency in the runtime.

JVM Runtime Memory

During program execution, the JVM organizes memory into different runtime areas. Each area has a different purpose.

JVM Runtime Memory Areas
JVM MEMORY

├── Method Area
│
├── Heap
│
├── Java Stack
│   └── One per thread
│
├── PC Register
│   └── One per thread
│
└── Native Method Stack
    └── Associated with native execution

Method Area

The Method Area is a logical JVM runtime area associated with class-level structures and metadata.

  • Class metadata.
  • Runtime constant pool information.
  • Method information.
  • Field information.
  • Class-related structures.
  • Static data associated with loaded classes.
Method Area Concept
METHOD AREA

├── Class Information
├── Method Information
├── Field Information
├── Runtime Constants
└── Class-Level Data
Implementation Detail
  • The JVM specification defines logical runtime areas.
  • Actual implementation details can vary between JVM implementations.
  • In HotSpot, class metadata is associated with an implementation area known as Metaspace.

Heap Memory

The Heap is the runtime memory area where objects and arrays are generally allocated.

Object Creation
Student student =
    new Student();
Reference and Object
STACK

student
   │
   │ Reference
   ▼

HEAP

┌─────────────────────┐
│   Student Object    │
└─────────────────────┘
Heap Characteristics
  • Objects are generally allocated in the heap.
  • Arrays are objects and are allocated in the heap.
  • Heap memory is shared among application threads.
  • Garbage collection primarily manages heap objects.
  • Heap size can affect application performance.

Java Stack

Each Java thread has its own Java Virtual Machine stack. The stack stores frames created during method calls.

Thread Stacks
JVM PROCESS

├── Thread 1
│   └── Java Stack 1
│
├── Thread 2
│   └── Java Stack 2
│
└── Thread 3
    └── Java Stack 3

When a method is called, a new stack frame is created. When the method finishes, its frame is removed.

Stack Frames

A stack frame stores information required for one method invocation.

Stack Frame
STACK FRAME

├── Local Variables
├── Operand Stack
├── Method Execution Data
└── Return Information
Method Call Example
public static void main(String[] args) {

    int result = add(10, 20);

}

static int add(int a, int b) {

    int total = a + b;

    return total;

}
Stack During add()
TOP OF STACK

┌─────────────────────┐
│     add() Frame     │
│                     │
│ a = 10              │
│ b = 20              │
│ total = 30          │
└─────────────────────┘

┌─────────────────────┐
│    main() Frame     │
│                     │
│ args                │
│ result              │
└─────────────────────┘

BOTTOM OF STACK

PC Register

PC stands for Program Counter. Each Java thread has its own PC register. It helps track the current JVM instruction associated with the executing thread.

PC Registers
THREAD 1

PC Register 1
Current Instruction


THREAD 2

PC Register 2
Current Instruction


THREAD 3

PC Register 3
Current Instruction

Native Method Stack

The Native Method Stack supports execution associated with native methods. Native methods are implemented outside normal Java bytecode, often using languages such as C or C++.

Native Method Flow
JAVA CODE

    │
    ▼

NATIVE METHOD CALL

    │
    ▼

JNI

    │
    ▼

NATIVE LIBRARY

    │
    ▼

OPERATING SYSTEM

Heap vs Stack

Heap and Stack Comparison
HEAP

Stores:
Objects and Arrays

Shared:
Across Threads

Managed By:
Garbage Collector


STACK

Stores:
Method Frames
Local Execution Data

Owned By:
Individual Thread

Managed By:
Method Call and Return

Heap

Shared runtime memory primarily used for objects and arrays.

Stack

Per-thread memory used for method execution frames.

Do Not Oversimplify
  • A local variable is associated with a stack frame.
  • A local reference can point to an object in the heap.
  • An object is not stored inside a local reference variable.
  • Actual JVM implementation details can be more complex than simple diagrams.

Execution Engine

The Execution Engine is responsible for executing the bytecode of loaded classes.

Execution Engine
BYTECODE

    │
    ▼

EXECUTION ENGINE

├── Interpreter
│
├── JIT Compiler
│
└── Garbage Collector

    │
    ▼

NATIVE EXECUTION

Interpreter

The interpreter executes bytecode instructions as the program runs. This allows execution to begin without compiling the entire application into native machine code in advance.

Interpreter
BYTECODE

Instruction 1
    │
    ▼
Execute

Instruction 2
    │
    ▼
Execute

Instruction 3
    │
    ▼
Execute
Interpreter Advantage
  • Execution can begin quickly.
  • The JVM does not need to compile every method into native code before the application starts.

JIT Compiler

JIT stands for Just-In-Time compiler. The JIT compiler identifies code that benefits from runtime compilation and converts it into optimized native machine code.

JIT Compilation
BYTECODE

    │
    ▼

FREQUENTLY EXECUTED CODE

    │
    ▼

JIT COMPILER

    │
    ▼

OPTIMIZED NATIVE CODE

    │
    ▼

FASTER REPEATED EXECUTION
Why JIT Matters
  • Frequently executed code can be optimized.
  • Compiled native code can be reused.
  • The JVM can optimize based on runtime behavior.
  • Long-running applications can achieve strong performance.

Interpreter vs JIT

Interpreter and JIT
INTERPRETER

Bytecode
   │
   ▼
Execute Instructions
During Runtime


JIT COMPILER

Frequently Executed Bytecode
   │
   ▼
Compile to Native Code
   │
   ▼
Execute Optimized Code

Interpreter

Begins executing bytecode without waiting for broad native compilation.

JIT Compiler

Compiles selected code into optimized native instructions during execution.

HotSpot JVM

HotSpot is a widely used JVM technology associated with identifying frequently executed areas of code and applying runtime optimizations.

HotSpot Concept
APPLICATION RUNS

        │
        ▼

JVM OBSERVES EXECUTION

        │
        ▼

IDENTIFIES HOT CODE

        │
        ▼

JIT COMPILATION

        │
        ▼

RUNTIME OPTIMIZATION

        │
        ▼

IMPROVED PERFORMANCE

This adaptive optimization model is one reason Java applications can become highly optimized during long-running execution.

Garbage Collector

The Garbage Collector, commonly called GC, automatically manages memory used by objects that are no longer reachable by the application.

Object Reference
Student student =
    new Student();

student = null;
Garbage Collection Concept
STEP 1

student ──────────► Student Object


STEP 2

student = null


STEP 3

Student Object

No Longer Reachable

        │
        ▼

Eligible for
Garbage Collection
Eligible Does Not Mean Immediately Deleted
  • An unreachable object may become eligible for garbage collection.
  • The developer does not control the exact collection time.
  • The JVM decides when garbage collection should occur.
  • Calling System.gc() does not guarantee immediate collection.

Automatic Memory Management

Java provides automatic memory management. Developers create objects, while the JVM manages the recovery of heap memory associated with objects that are no longer reachable.

Memory Management
DEVELOPER

Creates Objects

        │
        ▼

HEAP MEMORY

        │
        ▼

OBJECTS BECOME UNREACHABLE

        │
        ▼

GARBAGE COLLECTOR

        │
        ▼

MEMORY CAN BE RECLAIMED
Benefits
  • Reduces manual memory deallocation.
  • Avoids direct free/delete style memory management for normal Java objects.
  • Helps reduce certain categories of memory errors.
  • Allows the runtime to optimize memory management strategies.

When Objects Become Eligible for Garbage Collection

An object becomes eligible for garbage collection when it is no longer reachable through live references according to the JVM reachability model.

Reference Set to null
Student student =
    new Student();

student = null;
Reference Reassigned
Student student =
    new Student();

student =
    new Student();
Reassignment
BEFORE

student ──────────► Object A


AFTER

student ──────────► Object B


Object A

No Remaining Reachable Reference

        │
        ▼

May Become Eligible for GC

Native Method Interface

JNI stands for Java Native Interface. It provides mechanisms for Java code to interact with native code and libraries.

JNI
JAVA APPLICATION

        │
        ▼

JAVA NATIVE INTERFACE

        │
        ▼

NATIVE CODE

C / C++ / Platform Libraries

        │
        ▼

OPERATING SYSTEM OR HARDWARE
Why Native Integration May Be Used
  • Access existing native libraries.
  • Interact with operating-system-specific functionality.
  • Use specialized hardware APIs.
  • Integrate with legacy native code.
  • Perform tasks not directly available through standard Java APIs.

Native Libraries

Native libraries contain machine-specific code compiled for a particular operating system and processor architecture.

Native Library Dependency
JAVA BYTECODE

Platform Independent

        │
        ▼

JNI

        │
        ▼

NATIVE LIBRARY

Platform Specific

        │
        ▼

OPERATING SYSTEM
Portability Impact
  • Pure Java code is generally easier to move between platforms.
  • Native libraries may require different builds for different operating systems.
  • Heavy native dependencies can reduce application portability.

Complete JVM Execution Flow

Complete Execution Flow
1. WRITE SOURCE CODE

Hello.java

        │
        ▼

2. COMPILE

javac Hello.java

        │
        ▼

3. GENERATE BYTECODE

Hello.class

        │
        ▼

4. START JVM

java Hello

        │
        ▼

5. LOAD CLASSES

Class Loader Subsystem

        │
        ▼

6. LINK CLASSES

Verification
Preparation
Resolution

        │
        ▼

7. INITIALIZE CLASSES

Static Initialization

        │
        ▼

8. CREATE RUNTIME MEMORY

Method Area
Heap
Stacks
PC Registers
Native Stacks

        │
        ▼

9. EXECUTE BYTECODE

Interpreter
+
JIT Compiler

        │
        ▼

10. MANAGE MEMORY

Garbage Collector

        │
        ▼

11. INTERACT WITH SYSTEM

Native Interface
Operating System
Hardware

        │
        ▼

12. PRODUCE OUTPUT

Practical Compilation Example

Consider a simple Java program named Hello.java.

Hello.java
public class Hello {

    public static void main(String[] args) {

        System.out.println(
            "Hello from Java"
        );

    }

}

Compile the program using javac.

Compile
javac Hello.java

After successful compilation, the directory contains the source file and generated class file.

Files
Hello.java
Hello.class

Run the compiled program using the java command.

Run
java Hello
Output
Hello from Java
What Happened Internally
Hello.java

    │
    ▼

javac

    │
    ▼

Hello.class

    │
    ▼

java Hello

    │
    ▼

JVM Starts

    │
    ▼

Hello Class Loaded

    │
    ▼

main() Located

    │
    ▼

Bytecode Executed

    │
    ▼

Hello from Java

Multiple JVM Implementations

JVM is a specification and execution model. Different organizations and projects can provide implementations that follow applicable Java platform requirements.

HotSpot

A widely used JVM implementation associated with OpenJDK and many JDK distributions.

OpenJ9

An alternative JVM implementation focused on different runtime characteristics.

Specialized Runtimes

Different Java runtimes may optimize for particular workloads and deployment environments.

JVM Specification and Implementations
JVM SPECIFICATION

        │
        ├───────────────┬───────────────┐
        ▼               ▼               ▼

Implementation A   Implementation B   Implementation C

        │               │               │
        ▼               ▼               ▼

Different Runtime Strategies

        │
        ▼

Java Bytecode Execution

JDK Distributions

Developers can obtain Java through different JDK distributions. These distributions are built from Java platform implementations and may differ in support, packaging, update policies, licensing terms, and additional tooling.

Important Concept
  • Java is not limited to one downloadable JDK package.
  • Multiple vendors provide JDK distributions.
  • Different distributions can target different operating systems and architectures.
  • Support and update policies may differ.
  • The Java version required by the project is usually more important than the brand name for basic learning.

Modern JDK and JRE

Older Java learning materials often describe the JDK and standalone JRE as separate downloads. This remains useful conceptually, but modern Java deployment has changed.

Modern applications may use a complete JDK during development and deployment, or create custom runtime images containing only the modules required by the application.

Traditional Model
DEVELOPER MACHINE

Install JDK


USER MACHINE

Install Standalone JRE
Modern Possibilities
DEVELOPMENT

Full JDK

        │
        ▼

DEPLOYMENT OPTIONS

├── Full JDK Runtime
│
├── Vendor Runtime Package
│
├── Custom Runtime Image
│
└── Application-Specific Package
Why This Matters
  • The conceptual JDK, JRE, and JVM relationship is still essential.
  • Modern JDK distributions may not provide a separate traditional JRE download.
  • Do not assume every modern Java installation has a separate jre directory.
  • Modern Java supports custom runtime creation.

Development vs Execution

Development and Execution
DEVELOPMENT TIME

Developer
   │
   ▼
Write .java
   │
   ▼
JDK Tools
   │
   ▼
Compile
   │
   ▼
.class Bytecode


RUNTIME

.class Bytecode
   │
   ▼
Runtime Environment
   │
   ▼
JVM
   │
   ▼
Execute

Development Time

Source code is written, compiled, tested, debugged, documented, and packaged.

Runtime

Compiled application code is loaded and executed through the JVM.

Real-World Analogy

Imagine that Java development is similar to writing and translating a book for readers in different countries.

Java Developer

The author who writes the original content.

JDK

The complete authoring toolkit containing writing, checking, translation, and publishing tools.

Bytecode

A universal intermediate format understood by compatible translators.

JVM

The local translator that converts the universal format into instructions for the local environment.

Analogy
AUTHOR

Writes Content

        │
        ▼

AUTHORING TOOLKIT

JDK

        │
        ▼

UNIVERSAL FORMAT

Bytecode

        │
        ▼

LOCAL TRANSLATOR

JVM

        │
        ▼

LOCAL PLATFORM

Windows / Linux / macOS

Common Errors

javac is not recognized
  • The JDK may not be installed.
  • The PATH environment variable may not include the JDK bin directory.
  • The terminal may need to be restarted after configuration.
  • The command may be using the wrong Java installation.
Could not find or load main class
  • The class name may be incorrect.
  • The current directory may be incorrect.
  • The classpath may be incorrect.
  • The package name may not match the directory structure.
  • The class file may not have been compiled.
Unsupported Class Version
  • The program may have been compiled with a newer Java version.
  • The runtime may be older than the compiler target.
  • Compile and run using compatible Java versions.

Common Misconceptions

Avoid These Misconceptions
  • JDK, JRE, and JVM are not the same thing.
  • JDK does not stand for Java Deployment Kit.
  • JRE does not compile Java source code in the traditional model.
  • JVM does not normally execute .java source code directly.
  • javac produces bytecode, not a normal native executable.
  • Bytecode is not the same as Java source code.
  • Bytecode is not the same as processor-specific machine code.
  • The JVM implementation is platform dependent.
  • Java bytecode is designed to be platform independent.
  • The same JVM binary does not run on every operating system.
  • Each supported platform needs an appropriate JVM implementation.
  • Java is not purely interpreted.
  • Modern JVMs use interpretation and runtime compilation techniques.
  • JIT does not compile Java source files during normal bytecode execution.
  • Objects are generally allocated in heap memory.
  • A reference variable is not the same as the object it references.
  • Each Java thread has its own Java stack.
  • Heap memory is shared across application threads.
  • Garbage collection does not guarantee immediate object removal.
  • System.gc() does not guarantee immediate garbage collection.
  • The JVM specification and a JVM implementation are not exactly the same concept.
  • Modern Java installations may not contain a separate traditional JRE directory.
  • A standalone JRE download is not required for every modern deployment strategy.
  • Using native libraries can reduce portability.
  • Platform independence does not eliminate all compatibility issues.

Common Interview Questions

What is JDK?

JDK stands for Java Development Kit. It provides the tools and runtime capabilities required to develop and run Java applications.

What is JRE?

JRE stands for Java Runtime Environment. Conceptually, it provides the JVM, runtime libraries, and supporting components required to run Java applications.

What is JVM?

JVM stands for Java Virtual Machine. It is the execution environment responsible for running Java bytecode.

What is the difference between JDK and JRE?

The JDK includes development tools and runtime capabilities, while the JRE concept focuses on the components required for application execution.

What is the difference between JRE and JVM?

The JVM executes bytecode, while the JRE concept includes the JVM together with runtime libraries and supporting components.

Why is Java platform independent?

Java source code is compiled into bytecode that can run through compatible JVM implementations on different platforms.

Is JVM platform independent?

No. JVM implementations are built for specific operating systems and hardware architectures.

What is bytecode?

Bytecode is the intermediate instruction format generated by the Java compiler for execution by the JVM.

What does javac do?

javac compiles Java source code into Java bytecode.

What does the java command do?

The java command launches the Java runtime and starts execution of a Java application.

What is JIT?

JIT stands for Just-In-Time compiler. It compiles selected bytecode into optimized native machine code during execution.

What is the Class Loader?

The Class Loader Subsystem finds and loads class definitions required by a Java application.

What are the major JVM memory areas?

Major logical runtime areas include the Method Area, Heap, Java Stacks, PC Registers, and Native Method Stacks.

What is stored in Heap memory?

Objects and arrays are generally allocated in Heap memory.

What is stored in the Java Stack?

The Java Stack stores frames created during method calls.

What is Garbage Collection?

Garbage Collection is the automatic process of reclaiming heap memory from objects that are no longer reachable.

Frequently Asked Questions

Do I need the JDK to learn Java?

Yes. A JDK provides the compiler and other tools needed to develop Java programs.

Can I run Java without a JVM?

Traditional Java bytecode execution requires a compatible Java runtime environment with JVM capabilities.

Does the JVM understand Java source code?

The traditional process compiles Java source code into bytecode first. The JVM then executes the bytecode.

What file does javac generate?

javac normally generates one or more .class files containing bytecode.

Can one .java file create multiple .class files?

Yes. Depending on the source structure, compilation can produce multiple class files.

Is bytecode human readable?

It is a binary class-file format and is not intended to be read like Java source code, although tools such as javap can display bytecode-related information.

Does Java compile or interpret?

Java uses both compilation and runtime execution techniques. Source code is compiled into bytecode, and the JVM can interpret and JIT-compile bytecode.

Why is Java slower at startup than some native programs?

Starting the runtime, loading classes, and performing runtime optimization can add startup overhead, although actual performance depends on the application and runtime.

Can Java become fast after running for some time?

Long-running applications can benefit from runtime profiling and JIT optimization of frequently executed code.

Does every thread share the same stack?

No. Each Java thread has its own Java Virtual Machine stack.

Do threads share Heap memory?

The Heap is shared among application threads.

Can I manually delete a Java object?

Java does not use a normal delete operation for objects. The garbage collector manages reclamation of unreachable objects.

Can I force garbage collection?

You can request garbage collection through mechanisms such as System.gc(), but immediate collection is not guaranteed.

What is HotSpot?

HotSpot is a widely used JVM technology that performs adaptive runtime optimization.

What is Metaspace?

Metaspace is a HotSpot implementation area used for class metadata.

What is JNI?

JNI stands for Java Native Interface and allows Java code to interact with native code.

Does every modern JDK include a separate JRE folder?

No. Modern Java distributions and deployment models do not necessarily include a separate traditional JRE directory.

Can I create a custom Java runtime?

Modern Java provides tools such as jlink that can create custom runtime images containing selected modules.

What is the easiest way to remember JDK, JRE, and JVM?

JDK develops and runs, JRE represents the runtime environment, and JVM executes bytecode.

Key Takeaways

  • Java is both a programming language and a software platform.
  • JDK stands for Java Development Kit.
  • The JDK is used to develop and run Java applications.
  • The JDK contains development tools.
  • javac is the Java compiler.
  • The java command launches Java applications.
  • jar manages Java archives.
  • javadoc generates documentation.
  • javap inspects compiled class information.
  • jshell provides an interactive Java environment.
  • JRE stands for Java Runtime Environment.
  • The JRE concept represents the environment required to run Java applications.
  • The JRE includes JVM capabilities and runtime components.
  • JVM stands for Java Virtual Machine.
  • The JVM executes Java bytecode.
  • Java source files normally use the .java extension.
  • Compiled Java class files use the .class extension.
  • javac converts Java source code into bytecode.
  • Bytecode is an intermediate instruction format.
  • Bytecode is not normal native machine code.
  • The JVM loads and executes bytecode.
  • Java bytecode is designed for platform independence.
  • JVM implementations are platform dependent.
  • Different platforms require appropriate JVM implementations.
  • WORA means Write Once, Run Anywhere.
  • WORA depends on compatible runtime environments.
  • Native dependencies can reduce portability.
  • The Class Loader Subsystem loads classes.
  • Class processing includes loading, linking, and initialization.
  • Linking includes verification, preparation, and resolution.
  • The Bootstrap Class Loader loads fundamental runtime classes.
  • The Platform Class Loader loads platform classes.
  • The Application Class Loader loads application classes.
  • Class loading commonly uses parent delegation.
  • The JVM manages multiple runtime memory areas.
  • The Method Area stores class-related structures.
  • Objects and arrays are generally allocated in the Heap.
  • Each Java thread has its own Java Stack.
  • Method calls create stack frames.
  • Stack frames contain local execution data.
  • Each thread has a PC Register.
  • Native Method Stacks support native execution.
  • Heap memory is shared among threads.
  • Java Stacks belong to individual threads.
  • The Execution Engine executes bytecode.
  • The Interpreter executes bytecode instructions during runtime.
  • The JIT Compiler compiles selected bytecode into native code.
  • HotSpot uses runtime information for optimization.
  • Garbage Collection manages unreachable objects.
  • Objects eligible for garbage collection are not necessarily removed immediately.
  • System.gc() does not guarantee immediate collection.
  • JNI allows Java to interact with native code.
  • Native libraries are platform specific.
  • Multiple JVM implementations exist.
  • Multiple JDK distributions exist.
  • Modern Java may not use a separately installed traditional JRE.
  • Custom runtime images can be created for modern applications.
  • JDK, JRE, and JVM are related but different concepts.

Summary

The JDK, JRE, and JVM form the foundation of Java development and execution. Although the three terms are closely related, each represents a different responsibility.

The JDK, or Java Development Kit, provides the tools required to develop Java applications. These tools include the javac compiler, the java launcher, archive tools, documentation tools, debugging tools, monitoring tools, and runtime capabilities.

The JRE, or Java Runtime Environment, is the conceptual runtime environment required for executing Java applications. It includes JVM capabilities, runtime libraries, and supporting components.

The JVM, or Java Virtual Machine, is the execution engine responsible for running Java bytecode. It loads classes, verifies code, manages memory, executes instructions, performs runtime optimization, manages garbage collection, and interacts with the underlying system.

Java source code is written in .java files. The javac compiler translates source code into bytecode stored in .class files. The JVM then loads and executes this bytecode.

This intermediate bytecode model is the foundation of Java platform independence. The same compatible bytecode can run on Windows, Linux, macOS, and other supported platforms through appropriate JVM implementations.

The bytecode is designed to be platform independent, but the JVM implementation itself is platform dependent because it must communicate with the local operating system and hardware.

Inside the JVM, the Class Loader Subsystem loads required classes. Classes pass through loading, linking, and initialization. The JVM then organizes runtime data into areas such as the Method Area, Heap, Java Stacks, PC Registers, and Native Method Stacks.

The Execution Engine runs bytecode using techniques such as interpretation and Just-In-Time compilation. Frequently executed code can be compiled into optimized native instructions, allowing long-running Java applications to achieve strong performance.

The JVM also provides automatic memory management through garbage collection. Objects that are no longer reachable may become eligible for garbage collection, allowing their heap memory to be reclaimed.

Modern Java deployment has evolved beyond the older model of installing a separate JRE everywhere. Developers can use full JDK distributions, vendor runtime packages, or custom runtime images depending on application requirements.

Understanding JDK, JRE, and JVM gives you the foundation required to understand Java installation, compilation, program execution, memory management, performance, class loading, and platform independence.

Lesson 3 Completed
  • You understand the Java platform.
  • You understand the complete JDK, JRE, and JVM relationship.
  • You know what JDK means.
  • You know what the JDK contains.
  • You understand important JDK tools.
  • You know what javac does.
  • You know what the java command does.
  • You understand the role of other JDK tools.
  • You know what JRE means.
  • You understand what the JRE contains.
  • You understand the difference between JDK and JRE.
  • You know what JVM means.
  • You understand why the JVM exists.
  • You understand the source-code-to-execution process.
  • You know what Java source code is.
  • You understand Java compilation.
  • You know what bytecode is.
  • You understand the purpose of .class files.
  • You understand how bytecode is executed.
  • You understand Java platform independence.
  • You understand the WORA principle.
  • You know why Java is called platform independent.
  • You know why the JVM implementation is platform dependent.
  • You understand the complete relationship between development and execution.
  • You understand the basic JVM architecture.
  • You understand the Class Loader Subsystem.
  • You understand the loading phase.
  • You understand the linking phase.
  • You understand the initialization phase.
  • You know the major built-in class loaders.
  • You understand the Bootstrap Class Loader.
  • You understand the Platform Class Loader.
  • You understand the Application Class Loader.
  • You understand parent delegation.
  • You know the major JVM runtime memory areas.
  • You understand the Method Area.
  • You understand Heap memory.
  • You understand Java Stacks.
  • You understand stack frames.
  • You understand PC Registers.
  • You understand Native Method Stacks.
  • You understand the difference between Heap and Stack.
  • You understand the Execution Engine.
  • You understand the Interpreter.
  • You understand the JIT Compiler.
  • You understand the difference between interpretation and JIT compilation.
  • You understand the HotSpot optimization concept.
  • You understand Garbage Collection.
  • You understand automatic memory management.
  • You understand when objects become eligible for garbage collection.
  • You understand JNI.
  • You understand native libraries.
  • You understand the complete JVM execution flow.
  • You know how to compile and run a Java program.
  • You understand that multiple JVM implementations exist.
  • You understand that multiple JDK distributions exist.
  • You understand the modern JDK and JRE model.
  • You understand development time and runtime.
  • You are ready to install and configure Java.
Next Lesson →

Java Installation