Java编译器/解释器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3180456/
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
Java compiler/interpreter
提问by JavaUser
Why we do we say that Java is a compiled and interpreted language?
为什么我们说 Java 是一种编译型和解释型语言?
What is the advantage of this (being compiled and interpreted)?
这有什么好处(被编译和解释)?
采纳答案by Chris Thompson
Java is compiled to an intermediate "byte code" at compilation time. This is in contrast to a language like C that is compiled to machine language at compilation time. The Java byte code cannot be directly executed on hardware the way that compiled C code can. Instead the byte code must be interpreted by the JVM (Java Virtual Machine) at runtime in order to be executed. The primary drawback of a language like C is that when it is compiled, that binary file will only work on one particular architecture (e.g. x86).
Java 在编译时被编译为中间的“字节码”。这与在编译时编译为机器语言的 C 语言形成对比。Java 字节码不能像编译后的 C 代码那样直接在硬件上执行。相反,字节码必须在运行时由 JVM(Java 虚拟机)解释才能执行。像 C 这样的语言的主要缺点是,当它被编译时,二进制文件只能在一种特定的体系结构(例如 x86)上运行。
Interpreted languages like PHP are effectively system independent and rely on a system and architecture specific interpreter. This leads to much greater portability (the same PHP scripts work on Windows machines and Linux machines, etc.). However, this interpretation leads to a significant performance decrease. High-level languages like PHP require more time to interpret than machine-specific instructions that can be executed by the hardware.
像 PHP 这样的解释型语言实际上是独立于系统的,并且依赖于系统和架构特定的解释器。这带来了更大的可移植性(相同的 PHP 脚本可以在 Windows 机器和 Linux 机器等上运行)。但是,这种解释导致性能显着下降。与可由硬件执行的特定于机器的指令相比,像 PHP 这样的高级语言需要更多的时间来解释。
Java seeks to find a compromise between a purely compiled language (with no portability) and a purely interpreted language (that is significantly slower). It accomplishes this by compiling the code into a form that is closer to machine language (actually, Java byte code is a machine language, simply for the Java Virtual Machine), but can still be easily transported between architectures. Because Java still requires a software layer for execution (the JVM) it is an interpreted language. However, the interpreter (the JVM) operates on an intermediate form known as byte code rather than on the raw source files. This byte code is generated at compile time by the Java compiler. Therefore, Java is also a compiled language. By operating this way, Java gets some of the benefits of compiled languages, while also getting some of the benefits of interpreted languages. However, it also inherits some limitations from both of these languages.
Java 试图在纯编译语言(没有可移植性)和纯解释性语言(明显慢得多)之间找到折衷。它通过将代码编译成更接近机器语言的形式来实现这一点(实际上,Java 字节码是一种机器语言,只是为了 Java 虚拟机),但仍然可以轻松地在体系结构之间传输。因为 Java 仍然需要一个软件层来执行(JVM),所以它是一种解释性语言。但是,解释器(JVM)在称为字节码的中间形式上运行,而不是在原始源文件上运行。该字节码由 Java 编译器在编译时生成。因此,Java 也是一种编译语言。通过这种方式,Java 获得了编译语言的一些好处,同时也获得了解释型语言的一些好处。但是,它也继承了这两种语言的一些限制。
As Bozho points out, there are some strategies for increasing the performance of Java code (and other byte code languages like .Net) through the use of Just in Time (JIT) compilation. The actual process varies from implementation to implementation based on the requirements, but the end-result is that the original code is compiled into byte code at compile time, but then it is run through a compiler at runtime before it is executed. By doing this, the code can be executed at near-native speeds. Some platforms (I believe .Net does this) save the result of the JIT compilation, replacing the byte code. By doing this, all future executions of the program will execute as though the program was natively compiled from the beginning.
正如 Bozho 指出的那样,有一些策略可以通过使用即时 (JIT) 编译来提高 Java 代码(以及其他字节码语言,如 .Net)的性能。实际过程因需求而异,但最终的结果是将原始代码在编译时编译为字节码,然后在运行时通过编译器运行才执行。通过这样做,代码可以以接近本机的速度执行。一些平台(我相信 .Net 会这样做)保存 JIT 编译的结果,替换字节码。通过这样做,程序的所有未来执行都将像程序从一开始就本地编译一样执行。
回答by Bozho
回答by PeterMmm
Compiled: Your program is syntactically a correct Java program, before the program starts.
已编译:在程序启动之前,您的程序在语法上是一个正确的 Java 程序。
Interpreted: Run on different platforms the same (byte-)code.
解释:在不同平台上运行相同的(字节)代码。
Compiled: When your program has compiled correctly you can be shure to have 80% of software bugs under control. And your code will not stop because you have not correctly closed a code block, etc.
已编译:当您的程序正确编译后,您可以确保 80% 的软件错误得到控制。并且您的代码不会因为您没有正确关闭代码块等而停止。
Interpreted: You know what Applets are ? It was the "killer" application when Java came out. Your browser downloads the applet from the website and run the applet code in your browser. That is not very cool. But, the same applet runs on Windows, Linux, Macs, Solaris, ... because runs/interpreted an intermedium language: the byte code.
解读:你知道Applet是什么吗?它是 Java 出现时的“杀手级”应用程序。您的浏览器从网站下载小程序并在您的浏览器中运行小程序代码。那不是很酷。但是,同一个小程序可以在 Windows、Linux、Mac、Solaris 上运行……因为运行/解释了一种中间语言:字节码。
回答by Viele
Java is compiled, into byte code not binaries. The byte codes are not executable directly, they need the Java Virtual Machine to do a just in time compile and compile them again into machine code at runtime.
Java 被编译成字节码而不是二进制文件。字节码不能直接执行,它们需要 Java 虚拟机进行及时编译,并在运行时将它们再次编译成机器码。
At a very basic level, it separate the code that programmers write from the local machine where the JVM operates on, hence better portability. While compiling to bytecode helps the performance of just in time compile, reduce file sizes, and more or less help conceal real code. (it also eliminates some compile time error)
在非常基础的层面上,它将程序员编写的代码与 JVM 运行的本地机器分开,因此具有更好的可移植性。编译为字节码有助于及时编译的性能,减少文件大小,或多或少有助于隐藏真实代码。(它还消除了一些编译时错误)
回答by amphetamachine
Java is considered a "compiled" language because code is compiledinto bytecode format that is then run by the Java Virtual Machine (JVM). This gives several advantages in the realm of performance and code optimization, not to mention ensuring code correctness.
Java 被认为是一种“编译”语言,因为代码被编译成字节码格式,然后由 Java 虚拟机 (JVM) 运行。这在性能和代码优化领域提供了几个优势,更不用说确保代码正确性了。
It is considered an "interpreted" language because, after the bytecode is compiled, it is runnable on any machine that has a JVM installed. It is in this way that Java is much like an interpreted language in that, for the most part, it doesn't depend on the platform on which is is being run. This behavior is similar to other interpreted languages such as Perl, Python, PHP, etc.
它被认为是一种“解释型”语言,因为在编译字节码之后,它可以在任何安装了 JVM 的机器上运行。正是在这一点上,Java 很像一种解释性语言,因为在大多数情况下,它不依赖于正在运行的平台。这种行为类似于其他解释型语言,如 Perl、Python、PHP 等。
One theoretical downside to the fact that Java programs can be run on any system in absence of the source code is that, while this method of distribution ensures cross-platform compatibility, the developers have one less reason to release their source code, driving a wedge between the ideological meanings of the phrases "cross-platform" and "open source".
Java 程序可以在没有源代码的情况下在任何系统上运行这一事实的一个理论上的缺点是,虽然这种分发方法确保了跨平台兼容性,但开发人员没有任何理由来发布他们的源代码,从而推动了一个楔子介于“跨平台”和“开源”这两个词的意识形态含义之间。
回答by Pascal Thivent
Why do we say Java is compiled and interpreted language.
为什么说Java是编译型和解释型语言。
Because source code (.java
files) is compiledinto bytecode (.class
files) that is then interpretedby a Java Virtual Machine(also known as a JVM) for execution (the JVM can do further optimization but this is anoher story).
由于源代码(.java
文件)被编译成字节码(.class
文件),然后解释由Java虚拟机执行(也被称为一个JVM)(在JVM可以做进一步的优化,但是这是anoher故事)。
What is the advantage over this(being compiled/interpreted)
与此相比有什么优势(正在编译/解释)
Portability. The samebytecode can be executed on any platform as long as a JVM is installed ("compile once, run anywhere").
可移植性。在相同的字节码可以在任何平台上只要安装一个JVM执行长(“一次编译,到处运行”)。