0

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:

  1. When we say interpret, it's mean translation this bytecode to machine readable code(otherwise compiling)?
  2. So if JVM "compile" bytecode to machine readable code and JIT do basically the same thing (converting bytecode to machine readable code (compiling otherwise)), what the advantages in using JIT?

Thanks for answer.

RickSanch3z
  • 129
  • 7

1 Answers1

3

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • so when you say "_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._" how JVM executes? I though JVM should communicate with hardware or underlying OS. Thanks. – RickSanch3z Aug 01 '17 at 00:50
  • It just runs the code, same as you would. Function calls, system calls, loops, assignments, whatever. It's code itself you know, it's already in communication with the hardware and the OS. No mystery here. – user207421 Aug 01 '17 at 00:58
  • As far as I know each platform has JVM specially written for it. On the other hand we have unique bytecode that can be transported between platforms. Does this fact not imply that JVM should translate bytecode to machine readable code? For example *134 (0x86) i2f* conversion opcode in internal to Java platform. I have hard time to understand. Need to re-read with fresh head or go deeper in this subject. – RickSanch3z Aug 01 '17 at 01:11
  • No, it doesn't mean that. I've explained all that, and given an example. See edit. – user207421 Aug 01 '17 at 01:13