Courses / Complete Java Course by codesofrohan / Introduction / JDK vs JRE vs JVM Explained

JDK vs JRE vs JVM Explained

When you start learning Java, you will frequently hear three important terms:

  • JDK (Java Development Kit)

  • JRE (Java Runtime Environment)

  • JVM (Java Virtual Machine)

Many beginners get confused because these three terms seem similar. However, each one has a different purpose. Understanding the relationship between JDK, JRE, and JVM is essential because every Java program depends on them.

In this chapter, we will understand what each one does, how they work together, and why Java is called a platform-independent programming language.


Understanding Through a Real-World Example

Imagine you are writing a book.

  • You (the writer) need a laptop, a text editor, grammar tools, and software to create the book.

  • The reader only needs a PDF reader or a book to read it.

  • The operating system helps the reader display the book correctly.

Similarly,

  • JDK is everything needed to develop Java programs.

  • JRE is everything needed to run Java programs.

  • JVM is the engine that actually executes the Java program.

Think of it like this:

Developer → JDK → Creates Java Program → JRE → JVM → Program Runs


What is JVM (Java Virtual Machine)?

The Java Virtual Machine (JVM) is the heart of Java.

It is a virtual machine that runs Java bytecode. Instead of running your Java program directly, the JVM converts the bytecode into machine code that your operating system understands.

Since every operating system has its own JVM implementation, the same Java program can run on Windows, Linux, or macOS without changing the code.

This is the main reason Java follows the principle:

Write Once, Run Anywhere (WORA).


What Does JVM Do?

The JVM performs several important tasks:

  • Loads Java classes into memory.

  • Verifies the bytecode for security.

  • Converts bytecode into machine code.

  • Executes the program.

  • Manages memory automatically.

  • Performs garbage collection.

  • Handles exceptions during execution.

Without the JVM, Java programs cannot run.


Real-Life Analogy of JVM

Imagine a translator in an international meeting.

Suppose:

  • An Indian person speaks Hindi.

  • A Japanese person speaks Japanese.

  • An American speaks English.

None of them understand each other's language.

A translator converts every language into one that everyone understands.

Similarly,

  • Java source code is first converted into bytecode.

  • The JVM translates that bytecode into the machine language of the operating system.

The translator is different in every country, but the original speech remains the same.

Likewise, every operating system has its own JVM, but the Java program remains unchanged.


What is JRE (Java Runtime Environment)?

The Java Runtime Environment (JRE) provides everything required to run Java applications.

It contains:

  • JVM

  • Java libraries

  • Runtime files

  • Supporting resources

If you only want to run Java software and do not plan to develop Java applications, then only the JRE is required.


Real-Life Example

Suppose you download a Java-based game.

You do not want to modify its source code.

You simply want to play it.

In this case, your computer only needs the JRE.

It provides everything necessary for running the game.


Components of JRE

The JRE mainly includes:

  • JVM

  • Core Java libraries

  • Runtime classes

  • Configuration files

  • Supporting packages

Notice that the JRE does not include development tools such as the Java compiler.


What is JDK (Java Development Kit)?

The Java Development Kit (JDK) is a complete package used by Java developers.

It includes everything required to:

  • Write Java code

  • Compile Java code

  • Debug applications

  • Run Java applications

  • Package applications

Whenever you install Java for programming, you are usually installing the JDK.


Components of JDK

The JDK contains:

  • JRE

  • JVM

  • Java Compiler (javac)

  • Debugger

  • Documentation tools

  • Jar tool

  • Development utilities

In simple words,

JDK = JRE + Development Tools


Real-World Analogy of JDK

Imagine you are building a car.

To build it, you need:

  • Tools

  • Machines

  • Engineers

  • Testing equipment

  • Paint shop

  • Assembly line

This complete factory is like the JDK.

Once the car is built, the customer only needs the car to drive.

That is similar to the JRE.

The engine inside the car that actually makes it move is like the JVM.


Relationship Between JDK, JRE, and JVM

The relationship can be understood as follows:

JDK
│
├── JRE
│     │
│     └── JVM

Or simply:

JDK contains JRE, and JRE contains JVM.

Every Java developer needs the JDK.

Every Java application needs the JRE.

Every Java program is executed by the JVM.


Java Program Execution Process

Let's understand what happens when you run a Java program.

Step 1: Write Java Code

Create a Java file.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

This file is called the source code.


Step 2: Compile the Code

The Java compiler (javac) converts the source code into bytecode.

Hello.java
      ↓
 javac
      ↓
Hello.class

The .class file contains bytecode.


Step 3: JVM Loads the Bytecode

The JVM loads the .class file into memory.

Before execution, it verifies that the bytecode is safe and valid.


Step 4: Bytecode Becomes Machine Code

The JVM uses the Just-In-Time (JIT) Compiler to convert bytecode into machine code.

Machine code is what your processor understands.


Step 5: Program Executes

Finally, your Java program runs successfully.

Source Code
     ↓
Compiler
     ↓
Bytecode
     ↓
JVM
     ↓
Machine Code
     ↓
Output

Why Doesn't Java Compile Directly to Machine Code?

Languages like C and C++ compile directly into machine code.

That machine code works only on the operating system for which it was compiled.

Java takes a different approach.

Instead of generating machine code immediately, Java creates bytecode.

This bytecode can run on any operating system that has a compatible JVM.

This is why Java applications are highly portable.


Everyday Example

Suppose you create a YouTube video.

People can watch it on:

  • Android

  • iPhone

  • Windows

  • Smart TV

  • Tablet

You upload only one video.

Each device has its own video player that knows how to play it.

Similarly,

  • Your Java code is written once.

  • It is converted into bytecode.

  • Each operating system has its own JVM to execute that bytecode.

This is exactly how Java achieves platform independence.


JDK vs JRE vs JVM

Feature JDK JRE JVM
Full Form Java Development Kit Java Runtime Environment Java Virtual Machine
Used By Developers End Users Java Runtime
Purpose Develop and Run Java Programs Run Java Programs Execute Bytecode
Contains Compiler ✅ Yes ❌ No ❌ No
Contains JVM ✅ Yes ✅ Yes Itself
Contains Development Tools ✅ Yes ❌ No ❌ No

Key Points to Remember

  • JDK is used for developing Java applications.

  • JRE is used for running Java applications.

  • JVM executes Java bytecode.

  • JDK contains JRE.

  • JRE contains JVM.

  • JVM converts bytecode into machine code.

  • Java achieves Write Once, Run Anywhere (WORA) using the JVM.

  • Every operating system has its own JVM implementation.

  • Without the JVM, a Java program cannot execute.


Conclusion

JDK, JRE, and JVM are the three fundamental building blocks of the Java ecosystem. Think of the JDK as the complete toolbox for developers, the JRE as the environment needed to run Java applications, and the JVM as the intelligent engine that translates bytecode into machine code for the underlying operating system. Once you understand how these three components work together, Java's platform independence becomes much easier to understand. This knowledge forms the foundation for learning more advanced Java topics such as the JIT Compiler, Garbage Collection, Class Loaders, and Java Memory Management in the upcoming lessons.

🔖 Bookmark saved successfully!