←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

How Java Works: JVM Concepts, JDK vs JRE & Java Platform Overview

Study material and explanations for Unit 1

How Java Works: JVM Concepts, JDK vs JRE & Java Platform Overview

1. How Java Works (Two-Stage Execution Model)

In traditional languages like C or C++, the compiler translates source code directly into native machine code (.exe), which only runs on the specific operating system and processor where it was compiled.

Java solves this problem by using a two-stage process combining Compilation and Interpretation:

Figure 1.3: How Java Achieves Platform Independence (WORA)
HelloWorld.javaJava Source Code
→
javac CompilerChecks syntax & compiles
→
HelloWorld.classUniversal Bytecode
→
OS-Specific JVMWindows / Linux / Mac JVM
→
Machine CodeExecutes on CPU

Step 1: Compilation (javac)

  • You write your program in a plain text file with a .java extension (e.g., Student.java).
  • The Java Compiler (javac) checks your code for syntax errors and compiles it into an intermediate, platform-independent representation called Bytecode, saved with a .class extension (Student.class).
  • What is Bytecode? Bytecode is a highly optimized set of instructions designed to be executed by the Java Virtual Machine (JVM). It is not machine code for any physical CPU—it is machine code for a virtual computer (the JVM).

Step 2: Execution / Interpretation (java / JVM)

  • When you run the program using the java Student command, the Java Virtual Machine (JVM) installed on that computer loads the Student.class bytecode.
  • Using its Interpreter and JIT (Just-In-Time) Compiler, the JVM translates the universal Bytecode into native machine instructions (0s and 1s) specific to the host operating system (Windows, Linux, or macOS) and executes it.

Important Viva Fact: Java is platform-independent, but the JVM is platform-dependent! (Windows has a different JVM installer than Linux or macOS, though all of them understand the exact same .class Bytecode).


2. Java Virtual Machine (JVM) Architecture & Concepts

The JVM (Java Virtual Machine) is an abstract computing machine (a software-based virtual computer) that provides the runtime environment in which Java bytecode is executed.

When asked to explain JVM Concepts in DAVV exams, explain its 3 main subsystems:

Figure 1.4: Internal Architecture of the Java Virtual Machine (JVM)
1Class Loader Subsystem
Loads, links, and initializes .class files into RAM (Bootstrap, Extension, Application Loaders)
2Runtime Data Areas (Memory)
Method Area (class data), Heap (objects), Java Stacks (local vars/method calls), PC Register, Native Stack
3Execution Engine
Interpreter + JIT (Just-In-Time) Compiler + Garbage Collector (GC)

1. Class Loader Subsystem

Whenever you run a Java program, the Class Loader is responsible for finding and loading the required .class files from the disk into main memory (RAM). It performs three activities:

  • Loading: Reads the .class file and stores its binary data in the Method Area.
  • Linking (Verification, Preparation, Resolution): The Bytecode Verifier checks whether the generated bytecode is valid and does not violate security rules (e.g., no illegal memory access).
  • Initialization: Assigns initial values to static variables and executes static blocks.

2. JVM Runtime Data Areas (Memory Areas)

The JVM divides the memory it gets from the OS into 5 regions:

  1. Method Area: Stores class-level data such as class names, method code, static variables, and constant pool. (Shared across all threads).
  2. Heap Area: The runtime data area where all Objects (new) and Arrays are allocated memory. (Shared across all threads and cleaned by the Garbage Collector).
  3. Java Stack Area: Every thread gets its own private Stack. Each time a method is called, a Stack Frame is pushed onto the stack to store local variables and partial results.
  4. PC (Program Counter) Register: Holds the memory address of the currently executing JVM instruction for each thread.
  5. Native Method Stack: Holds instruction details for non-Java (C/C++) native methods used by the JVM.

3. Execution Engine

The Execution Engine actually executes the bytecode loaded into memory using three components:

  • Interpreter: Reads and executes bytecode instructions line by line. It starts quickly, but if a method or loop is called 10,000 times, interpreting it every single time is slow.
  • JIT (Just-In-Time) Compiler: Overcomes the slowness of the interpreter! The JIT compiler identifies “hot spots” (methods or loops executed repeatedly), compiles that entire block of bytecode into native machine code once, and caches it so future calls run at native CPU speed.
  • Garbage Collector (GC): Automatically runs in the background to scan the Heap memory, identify unreferenced (dead) objects, and free up their memory.

3.Java Platform Overview: JDK, JRE, and JVM

What is the “Java Platform”? Unlike normal platforms that consist of hardware + OS (like Intel + Windows), the Java Platform is a software-only platform that runs on top of other hardware-based platforms.

It consists of three nested layers: JVM, JRE, and JDK.

Figure 1.5: Relationship Between JDK, JRE, and JVM
JDK (Java Development Kit) — Full kit for Developers (JRE + javac, javadoc, jar, jdb)
JRE (Java Runtime Environment)
For End-Users to RUN Java apps (JVM + Core Libraries java.lang, java.util, etc.)
JVM (Java Virtual Machine)
The core engine that executes Bytecode (.class) + JIT Compiler

Formula: JDK = JRE + Development Tools (javac, javadoc, jdb, jar)

Formula: JRE = JVM + Core Class Libraries (rt.jar / java.base)

Comparison: JVM vs JRE vs JDK

Feature JVM (Java Virtual Machine) JRE (Java Runtime Environment) JDK (Java Development Kit)
Full Form Java Virtual Machine Java Runtime Environment Java Development Kit
Primary Purpose Executes .class bytecode and manages memory/GC. Provides the environment to run (execute) compiled Java programs. Provides tools to develop, compile, debug, and run Java programs.
Who Needs It? Internal engine inside JRE. End Users / Clients who only want to run a Java application. Java Developers / Students who write .java code.
Components Class Loader + Memory Areas + Execution Engine (JIT & GC). JVM + Standard Class Libraries (java.lang, java.io, java.util). JRE + Compiler (javac), Debugger (jdb), Archiver (jar), Doc generator (javadoc).

4. Editions of the Java Platform

To serve different types of devices and applications, Oracle provides four editions of the Java Platform:

  1. Java SE (Standard Edition / Core Java):
    • The foundation of Java. Provides core APIs (java.lang, java.io, java.net, java.sql, java.awt) for desktop applications, command-line tools, and applets. (Our BCA syllabus focuses on Java SE!)
  2. Java EE / Jakarta EE (Enterprise Edition / Advanced Java):
    • Built on top of Java SE for large-scale, multi-tiered, secure enterprise and web applications (Servlets, JSP, EJB, Web Services used by banks and e-commerce).
  3. Java ME (Micro Edition):
    • A lightweight subset of Java SE designed for small, resource-constrained embedded devices like smart cards, IoT sensors, TV set-top boxes, and old feature phones.
  4. JavaFX:
    • A modern platform for creating rich internet and desktop GUI applications using hardware-accelerated graphics.

💡 Exam Preparation Tips for DAVV BCA

  • Always write the two equations: JDK = JRE + Dev Tools and JRE = JVM + Libraries. Examiners award instant marks for this clarity!
  • If asked “Why is Java both compiled and interpreted?”, explain the role of javac (Compiler → Bytecode) and JVM (Interpreter + JIT Compiler → Machine Code).