JVM 解释与 JIT 编译。 JVM 不会将字节码编译为机器可读吗?

JVM interpretation vs JIT compilation. Does JVM not compile bytecode to machie readable?

我有一个关于 JVM(Java 虚拟机)和 JIT(即时)的问题。 据我所知,JVM 将字节码(来自 .class 扩展文件)作为输入并解释该字节码。 问题是:

  1. 当我们说解释时,是指将这个字节码翻译成机器可读的代码(否则编译)?
  2. 所以如果 JVM "compile" 字节码到机器可读代码和 JIT 做的事情基本相同(将字节码转换为机器可读代码(否则编译)),那么使用 JIT 有什么优势?

感谢您的回答。

When we say interpret, it's mean translation this bytecode to machine readable code(otherwise compiling)?

不是,是口译。想象一个巨大的 switch 语句切换操作码本身,其中每个 case 从字节码中提取所需的操作数(如果有的话),然后直接执行实现每个操作码所需的代码。例如,考虑 iadd:

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

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?

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

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