A multiplatform programming language is Java. The JVM, or Java Virtual Machine, is a virtual machine that enables the execution of Java programs. A JVM’s function is to translate Java bytecode into native machine code. It has several components that make it up, such as the Compiler or Execution Engine, Run-time Environment, and ClassLoader.

The JVM allows for the principle of “Code Once, Run Anywhere.” Read this article as we show you the different parts of JVM and explain how they work.

JVM Structure

The Java Virtual Machine comprises three fundamental components. The execution engine, the ClassLoader, and the Runtime Data Area are what make up the JVM.

JVM Components

Execution Engine

The role of the Execution Engine is to translate the high-level program into bytecodes that run inside the JVM. The heart of the JVM, where real execution happens, is called the Execution Engine. As you may tell already, the Execution Engine is one of the most important components of the Java Virtual Machine. Additionally, it manages communications with the JVM’s memory and other parts.

Runtime Data Area

The bytecodes, objects, variables, and values are kept in the runtime data area. The method area, heap, stack, native method stacks, and PC Register are the five components that make up the runtime data area. These places preserve various states of the program. Because it holds the data that the application is now using, it is known as the data region of JVM.

ClassLoader

One of the most crucial parts of the Java Virtual Machine is the ClassLoader. A class loader is in charge of bringing up an application’s class files and instantiating them on the runtime’s accessible classes. An application can declare only one class loader to be used for the application, but the JVM allows more than one class loader to be used. This does a lot to modularize the application and distribute the classes across multiple processes.

How JVM Works

JVM works in a few different ways. First, any code that is compiled into a JVM byte code is translated into the machine language of the targeted platform. The JVM’s compiler component performs this process. After the code is translated, it is then stored by the VM in the Runtime Data Area. When the application is started, the instructions are retrieved from the Runtime Data Area by the VM and executed by the Execution Engine. These instructions are used to execute the bytecodes and are interpreted by the processor based on the architecture of the target machine.

JVM Internal Architecture

The JVM Internal Architecture is responsible for executing the program written in the Java language. It accomplishes this by using a set of instructions called the Bytecode for a given program. There is a need for a JVM to execute all the programs written in Java, as the Java language does not directly produce machine code but produces byte code instead.

JVM Memory Architecture

The Java Virtual Machine’s Memory Architecture is contained in the Runtime Data Area. This area contains all of the information that is necessary for the application to run properly.

The Runtime Data Area is a very important component of the Java Virtual Machine as it stores the classes and objects that are used by the Java program at runtime. This area also contains all of the other information that is necessary for the application to run properly. This data area is contained within the Physical Memory Address Space and is divided into multiple sections, each containing a different type of information used by the program, such as classes, objects, and method handles.

The method area, heap, stack, native method stacks, and PC Register are the five components that make up the runtime data area.

Method Area

Method Area contains all of the method information that the program uses at runtime. This contains the method handle for each class method that the application is currently using.

Stack

This is the area that contains the variables or constants that the program is using at runtime. If a variable or constant needs to be referenced at runtime, then it is first placed on the stack, and then the value is retrieved when the variable or constant is needed again during the execution process.

Heap

Basically, the software keeps all of its object instances in a space called the Heap.

Native Method Stacks

Equally important, the native methods that the Java Virtual Machine has loaded at runtime make use of native method stacks.

PC (Program Counter) Register

The JVM uses the Program Counter Register on an internal level. Accordingly, the PC Register keeps track of where the program is executing. Due to this, JVM gets access to the location of the code, all thanks to the PC Register. It also works together with the instruction pointer.

JVM Diagram in Java

Below is the graphical representation of the JVM diagram in Java language, which shows the internal architecture of Java Virtual Machine and the JVM internals.

Credit: https://www.freecodecamp.org/news/jvm-tutorial-java-virtual-machine-architecture-explained-for-beginners/

How to Write a JVM Performing Operation

Accordingly, let’s look at a simple addition operation in Java. The below code demonstrates a simple programming exercise in which we assign the integer 10 to the variable var1. Additionally, create the variable var2, and assign it the value of 2. Then display the total as the command finishes performing.

Syntax

import java.io.*;
class AssignExercise {
public static void main(String[] args)  {
int var1 = 10;         //set the value of var1 to 1.
int var2 = 2;          //set the value of var2 to 2.
System.out.println(var1 + var2); //display the result of adding two numbers and storing them in the result variable.
}
}

The above example demonstrates the java code to write JVM performing operations.

What Are the Main Operations in JVM

Chiefly, the JVM transforms human-readable source code into extremely tiny, JVM-interpretable byte codes. The JVM starts the execution process by loading the class file, performing compilation, parsing the source code and translating it into bytecodes, and finally executing them. Another main operation of the JVM is to ensure the code runs on every platform.