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.

4

1 回答 1

3

当我们说解释时,是指将这个字节码翻译成机器可读的代码(否则编译)?

不,这意味着翻译。想象一个巨大的 switch 语句在操作码本身上进行切换,其中每个案例都会从字节码中提取所需的操作数,然后直接执行实现每个操作码所需的代码。例如,考虑iadd

case IADD:
    push(pop()+pop());
    break;

因此,如果 JVM 将字节码“编译”为机器可读的代码

它没有。

和 JIT 做基本相同的事情(将字节码转换为机器可读代码(否则编译)),使用 JIT 有什么优势?

首先,从 Java 1.3 开始,术语 JIT 已过时。我们现在拥有的是 HotSpot JVM,它是一种高度优化的 JIT,它可以选择性地将字节码中的热点转换为机器码,使用通常只有高度优化的编译器才能找到的技术,而早期的 JIT(一个) 是第三方产品,并且 (b) 不加选择地遇到的任何字节码都散布了机器代码。

其次,解释!= 编译,如上所述。如果 HotSpot 注意到字节码的特定片段在很大一部分时间都在执行,它会将其编译为机器码,这样它就可以直接执行而无需解释。

于 2017-08-01T00:38:59.863 回答