Difference Between JDK, JRE, and JVM

java dev.to

Introduction

When I started learning Java, I often heard the terms JDK, JRE, and JVM. At first, these names sounded confusing because they are closely related. I wondered why Java needed all three and what each one actually does.

After learning more about Java, I realized that understanding JDK, JRE, and JVM is important because they are the foundation of how Java programs are developed and executed. In this blog, I will explain these concepts in a simple way that beginners can easily understand.


What is JVM?

JVM stands for Java Virtual Machine.

The JVM is responsible for running Java programs. When a Java program is compiled, it is converted into a special format called bytecode. The JVM reads this bytecode and converts it into machine code that the computer can understand.

One of the main advantages of the JVM is platform independence. A Java program can run on different operating systems such as Windows, Linux, and macOS because each operating system has its own JVM implementation.

Functions of JVM

  • Executes Java bytecode
  • Manages memory
  • Performs garbage collection
  • Provides platform independence

In simple terms, the JVM acts like a translator between Java code and the computer.


What is JRE?

JRE stands for Java Runtime Environment.

The JRE provides everything needed to run Java applications. It includes the JVM and the necessary libraries required for execution.

If a user only wants to run a Java application and does not need to write Java code, installing the JRE is sufficient.

Components of JRE

  • JVM
  • Java class libraries
  • Supporting files

Think of the JRE as a package that contains the JVM along with all the resources needed to execute Java programs.


What is JDK?

JDK stands for Java Development Kit.

The JDK is mainly used by developers who want to create Java applications. It contains the JRE and additional tools that help in writing, compiling, debugging, and running Java programs.

When we install Java for development purposes, we usually install the JDK.

Components of JDK

  • JRE
  • JVM
  • Java compiler (javac)
  • Debugging tools
  • Development utilities

Without the JDK, developers cannot compile Java source code into bytecode.


Understanding with a Simple Example

Suppose you write a Java program called HelloWorld.java.

Step 1: Write the Code

You create a Java file using a text editor or an IDE.

Step 2: Compile the Program

The compiler provided by the JDK converts the source code into bytecode.

javac HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

This creates a file called HelloWorld.class.

Step 3: Run the Program

The JVM reads the bytecode and executes it.

java HelloWorld
Enter fullscreen mode Exit fullscreen mode

The JRE provides the environment, while the JVM actually runs the program.


Relationship Between JDK, JRE, and JVM

The relationship can be represented as:

JDK
 └── JRE
      └── JVM
Enter fullscreen mode Exit fullscreen mode

This means:

  • JDK contains JRE
  • JRE contains JVM
  • JVM executes Java programs

Real-Life Analogy

A simple way to remember these concepts is by comparing them to a car.

JVM = Engine

The engine performs the actual work of running the car.

JRE = Car

The car contains the engine and everything needed to drive.

JDK = Car Factory

The factory contains all the tools needed to build and test the car.

Just as a factory includes everything required to produce a car, the JDK includes everything required to develop Java applications.


Comparison Table

Feature JVM JRE JDK
Runs Java Programs Yes Yes Yes
Contains JVM No Yes Yes
Contains Libraries No Yes Yes
Contains Compiler No No Yes
Used by Developers No No Yes
Used for Execution Yes Yes Yes

References

Source: dev.to

arrow_back Back to Tutorials