Technical confusion between compilation and interpretation

I read many definitions and statements about "Interpretation" and "compilation". But I'm still very confused.

From a technical point of view, what is REALLY the difference between interpretation and compilation under the hood? Let me clarify (please correct any wrong concept that I might have):

In java, the source code is “compiled” in ByteCode, which is then “interpreted” and / or “compiled just in time” into machine code. But what is the difference between compilation and interpretation over time? I mean, in the end, as far as I know, the CPU will only run machine code. Thus, in the interpretation, ARE instructions are also converted to machine code that can be understood by the CPU. So where do we draw the line between compilation and just-in-time interpretation?

PS This is my concept. This may be completely wrong. In that case, please excuse my stupidity and correct me.

Thanks.

+7
source share
5 answers

1 .. Frankly, the idea that java has both a compiler and an interpreter is a myth , its behavior , which is labeled Compilation and translator.

2. The Java compiler compiles human readable code into byte code. Which is then converted by JIT (Just In Time Compiler) at run time to machine level executable code.

3. In Runtime, the JIT identifies the intensive part of the execution code, and then converts it into machine-level executable code, this part of the code is known as Hot-Spot , which is why JIT is called the Hot-Spot compiler .

4. JIT uses the Virtual memory table (V-table) , which is a pointer to a method in the class . Then the Hot-Spot code is converted into executable code of its machine level, its address is stored here, and when this part is called again, then this stored address directly extracts it . This behavior of JIT to keep compiling small amount of code during Run time is assumed to be Interpreted Behavior and the JIT behaviour of storing this for later use is assumed as Compilation .

5. The virtual memory table also has a table that stores the address of the byte code, which can be used if necessary.

+3
source

When the code is compiled, the generated artifact is directly understood by the hardware. This is mainly machine code sent directly to the CPU. It also means that an artifact compiled against a given processor architecture will not work on another. The advantage is immediate start-up and excellent performance.

In interpreted environments, there is no compilation at all, or the result of such a step is intermediate code. This code represents two abstractions that should be sent directly to the processor. Instead, a separate layer (virtual machine, interpreter) is required, which reads this artifact and executes it in some sandbox environment. The advantage of this approach is portability. Intermediate code can work on any platform where its own interpreter is available. Unfortunately, performance is almost always worse.

JIT in Java is a hybrid technology. The first bytecode is interpreted, each bytecode instruction is executed by the interpreter. However, at some point in time (and under certain conditions), the bytecode is converted to machine code and sent directly to the CPU to improve performance. This approach brings the best of both worlds - intermediate code portability and native code speed. Moreover, JIT knows much more about the runtime of your code (how many times does this loop get called on average? Is this method really virtual?), So machine code can be faster than the one generated by a regular compiler (!)

+3
source

You are right that in the end everything should be converted to machine code. The main difference is that in the case of the interpreter, this translation occurs every time the code is executed, while the compiler does this translation ahead of time, after which the compiler is not required to run the program.

Just-in-time compilation is a combination of the two, where the JIT compiler is still required to run the program and the code is compiled at runtime.

Compilation takes time, but it is beneficial when the same code fragment is run several times, for example. in the loop. The HotSpot Java Virtual Machine takes this approach, first interpreting the bytecode directly, and then JIT-compiling a piece of code when it runs a certain number of times.

+1
source

Interpreters interpret the code line by line and solve the machine code at runtime;

The compiler consumes the piece of code and solves the machine code at compile time;

The JIT compiler is a hybrid approach whereby the code is generated at runtime (but may already be cached to improve performance), but consumed in a piece.

+1
source

The interpreted environment includes instructions executed immediately after parsing, where both parsing and execution are performed by the interpreter. This means that the machine on which you run the code must have an interpreter to run the program. one

The compiler will analyze the instructions in machine code and save them for later execution. However, Java is still compiling 2 , which means this process turns the instructions into a ByteCode, which the translator will then use.

+1
source

All Articles