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

发布于 2024-08-02 08:11:46 字数 112 浏览 4 评论 0原文

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

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

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".

I would like to know how Java works in that respect. How is the user written Java code run by the computer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

断肠人 2024-08-09 08:11:46

Java 实现通常使用两步编译过程。 Java 源代码由 Java 编译器编译为字节码。字节码由 Java 虚拟机 (JVM) 执行。现代 JVM 使用一种名为即时 (JIT) 编译 的技术来进行编译硬件 CPU 在运行时动态理解的字节码到本机指令。

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

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

总而言之,根据执行环境,字节码可以:

  • 提前编译并作为本机代码执行(类似于大多数 C++ 编译器)
  • 即时编译并执行
  • 直接由支持的处理器解释
  • 执行(字节码是本机指令一些CPU的集合)

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.

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.)

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.

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)
情感失落者 2024-08-09 08:11:46

输入图像描述这里

用 Ja​​va 编写的代码是:

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

java什么时候解释字节码,什么时候编译它?应用程序代码最初被解释,但是JVM监视哪些字节码序列被频繁执行,并将它们翻译成机器代码以便在硬件上直接执行。对于只执行几次的字节码,这可以节省编译时间并减少初始延迟;对于频繁执行的字节码,JIT编译用于在初始阶段的缓慢解释之后高速运行。此外,由于程序花费大部分时间执行其少数代码,因此编译时间的减少是显着的。最后,在初始代码解释期间,可以在编译之前收集执行统计信息,这有助于执行更好的优化。

enter image description here

Code written in Java is:

  • First compiled to bytecode by a program called javac as shown in the left section of the image above;
  • 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.

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.

記柔刀 2024-08-09 08:11:46

术语“解释型语言”或“编译型语言”没有意义,因为任何编程语言都可以被解释和/或编译。

至于现有的 Java 实现,大多数都涉及到字节码的编译步骤,所以它们涉及到编译。运行时还可以动态加载字节码,因此始终需要某种形式的字节码解释器。
该解释器可能会也可能不会在内部使用编译为本机代码。

如今,部分即时编译已用于许多曾经被认为是“解释型”的语言,例如 JavaScript。

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.

These days partial just-in-time compilation is used for many languages which were once considered "interpreted", for example JavaScript.

诺曦 2024-08-09 08:11:46

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

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

云之铃。 2024-08-09 08:11:46

Java 是一种编译型编程语言,但它不是直接编译为可执行机器代码,而是编译为称为 JVM 字节码的中间二进制形式。然后编译和/或解释字节代码以运行程序。

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.

无边思念无边月 2024-08-09 08:11:46

两者兼而有之。首先,java 被编译(有些人更喜欢说“翻译”)为字节码,然后根据 JIT 的情绪进行编译或解释。

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.

少钕鈤記 2024-08-09 08:11:46

Java既进行编译又进行解释,

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

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

Javac 是将 Java 代码编译为字节码的 Java 编译器。 JVM 是 Java 虚拟机,它运行/解释/将字节码转换为本机机器代码。在Java中,虽然它被认为是一种解释性语言,但当字节码位于JVM中时,它可能会使用JIT(Just-in-Time)编译。 JIT 编译器读取许多部分(或完整的,很少)的字节码,并将它们动态编译为机器代码,以便程序可以运行得更快,然后缓存并在以后重用,而无需重新编译。因此 JIT 编译将编译代码的速度与解释的灵活性结合起来。

解释语言是一种编程语言,其大部分内容实现直接、自由地执行指令,无需事先将程序编译成机器语言指令。解释器直接执行程序,将每个语句翻译成一系列已经编译成机器代码的一个或多个子例程。

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

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

Java does both compilation and interpretation,

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.

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.

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.

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.

泪眸﹌ 2024-08-09 08:11:46

Java 是一种字节编译语言,针对名为 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.

如果没结果 2024-08-09 08:11:46

引用自: https://blogs.oracle.com/ask-arun/entry/run_your_java_applications_faster< /a>

应用程序开发人员可以在当今市场上提供的任何各种操作系统上开发应用程序代码。 Java 语言在这个阶段与操作系统无关。 Java 应用程序开发人员编写的出色源代码现在被编译为 Java 字节代码,在 Java 术语中称为客户端编译。这种对 Java 字节代码的编译使得 Java 开发人员能够“编写一次”。 Java 字节代码可以在任何兼容的操作系统和服务器上运行,因此使源代码与操作系统/服务器无关。 Java字节码创建后,Java应用程序和底层操作系统/服务器之间的交互更加密切。旅程仍在继续 - 企业应用程序框架在称为 Java 虚拟机 (JVM) 或 Java 运行时环境 (JRE) 的运行时环境中执行这些 Java 字节代码。 JVM 与底层操作系统和硬件有着密切的联系,因为它利用操作系统和服务器提供的资源。 Java 字节代码现在被编译为特定于平台的机器语言可执行代码。这称为服务器端编译。

所以我想说Java绝对是一种编译语言。

Quotation from: 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.

So I would say Java is definitely a compiled language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文