37

I am really struggling to understand the following thing

Previously I know:

When a Java program is compiled .class file will be generated. In that code is in the form of bytes. Then the JVM will translate that byte code into machine understandable format.

Now I see in one of the questions in SO

A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine

So here JIT is converting the bytecode to machine instructions. Then what is the use of JVM. We are able to do this with JIT. In my knowledge JIT is for only improving the performance of JVM.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Anusha
  • 419
  • 1
  • 4
  • 7

4 Answers4

41

The JIT is just part of the JVM. Other parts include the bytecode interpreter, the class loading verification and linking mechanisms, and the native code support for stuff like reflection, I/O and so on.

In that sense, the JIT doesn't make the JVM run faster at all. Instead it makes Java code run faster ... than it would if the JVM just interpreted it.

In reality, the JVM does start out interpreting the bytecodes. After a period, the JVM then uses its JIT compiler to compile heavily used methods to native code, using statistics that were gathered while interpreting to tune the code for the problem at hand.

By the way, this part of the text that you quoted is clumsy and technically inaccurate:

A Just-In-Time (JIT) compiler is a feature of the run-time interpreter ... (context)

In fact, the JIT is not a feature of the interpreter. Rather, the JIT is functionality of the JVM that works alongside the interpreter.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

To be clear :

JVM performs everything like :

It stays on the top of the operating system and provides abstraction between the compiled java program and operating system. This way, java compiled program doesn’t have to worry about which platform it has to work. Java program compiles code into bytecodes which JVM can understand and execute.

JIT

When JVM compiles the class file, it doesn’t complete the full class file; it compiles only a part of it on need basis. This avoids heavy parsing of complete source code. This type of compilation is termed as JIT or Just-In-Time compilation. JVM is Platform(OS) dependent Code generation JIT is Platform Oriented, generates the native byte code, so it is faster one than JVM :)

Community
  • 1
  • 1
Abraham K
  • 624
  • 1
  • 8
  • 24
  • But i see statement JIT compiles entire class file once.But in your statement jvm is doing compilation.Please explain – Anusha May 08 '13 at 11:44
  • @Anusha yah...JVM does the compilation part [line by line] a native interpreter for java..but JIT is a optional one, which also compiles, not everything just a necessary methods/functions. For a case consider : JIT compiler does not check Java boundary conditions such as Null pointer or array out of bounds exceptions. The only way the JIT compiler knows it has a null pointer exception is by a signal raised by the operating system. – Abraham K May 08 '13 at 12:05
  • This is just wrong. The javac Java compiler parses the Java source code and compiles it to Java bytecode (machine independent), your .class files. The JIT compiler compiles that bytecode to native machine code (CPU arch specific). The JIT output machine code still performs bounds checking and Java exception handling. – arjunyg Aug 30 '17 at 19:04
2

The Java Virtual Machine (JVM) provides the entire environment for running a Java program. It integrates with the Operating System, loads classes, and runs programs. The Just-In-Time compiler (JIT) is just a small piece that can be disabled (-Xint) but when enabled, provides a useful improvement in performance. There have been implementations of the JVM that didn't include a JIT, and implementations that worked by pre-compiling Java to machine code just the same as traditional languages, such as C or C++.

Craig Trader
  • 15,507
  • 6
  • 37
  • 55
-1

It compiles it just in time for the JVM with optimizations.

niklas
  • 143
  • 10