JVM是编译器还是解释器?

发布于 2024-12-08 14:59:15 字数 134 浏览 0 评论 0 原文

我有一个关于 JVM 的非常基本的问题:它是编译器还是解释器?

如果是解释器,那么 JVM 内部存在的 JIT 编译器呢?
如果两者都不是,那么 JVM 到底是什么? (我不想要jVM的将字节代码转换为机器特定代码等的基本定义。)

I have a very basic question about JVM: is it a compiler or an interpreter?

If it is an interpreter, then what about JIT compiler that exist inside the JVM?
If neither, then what exactly is the JVM? (I dont want the basic definition of jVM of converting byte code to machine specific code etc.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉城 2024-12-15 14:59:15

首先,让我们清楚地了解以下术语:

  • Javac 是 Java 编译器 - 将 Java 代码编译为字节码

  • JVM 是 Java 虚拟机 -- 运行/解释/将字节码转换为本机机器代码

  • JIT 是即时编译器 -- 编译在本地执行之前,在运行时将给定的字节码指令序列转换为机器代码。其主要目的是对性能进行大量优化。

现在,让我们找到您问题的答案:

  1. JVM:它是编译器还是解释器?

    口译员

  2. JVM内部存在的JIT编译器怎么样?

    如果您完整地阅读了此回复,您现在可能已经知道了。

  3. JVM到底是什么?

    • JVM 是驻留在 RAM 上的虚拟平台
    • 其组件类加载器.class文件加载到RAM中
    • JVM 中的字节码验证器组件会检查代码中是否存在任何访问限制违规。 (这是java安全的主要原因之一)
    • 接下来,执行引擎组件将字节码转换为可执行的机器代码

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:

  1. JVM: is it a compiler or an interpreter?

    An interpreter

  2. What about JIT compiler that exist inside the JVM?

    If you read this reply completely, you probably know it now.

  3. What exactly is the JVM?

    • JVM is a virtual platform that resides on your RAM
    • Its component, Class loader loads the .class file into the RAM
    • The Byte code Verifier component in JVM checks if there are any access restriction violations in your code. (This is one of the principal reasons why java is secure)
    • Next, the Execution Engine component converts the Bytecode into executable machine code
人疚 2024-12-15 14:59:15

两者都有一点,但都不是传统意义上的。

现代 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.

心作怪 2024-12-15 14:59:15

就像评论部分中已经指出的 @delnan 一样,它都不是

JVM 是运行 Java 字节码

JVM 有多种实现:

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

...和 ​​还有很多其他

大多数其他人在谈论 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.

情栀口红 2024-12-15 14:59:15

两者都是。它首先解释字节码,然后可以(如果它认为值得的话)将该字节码编译为本机机器代码。

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.

唔猫 2024-12-15 14:59:15

两者都是。它可以解释字节码,并将其编译为本机代码。

It's both. It can interpret bytecode, and compile it to native code.

烂人 2024-12-15 14:59:15

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.

可是我不能没有你 2024-12-15 14:59:15

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文