JVM是编译器还是解释器?
我有一个关于 JVM 的非常基本的问题:它是编译器还是解释器?
如果是解释器,那么 JVM 内部存在的 JIT 编译器呢?
如果两者都不是,那么 JVM 到底是什么? (我不想要jVM的将字节代码转换为机器特定代码等的基本定义。)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,让我们清楚地了解以下术语:
Javac 是 Java 编译器 - 将 Java 代码编译为字节码
JVM 是 Java 虚拟机 -- 运行/解释/将字节码转换为本机机器代码
JIT 是即时编译器 -- 编译在本地执行之前,在运行时将给定的字节码指令序列转换为机器代码。其主要目的是对性能进行大量优化。
现在,让我们找到您问题的答案:
JVM:它是编译器还是解释器?
口译员
JVM内部存在的JIT编译器怎么样?
如果您完整地阅读了此回复,您现在可能已经知道了。
JVM到底是什么?
.class
文件加载到RAM中First, let's have a clear idea of the following terms:
Javac is Java Compiler -- Compiles your Java code into Bytecode
JVM is Java Virtual Machine -- Runs/ Interprets/ translates Bytecode into Native Machine Code
JIT is Just In Time Compiler -- Compiles the given bytecode instruction sequence to machine code at runtime before executing it natively. Its main purpose is to do heavy optimizations in performance.
So now, Let's find answers to your questions:
JVM: is it a compiler or an interpreter?
An interpreter
What about JIT compiler that exist inside the JVM?
If you read this reply completely, you probably know it now.
What exactly is the JVM?
.class
file into the RAM两者都有一点,但都不是传统意义上的。
现代 JVM 会在第一次需要时将字节码编译为本机代码。在这种情况下,“JIT”代表“及时”。它从外部充当解释器,但实际上在幕后它正在编译成机器代码。
不要将 JVM 与 Java 编译器混淆,后者将源代码编译为字节码。因此,将其视为“编译器”是没有用的,而是知道它在后台确实进行了一些编译。
It is a little of both, but neither in the traditional sense.
Modern JVMs take bytecode and compile it into native code when first needed. "JIT" in this context stands for "just in time." It acts as an interpreter from the outside, but really behind the scenes it is compiling into machine code.
The JVM should not be confused with the Java compiler, which compiles source code into bytecode. So it is not useful to consider it "a compiler" but rather to know that in the background it does do some compilation.
就像评论部分中已经指出的 @delnan 一样,它都不是。
JVM 是运行 Java 字节码。
JVM 有多种实现:
...和 还有很多其他。
大多数其他人在谈论 JVM 时的答案都指的是 HotSpot 或
上述方法的一些混合来实现 JVM。
Like @delnan already stated in the comment section, it's neither.
JVM is an abstract machine running Java bytecode.
JVM has several implementations:
...and many others.
Most of the others answers when talking about JVM refer either to HotSpot or
some mixture of the above approaches to implementing the JVM.
两者都是。它首先解释字节码,然后可以(如果它认为值得的话)将该字节码编译为本机机器代码。
It is both. It starts by interpreting bytecode and can (should it decide it is worth it) then compile that bytecode to native machine code.
两者都是。它可以解释字节码,并将其编译为本机代码。
It's both. It can interpret bytecode, and compile it to native code.
Javac是一个编译器,但不是传统的编译器。
编译器通常将源代码转换为机器级语言以供执行,并且这是一次性完成的,即整个代码一次性转换为机器级语言。 (更多内容见下文)。
而JavaC将其转换为字节码而不是机器级语言。
JIT 是一个 Java 编译器,但也充当解释器。典型的编译器会将所有代码立即从源代码转换为机器级语言。相反,JIT 逐行执行(逐行执行是解释器的一个功能)并将 JavaC 生成的字节码转换为字节码。转换为机器级语言并执行它。包含 JIT 的 JVM 有多种实现。 Hotspot 是 Java 编程的主要热点之一。热点实现使 JIT 通过将重复的代码块立即转换为机器级语言(如上面提到的编译器)来优化执行,以便它们可以更快地执行,而不是逐行转换每行代码。
因此,就编译器和解释器的典型定义而言,答案并不是黑白分明的。
这是我在阅读了一些在线答案、博客等后的理解。如果有人有建议来提高这种理解,请随时提出。
Javac is a compiler but not a traditional compiler.
A compiler typically converts source code to Machine level language for execution and that is done in a single shot i.e. entire code is taken and converted to machine level language at ONCE. (more on this below).
While, JavaC converts it to Bytecode instead of machine level language.
JIT is a Java compiler but also acts as an interpreter. A typical compiler will convert all the code at once from source code to machine level language. Instead, JIT goes line by line (line by line execution is a feature of Interpreters) and converts bytecode generated by JavaC into machine level language and executes it. JVM which has JIT in it has multiple implementations. Hotspot being one of the major ones for Java programming. Hotspot implementation makes JIT optimize the execution by converting chunks of code which are repetitive into Machine level language at once (like a compiler as mentioned above) so that they can be executed faster instead of converting each line of code 1 by 1.
So, the answer is not Black and White with respect to the typical definitions of Compiler and Interpreter.
This is my understanding after reading several online answers, blogs, etc. If somebody has suggestions to improve this understanding, please feel free to suggest.
JVM既有编译器又有解释器。因为编译器编译代码并生成字节码。之后解释器将字节码转换为机器可理解的代码。
示例:编写并编译一个程序并在 Windows 上运行。将 .class 文件带到另一个操作系统(Unix),它将运行,因为解释器将字节码转换为机器可理解的代码。
JVM have both compiler and interpreter. Because the compiler compiles the code and generates bytecode. After that the interpreter converts bytecode to machine understandable code.
Example: Write and compile a program and it runs on Windows. Take the .class file to another OS (Unix) and it will run because of interpreter that converts the bytecode to machine understandable code.