How does JVM convert bytecode into machine code?

Share
Java is known for its platform-independent feature which is based on the principle of “Write once, Run everywhere”.
The reason for being platform-independent is JVM (Java Virtual Machine) that produces bytecode and this bytecode provides the freedom to run anywhere and on any machine where JVM is installed.
However, the execution time slightly decreases as we compare to platform-dependent programming languages such as C, C++.
JVM makes Java codes portable and secure as it provides a sandbox where all the programs run and restrict untrusted code.

Key Terms :

bytecode: It is the intermediate code which is an optimized set of instructions generated by Java Compiler.
JVM( Java Virtual Machine): It is a program responsible for the execution of bytecode. It provides a Java  Runtime environment to run a Java program.

 

How does a Java Program execute?

Figure 1 : How JVM converts bytecode to machine code
  • A Java source file is always is saved with .java extension
  • After Java file is created, Java compiler compiles the code into an intermediate code termed as bytecode with an extension of .class. This bytecode is packaged in a JAR file (Java Archive file)
  • Now, this newly created bytecode is accepted by JVM. JVM stands for Java Virtual Machine that converts the bytecode to native code.
  • This native code can be easily understood by System (OS)

 

JVM Converts Bytecode to Machine Code

Figure 2 : JVM Architecture
JVM ( Java Virtual Machine ) receives this bytecode which is generated by Java Compiler. In JVM, there are two main components that perform all the jobs to convert the bytecode to native code, Classloader, and Execution Engine.

Classloader   [ Click here to read  detailed working ] :

As the name suggests, it reads .class file (bytecode) and loads necessary files and resources onto JRE. It is responsible for loading, linking, and initialization in order to load, verify, prepare and initialize the class.
JVM uses memory in order to run a program, that memory area is referred to as Runtime Data Area which consists Method Area, Heap, Java Stack, Pc Registers and Native method Stack.

Execution Engine

After the code successfully is loaded, the main task which is the execution of code is done by Execution Engine.
Execution  Engine is the second components where Interpreter, JIT compiler and Garbage collector comprised.
They are responsible for the execution of code.
  • Interpreter: It reads Java bytecode line by line and accountable for program execution. So, if we compare the performance then interpreter somewhere falls behind the compiler’s code execution.
  • JIT Compiler: It precompiles the portions of bytecode into native code as it is not possible to compile the entire Java program while multiple runtimes checks. Only essential parts are compiled and the remaining code is interpreted by the interpreter. Interpreter directly refers to this precompiled code without reinterpreting it. Hence, It boosts up the performance.

This post was last modified on June 5, 2021

Sandeep Verma

Published by
Sandeep Verma
Tags: bytecode in java Classloader java jvm bytecode JVM convert bytecode into machine code machine code working