🚀 Java Architecture (Step-by-Step)
1. What is Java?
- Java is a high-level, object-oriented, platform-independent programming language.
- Its biggest feature = Write Once, Run Anywhere (WORA).
👉Example:
If you write a Java program on Windows, the same program can run on Linux or Mac without changes.
2. Java Program Execution Flow
Java works in these main steps:
- Write code in
.java
file (human-readable).
Example:
class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
- Compile code with
javac
(Java Compiler).
- Converts
.java
→ .class
(bytecode).
- Bytecode is platform-independent.
- Run code with
java
command.
- JVM loads
.class
file.
- JVM executes bytecode using Execution Engine + JIT.
3. Main Components of Java Architecture
Java Architecture has 3 core parts:
(A) JVM (Java Virtual Machine)
- Acts as a virtual computer inside your machine.
- Executes bytecode and converts it into machine code.
- Provides Memory Management + Security + Garbage Collection.
👉Interview Point:
- JVM is platform dependent (Windows JVM ≠Linux JVM).
- But bytecode is the same → Java is platform independent.
(B) JRE (Java Runtime Environment)
- Provides the environment to run Java applications.
- Includes:
- JVM
- Libraries (API)
- Supporting files
👉Interview Tip:
If you only need to run Java programs (not write/compile), you just need JRE.
(C) JDK (Java Development Kit)
- Complete package for developers.
- Includes:
- JRE (to run programs)
- Compiler (
javac
) to compile code
- Debugger + Development tools
👉Interview Tip:
- JDK = JRE + Development Tools
- Developers install JDK, normal users can use JRE.
4. Detailed JVM Architecture
JVM itself has sub-components:
1. ClassLoader
- Loads
.class
files into memory.
- 3 types:
- Bootstrap ClassLoader → loads core classes (
java.lang.*
).
- Extension ClassLoader → loads classes from
ext
libraries.
- Application ClassLoader → loads user-defined classes.
👉Example:
When you write System.out.println
,
java.lang.System
is loaded by Bootstrap ClassLoader.
2. Memory Areas in JVM
JVM divides memory into parts when running a program:
- Method Area: Stores class-level info (fields, methods, constants).
- Heap: Stores objects (created with
new
).
- Stack: Stores method calls, local variables.
- PC Register: Keeps track of current instruction.
- Native Method Stack: Stores non-Java methods (like C/C++).
👉Example Flow:
Student s = new Student();
s
(reference) stored in Stack.
new Student()
object stored in Heap.
3. Execution Engine
- Runs bytecode with two parts:
- Interpreter → executes bytecode line by line (slower).
- JIT Compiler (Just-In-Time) → compiles frequently used code into machine code (faster).
👉Example:
If a method is called 1000 times, JIT compiles it into native code so JVM doesn't re-interpret every time.
4. Garbage Collector (GC)
- Frees memory by removing unused objects from Heap.
- Runs automatically.
- Uses algorithms like Mark and Sweep.
👉Example:
Student s1 = new Student();
s1 = null; // old object becomes unreachable → Garbage Collector cleans it
5. Step-by-Step Flow of Program Execution
- Source Code (
.java
) → written by developer.
- Compiler (
javac
) → converts to Bytecode (.class
).
- ClassLoader → loads
.class
file into JVM.
- Bytecode Verifier → checks security (no illegal code).
- Execution Engine (Interpreter + JIT) → executes code.
- Garbage Collector → cleans unused objects.
7. Key Interview Questions
1. Why is Java platform independent?
- Because of bytecode + JVM for each OS.
2. Why is JVM platform dependent?
- Each OS has its own implementation of JVM.
3. What is the role of JIT?
- Improves performance by compiling frequently used bytecode into native machine code.
4. Difference between JVM, JRE, JDK?
- JVM → Executes bytecode.
- JRE → JVM + Libraries (runtime environment).
- JDK → JRE + Development tools (for developers).
5. What happens when you run a Java program?
- Code → Compiled into Bytecode → Loaded by ClassLoader → Verified → Executed by Execution Engine.