Java Architecture - Complete Interview Mastery
From Source Code to Execution: Every Detail You Need to Crack Any Java Interview
High-level JVM Architecture
1. Java Platform Independence: The Core Philosophy
- Write Once, Run Anywhere (WORA) β Java's golden promise.
- Achieved via bytecode + platform-specific JVM.
- Java source (
.java) β compiled to bytecode (.class) β same
bytecode runs on any OS with JVM.
TargetInterview Question:
Q: "Is Java 100% platform independent?"
A: Only the bytecode is platform independent. JVM is platform
dependent β each OS has its own JVM implementation.
WarningTricky Point: Native methods (JNI) break platform
independence. If your code uses JNI, it becomes OS-specific.
2. Java Program Execution Flow (Step-by-Step)
- Write:
Hello.java (human-readable)
- Compile:
javac Hello.java β Hello.class (bytecode)
- Load: JVM starts β
java Hello β ClassLoader loads .class
- Link: Verify β Prepare β Resolve
- Initialize: Static blocks/variables
- Execute: Execution Engine runs bytecode
3. JDK vs JRE vs JVM β The Holy Trinity
| Component |
Purpose |
Contains |
Needed For |
| JVM |
Executes bytecode |
Execution Engine, Memory, GC |
Running Java apps |
| JRE |
Runtime environment |
JVM + Core Libraries + Files |
Running (not developing) |
| JDK |
Development kit |
JRE + javac, javadoc, debugger |
Developing Java apps |
LightbulbPro Tip:
JDK = JRE + Development Tools
JRE = JVM + Core Libraries
GearAdvanced: OpenJDK vs Oracle JDK? Both are same now (post
Java 11). Oracle JDK has commercial support.
4. Deep Dive: JVM Internal Architecture
Detailed JVM Internal Structure
1. Class Loader Subsystem HOT
- Responsible for Loading, Linking, Initialization
- Follows Delegation Model (Parent-first)
- 3 Built-in Class Loaders:
Bootstrap (null) β Extension β Application
- Bootstrap ClassLoader:
Loads core Java classes (like
java.lang, java.util) from rt.jar.
Written in native code (C/C++). It has no parent (parent = null).
- Extension ClassLoader:
Loads classes from the
jre/lib/ext folder (Java extensions).
- Application ClassLoader:
Loads user-defined classes from the
CLASSPATH (your projectβs classes and JARs).
TargetQ: Who loads the String class?
A: Bootstrap ClassLoader (it's in rt.jar)
2. Runtime Data Areas (Memory Layout) MOST ASKED
- Method Area: Class metadata, static variables, method bytecode, constant pool
- Heap: All objects, instance variables, arrays
- Stack: One per thread β stores method frames (local vars, operand stack, return
address)
- PC Register: Current executing instruction (per thread)
- Native Method Stack: For native (C/C++) methods
CodeExample:
String name = new String("Alice");
name β Stack (reference)
"Alice" β String Pool (Heap)
new String(...) β new object in Heap
WarningTricky: String s = "hello"; β goes to
String Pool (in Heap, not Method Area post-Java 7)
3. Execution Engine β The Real Executor
- Interpreter: Executes code line by line β easy to start, but slower.
- JIT (Just-In-Time) Compiler: Speeds things up by converting frequently used code into
native machine code.
- Types of JIT:
- C1 (Client JIT): Quick compilation, basic optimizations β used for desktop
apps.
- C2 (Server JIT): Slower to compile, but much faster at runtime β used for
long-running server apps.
βοΈ How JIT Works (Simplified):
- Interpreter starts running your code.
- JVM keeps track of frequently used (βhotβ) methods.
- Hot methods are compiled by C1 for quick speed-up.
- If theyβre used a lot, C2 recompiles them with deeper optimizations
(like removing unnecessary checks or locks).
4. Garbage Collection (GC) ADVANCED
- Garbage Collection = Automatic memory cleanup in Java.
- Removes objects that are no longer used by any part of the program.
- Process: Mark unreachable objects β Sweep and free memory.
- Young Generation:
New objects are created here (Eden + Survivor S0/S1).
β Cleaned by Minor GC.
- Old Generation:
Long-lived objects are moved here.
β Cleaned by Major (Full) GC.
- Metaspace: Stores class metadata (replaced
PermGen in Java 8).
π§ Types of Garbage Collectors
- Serial GC: Simple, single-threaded β best for small applications.
- Parallel GC: Uses multiple threads β faster for multi-core CPUs.
- CMS (Concurrent Mark Sweep): Reduces pause time β works alongside the app.
- G1 (Garbage First): Modern, region-based β balances speed and low pauses (default in
Java 9+).
π― Q: When does an object become eligible for GC?
A: When no live thread or variable can access it (no active references).
5. Class Loading & Linking Process
- Loading: Read
.class file β create Class object
- Linking:
- Verify: Check bytecode safety (no stack overflow, valid ops)
- Prepare: Allocate memory for static variables, set defaults
- Resolve: Replace symbolic references with direct pointers
- Initialization: Execute
static blocks, assign static vars
static int x = 10;
static { System.out.println("Static block"); }
β Executed during
Initialization phase
6. 25 Must-Know Interview Questions & Answers (Simplified)
1. Why is Java platform-independent but JVM platform-dependent?
Java code is compiled into bytecode, which runs on any JVM.
But each JVM is written in native code (C/C++) and made separately for each OS (Windows, Linux, etc.).
2. What does a ClassLoader do?
Loads .class files into JVM memory when needed.
It follows a parent-first rule, provides security, and allows dynamic loading of classes.
3. Can you make your own ClassLoader?
Yes! You can extend ClassLoader and override findClass().
Used in frameworks, servers, and plugin-based systems.
4. Difference between Stack and Heap memory?
| Stack |
Heap |
| Used by each thread separately |
Shared by all threads |
| Stores local variables and references |
Stores objects |
| Fast and automatically cleaned |
Managed by Garbage Collector |
| Follows LIFO order |
Slower but larger memory area |
5. What is JIT Compiler and how does it help?
JIT (Just-In-Time) compiler converts bytecode into machine code during runtime.
It speeds up performance by avoiding repeated interpretation.
6. When does Full GC happen?
When the Old Generation memory is full or when System.gc() is suggested.
(Note: Itβs only a suggestion, not a command.)
7. What is Metaspace?
It replaced PermGen in Java 8.
Stores class-related metadata and uses native memory (not heap).
Expands automatically when needed.
8. Can finalize() stop Garbage Collection?
Not really. It runs only once before GC.
If an object becomes reachable again inside finalize(), it gets a temporary βsecond lifeβ, but
only once.
9. What is the String Pool?
A special area in the Heap where Java stores string literals to save memory.
Identical strings share the same memory location.
10. Why is String immutable in Java?
To ensure security, thread-safety, and efficient caching.
It also helps in safe class loading and prevents accidental changes.
Bonus: Pro Tips for Interview
- Draw JVM diagram on paper β interviewers love it!
- Explain
new Student() β where reference and object go
- Know difference:
StackOverflowError vs OutOfMemoryError
- Mention Java 8+ changes: Metaspace, String deduplication
- Be ready for
System.gc() vs actual GC trigger
TrophyGolden Answer Structure:
1. Define β 2. Explain with example β 3. Draw diagram β
4. Mention edge case