76

I couldn't find the difference between JIT and Interpreters.

Jit is intermediary to Interpreters and Compilers. During runtime, it converts byte code to machine code ( JVM or Actual Machine ?) For the next time, it takes from the cache and runs Am I right?

Interpreters will directly execute bytecode without transforming it into machine code. Is that right?

How the real processor in our pc will understand the instruction.?

Please clear my doubts.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Manoj
  • 5,707
  • 19
  • 56
  • 86
  • 3
    Till now computers don't execute anything other than machine code. – humble_wolf Aug 16 '18 at 06:22
  • Interpreters converts bytecode into corresponding native machine code for execution, bytecodes are not directly executing. – Suseendran P Apr 23 '19 at 02:22
  • 5
    I came here while googling, if you are like me, you can find more information on this [great answer on SoftwareEngineering](https://softwareengineering.stackexchange.com/questions/246094/understanding-the-differences-traditional-interpreter-jit-compiler-jit-interp) – mTorres May 11 '19 at 10:10

7 Answers7

92

First thing first:
With JVM, both interpreter and compiler (the JVM compiler and not the source-code compiler like javac) produce native code (aka Machine language code for the underlying physical CPU like x86) from byte code.

What's the difference then:
The difference is in how they generate the native code, how optimized it is as well how costly the optimization is. Informally, an interpreter pretty much converts each byte-code instruction to corresponding native instruction by looking up a predefined JVM-instruction to machine instruction mapping (see below pic). Interestingly, a further speedup in execution can be achieved, if we take a section of byte-code and convert it into machine code - because considering a whole logical section often provides rooms for optimization as opposed to converting (interpreting) each line in isolation (to machine instruction). This very act of converting a section of byte-code into (presumably optimized) machine instruction is called compiling (in the current context). When the compilation is done at run-time, the compiler is called JIT compiler.

enter image description here

The co-relation and co-ordination:
Since Java designer went for (hardware & OS) portability, they had chosen interpreter architecture (as opposed to c style compiling, assembling, and linking). However, in order to achieve more speed up, a compiler is also optionally added to a JVM. Nonetheless, as a program goes on being interpreted (and executed in physical CPU) "hotspot"s are detected by JVM and statistics are generated. Consequently, using statistics from interpreter, those sections become candidate for compilation (optimized native code). It is in fact done on-the-fly (thus JIT compiler) and the compiled machine instructions are used subsequently (rather than being interpreted). In a natural way, JVM also caches such compiled pieces of code.

Words of caution:
These are pretty much the fundamental concepts. If an actual implementer of JVM, does it a bit different way, don't get surprised. So could be the case for VM's in other languages.

Words of caution:
Statements like "interpreter executes byte code in virtual processor", "interpreter executes byte code directly", etc. are all correct as long as you understand that in the end there is a set of machine instructions that have to run in a physical hardware.

Some Good References: [I've not done extensive search though]

  • [paper] Instruction Folding in a Hardware-Translation Based Java Virtual Machine by Hitoshi Oi
  • [book] Computer organization and design, 4th ed, D. A. Patterson. (see Fig 2.23)
  • [web-article] JVM performance optimization, Part 2: Compilers, by Eva Andreasson (JavaWorld)

PS: I've used following terms interchangebly - 'native code', 'machine language code', 'machine instructions', etc.

KGhatak
  • 6,995
  • 1
  • 27
  • 24
  • 2
    You interchangeably uses the terms "machine code", "native instructions" and "machine instructions". Is there any difference between them in this context? – Steve.NayLinAung Oct 29 '18 at 20:13
  • I do not feel that there is any difference in the meanings for these terms. It's more of a writing choice based on what came naturally in my mind. – KGhatak Nov 09 '18 at 10:19
  • @KGhatak so what you're saying is that the JVM acts as both an interpreter and a compiler? Also instead of portability independence sounds more appropriate, wdyt? – DaCruzR Apr 22 '20 at 17:05
  • @ Rayner Da Cruz - yes, 'JVM acts as both an interpreter and a compiler'. I think platform-independence is portability - so better stick to the current wordings around the portability. – KGhatak May 13 '20 at 16:30
23
  • Interpreter: Reads your source code or some intermediate representation (bytecode) of it, and executes it directly.

  • JIT compiler: Reads your source code, or more typically some intermediate representation (bytecode) of it, compiles that on the fly and executes native code.

gpeche
  • 21,974
  • 5
  • 38
  • 51
  • 28
    What's the difference here between "Interpreter executing code _directly_" vs "JIT Compiler executing _native code_"? – ITisha Mar 01 '19 at 15:25
  • Depending on your answer they are the same thing. An interpreter, also interprets on the fly. So what's the difference? – ozanmuyes Jan 17 '23 at 21:18
  • @ozanmuyes when comparing "jit compiler" vs "interpreter", the difference is that "interpreter" does not compile to native code while "jit compiler" does. Of course you can talk about interpreters in a more generic way such that the definition includes jit – gpeche Jan 19 '23 at 19:29
  • 2
    How can an interpreter (or any program) execute code *directly*? How can the CPU understand code than isn't machine code? – sadcat_1 Jun 22 '23 at 10:07
19

Jit is intermediary to Interpreters and Compilers. During runtime, it converts byte code to machine code ( JVM or Actual Machine ?) For the next time, it takes from the cache and runs Am i right?

Yes you are.

Interpreters will directly execute bytecode without transforming it into machine code. Is that right?

Yes, it is.

How the real processor in our pc will understand the instruction.?

In the case of interpreters, the virtual machine executes a native JVM procedure corresponding to each instruction in byte code to produce the expected behaviour. But your code isn't actually compiled to native code, as with Jit compilers. The JVM emulates the expected behaviour for each instruction.

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
  • 7
    Interpreters converts bytecode into corresponding native machine code for execution, bytecodes are not directly executing. – Suseendran P Apr 23 '19 at 02:16
11

A JIT Compiler translates byte code into machine code and then execute the machine code.

Interpreters read your high level language (interprets it) and execute what's asked by your program. Interpreters are normally not passing through byte-code and jit compilation.

But the two worlds have melt because numerous interpreters have take the path to internal byte-compilation and jit-compilation, for a better speed of execution.

Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40
3

Interpreter: Interprets the bytecode if a method is called multiple times every time a new interpretation is required.

JIT: when a code is called multiple time JIT converts the bytecode in native code and executes it

Manjunath
  • 169
  • 3
  • 1
    There is a difference between Interpreter and Byte Interpreter, the Byte Interpreter interprets bytecode, while the Interpreter deals with the high level language. – Salim Mazari Boufares Nov 27 '21 at 18:58
0

The first time a class is referenced in JVM the JIT Execution Engine re-compiles the .class files (primary Binaries) generated by Java Compiler containing JVM Instruction Set to Binaries containing HOST system’s Instruction Set. JIT stores and reuses those recompiled binaries from Memory going forward, there by reducing interpretation time and benefits from Native code execution.

And there is another flavor which does Adaptive Optimization by identifying most reused part of the app and applying JIT only over it, there by optimizing over memory usage.

On the other hand a plain old java interpreter interprets one JVM instruction from class file at a time and calls a procedure against it.

Find a detail comparison here

bitan
  • 444
  • 4
  • 14
0

I'm pretty sure that JIT turns byte code into machine code for whatever machine you're running on right as it's needed. The alternative to this is to run the byte code in a java virtual machine. I'm not sure if this the same as interpreting the code since I'm more familiar with that term being used to describe the execution of a scripting (non-compiled) language like ruby or perl.

Aaron
  • 13,349
  • 11
  • 66
  • 105