简体   繁体   English

JVM 是编译器还是解释器?

[英]Is the JVM a compiler or an interpreter?

I have a very basic question about JVM: is it a compiler or an interpreter?我有一个关于 JVM 的非常基本的问题:它是编译器还是解释器?

If it is an interpreter, then what about JIT compiler that exist inside the JVM?如果是解释器,那么JVM内部的JIT编译器呢?
If neither, then what exactly is the JVM?如果两者都不是,那么 JVM 究竟是什么? (I dont want the basic definition of jVM of converting byte code to machine specific code etc.) (我不想要将字节码转换为机器特定代码等的 JVM 基本定义。)

First, let's have a clear idea of the following terms首先,让我们清楚地了解以下术语

Javac is Java Compiler -- Compiles your Java code into Bytecode Javac是 Java Compiler -- 将你的 Java 代码编译成字节码

JVM is Java Virtual Machine -- Runs/ Interprets/ translates Bytecode into Native Machine Code JVM是 Java 虚拟机——运行/解释/将字节码转换为本地机器码

JIT is Just In Time Compiler -- Compiles the given bytecode instruction sequence to machine code at runtime before executing it natively. JIT是即时编译器——在本地执行之前,在运行时将给定的字节码指令序列编译为机器代码。 It's main purpose is to do heavy optimizations in performance.它的主要目的是对性能进行大量优化。

So now, Let's find answers to your questions..那么现在,让我们为您的问题寻找答案..

1) JVM: is it a compiler or an interpreter? 1) JVM: is it a compiler or an interpreter? -- Ans: Interpreter -- Ans:翻译

2) what about JIT compiler that exist inside the JVM? 2) what about JIT compiler that exist inside the JVM? -- Ans: If you read this reply completly, you probably know it now -- Ans:如果你完整地阅读了这个回复,你现在可能知道了

3) what exactly is the JVM? 3) what exactly is the JVM? -- Ans: ——答:

  • JVM is a virtual platform that resides on your RAM JVM 是驻留在 RAM 上的虚拟平台
  • Its component, Class loader loads the .class file into the RAM它的组件类加载器.class文件加载到 RAM 中
  • The Byte code Verifier component in JVM checks if there are any access restriction violations in your code. JVM 中的字节代码验证器组件会检查您的代码中是否存在任何访问限制违规。 (This is one of the principle reasons why java is secure) (这是java安全的主要原因之一)
  • Next, the Execution Engine component converts the Bytecode into executable machine code接下来,执行引擎组件将字节码转换为可执行的机器码

Hope this helped you..希望这对你有帮助..

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.现代 JVM 获取字节码,并在第一次需要时将其编译为本机代码。 "JIT" in this context stands for "just in time."在这种情况下,“JIT”代表“及时”。 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. JVM 不应与 Java 编译器混淆,后者将源代码编译为字节码。 So it is not useful to consider it "a compiler" but rather to know that in the background it does do some compilation.因此,将其视为“编译器”是没有用的,而是要知道它在后台确实进行了一些编译。

Like @delnan already stated in the comment section, it's neither .就像@delnan已经在评论部分中提到的那样,它既不是.

JVM is an abstract machine running Java bytecode . JVM是运行Java 字节码的抽象机器。

JVM has several implementations: JVM 有几种实现:

  • HotSpot (interpreter + JIT compiler) HotSpot (解释器 + JIT编译器)
  • Dalvik (interpreter + JIT compiler) Dalvik (解释器 + JIT 编译器)
  • ART ( AOT compiler + JIT compiler) ARTAOT编译器 + JIT 编译器)
  • GCJ (AOT compiler) GCJ (AOT 编译器)
  • JamVM (interpreter) JamVM (解释器)

...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.在谈论 JVM 时,大多数其他答案是指 HotSpot 或上述实现 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 is a compiler but not a traditional compiler. Javac 是一个编译器,但不是传统的编译器。 A compiler typically converts source code to Machine level language for execution and that is done in a single shot ie 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.同时,JavaC 将其转换为字节码而不是机器级语言。

JIT is a Java compiler but also acts as an interpreter. JIT 是 Java 编译器,但也充当解释器。 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.相反,JIT 逐行(逐行执行是解释器的一个特性)并将 JavaC 生成的字节码转换为机器级语言并执行它。 JVM which has JIT in it has multiple implementations.包含 JIT 的 JVM 有多种实现。 Hotspot being one of the major ones for Java programming. Hotspot 是 Java 编程的主要热点之一。 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. Hotspot 实现使 JIT 通过将重复的代码块一次转换为机器级语言(如上面提到的编译器)来优化执行,以便它们可以更快地执行,而不是一一转换每一行代码。因此,对于编译器和解释器的典型定义,答案不是黑白分明。

This is my understanding after reading several online answers, blogs, etc. If somebody has suggestions to improve this understanding, please feel free to suggest.这是我看了几个网上的回答、博客等后的理解,如果有人对提高这种理解有建议,请随时提出建议。

I have a very basic question about JVM: is it a compiler or an interpreter?我有一个关于 JVM 的非常基本的问题:它是编译器还是解释器?

If it is an interpreter, then what about JIT compiler that exist inside the JVM?如果是解释器,那么JVM内部的JIT编译器呢?
If neither, then what exactly is the JVM?如果两者都不是,那么JVM到底是什么? (I dont want the basic definition of jVM of converting byte code to machine specific code etc.) (我不想要将字节码转换为机器特定代码等的 JVM 基本定义。)

JVM have both compiler and interpreter. JVM 有编译器和解释器。 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.示例:编写并编译一个程序,它在 Windows 上运行。 Take the .class file to another OS (Unix) and it will run because of interpreter that converts the bytecode to machine understandable code.将 .class 文件带到另一个操作系统(Unix),它将运行,因为解释器将字节码转换为机器可理解的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM