I have a question about JVM(Java Virtual Machine) and JIT(Just-in-Time). As far as I know JVM take as input a bytecode (from .class extension file) and interpret this bytecode. The questions are:
Thanks for answer.
When we say interpret, it's mean translation this bytecode to machine readable code(otherwise compiling)?
No, it means interpreting. Think of a giant switch statement switching on the opcode itself, where each case pulls the required operands if any out of the byte-code and then directly executes the code required to implement each opcode. For example, consider iadd
:
case IADD:
push(pop()+pop());
break;
So if JVM "compile" bytecode to machine readable code
It doesn't.
and JIT do basically the same thing (converting bytecode to machine readable code (compiling otherwise)), what the advantages in using JIT?
First, the term JIT is obsolete as of Java 1.3. What we now have is the HotSpot JVM, which is a kind of highly-optimizing JIT, that selectively converts hot-spots in the byte code into machine code, using technology normally only found in a highly optimizing compiler, whereas the early JITs (a) were third-party products and (b) spattered machine code for any byte code it encountered pretty indiscriminately.
Secondly, interpretation != compilation, as noted above. If HotSpot notices that a particular piece of the byte-code is being executed a large fraction of the time it will compile it to machine code so it can execute directly without interpretation.