Java 什么是字节码以及 JVM 如何处理它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2203248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are bytecodes and how does the JVM handle them
提问by Midnight Blue
I heard many times that Java implemments JIT(just-in-time) compilation, and its bytecodes which are portable across platforms get "interpreted" by JVM. However, I don't really know what the bytecodes are, and what the JVM actually mean in Java language architecture; I would like to know more about them.
我多次听说 Java 实现了 JIT(即时)编译,其跨平台可移植的字节码由 JVM“解释”。但是,我真的不知道字节码是什么,以及 JVM 在 Java 语言架构中的实际含义;我想更多地了解他们。
采纳答案by Geoff Reedy
The JVM (Java Virtual Machine) has an instruction set just like a real machine. The name given to this instruction set is Java Bytecode. It is described in the Java Virtual Machine Specification. Other languages are translated into a bytecode before execution, for example ruby and python. Java's bytecode is at a fairly low level while python's is much more high level.
JVM(Java 虚拟机)具有与真机一样的指令集。该指令集的名称是 Java 字节码。它在Java 虚拟机规范中进行了描述。其他语言在执行前被翻译成字节码,例如 ruby 和 python。Java 的字节码处于相当低的水平,而 Python 的字节码要高得多。
Interpretation and JIT compilation are two different strategies for executing bytecode. Interpretation processes bytecodes one at a time making the changes to the virtual machine state that are encoded in each instruction. JIT compilation translates the bytecode into instructions native to the host platform that carry out equivalent operations.
解释和 JIT 编译是执行字节码的两种不同策略。解释一次处理一个字节码,对每条指令中编码的虚拟机状态进行更改。JIT 编译将字节码转换为执行等效操作的主机平台本机指令。
Interpretation is generally quick to start but slow during execution, while JIT has more startup overhead but runs quicker afterwards. Modern JVMs use a combination of interpretation and JIT techniques to get the benefit of both. The bytecode is first interpreted while the JIT is translating it in the background. Once the JIT compilation is complete, the JVM switches to using that code instead of the interpreter. Sometimes JIT compilation can produce better results than the ahead-of-time compilation used for C and C++ because it is more dynamic. The JVM can keep track of how often code is called and what the typical paths through the code are and use this information to generate more efficient code while the program is running. The JVM can switch to this new code just like when it initially switches from the interpreter to the JIT code.
解释通常启动快但执行时慢,而 JIT 启动开销更大但之后运行速度更快。现代 JVM 结合使用解释和 JIT 技术来获得两者的好处。字节码首先被解释,而 JIT 在后台翻译它。一旦 JIT 编译完成,JVM 就会切换到使用该代码而不是解释器。有时 JIT 编译可以产生比用于 C 和 C++ 的提前编译更好的结果,因为它更具动态性。JVM 可以跟踪代码被调用的频率以及通过代码的典型路径是什么,并使用此信息在程序运行时生成更高效的代码。JVM 可以切换到这个新代码,就像它最初从解释器切换到 JIT 代码一样。
Just like there are other languages that compile to native code, like C, C++, Fortran; there are compilers for other languages that output JVM bytecode. One example is the scala language. I believe that groovy and jruby can also convert to java bytecode.
就像有其他语言可以编译为本机代码一样,例如 C、C++、Fortran;有其他语言的编译器可以输出 JVM 字节码。一个例子是 scala 语言。我相信groovy和jruby也可以转换成java字节码。
回答by Eric Petroelje
Bytecode is a step between your source code and actual machine code. The JVM is what takes the bytecode and translates it into machine code.
字节码是源代码和实际机器代码之间的一个步骤。JVM 将字节码转换为机器码。
JIT refers to the fact that the JVM does this translation on the fly when the program is executed, rather than in a single step (like in a traditionally compiled/linked language like C or C++)
JIT 指的是 JVM 在执行程序时即时执行此转换,而不是在单个步骤中执行此转换(如在传统的编译/链接语言中,如 C 或 C++)
The point of bytecode is that you get better performance than a strictly interpreted language (like PHP for example) because the bytecode is already partially compiled and optimized. Also, since the bytecode doesn't need to be directly interpreted by the CPU, it doesn't need to be tied to any specific CPU architecture which makes it more portable.
字节码的重点在于,您可以获得比严格解释型语言(例如 PHP)更好的性能,因为字节码已经部分编译和优化。此外,由于字节码不需要由 CPU 直接解释,因此它不需要绑定到任何特定的 CPU 架构,这使其更具可移植性。
The disadvantage of course is that it will generally be a bit slower than a natively compiled application since the JVM still has to do some work in translating the bytecode to machine code.
缺点当然是它通常比本地编译的应用程序慢一点,因为 JVM 仍然需要做一些将字节码转换为机器码的工作。
回答by Cesar
When you compile something in Java, the compiler generates bytecode. This is native code for the Java Virtual Machine. The JVM then translates the bytecode to native code for your processor/architecture, this is where the JIT happens. Without JIT, the JVM would translate the program one instruction at a time, which is very slow.
当你用 Java 编译某些东西时,编译器会生成字节码。这是 Java 虚拟机的本机代码。JVM 然后将字节码转换为处理器/架构的本机代码,这就是 JIT 发生的地方。如果没有 JIT,JVM 会一次翻译一条指令,速度非常慢。
回答by Andreas Dolk
jcyang already provided a link to wikipedia, but this one is a better match to your question:
jcyang 已经提供了维基百科的链接,但这个链接更适合您的问题:
The Java Compiler compiles Java Source code to class files. The class's methods are translated to Byte Code and the Java virtual machine (JVM) interpretes this byte code. A Just In Time compiler (JIT) may be used to translate the byte code to machine code to speed up execution of class methods.
Java 编译器将 Java 源代码编译为类文件。类的方法被转换为字节码,Java 虚拟机 (JVM) 解释这个字节码。即时编译器 (JIT) 可用于将字节码转换为机器码以加速类方法的执行。
回答by Kaleb Brasee
Bytecode is the JVM equivalent of machine language instructions.
字节码是机器语言指令的 JVM 等价物。
回答by Bessie Hamilton
JVM is a virtual machine which is used to run Java code. We can compare JVM with a compiler as without it we cannot compile Java code and make applications. JVM is nothing but a piece of code that will testify your Java code. The main task of JVM is to convert the Java code into Java bytecode and compile it. This makes Java development easy. Check out this articleif you want to know more about how does Java Virtual Machine Works?
JVM 是一个虚拟机,用于运行 Java 代码。我们可以将 JVM 与编译器进行比较,因为没有它我们就无法编译 Java 代码并制作应用程序。JVM 只不过是一段可以证明您的 Java 代码的代码。JVM 的主要任务是将 Java 代码转换为 Java 字节码并进行编译。这使得 Java 开发变得容易。如果您想了解有关 Java 虚拟机如何工作的更多信息,请查看这篇文章?