×

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

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.