当 JIT 也编译字节码时,为什么 Java 既是编译型语言又是解释型语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20880716/
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
Why Java is both compiled and interpreted language when the JIT also compiles the bytecode?
提问by Ragul
I read that, a java source code is compiled into 'bytecode' then it is 'Compiled' again by JIT into 'machine code'. That is, the source code is first compiled into a platform independent bytecode and then compiled again to a machine specific code. Then why it is called as both interpreted and compiled language? Where the interpretation takes place?
我读到,Java 源代码被编译成“字节码”,然后它被 JIT 再次“编译”成“机器代码”。也就是说,源代码首先被编译成与平台无关的字节码,然后再次编译成机器特定的代码。那么为什么它被称为解释型和编译型语言呢?解释发生在哪里?
采纳答案by Aniket Thakur
There is a bit of misunderstanding here.
这里有一点误解。
In normal circumstances java compiler(javac
) compiles java code to bytecodes and java interpreter(java
) interpretes these bytecodes(line by line), convert it into machine language and execute.
在正常情况下,java compiler( javac
)将java代码编译成字节码,java interpreter( java
)解释这些字节码(逐行),将其转换成机器语言并执行。
JIT(Just in time)
compiler is a bit different concept. JVM maintains a count of times a function is executed. If it exceeds the limit then JIT comes into picture. java code is directly compiled into machine language and there on this is used to execute that function.
JIT(Just in time)
编译器是一个有点不同的概念。JVM 维护一个函数执行的次数。如果它超过了限制,那么 JIT 就会出现。java代码被直接编译成机器语言,并在那里用于执行该功能。
回答by Jorge_B
For the sake of precision, let's make clear this is not a Java programming language question, but a JVM feature.
为了精确起见,让我们明确这不是 Java 编程语言问题,而是 JVM 特性。
In JVM first implementations, JIT didn't exist and bytecode was always interpreted. This was due to a design decision to make compiled code independent of the physical machine and OS running java, and is still valid today.
在 JVM 第一个实现中,JIT 不存在并且字节码总是被解释。这是由于设计决定使编译后的代码独立于运行 java 的物理机器和操作系统,并且今天仍然有效。
As a later refination, JIT was introduced in the JVM implementation for a faster execution, but the bytecode must still be valid and pass all the validations before being translated to binary. This way you keep the platform independence, all the sanity and security checks and you gain performance.
作为后来的改进,在 JVM 实现中引入了 JIT 以加快执行速度,但字节码必须仍然有效并在转换为二进制之前通过所有验证。通过这种方式,您可以保持平台独立性、所有健全性和安全性检查,并获得性能。
回答by Benjamin Gruenbaum
Java is a programming language.
Java 是一种编程语言。
It has a specification (the JLS) that defines how Java programs should act.
它有一个规范(JLS)来定义 Java 程序应该如何运作。
As a language itself, it does not specify how it should be executed on different platforms. The way it runs, with a JIT or without a JIT is entirely implementation based.
作为一种语言本身,它没有指定它应该如何在不同平台上执行。它运行的方式,有 JIT 或没有 JIT,完全基于实现。
If I write a Java runtime tomorrow that does not do JIT compilation at all I can call Java interpreted.
If I take a Java machine (and people seriously made those) that uses Java bytecode as assembly, I can call Java strictly compiled.
如果我明天编写一个完全不进行 JIT 编译的 Java 运行时,我可以调用 Java 解释。
如果我拿一台使用 Java 字节码作为程序集的 Java 机器(并且人们认真地制造了那些机器),我可以称 Java 为严格编译的。
A lot of other languages do this:
许多其他语言都这样做:
- Is python an interpreted language? (CPython) or is it JITed (PyPy)?
- Is Lua interpreted (old lua interpreters) or is it compiled (LuaJIT)?
- Is JavaScript interpreted (IE6 style) or is it compiled (v8)?
- python是解释型语言吗?(CPython) 还是 JITed (PyPy)?
- Lua 是解释的(旧的 lua 解释器)还是编译的(LuaJIT)?
- JavaScript 是解释的(IE6 风格)还是编译的(v8)?
回答by Chris Mantle
It serves two purposes. The first is to ensure that the code is syntactically and semantically correct. Secondly, the compilation process produces byte-code. As you note, this is an architecture-agnostic intermediate language that can be interpreted or just-in-time compiled to native code by the JVM for a specific machine architecture. By compiling to byte-code, much of the overhead associated with compilation can be done in advance, leaving the JVM to generate native code from or interpret byte-code that has been thoroughly and rigorously checked beforehand.
它有两个目的。首先是确保代码在语法和语义上是正确的。其次,编译过程产生字节码。正如您所注意到的,这是一种与体系结构无关的中间语言,可以由 JVM 解释或即时编译为特定机器体系结构的本机代码。通过编译为字节码,与编译相关的大部分开销可以提前完成,让 JVM 生成本地代码或解释事先经过彻底和严格检查的字节码。
回答by S. Panwar
javac is a compiler and it converts java code into bytecode (see bytecode) which is easy to run on any machine if we have a JVM (java Virtual Machine). and interpreter converts java bytecode into machine code.
javac 是一个编译器,它将 java 代码转换为字节码(请参阅字节码),如果我们有 JVM(java 虚拟机),它很容易在任何机器上运行。和解释器将 java 字节码转换为机器码。
回答by Premraj
Java is Hybrid Language i.e. it is both Compiled(work done upfront) and Interpreted(work done receiving-end).
Java 是混合语言,即它既是编译的(预先完成的工作)又是解释的(完成接收端的工作)。
Byte code is an IL(Intermediate Language) to Java. Java source code compiles to Bytecode by javac
. Sometimes this byte code again compiles into Machine language which is referred as JIT(Just-In-Time) compilation.
字节代码是IL(我ntermediate大号anguage)到Java。Java 源代码通过javac
. 有时,这种字节码再次编译成被称为JIT机器语言(Ĵust-我正ŤIME)编译。
JIT compilation is a way of executing computer code that involves compilation during execution of a program – at run time – rather than prior to execution. source
JIT 编译是一种执行计算机代码的方式,它涉及在程序执行期间(在运行时)而不是在执行之前进行编译。来源
JVM(without JIT)interprets the java Intermediate Language byte code to native machine language as follows:
JVM(无 JIT)将 java 中间语言字节码解释为本地机器语言,如下所示:
JVM is an abstract computing machine, it has several implementations:
JVM 是一个抽象的计算机器,它有几个实现:
HotSpot(Interpreter + JIT compiler) : the primary reference Java VM implementation. Used by both Oracle Java and OpenJDK.
JamVM(Interpreter) Developed to be an extremely small virtual machine compared to others. Designed to use GNU Classpath. Supports several architectures. GPL.
ART(Interpreter + AOT compiler i.e. Ahead-of-time compilation) Android RunTime is an application runtime environment used by the Android operating system replacing Dalvik(interpreter + JIT compiler).
HotSpot(解释器 + JIT 编译器):主要参考 Java VM 实现。由 Oracle Java 和 OpenJDK 使用。
JamVM(解释器) 与其他虚拟机相比,它被开发为一个非常小的虚拟机。旨在使用 GNU 类路径。支持多种架构。通用公共许可证。
ART(口译+ AOT编译即超前-的即时编译)甲ndroid ř未ŤIME是由Android操作系统替换使用的应用程序的运行时环境的Dalvik(解释+ JIT编译器)。
回答by Ashwin J Chhetri
Unlike other programming language java is compiled and interpreted language. Java IDE acts as a compiler and JVM(java virtual machine) behave like an interpreter. i.e. when any program let say Hello, is saved after compiling as Hello.java and after compiling this file we get Hello.Class extension file is called as class-file, byte-code or intermediate code. Byte-code is not dependent for any specific machine so it is also called as intermediate code. To convert this byte-code into machine code or machine understandable format JVM is used which is different for different operating system. JIT(Just in Time Compiler) is a part of JVM that is enabled by default compiles the bytecode into the native machine code compiling in 'just in time'.
与其他编程语言不同,java 是编译型和解释型语言。Java IDE 充当编译器,而 JVM(Java 虚拟机)的行为类似于解释器。即当任何程序让我们说 Hello 时,在编译为 Hello.java 后保存,编译此文件后我们得到 Hello.Class 扩展文件,称为类文件、字节码或中间代码。字节码不依赖于任何特定机器,因此也称为中间码。将此字节码转换为机器码或机器可理解的格式使用JVM,这对于不同的操作系统是不同的。JIT(Just in Time Compiler)是 JVM 的一部分,默认情况下启用将字节码编译为“及时”编译的本机机器代码。