Java 2
Java 2
Web Applications.
Enterprise Systems.
Mobile Apps.
Backend Systems.
API’s.
Java Architecture 1
Feature Description
Java Architecture :
Java has very well though-out architecture where there are multiple
components that work together to execute java programs, manage and
assign memory and maintain dependencies along with making them
platform-independent.
Java Architecture 2
Most commonly used is OpenJDK which is free by Oracle.corp.
🧩 Components of JDK:
Components Functions
Any computer that has a JRE installed can run a java program. This is what
makes Java cross-platform as the byte code can be run on any machine (Win,
Mac, linux) as long as it has a JRE installed.
🧩 Components of JRE:
Component Description
JVM (Java Virtual Machine) Core engine that runs Java bytecode.
Java Architecture 3
JVM is the Heart of Java programming language.
JVM interprets/compiles the byte code into machine language, linking it with
all dependencies, assign and manage memory of the program and ultimately
run the source code on the machine.
There are several components that reside within the JVM that make this
happen.
🧩 Components of JVM:
Class loader.
Execution Engines.
Garbage Collectors.
Structure:
JDK
├── JRE
│ ├── JVM
│ │ ├── Class Loader
│ │ ├── Execution Engine (Interpreter + JIT)
│ │ ├── Memory Areas
│ │ └── GC
│ └── Class Libraries
├── Compiler (javac)
└── Other Tools (debugger, javadoc, etc.)
Java Architecture 4
Step 1 - Compilation :
Each individual .java source file is picked up by the JDK (Java Development
Kit).
The javac compiler, a part of the JDK, compiles the source code into
bytecode.
The compiled bytecode can now be executed on any system that has a Java
Virtual Machine (JVM).
# Terminal Command:
"dev_vedant@Vedants-MacBook-Pro Java_Basics %" javac filename.java
🧠 Why Bytecode?
Bytecode serves as an intermediate representation. It allows
Java to be a “write once, run anywhere” language - the JVM
on each platform takes care of converting bytecode to native
machine code.
Java Architecture 5
Step 2 - Execution via JRE :
java launcher:
The java launcher is an executor that invokes the JVM and passes it the
.class file for conversion into machine code.
We don’t need to give .class extension as it auto-picks the bytecode file and
loads it into a class loader.
Java Architecture 6
It forms the first stage of JVM execution process, after the java execution
command is invoked.
The Class Loader reads the .class file (compiled bytecode) from the file
system, network, or JAR.
It loads the binary data (byte array) of that class into JVM memory,
specifically into the Method Area.
Modifier info
Annotations, etc.
Java Architecture 7
JVM has 3 main Class Loaders, each for specific tasks instead of one to make
JVM more efficient and performant.
The Bootstrap Loader is responsible for loading only the required java.base class
files.
These base classes are from the Java Standard Library and are loaded based
on the requirements of the program.
java.util.*
java.io.*
java.net.*
Java Architecture 8
Aspect Details
Platform classes reside in the $JAVA_HOME/lib/modules file, which contains all the
platform modules in Java 9 and later.
java.sql
java.xml
jdk.crypto.ec
etc.
It is implemented by jdk.internal.loader.ClassLoaders$PlatformClassLoader .
Aspect Details
Java Architecture 9
Aspect Details
The Application ClassLoader is the default class loader that loads your
application’s classes including the classes you write as well as any external
dependencies (such as third-party libraries or JAR files) you add to your
project.
This includes your project’s compiled output directories, external libraries (JAR
files), and modules that are not part of the Java platform.
JAR files
Hierarchy:
Java Architecture 10
It follows the parent delegation model, meaning if it cannot find a class, it
delegates the loading request upwards.
The delegation model is a hierarchical approach where class loading requests are
delegated upward in the class loader hierarchy before being handled locally.
1. When a class loader is asked to load a class, it does not load it immediately.
3. If the parent is able to find and load the class, that class is returned and used.
4. If the parent fails to find the class, then the current class loader attempts to
load the class itself.
Example in Practice:
It will first ask the Platform ClassLoader, which in turn asks the Bootstrap
ClassLoader.
If none of the parents can load the class, only then does the Application
ClassLoader try to load it from the classpath.
✅ Security: Each class loader is responsible for loading classes from trusted
locations. Delegating ensures core Java classes are always loaded by trusted
loaders (like Bootstrap), reducing risks.
Java Architecture 11
✅ Separation of Concerns: Different loaders are designed to handle specific
types of classes (e.g., core Java classes, platform APIs, application code),
improving modularity and performance.
always loaded by the Bootstrap loader, no matter who asked for it.
Application ClassLoader
↓
Platform ClassLoader
↓
Bootstrap ClassLoader
Java Architecture 12
Linking:
Linking is the second step in class loading where it prepares the class files (byte
code) by verifying, organizing, assigning memory and resolving references.
There are 3 sub-steps in Linking:
Verification:
Purpose: So that faulty or malicious code doesn’t crash or hijack the JVM.
Java Architecture 13
Preparation:
Initializes these variables with default values ( 0 , false , 0.0, etc. for primitive
types, null for reference types).
Resolution:
Is Final step of linking where symbolic references are resolved and classes
are loaded from the class locations.
Bytecode doesn’t store the actual classes, it just contains their locations like
for;
The linker looks up to the Class loader to find which class file contains this
class and loads it into the method area for the JVM to use during
interpretation.
Initialization:
The final step in the class lifecycle after linking.
Ensures the class is fully prepared before any main() method or object
constructors run.
Java Architecture 14
Static variables and static blocks are executed exactly once per class, in the
order they appear in the source code.
If there are no static variables or blocks, the JVM still marks the class as
initialized to allow further processing.
Example:
class Test {
static int x = initializeX(); // 1st: initializeX() method runs first
static int y = 20; // 2nd: y is assigned 20
static {
System.out.println("Inside static block"); // 3rd: static block executes last
}
Initializing x
Inside static block
Now the Classes are ready to be processed and they are now loaded into the
Memory Area.
Java Architecture 15
During runtime, the JVM borrows memory from the OS and allocates it to
different memory areas. Each area is responsible for storing a specific kind of data
— from class metadata to method stacks and native language stacks.
Java Architecture 16
Method Meta data.
Static Variables.
Superclass names,
Implemented Interface.
// Example :
public class Car extends Vehicle implements Serializable(){
int wheels = 4; - // instance variable
static int CarsCreated = 0; - // static variable
}
// JVM stores:
/*
* Car -> class name
* Vehicle -> superclass
* Serializable -> Interface.
*/
Method Name.
Parameter Types.
Access Modifiers.
// Example:
public void startEngine(String keyType) {....}
Java Architecture 17
public static int getCarCount() { return carsCreated; }
// JVM stores:
/*
* Method name -> startEngine and getCarCount.
* Return Type -> void and int respectively.
* Parameter type -> String for keyType.
* Access Modifiers -> public.
*/
3. Static Variables:
// Example:
public class Test(){
static int count = 0;
}
Each class has its own runtime constant pool in method area. It stores:
String literals.
Final constants.
Java Architecture 18
// Vedant set in constant pool.
// 20 inlined as constant value.
Heap Area:
Heap Area is the runtime data area from which memory for all objects and arrays
is allocated. This is shared by all threads.
Heap Structure is divided into two parts:
Young Generation (Young Gen) :
These objects are more stable that Young Generations as they persist for
longer times.
Requires Major GC cycles that are expensive in terms of time and resources,
but not that frequent.
Most Objects are volatile, unstable and die quickly because they are short
lived ( temp variables, etc).
Frequent Minor GC cycles for Eden and Survivors. Not that resource
intensive to run Minor GC’s.
Java Architecture 19
Old Generation (Stable) :
Only Long-Lived objects make it here, those which survive Young Gen.
Infrequent Major GC cleans happen to remove stale Instances but not that
frequently.
A frame contains:
When the method finishes execution, its frame is popped off the stack.
The Frame is pooped off when the method completes execution.
Work on LIFO structure. Meaning the latest frame is resolved first then
popped off.
Non Heap: only stores references and not the complete arrays or objects.
The PC Register holds the address of the next executing instruction (next
line).
Java Architecture 20
For non-native methods (written in Java), the PC register points to the JVM
bytecode instructions currently being executed.
How it works:
Java Architecture 21
It reads the bytecode loaded into the Method Area and interprets or
compiles it into machine code that the CPU can run.
1. Interpreter:
Compiles these hot spots into native machine code for faster
execution.
Other features:
Execution context:
Uses memory areas like the PC Register, JVM Stack, Heap, and Method
Area during execution.
Java Architecture 22