All About Java Virtual Machine

What is Java Virtual Machine?

Java Virtual Machine or JVM is a Java run-time system. It is a virtual machine environment that helps drive a Java code. Unlike other programming languages, whenever we compile a Java program, it never generates a machine code. In Spite of that the Java compiler produces Bytecode for the Java Virtual Machine.

How does JVM work? 

JVM allows us to achieve both portability and security problems:

1. Platform Independence:

In computer programming, the word platform represents the environment in which a program runs. Being platform-independent simply means a program can be easily executed on different platforms without rewriting and recompiling. This is possible only .because translating a Java program into bytecode makes it easier to execute a program in different environments because only JVM needs to be implemented for each platform.

Although the JVM will differ from platform to platform, the same Java bytecode is understood by them. This makes Java a platform-independent technology and also justifies WORA(Write Once Run Anywhere).

2. Security:

JVM plays an important role in security. The Java program being executed by JVM also helps to make it secure because the JVM is in charge of the execution environment, it can contain the program and prevent the problems arising outside of the system. This is because the bytecode has been highly optimized, bytecode allows the JVM to run programs faster. JVM also verifies the bytecode which ensures further safe operation of the program.

Architecture of JVM:

The internal architecture of a Java Virtual Machine or JVM consists of:

1. ClassLoader:

The class loader initializes the class file at runtime. It dynamically loads the class and reads the .class file and also saves the bytecode created in the method area. It’s three main functions are Loading, Linking and Initialization. There are three main types of the class loader-

  1. Bootstrap ClassLoader: The bootstrap class loader is responsible for loading the java.lang.ClassLoader. It is an important part of JVM which loads the JDK internal classes from the locations rt.jar. It is also known as a primordial class loader.
  2. Extension ClassLoader: The extension class loader loads the classes from JDK extension library.
  3. System Class Loader: The main function of system class loader is to load the classes from the currently set classpaths.

2. Method Area:

There is one method area for each JVM. Method area stores all the class level data such as class name, methods, static variables, non-static variables, fields etc.

 3. Heap:

In the heap area, object data and corresponding instance variables are stored by default.

4. Stack:

A separate runtime stack will be create for every separate thread. Every block of stack created is called a stack frame which is used for storing the corresponding local variables. The stack is terminated automatically by the JVM after the thread is destroyed.

5. Program Counter Registers:

A separate PC register will create for every thread. It holds data currently executing data. Once the execution stops, automatically the PC register will be update to the next section.

6. Native Method Stack:

Contains all the native methods used in our program.

7. Execution Engine:

Execution engine executes the bytecode line by line. It contains three main components :

  1. Interpreter: 

A very important member of the JVM is the interpreter. The interpreter converts bytecode instructions according to the underlined platform instruction as well as the run state and executes the code.

  1. Just-In-Time Compiler (JIT):

The JIT compiler compiles the code as it is need during the execution. Only the code that will benefit from the compilation is compile. The remaining code is simply interpret. In this way, the Just-In-Time compiler provides a kind of performance boost and also increases the efficiency of the interpreter.

  1. Garbage Collector:

In Java, the user does not have to worry about how the objects are destroyed which are no longer in use since the deallocation of memory is handled automatically. The technique used to accomplish is refer garbage collection. In garbage collection, when an unreference object exists, the memory claim by this object can be reclaim by another object.

8. Java Native Interface:

Java Native Interface (JNI) is a framework which provides an interface to interact with the native method libraries and to call the applications written in languages like C, C++, Assembly etc.

written by: Kritika Tiwari

Reviewed By: M SASANK SAI, Bujagawni Sai Teja Goud

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *