Java 是编译型还是解释型编程语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1326071/
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
Is Java a Compiled or an Interpreted programming language ?
提问by
In the past I have used C++ as a programming language. I know that the code written in C++ goes through a compilation process until it becomes object code "machine code".
过去我使用 C++ 作为编程语言。我知道用 C++ 编写的代码经过编译过程,直到它成为目标代码“机器代码”。
I would like to know how Java works in that respect. How is the user written Java code run by the computer?
我想知道 Java 在这方面是如何工作的。用户编写的Java代码是如何在计算机上运行的?
回答by Noon Silk
Java is compiled to bytecode, which then goes into the Java VM, which interprets it.
Java 被编译为字节码,然后进入 Java 虚拟机进行解释。
回答by Sam Harwell
Java is a compiled programming language, but rather than compile straight to executable machine code, it compiles to an intermediate binary form called JVM byte code. The byte code is then compiled and/or interpreted to run the program.
Java 是一种编译型编程语言,但它不是直接编译为可执行的机器代码,而是编译为称为 JVM 字节码的中间二进制形式。然后编译和/或解释字节码以运行程序。
回答by hobbs
Java is a byte-compiled language targeting a platform called the Java Virtual Machinewhich is stack-based and has some very fast implementations on many platforms.
Java 是一种字节编译语言,面向称为Java 虚拟机的平台,该平台基于堆栈并且在许多平台上有一些非常快速的实现。
回答by maykeye
Kind of both. Firstly java compiled(some would prefer to say "translated") to bytecode, which then either compiled, or interpreted depending on mood of JIT.
两种都有。首先将java编译(有些人更喜欢说“翻译”)到字节码,然后根据JIT的心情编译或解释。
回答by Mehrdad Afshari
Java implementations typically use a two-step compilation process. Java source code is compiled down to bytecodeby the Java compiler. The bytecode is executed by a Java Virtual Machine (JVM). Modern JVMs use a technique called Just-in-Time (JIT) compilationto compile the bytecode to native instructions understood by hardware CPU on the fly at runtime.
Java 实现通常使用两步编译过程。Java 源代码由 Java 编译器编译为字节码。字节码由 Java 虚拟机 (JVM) 执行。现代 JVM 使用一种称为即时 (JIT)编译的技术将字节码编译为硬件 CPU 在运行时即时理解的本机指令。
Some implementations of JVM may choose to interpret the bytecode instead of JIT compiling it to machine code, and running it directly. While this is still considered an "interpreter," It's quite different from interpreters that read and execute the high level source code (i.e. in this case, Java source code is not interpreted directly, the bytecode, output of Java compiler, is.)
JVM 的一些实现可能会选择解释字节码,而不是 JIT 将其编译为机器代码并直接运行。虽然这仍然被认为是一个“解释器”,但它与读取和执行高级源代码的解释器完全不同(即在这种情况下,Java 源代码不是直接解释的,Java 编译器的输出字节码是。)
It is technically possible to compile Java down to native code ahead-of-time and run the resulting binary. It is also possible to interpret the Java code directly.
从技术上讲,可以提前将 Java 编译为本机代码并运行生成的二进制文件。也可以直接解释 Java 代码。
To summarize, depending on the execution environment, bytecode can be:
总而言之,根据执行环境,字节码可以是:
- compiled ahead of time and executed as native code (similar to most C++ compilers)
- compiled just-in-time and executed
- interpreted
- directly executed by a supported processor (bytecode is the native instruction set of some CPUs)
- 提前编译并作为本机代码执行(类似于大多数 C++ 编译器)
- 及时编译并执行
- 解释的
- 由支持的处理器直接执行(字节码是某些 CPU 的本机指令集)
回答by starblue
The terms "interpreted language" or "compiled language" don't make sense, because any programming language can be interpreted and/or compiled.
术语“解释语言”或“编译语言”没有意义,因为任何编程语言都可以被解释和/或编译。
As for the existing implementations of Java, most involve a compilation step to bytecode, so they involve compilation. The runtime also can load bytecode dynamically, so some form of a bytecode interpreter is always needed. That interpreter may or may not in turn use compilation to native code internally.
至于Java的现有实现,大多数都涉及到字节码的编译步骤,因此它们涉及编译。运行时也可以动态加载字节码,因此总是需要某种形式的字节码解释器。该解释器可能会也可能不会在内部使用对本机代码的编译。
These days partial just-in-time compilation is used for many languages which were once considered "interpreted", for example Javascript.
如今,部分即时编译用于许多曾经被认为是“解释型”的语言,例如 Javascript。
回答by Teo
Quotation from: https://blogs.oracle.com/ask-arun/entry/run_your_java_applications_faster
引自:https: //blogs.oracle.com/ask-arun/entry/run_your_java_applications_faster
Application developers can develop the application code on any of the various OS that are available in the market today. Java language is agnostic at this stage to the OS. The brilliant source code written by the Java Application developer now gets compiled to Java Byte code which in the Java terminology is referred to as Client Side compilation. This compilation to Java Byte code is what enables Java developers to ‘write once'. Java Byte code can run on any compatible OS and server, hence making the source code agnostic of OS/Server. Post Java Byte code creation, the interaction between the Java application and the underlying OS/Server is more intimate. The journey continues - The enterprise applications framework executes these Java Byte codes in a run time environment which is known as Java Virtual Machine (JVM) or Java Runtime Environment (JRE). The JVM has close ties to the underlying OS and Hardware because it leverages resources offered by the OS and the Server. Java Byte code is now compiled to a machine language executable code which is platform specific. This is referred to as Server side compilation.
应用程序开发人员可以在当今市场上可用的各种操作系统中的任何一种上开发应用程序代码。Java 语言在此阶段与操作系统无关。由 Java 应用程序开发人员编写的精彩源代码现在被编译为 Java 字节代码,在 Java 术语中称为客户端编译。这种对 Java 字节代码的编译使 Java 开发人员能够“编写一次”。Java 字节代码可以在任何兼容的操作系统和服务器上运行,从而使源代码与操作系统/服务器无关。Java Byte代码创建后,Java应用与底层OS/Server的交互更加亲密。旅程还在继续——企业应用程序框架在称为 Java 虚拟机 (JVM) 或 Java 运行时环境 (JRE) 的运行时环境中执行这些 Java 字节代码。JVM 与底层操作系统和硬件有着密切的联系,因为它利用了操作系统和服务器提供的资源。Java 字节代码现在被编译为特定于平台的机器语言可执行代码。这称为服务器端编译。
So I would say Java is definitely a compiled language.
所以我会说Java绝对是一种编译语言。
回答by displayName
Code written in Java is:
用Java编写的代码是:
- First compiledto bytecode by a program called javacas shown in the left section of the image above;
- Then, as shown in the right section of the above image, another program called javastarts the Java runtime environment and it may compile and/or interpretthe bytecode by using the Java Interpreter/JIT Compiler.
- 首先由名为javac的程序编译为字节码,如上图左侧所示;
- 然后,如上图的右侧部分所示,另一个名为java 的程序启动 Java 运行时环境,它可以使用 Java Interpreter/JIT Compiler编译和/或解释字节码。
When does java interpret the bytecode and when does it compile it?The application code is initially interpreted, but the JVM monitors which sequences of bytecode are frequently executed and translates them to machine code for direct execution on the hardware. For bytecode which is executed only a few times, this saves the compilation time and reduces the initial latency; for frequently executed bytecode, JIT compilation is used to run at high speed, after an initial phase of slow interpretation. Additionally, since a program spends most time executing a minority of its code, the reduced compilation time is significant. Finally, during the initial code interpretation, execution statistics can be collected before compilation, which helps to perform better optimization.
java什么时候解释字节码,什么时候编译它?应用程序代码最初被解释,但 JVM 监控哪些字节码序列被频繁执行,并将它们转换为机器代码,以便在硬件上直接执行。对于只执行几次的字节码,这节省了编译时间并减少了初始延迟;对于频繁执行的字节码,JIT 编译用于在缓慢解释的初始阶段之后高速运行。此外,由于程序花费大部分时间执行其少数代码,因此减少的编译时间很重要。最后,在初始代码解释时,可以在编译前收集执行统计信息,有助于进行更好的优化。
回答by prime
Java does both compilation and interpretation,
Java同时进行编译和解释,
In Java, programs are not compiled into executable files; they are compiled into bytecode(as discussed earlier), which the JVM (Java Virtual Machine) then interprets / executes at runtime. Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on the disk with the file extension .class.
在 Java 中,程序不会被编译成可执行文件;它们被编译成字节码(如前所述),然后由 JVM(Java 虚拟机)在运行时解释/执行。当我们使用 javac 编译器时,Java 源代码被编译成字节码。字节码以文件扩展名 .class 保存在磁盘上。
When the program is to be run, the bytecode is convertedthe bytecode maybe converted, using the just-in-time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.
当程序要运行时,字节码被转换,字节码可以被转换,使用即时(JIT)编译器。结果是机器代码,然后将其送入内存并执行。
Javacis the Java Compilerwhich Compiles Java code into Bytecode. JVM is Java Virtual Machine which Runs/ Interprets/ translates Bytecode into Native Machine Code. In Java though it is considered as an interpreted language, It may use JIT (Just-in-Time) compilation when the bytecode is in the JVM. The JIT compiler reads the bytecodes in many sections (or in full, rarely) and compiles them dynamically into machine code so the program can run faster, and then cached and reused later without needing to be recompiled. So JIT compilation combines the speed of compiled code with the flexibility of interpretation.
Javac是Java Compiler,它将 Java 代码编译成字节码。JVM 是 Java 虚拟机,它运行/解释/将字节码转换为本地机器代码。在 Java 中,虽然它被认为是一种解释型语言,但当字节码在 JVM 中时,它可能会使用 JIT(即时)编译。JIT 编译器读取许多部分(或完整,很少)中的字节码,并将它们动态编译成机器代码,以便程序可以更快地运行,然后缓存并在以后重用,无需重新编译。所以 JIT 编译结合了编译代码的速度和解释的灵活性。
An interpreted languageis a type of programming language for which most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions. The interpreter executes the program directly, translating each statement into a sequence of one or more subroutines already compiled into machine code.
一个解释语言是一种类型的编程语言,其中大部分其实现方式的直接和自由地执行指令,而无需先前编译程序成机器语言指令。解释器直接执行程序,将每条语句翻译成一系列已经编译成机器码的一个或多个子程序。
A compiled languageis a programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters (step-by-step executors of source code, where no pre-runtime translation takes place)
甲编译语言是一种编程语言,其实现方式是通常的编译器(即从源代码生成机器代码转换器),而不是解释(步骤一步的源代码执行者,其中没有预运行时转换发生)
In modern programming language implementations like in Java, it is increasingly popular for a platform to provide both options.
在像 Java 这样的现代编程语言实现中,提供这两种选择的平台越来越受欢迎。