- Key Takeaways:
- Java Virtual Machine, popularly known as JVM, makes the Java platform independent with the Write Once, Run Anywhere principle.
- Java Virtual Machine (JVM) converts bytecode into machine instructions for execution.
- It handles class loading, bytecode verification, and memory management.
- JVM uses an interpreter, JIT compiler, and garbage collector for performance and efficiency.
- It is essential for writing optimized, secure, and portable Java applications.
Have you ever thought about how the same Java program would run equally well on any device, without changing the program in any way? This is possible because of the Java Virtual Machine (JVM). The JVM executes compiled bytecode, handles the memory management, and is responsible for the execution of the application while ensuring that it is reliable and platform independent.
In this blog, we’ll explore how the JVM works, its architecture, and its role in running Java programs efficiently.
Table of Contents:
What is Java Virtual Machine?
“The Java Virtual Machine (JVM) is an abstract computing machine that enables Java programs to run on any device or operating system without modification.”
It acts as an intermediary between Java code and the underlying hardware, making Java a platform-independent language.
JVM (Java Virtual Machine) generally runs Java applications as a run-time engine. The JVM is responsible for invoking the main() method in a Java program during execution. JVM is a part of JRE (Java Runtime Environment).
Java applications are WORA (Write Once Run Anywhere) applications, meaning a programmer can develop Java code on one system and expect it to run correctly on any other Java-enabled system without having to change or modify anything.
This is all possible because of the JVM. When we compile a .java file, the Java compiler outputs .class files (with byte-code) with the same class names present in the .java file. When we run the .class file, it goes through several steps. All of them together describe the JVM.
Key points about JVM:
- JVM executes Java bytecode, which is generated after compiling Java source code.
- JVM is part of the Java Runtime Environment (JRE).
- It allows Java programs to follow the principle of Write Once, Run Anywhere (WORA).
Want to strengthen your Java and programming fundamentals while learning JVM concepts hands-on?
Check out Intellipaat’s Free Programming Courses to build practical skills, earn certifications, and get lifetime access to expert-curated content.
How Java Virtual Machine (JVM) Works?
The activity of the Java Virtual Machine (JVM) can be described as a sequence of steps that starts with the running of a Java program and ends with the execution of the Java program. Each step in this workflow serves a specific purpose in allowing Java applications to be platform independent, secure, and high-performance. The working of JVM, aka Java Virtual Machine, can be divided into a few key steps with the following common workflow:
1. Compilation
The Java Compiler (javac) takes source code (.java file) and compiles it into a bytecode file (.class file).
Although Java bytecode is machine code, it is not machine code for any specific hardware operating system, it is universal. The bytecode is an intermediate file that the JVM can interpret and execute.
This is the first step towards Java’s “Write Once, Run Anywhere” (WORA) principle.
2. Class Loading
After the generation of bytecode, it needs to be loaded into memory prior to execution.
This is the job of the Class Loader subsystem.
The JVM makes use of three main types of class loaders:
- Bootstrap Class Loader – Loads the Java core classes (such as java.lang, java.util, etc.).
- Extension Class Loader – Loads the Java classes from the extensions directory (for example: jre/lib/ext).
- Application Class Loader – Loads classes from the application’s classpath.
Developers can also define custom class loaders that provide specialization.
3. Bytecode Verification
Once the class is loaded, the JVM will check the bytecode for correctness and security.
This step guarantees that the bytecode will not perform unsafe operations, such as:
- Illegally accessing memory.
- Bypassing access restrictions.
- Breaking language-level constraints.
The program will not execute if verification is not successful.
4. Execution Engine
During the Execution Engine, the program is actually executed. The Execution Engine translates bytecode into machine-level instructions that the hardware understands.
The Execution Engine consists of:
- Interpreter – Runs bytecode line by line, easy and straightforward but slow performance.
- Just-In-Time (JIT) Compiler – Compiles sections of bytecode that are used repeatedly into native machine code for better performance.
- Garbage Collector (GC) – A system component to assist with reclaiming memory that is no longer in use, to free up memory for other processes, to make more efficient use of memory.
5. Memory Management
JVM divides memory into different areas called Runtime Data Areas:
- Method Area– This is where class-level data is generated. This includes method information, constants, and static variables.
- Heap– Under the heap, objects are created, and any instance variables are stored.
- Stack– The stack stores method calls, local variables, and partial results. Each thread has its own stack.
- Program Counter (PC) Register– This register keeps track of the instruction being executed for each thread.
- Native Method Stack– The heap allows native (non-Java) methods to be executed, which are usually written in C or C++.
In order to help you understand the concept of JVM better, we have attached a video below. Hop on to it to quickly understand what the Java Virtual Machine Means and what it does.
Why JVM is Important?
An understanding of JVM architecture will facilitate the development of an optimized Java programs.
Main components of the JVM architecture:
- Class Loader Subsystem is a subsystem concerned with the loading, linking, and initialization of classes.
- Runtime Data Areas consist of multiple memory areas, including the Method Area, Heap, Stack, Program Counter(PC) Register, and Native Method Stack.
- Execution Engine is a subsystem responsible for interpreting or compiling the byte code.
- Native Interface allows for interfacing between the JVM and the native libraries of the machine.
- Garbage Collector is an automatic memory management system.
Get 100% Hike!
Master Most in Demand Skills Now!
Common JVM- Java Virtual Machine Terminologies
- JRE (Java Runtime Environment) – Supplies the JVM and libraries necessary for the execution of Java applications.
- JDK (Java Development Kit) – Contains the JRE, as well as development tools such as
javac
.
- Bytecode – The intermediate code produced by the Java compiler.
- Garbage Collection – Automatic memory management in the JVM.
- JIT Compiler – Enhances performance by compiling bytecode into native code.
Conclusion
The Java Virtual Machine (JVM) is at the core of Java to deliver platform independence, reliability, and performance. From compiling source code into bytecode to class loading, verification, execution, and memory management, every step in the JVM workflow ensures that Java programs run securely and efficiently across environments.
For developers, understanding how the JVM works is more than just theory, it helps in writing optimized code, troubleshooting performance issues, and making better use of Java’s features. Whether you are building enterprise applications, mobile apps, or backend systems, the JVM remains the silent engine that makes Java one of the most enduring programming languages in the world.
FAQ- Java Virtual Machine
1. What is the purpose of a Java Virtual Machine?
The Java Virtual Machine (JVM) executes Java bytecode and enables programs to run on any platform without modification. It also manages memory, ensures security, and optimizes performance through features like garbage collection and JIT compilation.
2. What are the types of virtual machine in Java?
In Java, there are mainly two types of virtual machines: the Java Virtual Machine (JVM), which runs Java bytecode, and the JavaScript Virtual Machine, used by browsers for JavaScript execution. However, within Java itself, JVM implementations like HotSpot, J9 (Eclipse OpenJ9), and GraalVM are the commonly used types.
3. What is the difference between JDK, JRE, and JVM?
JDK is the complete package with tools to develop and run Java programs, JRE provides only the libraries and JVM to run them, and JVM is the engine that actually executes the bytecode.
4. What are the four main tasks of JVM in Java?
The four main tasks of the JVM are: loading code, verifying code, executing code, and managing memory. Together, these ensure Java programs run securely, efficiently, and platform-independently.
5. How does JVM work internally?
Internally, the JVM loads compiled bytecode through the class loader, verifies it for safety, and executes it using the interpreter or JIT compiler. It also manages memory via runtime data areas and garbage collection during program execution.