Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
One of the most common questions asked is ‘ Is Java a compiled or an interpreted language?’.
The answer to this question lies in the fact that the Java Virtual Machine (JVM) converts Java code to platform-independent bytecode, which is then converted to machine code in binary form with the help of the Just-in-Time (JIT) compiler. We can say that Java uses both approaches to run its programs. The JIT compiler is a part of the JVM that improves performance by compiling bytecode to native code for repeated method calls. This shows the close relationship between the JVM and JIT compiler. Let us briefly explain what JVM and JIT are and then move on to see the differences between them.
The Java Virtual Machine is an abstract computing and virtual machine interface that drives the java code. It is a part of the Java Runtime Environment (JRE) that compiles bytecode into machine code for program execution by the CPU. Bytecode entering the JVM undergoes three phases after which it is compiled to machine code.
Loading: The referenced classes or methods are loaded using the class loader.
Verifying: The bytecode verifier checks the bytecode for instructions that may cause any harm for the present or future executions.
Initialisation and conversion: All static variables are assigned their values defined in the code or static block. Finally, the JIT plays its role by compiling the bytecode to machine code.
As mentioned before, the JIT compiler compiles bytecode to machine code “just-in-time” for it to run. By default, the JIT compiler is enabled and is activated on the calling of a Java method. It can be disabled to check and diagnose Java compilation problems. For other reasons, it is not recommended as it may slow down your code by running it in interpreter-only mode. The JIT compiler helps optimise and maximise the performance of complex Java programs without consuming processor time and memory space. It also keeps track of operational data during runtime, which can be used to improve future recompilations.
How does it work with the JVM?
The differences between the JVM and JIT can be highlighted while discussing the coordination between them during Java program execution.
The first time a method is called, it’s not compiled. For every method, JVM maintains a count to keep track of the number of times that method is called. It interprets the method until the count reaches a JIT compilation threshold.
After the method is compiled, subsequent calls to the method are counted and upon reaching the JIT compilation threshold, JIT compiles the method a second time implementing more optimisations to increase performance. This is repeated until maximum optimisation level is attained.
Q: How does the JRE play a role in executing a java program?
The Java Runtime Environment provides a platform to run and execute the source code. It contains the JVM and provides the libraries to execute the program. The JVM, in turn, contains the JIT compiler. The JRE is a minimum requirement to run Java applications on any computer. It is platform-dependent.
Q: What are the advantages and disadvantages of the JIT compiler?
Advantages:
It works transparently and does not require a C compiler.
It eliminates empty methods and common sub-expressions within a block of code.
It speeds up the performance of Java classes and avoids recompilation across sessions or instances where the code remains the same.
Disadvantages:
Since JIT compilation occurs at runtime, the process incurs overhead while validating and analysing code snippets.
JIT compilation involves storing compiled code fragments as cache that occupies memory.
It increases the complexity of Java program execution, making it difficult to understand for programmers.
Key Takeaways
JIT compilation allows Java programs to start and run while the code that is generated is not optimised for the platform. Without a JIT, the JVM starts up quickly but usually runs slower. This blog gives a brief explanation of what a JIT compiler is and its working. It also highlights the key differences between the JVM and JIT compiler.