在过去,我一直使用c++作为编程语言。我知道用c++写的代码要经过一个编译过程,直到它成为目标代码“机器代码”。

我想知道Java在这方面是如何工作的。用户编写的Java代码是如何被计算机运行的?


当前回答

用Java编写的代码是:

首先由一个名为javac的程序编译为字节码,如上图左侧所示; 然后,如上图右侧所示,另一个名为java的程序启动java运行时环境,它可以使用java解释器/JIT编译器编译和/或解释字节码。


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

Java既能编译也能解释,

在Java中,程序不会被编译成可执行文件;它们被编译成字节码(如前所述),然后JVM (Java虚拟机)在运行时解释/执行字节码。当我们使用javac编译器时,Java源代码被编译成字节码。字节码保存在文件扩展名为.class的磁盘上。

当程序要运行时,字节码被转换字节码可以被转换,使用即时(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.

解释型语言是一种编程语言,它的大部分实现直接而自由地执行指令,而不需要事先将程序编译成机器语言指令。解释器直接执行程序,将每个语句转换成一个或多个已经编译成机器代码的子例程序列。

编译语言是一种编程语言,它的实现通常是编译器(从源代码生成机器代码的翻译器),而不是解释器(源代码的逐步执行器,其中不发生运行前翻译)。

在Java等现代编程语言实现中,提供两种选项的平台越来越流行。

用Java编写的代码是:

首先由一个名为javac的程序编译为字节码,如上图左侧所示; 然后,如上图右侧所示,另一个名为java的程序启动java运行时环境,它可以使用java解释器/JIT编译器编译和/或解释字节码。


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实现通常使用两步编译过程。Java源代码由Java编译器编译成字节码。字节码由Java虚拟机(JVM)执行。现代jvm使用一种称为即时(JIT)编译的技术,在运行时将字节码编译为硬件CPU能够理解的本机指令。

JVM的一些实现可能会选择解释字节码,而不是JIT将其编译为机器码,然后直接运行它。虽然这仍然被认为是一个“解释器”,但它与读取和执行高级源代码的解释器有很大的不同(即,在这种情况下,Java源代码不直接解释,字节码,Java编译器的输出,被直接解释)。

从技术上讲,可以提前将Java编译成本机代码并运行生成的二进制代码。也可以直接解释Java代码。

总之,根据执行环境的不同,字节码可以是:

提前编译并作为本地代码执行(类似于大多数c++编译器) 及时编译并执行 解释 由受支持的处理器直接执行(字节码是某些cpu的本机指令集)

Java被编译成字节码,然后进入Java VM,由其解释。