×

JIT in Java

What is JIT ?

JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation is going to happen. So, before the interpreter, the JIT compiler quickly analyses the code and removes redundancy so that the interpretation can happen faster. It is one of the most confusing topics for developers.  JIT is a part of JVM ( Java virtual machine). The actual purpose of JIT is to compile bytecode to machine code at run time. During this conversion, JIT performs some optimization on code so that code can be executed very fast, and it also improves the performance of execution. At the run time if the program contains one instruction, which can be executed more than one time. At that time the JVM ( Java virtual machine ) was used to read the instruction and convert that instruction to machine code and execute that instruction on the CPU. Here the term " compiler" refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

Working on JIT ( Just in time ) compiler?

To understand how JIT works? First, let's understand how to compile and run a simple java program.

Abc.java

class Abc{
public static void main(String args[]){
System.out.println(" Hi ! I am abc ");
}
}

Output :

JIT in Java

Syntax to compile Java program :

javac filename.java

Syntax to run Java program :

java filename
JIT in Java

The figure shown above describes the complete execution of the Java program. The execution of the Java program is completely different when compared to c++. Because the Java program is compiled as well as interpreted. The compiler and the interpreter take part in the entire execution process.

Starting with the small rectangle box. In the first step i.e., the first box we have our Java code which is nothing but where we type our Java code and save our Java file as filename.java here I took the example as abc.java this is the very first step. And the next step is to invoke the java compiler by using the "javac" command.

When we type javac in our cmd (command prompt) the compiler is executed or invoked. Now the compiler will compile the entire java file and convert it into byte code with an extension “.class” i.e., filename.class here in the diagram it is compiled to abc.class. So, when we compile, we will be getting a class file that is written in jewerish language which we cannot understand or read. In the second step the compiler ofcourse, checks for syntax errors and different errors we had in our code.

If there is no error, then it will generate the bytecode or else it goes back to the first step. This stage is known as the bytecode stage. These first three stages ( java program, compiler, bytecode) are the stages compile time or the compilation stages.

When we invoke the Java program to run the bytecode i.e. when we give "java filename" in our cmd. This step is considered the fourth step. Now this byte code i.e., abc.class is loaded by the “Class loader”. “Class loader” is again a program inside JVM ( Java virtual machine ). It loads bytecode in JVM. Then there is one more program “Bytecode verifier” inside JVM. The " Bytecode verifier" checks for any errors or bugs in the bytecode. So this all happens inside JVM. After this “ Interpreter “ is invoked.

The basic difference between an Interpreter and a compiler is?

The compiler compiles the complete or entire file. For example, if the file contains ten lines. Then the compiler takes all lines and converts them into bytecode or class files only if there are no errors in the program. Whereas the interpreter interprets each line one by one. Now consider the same example, for ten lines of a file the interpreter first takes the first line and converts it into the class file or byte code. And then does the same with the second file.

So, after the " Bytecode verifier " verifies the class file it sends the file to either "Interpreter" or "JIT" depending on some conditions. The interpreter interprets the code line by line and sends it out of JVM i.e., to OS (Operating System) or any hardware, and executed.

Step 1: Java code (filename.java)

Step 2: Entire java file is converted into the class file with an extension ".class" by invoking javac in cmd.

Step 3: Checks for any error and converts it into bytecode.

Step 4: Bytecode is loaded by “Class loader”.

Step 5: Bytecode verifier checks for any errors or bugs in the class file.

Step 6: Depending upon JIT type. The JIT compiler or Interpreter is invoked.

Step 7: Sent to OS to get output.

To understand the JIT compiler, we should go with two cases.

Case 1:

Let us assume we do not use the JIT compiler in this case.

JIT in Java

Here we have five lines of code, this code is already a class code. Currently, we are at stage 6.  Now this byte code is sent to the interpreter. The interpreter interprets the class code line by line and generates the corresponding outputs and transfers them to the CPU to execute.

 As this is just a five-line code we will get our required output within a fraction of a second. If our program consists of thousands of lines. Then the interpreter goes line by line so it will take some time to produce output. The major point to note in case 1 is last two lines are the same. Even though they are the same the interpreter considers them as different lines and gives the same output. It is unnecessary, we could have saved one instruction.

Case 2:

JIT in Java

In case 2 we have the JIT compiler involved. The bytecode first goes to the JIT compiler. The JIT compiler analyses the entire code (since it is a compiler it analyses the entire code) and it sees if any redundant lines can be excluded. Her last line is extra if it is removed also the output will not change. So, it excludes the last line and transfers the first four lines to the interpreter. Now the interpreter assumes the bytecode has only four lines and interprets only four lines. And the output will also be the first four lines only.

We observe that one line was saved. This is where the JIT compiler comes into the picture. Invoking the JIT compiler is dependent on what type of JIT compiler is used. Some JIT compilers are very aggressive they are invoked every time, and some are not. Depending upon the coding and inbuilt nature of the JIT compiler it is being invoked. But depending upon the huge size the JIT compiler can be very efficient, and it can optimize the code and make the processing and execution very fast.


Related Topics

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

StringBuilder in Java

StringBuilder in Java Java StringBuilder class is introduced since JDK 1.5. The StringBuilder class is mainly used to create modifiable or mutable strings. Note that the StringBuilder class is not synchronized....

7 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

How to Create Immutable Classes in Java

Introduction Java is a programming language that is entirely object-oriented, and everything in it is seen as an object. And the blueprint or template of these objects are classes. Several classes...

3 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.