简体   繁体   English

Java 是编译型还是解释型编程语言?

[英]Is Java a Compiled or an Interpreted programming language ?

In the past I have used C++ as a programming language.过去,我使用 C++ 作为编程语言。 I know that the code written in C++ goes through a compilation process until it becomes object code "machine code".我知道用 C++ 编写的代码要经过编译过程,直到成为目标代码“机器代码”。

I would like to know how Java works in that respect.我想知道 Java 在这方面是如何工作的。 How is the user written Java code run by the computer?用户编写的Java代码是如何被计算机运行的?

Java implementations typically use a two-step compilation process. Java 实现通常使用两步编译过程。 Java source code is compiled down to bytecode by the Java compiler. Java 源代码由 Java 编译器编译为字节码 The bytecode is executed by a Java Virtual Machine (JVM).字节码由 Java 虚拟机 (JVM) 执行。 Modern JVMs use a technique called Just-in-Time (JIT) compilation to compile the bytecode to native instructions understood by hardware CPU on the fly at runtime.现代 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. JVM 的某些实现可能会选择解释字节码而不是 JIT 将其编译为机器码并直接运行。 While this is still considered an "interpreter," It's quite different from interpreters that read and execute the high level source code (ie in this case, Java source code is not interpreted directly, the bytecode, output of Java compiler, is.)虽然这仍然被认为是一个“解释器”,但它与读取和执行高级源代码的解释器有很大不同(即在这种情况下,Java 源代码不直接解释,字节码,Java 编译器的输出,是。)

It is technically possible to compile Java down to native code ahead-of-time and run the resulting binary.从技术上讲,可以提前将 Java 编译为本机代码并运行生成的二进制文件。 It is also possible to interpret the Java code directly.也可以直接解释 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)提前编译并作为本地代码执行(类似于大多数 C++ 编译器)
  • compiled just-in-time and executed即时编译并执行
  • interpreted解释的
  • directly executed by a supported processor (bytecode is the native instruction set of some CPUs)由支持的处理器直接执行(字节码是某些 CPU 的本机指令集)

在此处输入图像描述

Code written in Java is:用Java编写的代码是:

  • First compiled to bytecode by a program called javac as shown in the left section of the image above;如上图左侧所示,首先由名为javac的程序编译为字节码;
  • Then, as shown in the right section of the above image, another program called java starts the Java runtime environment and it may compile and/or interpret the bytecode by using the Java Interpreter/JIT Compiler.然后,如上图右侧所示,另一个名为java的程序启动 Java 运行环境,它可以使用 Java Interpreter/JIT Compiler编译和/或解释字节码。

When does java interpret the bytecode and when does it compile it? java什么时候解释字节码,什么时候编译它? 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.应用程序代码最初是被解释的,但是 JVM 监视哪些字节码序列被频繁执行并将它们翻译成机器代码以便在硬件上直接执行。 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.对于频繁执行的字节码,JIT 编译用于高速运行,经过初始阶段的缓慢解释。 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.最后,在初始代码解释期间,可以在编译前收集执行统计信息,这有助于进行更好的优化。

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.至于Java现有的实现,大多涉及到字节码的编译步骤,所以涉及到编译。 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.该解释器可能会也可能不会在内部使用对本机代码的编译。

These days partial just-in-time compilation is used for many languages which were once considered "interpreted", for example JavaScript.如今,部分即时编译被用于许多曾经被认为是“解释型”的语言,例如 JavaScript。

Java is compiled to bytecode, which then goes into the Java VM, which interprets it. Java 被编译为字节码,然后进入解释它的 Java VM。

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. Java 是一种编译型编程语言,但它不是直接编译为可执行的机器代码,而是编译为称为 JVM 字节码的中间二进制形式。 The byte code is then compiled and/or interpreted to run the program.然后编译和/或解释字节代码以运行程序。

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 的心情编译或解释。

Java does both compilation and interpretation, Java既做编译又做解释,

In Java, programs are not compiled into executable files ;在 Java 中,程序不会编译成可执行文件 they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then interprets / executes at runtime.它们被编译成字节码(如前所述),然后由 JVM(Java 虚拟机)在运行时解释/执行。 Java source code is compiled into bytecode when we use the javac compiler.当我们使用 javac 编译器时,Java 源代码被编译成字节码。 The bytecode gets saved on the disk with the file extension.class .字节码以文件扩展名 .class 保存在磁盘上

When the program is to be run, the bytecode is converted the bytecode may be converted, using the just-in-time (JIT) compiler.当程序要运行时, 字节码被转换 ,字节码可能被转换,使用即时(JIT)编译器。 The result is machine code which is then fed to the memory and is executed.结果是机器代码,然后将其送入内存并执行。

Javac is the Java Compiler which Compiles Java code into Bytecode. Javac是将 Java 代码编译成字节码的Java 编译器 JVM is Java Virtual Machine which Runs/ Interprets/ translates Bytecode into Native Machine Code. JVM 是 Java 虚拟机,它运行/解释/将字节码翻译成本机机器码。 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.在 Java 中,虽然它被认为是一种解释型语言,但当字节码在 JVM 中时,它可能会使用 JIT(即时)编译。 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. JIT 编译器读取许多部分(或完整,很少)的字节码,并将它们动态编译为机器代码,以便程序运行更快,然后缓存并在以后重用而无需重新编译。 So JIT compilation combines the speed of compiled code with the flexibility of interpretation.所以 JIT 编译结合了编译代码的速度和解释的灵活性。

An interpreted language is 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 language is 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 等现代编程语言实现中,提供这两种选项的平台越来越受欢迎。

1- Java is a compiled language. 1-Java 是一种编译语言。 2- Compiler is a part of Java development kit JDK. 2- Compiler 是 Java 开发工具包 JDK 的一部分。 3- Compiler in Java first checks for any syntax error in source code, and then if your source code has no errors the compiler starts converting it to a byte code. 3- Java 编译器首先检查源代码中的任何语法错误,然后如果您的源代码没有错误,编译器就开始将其转换为字节码。 4- Java virtual machine JVM takes the byte code and convert it to an executable machine code that runs on hardware and gives the program output to the user. 4- Java 虚拟机 JVM 获取字节码并将其转换为在硬件上运行的可执行机器码,并将程序输出提供给用户。 5- For more explanation please check this How Java Program Works 5- 有关更多解释,请查看Java 程序如何工作

Java is a byte-compiled language targeting a platform called the Java Virtual Machine which is stack-based and has some very fast implementations on many platforms. Java 是一种字节编译语言,目标平台称为Java 虚拟机,它是基于堆栈的,并且在许多平台上都有一些非常快速的实现。

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. Java 语言在这个阶段对于操作系统是不可知的。 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.由 Java 应用程序开发人员编写的出色源代码现在被编译为 Java 字节代码,在 Java 术语中称为客户端编译。 This compilation to Java Byte code is what enables Java developers to 'write once'.这种对 Java 字节代码的编译使 Java 开发人员能够“编写一次”。 Java Byte code can run on any compatible OS and server, hence making the source code agnostic of OS/Server. Java 字节代码可以在任何兼容的操作系统和服务器上运行,因此使源代码与操作系统/服务器无关。 Post Java Byte code creation, the interaction between the Java application and the underlying OS/Server is more intimate.在创建 Java 字节代码后,Java 应用程序与底层操作系统/服务器之间的交互更加密切。 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).旅程继续 - 企业应用程序框架在称为 Java 虚拟机 (JVM) 或 Java 运行时环境 (JRE) 的运行时环境中执行这些 Java 字节代码。 The JVM has close ties to the underlying OS and Hardware because it leverages resources offered by the OS and the Server. JVM 与底层操作系统和硬件有着密切的联系,因为它利用了操作系统和服务器提供的资源。 Java Byte code is now compiled to a machine language executable code which is platform specific. Java 字节代码现在被编译为特定于平台的机器语言可执行代码。 This is referred to as Server side compilation.这称为服务器端编译。

So I would say Java is definitely a compiled language.所以我会说 Java 绝对是一种编译语言。

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

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