In this core java programming tutorials, we will study and understand in detail the execution process of java program. We will understand step by step what happens exactly when a java program in compiled and executed. Also we will understand the working of Just-In-Time compiler and what is a JIT compiler in detail.
Execution Process of Java Program
Lets first see a complete flow diagram and then we will understand the execution process step by step. Note that there basically are 2 phases – Compile time and Run Time –
- Step 1: Writing the code in NetBeans IDE. As you can see in the diagram step 1 is to actually type the code. In this image above you can see we have our code file as abc.java
- Step 2: Once you have written the code you save itand click Run (if you are using Netbeans IDE otherwise you compile the code in the command prompt by using the command javac abc.java). This invokes the Java Compiler. The compiler checks the code for syntax errors and any other compile time errors and if no error is found the compiler converts the java code into an intermediate code(abc.class file) known as bytecode. This intermediate code is platform independent (you can take this bytecode from a machine running windows and use it in any other machine running Linux or MacOS etc). Also this bytecode is an intermediate code, hence it is only understandable by the JVM and not the user or even the hardware /OS layer.
- Step 3: This is the start of the Run Time phase, where the bytecode is loaded into the JVM by the class loader(another inbuilt program inside the JVM).
- Step 4: Now the bytecode verifier(an inbuilt program inside the JVM) checks the bytecode for its integrity and if not issues are found passes it to the interpreter.
- Step 5: Since java is both compiled and interpretted language, now the interpreter inside the JVM converts each line of the bytecode into executable machine code and passed it to the OS/Hardware i.e. the CPU to execute.
These are the standard steps involved in a typical java program execution scenario.
Working of JIT (Just-In-Time) Compiler
In the above steps we didn’t mention the working of JIT compiler. Lets understand this by taking 2 case examples –
- Case 1:
- In case 1 you can see that we are at the intepretation phase (Step 5 of the overall program execution). Lets assume we have 5 lines which are supposed to be interpretted to their corresponding machine code lines. So as you can see in the Case 1 there is no JIT involved. thus the interpreter converts each line into its corresponding machine code line. However if you notice the last 2 lines are the same (consider it a redundant line inserted by mistake). Clearly that line is redundant and does not have any effect on the actual output but yet since the interpreter works line by line it still creates 5 lines of machine code for 5 lines of the bytecode.
- Now this is inefficient right? lets see case 2
- Case 2:
- In case 2 we have the JIT compiler. Now before the bytecode is passed onto the interpreter for conversion to machine code, the JIT compiler scans the full code to see if it can be optimized. As it finds the last line is redundant it removes it from the bytecode and passes only 4 lines to the interpreter thus making it more efficient and faster as the interpreter now has 1 line less to interpret.
- So this is how JIT compiler speeds up the overall execution process.
This was just one scenario where JIT compiler can help in making the execution process fast and efficient. There are other cases like inclusion of only those packages needed in the code, code optimizations, redundant code removal etc which overall makes the process very fast and efficient. Also different JITs developed by different companies work differently and JIT compilers are an optional step and not invoked everytime.
So this was the complete Execution Process of Java Program in Detail with the Working of JIT Compiler.