The definition of interpretation (correct me if I'm wrong) is parsing code like so:
- Translate currently parsed line to some intermediate language.
- Run the translated line.
- Move to the next line.
- Translate it.
- Run it. And so on.
That is not a valid definition. It describes just one way of implementing an interpreter, but it excludes other ways. Most interpreters do not operate a line at a time like that. It is more normal to translate from source code to an intermediate form ahead of actually trying to execute the intermediate form.
And even the phrase "intermediate form" is potentially incorrect, in that it suggests that there is some other form for the code after the intermediate form.
If so, why do people say that the JVM interprets the Java Bytecode? What it does is execute it.
Interpretation is a form of execution. So there is no contradiction.
Interpretation as opposed to what?
As opposed to compiling the bytecodes to native code and executing the native code. Which is what typically happens in typical Java implementations ... one way or another.
In a typical JVM, the bytecodes are interpreted to start with. After a bit the JIT compiler compiles them to native code that can be executed directly. Other modes include:
- Interpreting bytecode all of the time; e.g. using
java -int
- Ahead of time compilation; e.g. using
gcj
.
- Load-time compilation ... in which the bytecodes are compiled to native code as they are loaded by the classloader; e.g. JNode does this.
FOLLOWUP QUESTION
JIT execution means to compile each line during execution, correct? If so, how is it different than interpretation?
Firstly, no that is not correct:
There is no such thing as "JIT execution". When people say that, they really mean "JIT compilation and execution" ... and they are not spelling it out.
JIT compilation is not done a line at a time. It is typically done a method at a time.
And the difference is in what it is doing. The JIT compiler's job it to compile bytecodes to native code. It does not execute the bytecodes. Execution of the bytecodes is done by either interpreting the bytecodes (before they have been JIT compiled) or executing the native code that the JIT compiler has produced.