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:
Step 1: Compilation (javac)
- You write your program in a plain text file with a
.javaextension (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.classextension (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 Studentcommand, the Java Virtual Machine (JVM) installed on that computer loads theStudent.classbytecode. - Using its Interpreter and JIT (Just-In-Time) Compiler, the JVM translates the universal Bytecode into native machine instructions (
0s and1s) 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
.classBytecode).
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:
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
.classfile 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
staticvariables and executesstaticblocks.
2. JVM Runtime Data Areas (Memory Areas)
The JVM divides the memory it gets from the OS into 5 regions:
- Method Area: Stores class-level data such as class names, method code, static variables, and constant pool. (Shared across all threads).
- 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). - 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.
- PC (Program Counter) Register: Holds the memory address of the currently executing JVM instruction for each thread.
- 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.
For End-Users to RUN Java apps (JVM + Core Libraries java.lang, java.util, etc.)
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:
- 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!)
- The foundation of Java. Provides core APIs (
- 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).
- 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.
- 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) andJVM(Interpreter + JIT Compiler → Machine Code).