Java Architecture - Complete Interview Mastery

From Source Code to Execution: Every Detail You Need to Crack Any Java Interview

JVM Architecture Overview

High-level JVM Architecture


1. Java Platform Independence: The Core Philosophy

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)

  1. Write: Hello.java (human-readable)
  2. Compile: javac Hello.java β†’ Hello.class (bytecode)
  3. Load: JVM starts β†’ java Hello β†’ ClassLoader loads .class
  4. Link: Verify β†’ Prepare β†’ Resolve
  5. Initialize: Static blocks/variables
  6. 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 Architecture

Detailed JVM Internal Structure

1. Class Loader Subsystem HOT

Bootstrap (null) β†’ Extension β†’ Application
TargetQ: Who loads the String class?
A: Bootstrap ClassLoader (it's in rt.jar)

2. Runtime Data Areas (Memory Layout) MOST ASKED

CodeExample:
String name = new String("Alice");
WarningTricky: String s = "hello"; β†’ goes to String Pool (in Heap, not Method Area post-Java 7)

3. Execution Engine – The Real Executor

βš™οΈ How JIT Works (Simplified):
  1. Interpreter starts running your code.
  2. JVM keeps track of frequently used (β€œhot”) methods.
  3. Hot methods are compiled by C1 for quick speed-up.
  4. If they’re used a lot, C2 recompiles them with deeper optimizations (like removing unnecessary checks or locks).

4. Garbage Collection (GC) ADVANCED

🧠 Types of Garbage Collectors

🎯 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

  1. Loading: Read .class file β†’ create Class object
  2. 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
  3. 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

TrophyGolden Answer Structure:
1. Define β†’ 2. Explain with example β†’ 3. Draw diagram β†’ 4. Mention edge case