How Java Works Internally JVM JRE and JDK Explained

Related Courses

How Java Works Internally: JVM, JRE, and JDK Explained

Introduction: What Actually Happens When You Press “Run” in Java?

Most beginners learn how to write Java code. Fewer learners truly understand what happens after they click “Run.”

  • Translates your code into machine-understandable instructions

  • Manages memory automatically

  • Controls how programs run on different operating systems

  • Protects applications from crashing due to low-level errors

  • Makes Java one of the most trusted enterprise languages in the world

At the center of all this are three important components:
JVM, JRE, and JDK

If you understand these well, Java stops feeling like “magic” and starts feeling like a designed system one you can control, debug, and optimize like a professional developer.

This blog explains how Java works internally in simple language, step by step, without assuming prior technical depth.

The Big Idea: Why Java Is Different From Other Languages

Many programming languages convert code directly into instructions that a computer’s processor understands.

Java takes a different approach.

Java uses a two-step execution model:
Convert your code into an intermediate format called bytecode

This is why Java follows the famous rule:
“Write once, run anywhere.”

Understanding the Three Pillars: JDK, JRE, and JVM

Let’s start with a simple relationship:
JDK contains JRE.
JRE contains JVM.

Think of it like this:

  • JDK = Workshop (for building Java programs)

  • JRE = Environment (for running Java programs)

  • JVM = Engine (that actually executes the program)

Each has a specific role.

What Is JDK (Java Development Kit)?

The JDK is what developers use to create Java programs.

If you want to write, compile, and test Java code, you need the JDK.

What JDK Includes

1. Java Compiler (javac)

This tool converts your .java file into .class files containing bytecode.

2. Development Tools

These include tools for:

  • Debugging

  • Packaging applications

  • Generating documentation

  • Monitoring performance

3. JRE

The JDK already contains the environment needed to run the program.

In Simple Words

If you are a Java developer, you install the JDK.
If you are just a Java user, you only need the JRE.

What Is JRE (Java Runtime Environment)?

The JRE is what allows a system to run Java programs.

It does not let you write or compile code. It only lets you execute it.

What JRE Contains

1. JVM

This is the core engine that runs the program.

2. Core Java Libraries

These are pre-built classes that handle:

  • Input and output

  • File handling

  • Networking

  • Data structures

  • Security

  • User interaction

3. Supporting Files

These help with configuration, security rules, and system integration.

Real-World Example

If you download a Java-based application like a banking system or enterprise tool, you don’t need the JDK. You only need the JRE to run it.

What Is JVM (Java Virtual Machine)?

The JVM is the heart of Java.

It does not understand Java code. It understands bytecode.

Its job is to:

  • Load bytecode

  • Verify it for safety

  • Convert it into machine instructions

  • Manage memory

  • Run the program efficiently

The JVM is the reason Java programs work the same way on Windows, Linux, and macOS.

Each operating system has its own JVM implementation, but all follow the same Java rules.

The Full Execution Flow: Step by Step

Let’s walk through what happens when you run a Java program.

Step 1: Writing the Source Code

You write a program in a file like:
HelloWorld.java
This file is written in human-readable Java syntax.
The computer does not understand this directly.

Step 2: Compilation by JDK

The Java compiler (javac) checks your code for:

  • Syntax errors

  • Type errors

  • Missing semicolons

  • Incorrect method usage

If everything is correct, it converts your code into a file called:
HelloWorld.class
This file contains bytecode.

Step 3: Bytecode - The Middle Language

Bytecode is not:

  • Java

  • Machine language

It is a platform-neutral instruction set.

This means the same .class file can run on:

  • Windows

  • Linux

  • macOS

  • Servers

  • Cloud platforms

As long as a JVM exists, the program can run.

Step 4: Class Loading in JVM

When you run the program, the JVM starts by loading classes into memory.

This happens in three stages:

Loading

The JVM finds the .class file and brings it into memory.

Linking

This includes:

  • Verifying the bytecode for security

  • Allocating memory for variables

  • Resolving references to other classes

Initialization

Static variables and blocks are initialized.

Only after this does the program begin execution.

Step 5: Bytecode Execution

The JVM uses two main techniques:

Interpreter

It reads bytecode line by line and executes it.

Just-In-Time (JIT) Compiler

It detects frequently used code and converts it into native machine code for faster execution.

This combination gives Java both:

  • Flexibility

  • High performance

JVM Memory Structure Explained Simply

One of Java’s biggest strengths is automatic memory management.

The JVM divides memory into different areas, each with a specific role.

1. Method Area

This stores:

  • Class information

  • Method definitions

  • Static variables

  • Runtime constant pool

This area is shared by all objects of the same class.

2. Heap

This is where objects live.

Every time you use new, memory is allocated in the heap.

The heap is managed automatically by the JVM’s Garbage Collector.

3. Stack

Each thread gets its own stack.

The stack stores:

  • Method calls

  • Local variables

  • Execution flow

When a method finishes, its stack frame is removed.

4. Program Counter (PC Register)

This keeps track of:

  • Which instruction is currently being executed

It helps the JVM resume execution correctly after switching between threads.

5. Native Method Stack

This is used when Java interacts with system-level code written in other languages like C or C++.

Garbage Collection: Java’s Hidden Superpower

In many languages, developers must manually free memory.

In Java, the Garbage Collector (GC) does this automatically.

What It Does

  • Finds objects that are no longer used

  • Frees their memory

  • Prevents memory leaks

Why This Matters in Real Systems

In large applications like banking systems or cloud platforms, memory errors can crash services.

Java’s garbage collection reduces this risk and improves system stability.

Class Loader System: How Java Finds Code

The JVM uses a special system to load classes safely.

There are three main loaders:

Bootstrap Class Loader

Loads core Java classes like String, System, and Object.

Extension Class Loader

Loads classes from Java extension libraries.

Application Class Loader

Loads classes from your project.

This layered approach prevents malicious code from replacing core Java functionality.

Security Inside the JVM

Java was designed with security in mind.

The JVM:

  • Verifies bytecode before execution

  • Restricts memory access

  • Controls file and network permissions

  • Prevents illegal operations

This is one reason Java is trusted in:

  • Financial systems

  • Government platforms

  • Enterprise applications

JRE vs JDK vs JVM: Clear Comparison Table

Component Purpose Who Uses It Contains
JVM Runs bytecode System Execution engine
JRE Runs Java programs Users JVM + Libraries
JDK Develops Java programs Developers JRE + Tools

How This Works in Real-World Applications

Example: Banking System

When a banking application runs:

  • JDK was used to build it

  • JRE runs it on servers

  • JVM manages memory, threads, and execution

  • Garbage collector keeps system stable

  • Security manager controls access

All of this happens silently while users see a simple login screen.

Java in Cloud and Enterprise Systems

Modern Java systems run in:

  • Containers

  • Virtual machines

  • Cloud platforms

But internally, the process remains the same:

  • Bytecode runs on JVM

  • Memory is managed automatically

  • Performance is optimized by JIT compilation

Understanding this helps developers:

  • Debug performance issues

  • Fix memory leaks

  • Tune system behavior

Why Interviewers Love These Concepts

When companies ask about JVM, JRE, and JDK, they are testing:

  • Your understanding of system design

  • Your debugging capability

  • Your knowledge of performance and memory

  • Your readiness for real-world development

These are not academic questions. They reflect production-level challenges.

Common Misunderstandings Cleared

“JVM and JRE Are the Same”

No. JVM is part of JRE.

“I Need JDK to Run Java Programs”

No. You only need JRE to run programs. You need JDK to build them.

“Bytecode Is Machine Code”

No. Bytecode is platform-neutral and runs on JVM, not directly on hardware.

Learning Tip: How to Master These Concepts Practically

Try this:

  • Write a simple Java program

  • Compile it manually using javac

  • Run it using java

  • Use a tool to monitor memory usage

  • Observe garbage collection logs

This turns theory into experience.

FAQ: JVM, JRE, and JDK Explained

  1. Do I need both JDK and JRE?
    If you are developing Java programs, you need JDK. If you only run them, you need JRE.

  2. Can Java run without JVM?
    No. JVM is essential for running Java bytecode.

  3. Is JVM part of the operating system?
    No. It is a software layer installed on top of the OS.

  4. Why is Java slower than some languages?
    It used to be. Modern JVMs use JIT compilation and perform very efficiently.

  5. What is the difference between bytecode and machine code?
    Bytecode runs on JVM. Machine code runs directly on hardware.

  6. Is garbage collection optional?
    No. It is built into the JVM and runs automatically.

  7. Can multiple Java programs run on one JVM?
    Yes. JVM can manage multiple threads and applications.

  8. Does each operating system have a different JVM?
    Yes, but they all follow the same Java standards.

  9. Is JRE still installed separately today?
    In most modern setups, JDK includes everything needed.

  10. Why should beginners learn these concepts early?
    Because it helps you debug better, write efficient code, and understand how real systems work. A solid Java course at NareshIT covers these fundamentals in depth.

Final Thoughts: Understanding Java Internals Makes You a Better Developer

Many people learn how to write Java code. Fewer people learn how Java runs.

When you understand JVM, JRE, and JDK:

  • You debug faster

  • You design better systems

  • You optimize performance

  • You stand out in interviews

  • You think like an engineer, not just a coder

Java stops being a black box and becomes a tool you truly control.

And in professional software development, that understanding is what separates learners from leaders.