Skip to content
beginnerPhase 9 · Java Foundations

JDK, JRE, JVM

Understand the Java ecosystem: JDK, JRE, JVM, and how Java code executes.

30m
0 problems
Topic Progress0%

Java Development Kit (JDK)

What is JDK?

The Java Development Kit (JDK) is a software development environment used for developing Java applications. It includes everything needed to develop, compile, debug, and run Java programs.

JDK Components

JDK
├── JRE (Java Runtime Environment)
│   ├── JVM (Java Virtual Machine)
│   ├── Java Core Libraries
│   └── Supporting Files
├── Compiler (javac)
├── Debugger (jdb)
├── JAR Tool
├── Javadoc
└── Other Development Tools

Key JDK Tools

  1. javac - Java compiler that converts .java to .class files
  2. java - Launches the JVM to run Java applications
  3. jar - Creates and manages JAR (Java Archive) files
  4. javadoc - Generates API documentation from source code
  5. jdb - Java debugger for finding and fixing bugs
  6. javap - Disassembles class files
  7. jconsole - Monitoring and management console

JDK Versions

# Check your Java version
java -version
javac -version

# Example output:
# java version "17.0.2" 2022-01-18 LTS
# Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
# Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

Installing JDK

# On Windows (using Chocolatey)
choco install oraclejdk17

# On macOS (using Homebrew)
brew install openjdk@17

# On Ubuntu/Debian
sudo apt install openjdk-17-jdk

# Set JAVA_HOME (Windows)
set JAVA_HOME=C:\\Program Files\\Java\\jdk-17

# Set JAVA_HOME (Linux/Mac)
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk

JDK Directory Structure

jdk-17/
├── bin/          # Executables (javac, java, jar)
├── conf/         # Configuration files
├── include/      # C/C++ header files
├── jmods/        # Module system files
├── lib/          # Libraries and classes
└── legal/        # Legal notices

Java Runtime Environment (JRE)

What is JRE?

The Java Runtime Environment (JRE) is a software package that provides everything needed to run Java applications. It's a subset of the JDK.

JRE Components

JRE
├── JVM (Java Virtual Machine)
│   ├── Class Loader
│   ├── Bytecode Verifier
│   ├── Execution Engine
│   └── Runtime Data Areas
├── Java Class Libraries
│   ├── java.lang
│   ├── java.util
│   ├── java.io
│   ├── java.net
│   └── java.sql
└── Supporting Files

Core Libraries

// java.lang - Basic classes
String, Integer, Double, Math, System, Object

// java.util - Collections and utilities
ArrayList, HashMap, HashSet, Collections, Arrays

// java.io - Input/Output
File, InputStream, OutputStream, BufferedReader

// java.net - Networking
URL, HttpURLConnection, Socket

// java.sql - Database access
Connection, Statement, ResultSet

JRE vs JDK

Feature JRE JDK
Purpose Run Java apps Develop Java apps
Includes JVM Yes Yes
Includes Compiler No Yes
Includes Debugger No Yes
File Size Smaller Larger
Use Case End users Developers

JRE Execution Flow

.class file → Class Loader → Bytecode Verifier → Execution Engine → Machine Code
     │                              │                      │
     │                              │                      └─ JIT Compiler
     │                              │                         Interpreter
     │                              └─ Security Check
     └─ Load bytecode

JRE Memory Areas

public class MemoryExample {
    // Stored in Method Area (shared)
    static String CONSTANT = "Hello";
    
    // Stored in Heap (shared)
    static int[] sharedArray = new int[100];
    
    public static void main(String[] args) {
        // Stored in Stack (thread-specific)
        int localVar = 42;
        String message = "World";
        
        // Object stored in Heap, reference in Stack
        MemoryExample obj = new MemoryExample();
    }
}

Java Virtual Machine (JVM)

What is JVM?

The Java Virtual Machine (JVM) is the runtime engine that executes Java bytecode. It's the heart of Java's platform independence.

JVM Architecture

┌─────────────────────────────────────────────────────┐
│                      JVM                             │
├─────────────────────────────────────────────────────┤
│  ┌─────────────┐  ┌─────────────────────────────┐  │
│  │ Class Loader │  │      Runtime Data Areas      │  │
│  │   System     │  │  ┌────────────┐  ┌────────┐ │  │
│  │             │  │  │Method Area │  │  Heap  │ │  │
│  │  Bootstrap  │  │  │            │  │        │ │  │
│  │  Extension  │  │  │            │  │        │ │  │
│  │  Application│  │  └────────────┘  └────────┘ │  │
│  └─────────────┘  │  ┌────────────┐  ┌────────┐ │  │
│                   │  │Stack(PER   │  │ PC     │ │  │
│  ┌─────────────┐  │  │  THREAD)   │  │Registers│ │  │
│  │ Execution   │  │  └────────────┘  └────────┘ │  │
│  │   Engine    │  └─────────────────────────────┘  │
│  │             │                                    │
│  │ Interpreter │  ┌─────────────────────────────┐  │
│  │ JIT Compiler│  │    Native Interface         │  │
│  │ GC          │  │    (JNI - Java Native       │  │
│  └─────────────┘  │     Interface)              │  │
│                   └─────────────────────────────┘  │
└─────────────────────────────────────────────────────┘

Memory Model

public class JVM {
    // Method Area: Stores class metadata, static variables, constants
    static int classVariable = 10;
    
    // Heap: Stores objects and arrays
    int[] array = new int[100];
    String name = new String("Amazon");
    
    public void method() {
        // Stack: Stores local variables and method calls
        int localVar = 42;  // Primitive on stack
        Object obj = new Object();  // Reference on stack, object on heap
        
        // PC Register: Stores address of current instruction
        // Each thread has its own PC register
    }
}

JIT Compilation

The JVM uses Just-In-Time (JIT) compilation for performance:

  1. Interpreter: Translates bytecode line by line (slow)
  2. JIT Compiler: Compiles hot methods to native code (fast)
  3. Method Cache: Stores compiled native code for reuse
// HotSpot JVM optimization
public class JITExample {
    // This method will be JIT compiled if called frequently
    public int calculate(int n) {
        return n * n + n / 2;
    }
    
    // The JIT compiler recognizes patterns and optimizes
    public long sum(int[] arr) {
        long sum = 0;
        for (int num : arr) {
            sum += num;  // Loop optimization
        }
        return sum;
    }
}

Garbage Collection

public class GCExample {
    public static void main(String[] args) {
        // Object eligible for GC when no references exist
        
        Object obj = new Object();  // Created
        obj = null;  // Now eligible for GC
        
        // Force garbage collection (not guaranteed)
        System.gc();
        
        // Finalize method (deprecated in Java 9+)
        // Called before object is reclaimed
    }
}

How They Relate

JDK vs JRE vs JVM Relationship

┌─────────────────────────────────────────┐
│                 JDK                      │
│  ┌───────────────────────────────────┐  │
│  │               JRE                  │  │
│  │  ┌─────────────────────────────┐  │  │
│  │  │             JVM              │  │  │
│  │  │  (Execute bytecode)         │  │  │
│  │  └─────────────────────────────┘  │  │
│  │  + Core Libraries                  │  │
│  │  + Runtime Support                 │  │
│  └───────────────────────────────────┘  │
│  + Compiler (javac)                      │
│  + Debugger (jdb)                        │
│  + JAR, Javadoc tools                    │
└─────────────────────────────────────────┘

Development vs Runtime

// DEVELOPMENT TIME (need JDK)
// 1. Write source code
public class App {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

// 2. Compile with javac (JDK tool)
// javac App.java → creates App.class

// RUNTIME (need JRE or JDK)
// 3. Run with java command
// java App → JVM executes bytecode

Scenario Matrix

Scenario JDK Needed JRE Needed JVM Needed
Write Java code Yes No No
Compile Java code Yes No No
Run Java application No Yes Yes
Debug Java application Yes No No
Create JAR files Yes No No
Deploy to production No Yes Yes

Common Misconceptions

Myth: You need JDK to run Java programs
Reality: You only need JRE (or JRE + JVM) to run compiled .class files

Myth: JVM is the same for all platforms
Reality: Each platform has its own JVM implementation (HotSpot, OpenJ9, GraalVM)

// Demonstrating JVM-specific behavior
public class JVMDemo {
    public static void main(String[] args) {
        // Get JVM information
        String vmName = System.getProperty("java.vm.name");
        String vmVersion = System.getProperty("java.vm.version");
        String os = System.getProperty("os.name");
        
        System.out.println("JVM: " + vmName);
        System.out.println("Version: " + vmVersion);
        System.out.println("OS: " + os);
        
        // Memory information
        Runtime runtime = Runtime.getRuntime();
        long maxMemory = runtime.maxMemory();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        
        System.out.printf("Max Memory: %d MB%n", maxMemory / 1024 / 1024);
        System.out.printf("Total Memory: %d MB%n", totalMemory / 1024 / 1024);
        System.out.printf("Free Memory: %d MB%n", freeMemory / 1024 / 1024);
    }
}

Distribution Options

  • Oracle JDK: Commercial, paid for production use
  • OpenJDK: Open-source, free
  • Amazon Corretto: Amazon's OpenJDK distribution
  • Azul Zulu: High-performance OpenJDK
  • GraalVM: Multi-language VM with polyglot support

Practice Problems

0/3solved
JDK, JRE, JVM Implementation

Implement JDK, JRE, JVM in Java. Include proper error handling and follow Java conventions.

Solution
// Java implementation:
// 1. Proper class structure
// 2. Error handling
// 3. JavaDoc comments
// 4. Unit tests
JDK, JRE, JVM Time Complexity

Analyze the time and space complexity of JDK, JRE, JVM operations. Optimize for common use cases.

Solution
// Complexity analysis:
// - Time: depends on implementation
// - Space: consider auxiliary space
// - Trade-offs between time and space
JDK, JRE, JVM Java Best Practices

Apply Java best practices when using JDK, JRE, JVM. Consider immutability, thread safety, and clean code.

Solution
// Best practices:
// 1. Use immutable objects where possible
// 2. Thread-safe implementations
// 3. Proper exception handling
// 4. Resource management (try-with-resources)
// 5. JavaDoc documentation

Quiz

1. Which component is responsible for executing Java bytecode?

Question 1 options

2. What is stored in the JVM's Method Area?

Question 2 options

3. What is the difference between JDK and JRE?

Question 3 options

4. What is the primary purpose of JDK, JRE, JVM?

Question 4 options

Flashcards

Question

What does JDK stand for and what does it include?

Answer

Java Development Kit. Includes JRE + compiler (javac) + debugger (jdb) + development tools.

Question

What is stored in JVM's Heap memory?

Answer

All object instances and arrays are stored in the Heap. It's shared among all threads.

Question

What is JIT compilation in JVM?

Answer

Just-In-Time compilation converts frequently executed bytecode to native machine code for better performance.

Question

What is JDK, JRE, JVM?

Answer

JDK, JRE, JVM is a key concept in Java programming.

Question

When to use JDK, JRE, JVM?

Answer

Use JDK, JRE, JVM when building production systems that require reliability, scalability, and maintainability.

Revision Notes

Key Takeaways

  • 1.JDK is for development, JRE is for running, JVM is the execution engine
  • 2.JVM's Heap stores all objects, Stack stores local variables per thread
  • 3.Method Area stores class metadata shared across threads
  • 4.JIT compilation improves performance by converting hot bytecode to native code
  • 5.Garbage Collection automatically manages heap memory

Interview Tips

  • Be able to explain the difference between JDK, JRE, and JVM
  • Understand JVM memory model for explaining space complexity
  • Know that each thread has its own Stack but shares Heap
  • Explain why Java is platform-independent (bytecode + JVM)

Cheat Sheet

JDK vs JRE vs JVM Cheat Sheet

JDK = JRE + Dev Tools

  • Compiler (javac)
  • Debugger (jdb)
  • JAR tool
  • Javadoc

JRE = JVM + Libraries

  • Core Java libraries (java.lang, java.util, etc.)
  • Runtime support

JVM = Execution Engine

  • Class Loader
  • Bytecode Verifier
  • Execution Engine (Interpreter + JIT)
  • Garbage Collector

Memory Areas:

  • Method Area: Class metadata, statics
  • Heap: Objects, arrays
  • Stack: Local variables, method calls
  • PC Register: Current instruction address