Java Architecture
Java Architecture
JAVA ARCHITECTURE
1. Java Architecture
JDK :
It is a software development environment which helps to develop softwares.
It includes predefined classes.
JRE :
Provides the libraries, JVM and other components to run Java applications.
Does not include development tools.
JVM :
Core part of Java Architecture that runs Java Bytecode.
Abstract machine that provides runtime environment.
JVM is platform dependent(Separate implementation for each OS).
NOTE : Java Language is platform independent but JVM is platform dependent.
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 1/16
5/25/25, 1:04 PM Java Architecture - HackMD
A. Class Loader
B. Runtime Data Area
C. Execution Engine
A. Class Loader
Demo.java file
|
Demo.class (Converted using javac)
|
Class Loader
When we try to use this .class file in our program, the class loader loads it into
the main memory.
The first class to be loaded into memory is usually the class that contains the
main method.
There are three phases in the class loading process : Loading, Linking and
Initialization.
Loading :
Loading involves taking the binary representation (bytecode) of a class or
interface with a particular name, and generate the original class and interface
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 3/16
5/25/25, 1:04 PM Java Architecture - HackMD
Sub class of the Bootstrap Class Loader and Super Class of the Application
Class Loader.
Loads the extensions of standard java libraries which are present in the
$JAVA_HOME/jre/lib/ext directory.
Application Class Loader :
This is the final class loader and sub class of Extension class loader.
It loads the files present on the classpath.
By default, the classpath is set to the current directory of the application.
The classpath can be also modified by adding the -classpath or -cp in
command line options.
JVM uses the ClassLoader.loadClass() method for loading the class into memory. It
tries to load the class based on a fully qualified name.
Linking :
After class loaded into memory, It undergoes the Linking process.
Linking a class or interface involves combining the different elements and
dependencies of the program together.
Linking includes the following steps :
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 4/16
5/25/25, 1:04 PM Java Architecture - HackMD
Verification :
This phase checks the structural correctness of the .class file by checking it
against a set of constraints or rules. If verification fails for some reason, we a
VerifyException.
e.g. : If the code has been built using java 11, but is being run on a system that has
java 8 installed. The verification phase will fail.
Preparation :
In this phase, the JVM allocates memory for the static fields of a class or interface,
and initialize them with a default values.
Resolution :
In this phase, symbolic references are replaced with the direct references present
in the runtime constant pool.
e.g. : if you have references to other classes or constant variables present in the
other classes, they are resolved in this phase and replaced with their actual
references.
Initialization :
Initialization involves executing the initialization method of the class or
interface (known as <clinit>).
This can include calling the class’s constructor, executing the static block, and
assigning values to all the static variables. This is the final stage of class
loading.
NOTE : JVM is multi-threaded.
Runtime Data Area :
There are five components inside the runtime data area:
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 5/16
5/25/25, 1:04 PM Java Architecture - HackMD
Method Area :
All the class level data such as the run-time constant pool, field, and method
data, and the code for methods and constructors are stored here.
If the memory available in the method area is not sufficient for the program
startup, the JVM throws an OutOfMemoryError.
The method area is created on the virtual machine start-up, and there is only
one method area per JVM.
Heap Area :
All the objects and their corresponding instance variables are stored here.
This is the run-time data area from which memory for all class instances and
arrays is allocated.
The heap is created on the virtual machine start-up, and there is only one
heap area per JVM.
NOTE: Since the method and heap areas share the same memory for multiple
threads, the data stored here is not thread safe.
Stack Area :
Whenever a new thread is created in the JVM, a separate runtime stack is also
created at the same time.
All local variables, method calls, and partial results are stored in the stack
area.
If the processing being done in a thread requires a larger stack size than
what’s available, the JVM throws a StackOverflowError.
For every method call, one entry is made in the stack memory which is called the
Stack Frame. When the method call is complete, the Stack Frame is destroyed.
The Stack Frame is divided into three sub-parts :
I . Local variables :
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 6/16
5/25/25, 1:04 PM Java Architecture - HackMD
Each frame contains an array of variables known as its local variables. All local
variables and their values are stored here. The length of this array is determined at
compile-time.
II. Operand Stack :
Each Frame contains a last-in-first-out (LIFO) stcak known as its operand stack.
This acts as a runtime workspace to perform any intermediate operations. The
maximum depth of this stack is determined at compile-time.
III. Frame Data :
All symbols corresponding to the method are stored here. This also stored the
catch block information in case of exceptions.
Program Counter (PC) Resisters :
The JVM supports multiple threads at same time.
Each thread has it’s own PC register to hold the address of the currently
executing JVM instruction.
Once the instruction is executed, the PC register is updated with the next
instruction.
Native Method Stack :
The JVM contains stacks that support native methods.
This methods are written in a language other than the Java, such as C and
C++.
For every new thread , a separate native method stack is also allocated.
Execution Engine :
Once the bytecode has been loaded into the main memory, and details are
available in the runtime data area, the next step is to run the program. The
execution engine handles this by executing the code present in each class.
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 7/16
5/25/25, 1:04 PM Java Architecture - HackMD
However, before executing the program, the bytecode needs to be converted into
machine language instructions. The JVM can use an interpreter or a JIT compiler
for execution engine.
Interpreter :
The interpreter reads and executes the bytecode instructions line by line. Due
to the line by line execution, the interpreter is comparatively slower.
Another disadvantage of the interpreter is that when a method is called
multiple times, every time a new interpretation is required.
JIT Compiler :
The JIT Compiler overcomes the disadvantage of the interpreter. The
Execution Engine first uses the interpreter to execute the bytecode, but when
it finds some repeated code, it uses the JIT compiler.
The JIT compiler then compiles the entire bytecode and changes it to native
machine code. This native machine code is used directly for repeated method
calls, which improves the performance of the system.
The JIT Compiler has the following components :
Intermediate Code Generator :
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 8/16
5/25/25, 1:04 PM Java Architecture - HackMD
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 9/16
5/25/25, 1:04 PM Java Architecture - HackMD
When it runs, it leads to a “stop the world” event where the entire application is
paused.
The JVM argument to use Serial Garbage Collector is -XX:+UseSerialGC
Parallel GC :
JNI acts as a bridge for permitting the supporting packages for other
programming languages such as C, C++, and so on. This especially helpful in
cases where you need to write code that is not entirely supported by Java, like
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 10/16
5/25/25, 1:04 PM Java Architecture - HackMD
It verifies the bytecode to ensure it's safe and doesn't violate Java rules.
D. Bytecode Verification:
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 13/16
5/25/25, 1:04 PM Java Architecture - HackMD
Checks for illegal code that could corrupt memory or violate access rules.
E. Execution (java):
You run the program using java HelloWorld.
The JVM starts and uses the Execution Engine to run the bytecode.
Preparation: Allocates memory for static fields and sets default values.
This typically happens in dynamic systems like app servers or plugins, not in
basic Java apps.
8. What is JIT (Just-In-Time) Compilation in JVM? How does it improve
performance?
JIT is a part of the JVM that improves performance by compiling bytecode into
native machine code at runtime.
9. What is the difference between interpreted and compiled code in the context
of JVM?
Feature Interpreted Code Compiled Code (via JIT)
Execution Bytecode is read and Bytecode is converted into
executed line-by-line native machine code
Performance Slower Faster
Flexibility High (easy to debug) Lower flexibility, optimized for
speed
Optimization Minimal Advanced optimizations
possible
Example JVM without JIT JVM with JIT enabled
https://hackmd.io/RNYlB1gbTz6NZ5jiNT3kMg 16/16