How Java Code Is Executed Behind The Scenes :- Step By Step Process

Aadishjain
6 min readSep 4, 2023

--

Ever wonder how java code is executed behind the scenes.Let’s explore how things work behind the scenes.

  1. Compilation and Execution Of Java Code
Main.java is created

To run this main.java we have to execute 2 commands in terminal
1. javac Main.java
2. java Main

javac Main.java

This command convert this .java class into .class file
Main.java is converted into Main.class which consist bytecode which is taken by the jvm. This .class file is created by the compiler.If u want to know what this class file consist of here is the link ClassFileStructure.
After the javac Main.java we need to tell jvm that this is our class file
which you have to work upon it. We can do this by executing java classname command

java Main

java Main
This trigger our jvm and execution of our java codes starts

2. JVM

Summarising what we have learn so far

Jvm will play huge role in java code execution.Jvm is Java Virtual Machine
is a abstract machine which take code as a input and perfrom operations in it to produce our output.Now this class file is taken input by the jvm.
Jvm consist of three main parts:-

2.1 ClassLoader
2.2 Memory Area
2.3 Execution Engine

2.1 ClassLoader

Loading
Code received as a input from the .class file. It loads all the contents required for the execution of our code.
Three types of ClassLoader are:-
a) Bootstarp Classloader — It loads all the java api class present in the rt.jar(in older version now this rt.jar is broken down into the component). Rt.jar consist of all java api such as java.util ,java.lang and many more.It is important to know this Bootstrap code is written in c/c++ language not in java
b) Extension Classloader — It is the child class of the Bootstarp which loads class from the extension directory which we are using in our code.
c) Application Classloader — It loads all the classes present in our classpath

Loading

This Whole process is called Loading of a class in jvm

Linkage
Linkage consist of three process:-
a) Verify — After class loading our code is verified by the bytecode verifier if something is fishy or not generated by the compiler it will throw the error saying Java. lang. VerifyError.
b) Prepare —After verifying the file jvm will alloacte the memory for the class level static variables and assign default values to static values
c) Resolution — It replaces symbol names in our programm with the original memory reference from the method area.

Linkage

After loading and linkage all its data is stored in jvm memory(we will talk about it later section)

Initialization
Static variables will be assigned values ,static blocks will be executed from parent to child & from top to bottom.It is triggered by using new/Staic keyword.

Now all our classes are loaded, files are verified but the main question is where all this data is stored in our jvm? Answer is Memory area.

Whole Process of ClassLoading

2.2 Memory Area
It consist of 5 main parts:-
a) Method area
b) Heap area
c) Stack area
d) Programm counter register
e) Native method stack

Method area
For every jvm only one method area is created.It stores class level binary data,static variables,constant pool of class is also stored(constant pool means all the constanst or things are required to run code.It consist of symbolic reference of the code.This reference are classes interfaces methods,variables).

Heap area
It stores objects and instance variables in it. Arrays are also objects they are also stored in heap.Jvm communicate with these objects by using runtime class.

Stack Area
For every thread a seperate stack will be created.All our method call ,call to variables are stored in the stack. Once all operations are performed in stack it is destroyed by the jvm just before terminating the thread.Each entry in stack is called as stack frame.
Stack frame consist of three parts:-
Local variables — All local variables in our code ,parameters are stored in this.
Operand stack — All operations are performed in this.Jvm use this as a workspace to perform operation.
Frame data — All symbolic reference related to the method are stored in it.Also contains reference to exception incase if exception is rised.

Programm counter register
It holds the address of the current executing instruction,once completed it incremented to the next instruction of the address.

Native Method Stack
Native method such as wait(),hashcode() (native methods are those which are also implemented in other languages such as C/C++ ).

Memory Area Of Java

Lets see how our code is executed after being loaded and stored in jvm

2.3 Execution Engine
This is a central component of the jvm.It consist of 2 parts
a) Interperator
b) JIT compiler

Interpreter
It main job is to read byte code and interpert into machine code and execute code line by line. That is why java is said to be both compiler and interpreter language.But the problem with this is that it read again same code if some function or method is invoking again and again which degrade our performance.
To overcome this jit compiler was introduced to optimise our code.

JIT Compiler
It helps in increasing our performance of our code.It Maintains a count value everytime if that piece of code is used and it reaches a particular threshold value jvm will not read line by line or interpret it but it will directly execute this code.At first time whole is read interpreted by the jvm.It also uses a profiler to keep a track of most used piece of code which is being reused.

Execution Engine also contains garbage collector(used to destroy the objects to free up the memory) and native interface (helps in calling c/c++ libraries or to be called by them for execution).

Execution Engine

Whole architecture of jvm

JVM

After this it produces a machine code which is understand by the computer and produces output to our console.So that’s how java code is processed and we get our output.

Javaprogram –> compiler –> .class files — -> JVM — ->bytecode(1’s&0’s)

Process Of Execution Of Java Code

Hope you gained something from this. Please do comment if you have any feedback. Cheers !

Want to connect ?
Linkedln
Twitter
Instagram

References
Java By Durga Sir
Oracle Docs

--

--