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

时间:2021-02-22 20:43:28

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代码?

9 个解决方案

#1


154  

Java implementations typically use a two-step compilation process. Java source code is compiled down to bytecode by the Java compiler. The bytecode is executed by a Java Virtual Machine (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.

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)
  • 提前编译并作为本机代码执行(类似于大多数c++编译器)
  • compiled just-in-time and executed
  • 即时编译和执行
  • interpreted
  • 解释
  • directly executed by a supported processor (bytecode is the native instruction set of some CPUs)
  • 由支持的处理器直接执行(字节码是一些cpu的本机指令集)

#2


45  

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。

#3


39  

[1]Image below explains it all...

下面的[1]图片解释了这一切……

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

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, [2]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.
  • 然后,如上图右侧部分所示,[2]另一个名为java的程序启动java运行时环境,它可以使用java解释器/JIT编译器编译和/或解释字节码。

When does java interpret the bytecode and when does it compile it? [3]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编译用于在慢速解释的初始阶段之后以高速运行。此外,由于程序在执行少数代码时花费的时间最多,因此编译时间的缩短是非常重要的。最后,在初始代码解释期间,可以在编译之前收集执行统计信息,这有助于更好地执行优化。


Click on the superscripted numbers in the answer for references.

单击“引用答案”中的上标数字。

#4


37  

Java is compiled to bytecode, which then goes into the Java VM, which interprets it.

Java被编译为字节码,然后进入Java VM中,Java VM对其进行解释。

#5


11  

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

#6


10  

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字节代码的中间二进制形式。然后编译和/或解释字节代码以运行程序。

#7


0  

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 converted the bytecode may be converted, using the just-in-time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.

当程序要运行时,字节码被转换为字节码,可以使用即时(JIT)编译器进行转换。结果是机器代码,然后输入内存并执行。

Javac is the Java Compiler which 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代码编译成字节码的Java编译器。JVM是Java虚拟机,它运行/解释/翻译字节码到本机代码。在Java中,尽管它被认为是一种解释语言,但当字节码在JVM中时,它可以使用JIT(即时)编译。JIT编译器在许多部分(或全部,很少)中读取字节码,然后动态地将它们编译成机器码,这样程序就可以运行得更快,然后缓存并在以后重用,而不需要重新编译。因此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这样的现代编程语言实现中,提供两个选项的平台越来越流行。

#8


-1  

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虚拟机的平台为目标的字节编译语言,它是基于堆栈的,在许多平台上有一些非常快速的实现。

#9


-2  

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字节代码可以在任何兼容的OS和服务器上运行,因此OS/ server的源代码是不可知的。Java字节代码创建后,Java应用程序与底层OS/服务器之间的交互更加密切。这个过程还在继续——企业应用程序框架在运行时环境(Java Virtual Machine, JVM)或Java运行时环境(Java Runtime environment, JRE)中执行这些Java字节代码。JVM与底层操作系统和硬件有着密切的联系,因为它利用了操作系统和服务器提供的资源。Java字节代码现在被编译成一种机器语言的可执行代码,它是特定于平台的。这称为服务器端编译。

So I would say Java is definitely a compiled language.

所以我认为Java绝对是一种编译语言。

#1


154  

Java implementations typically use a two-step compilation process. Java source code is compiled down to bytecode by the Java compiler. The bytecode is executed by a Java Virtual Machine (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.

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)
  • 提前编译并作为本机代码执行(类似于大多数c++编译器)
  • compiled just-in-time and executed
  • 即时编译和执行
  • interpreted
  • 解释
  • directly executed by a supported processor (bytecode is the native instruction set of some CPUs)
  • 由支持的处理器直接执行(字节码是一些cpu的本机指令集)

#2


45  

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。

#3


39  

[1]Image below explains it all...

下面的[1]图片解释了这一切……

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

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, [2]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.
  • 然后,如上图右侧部分所示,[2]另一个名为java的程序启动java运行时环境,它可以使用java解释器/JIT编译器编译和/或解释字节码。

When does java interpret the bytecode and when does it compile it? [3]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编译用于在慢速解释的初始阶段之后以高速运行。此外,由于程序在执行少数代码时花费的时间最多,因此编译时间的缩短是非常重要的。最后,在初始代码解释期间,可以在编译之前收集执行统计信息,这有助于更好地执行优化。


Click on the superscripted numbers in the answer for references.

单击“引用答案”中的上标数字。

#4


37  

Java is compiled to bytecode, which then goes into the Java VM, which interprets it.

Java被编译为字节码,然后进入Java VM中,Java VM对其进行解释。

#5


11  

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

#6


10  

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字节代码的中间二进制形式。然后编译和/或解释字节代码以运行程序。

#7


0  

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 converted the bytecode may be converted, using the just-in-time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.

当程序要运行时,字节码被转换为字节码,可以使用即时(JIT)编译器进行转换。结果是机器代码,然后输入内存并执行。

Javac is the Java Compiler which 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代码编译成字节码的Java编译器。JVM是Java虚拟机,它运行/解释/翻译字节码到本机代码。在Java中,尽管它被认为是一种解释语言,但当字节码在JVM中时,它可以使用JIT(即时)编译。JIT编译器在许多部分(或全部,很少)中读取字节码,然后动态地将它们编译成机器码,这样程序就可以运行得更快,然后缓存并在以后重用,而不需要重新编译。因此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这样的现代编程语言实现中,提供两个选项的平台越来越流行。

#8


-1  

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虚拟机的平台为目标的字节编译语言,它是基于堆栈的,在许多平台上有一些非常快速的实现。

#9


-2  

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字节代码可以在任何兼容的OS和服务器上运行,因此OS/ server的源代码是不可知的。Java字节代码创建后,Java应用程序与底层OS/服务器之间的交互更加密切。这个过程还在继续——企业应用程序框架在运行时环境(Java Virtual Machine, JVM)或Java运行时环境(Java Runtime environment, JRE)中执行这些Java字节代码。JVM与底层操作系统和硬件有着密切的联系,因为它利用了操作系统和服务器提供的资源。Java字节代码现在被编译成一种机器语言的可执行代码,它是特定于平台的。这称为服务器端编译。

So I would say Java is definitely a compiled language.

所以我认为Java绝对是一种编译语言。