Java编译器/解释器

发布于 2024-09-08 17:00:05 字数 57 浏览 2 评论 0原文

为什么我们说Java是一种编译型和解释型语言?

这样做的好处是什么(被编译和解释)?

Why we do we say that Java is a compiled and interpreted language?

What is the advantage of this (being compiled and interpreted)?

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

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

发布评论

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

评论(6

放血 2024-09-15 17:00:06

为什么说Java是编译型和解释型语言。

因为源代码(.java 文件)编译为字节码(.class 文件),然后解释用于执行的 Java 虚拟机(也称为 JVM)(JVM 可以做进一步的优化,但这是另一个故事)。

与此相比有什么优势(正在编译/解释)

与此(正在编译/解释)可移植性 。只要安装了 JVM,相同的字节码就可以在任何平台上执行(“编译一次,随处运行”)。

Why do we say Java is compiled and interpreted language.

Because source code (.java files) is compiled into bytecode (.class files) that is then interpreted by a Java Virtual Machine (also known as a JVM) for execution (the JVM can do further optimization but this is anoher story).

What is the advantage over this(being compiled/interpreted)

Portability. The same bytecode can be executed on any platform as long as a JVM is installed ("compile once, run anywhere").

素罗衫 2024-09-15 17:00:06

这是一个很长的主题,您最好阅读一下JIT。简而言之,Java 被编译为字节码,字节码随后(在 JVM 中)编译为机器码。

This is a long topic and you'd better read about JIT. In short, Java is compiled to bytecode, and the bytecode is later compiled (in the JVM) to machine code.

舟遥客 2024-09-15 17:00:06

Java 被认为是一种“编译”语言,因为代码被编译为字节码格式,然后由 Java 虚拟机 (JVM) 运行。这在性能和代码优化领域提供了多种优势,更不用说确保代码的正确性了。

它被认为是一种“解释型”语言,因为在编译字节码后,它可以在任何安装了 JVM 的机器上运行。正是通过这种方式,Java 很像一种解释性语言,因为在大多数情况下,它不依赖于运行的平台。这种行为与 Perl、Python、PHP 等其他解释语言类似。Java

程序可以在没有源代码的情况下在任何系统上运行这一事实的一个理论上的缺点是,虽然这种分发方法确保了跨由于平台兼容性,开发人员就少了一个发布源代码的理由,从而在“跨平台”和“开源”这两个词的意识形态含义之间产生了分歧。

Java is considered a "compiled" language because code is compiled into bytecode format that is then run by the Java Virtual Machine (JVM). This gives several advantages in the realm of performance and code optimization, not to mention ensuring code correctness.

It is considered an "interpreted" language because, after the bytecode is compiled, it is runnable on any machine that has a JVM installed. It is in this way that Java is much like an interpreted language in that, for the most part, it doesn't depend on the platform on which is is being run. This behavior is similar to other interpreted languages such as Perl, Python, PHP, etc.

One theoretical downside to the fact that Java programs can be run on any system in absence of the source code is that, while this method of distribution ensures cross-platform compatibility, the developers have one less reason to release their source code, driving a wedge between the ideological meanings of the phrases "cross-platform" and "open source".

回忆凄美了谁 2024-09-15 17:00:06

Java 被编译为字节代码而不是二进制文件。字节码不能直接执行,需要Java虚拟机进行及时编译,并在运行时再次编译成机器码。

在非常基础的层面上,它将程序员编写的代码与 JVM 运行的本地机器分开,因此具有更好的可移植性。虽然编译为字节码有助于提高即时编译的性能,减少文件大小,或多或少有助于隐藏真实代码。 (它还消除了一些编译时错误)

Java is compiled, into byte code not binaries. The byte codes are not executable directly, they need the Java Virtual Machine to do a just in time compile and compile them again into machine code at runtime.

At a very basic level, it separate the code that programmers write from the local machine where the JVM operates on, hence better portability. While compiling to bytecode helps the performance of just in time compile, reduce file sizes, and more or less help conceal real code. (it also eliminates some compile time error)

笑叹一世浮沉 2024-09-15 17:00:06

已编译:在程序启动之前,您的程序在语法上是正确的 Java 程序。

解释:在不同平台上运行相同的(字节)代码。

已编译:当您的程序已正确编译时,您可以放心控制 80% 的软件错误。而且你的代码不会因为你没有正确关闭代码块等而停止。

解释:你知道Applet是什么吗?当Java问世时,它是“杀手级”应用程序。您的浏览器从网站下载小程序并在浏览器中运行小程序代码。这不太酷。但是,同一个小程序可以在 Windows、Linux、Mac、Solaris 上运行,...因为运行/解释一种中间语言:字节码。

Compiled: Your program is syntactically a correct Java program, before the program starts.

Interpreted: Run on different platforms the same (byte-)code.

Compiled: When your program has compiled correctly you can be shure to have 80% of software bugs under control. And your code will not stop because you have not correctly closed a code block, etc.

Interpreted: You know what Applets are ? It was the "killer" application when Java came out. Your browser downloads the applet from the website and run the applet code in your browser. That is not very cool. But, the same applet runs on Windows, Linux, Macs, Solaris, ... because runs/interpreted an intermedium language: the byte code.

初相遇 2024-09-15 17:00:05

Java 在编译时被编译为中间“字节代码”。这与 C 等语言在编译时编译为机器语言形成对比。 Java 字节代码不能像编译的 C 代码那样直接在硬件上执行。相反,字节码必须在运行时由 JVM(Java 虚拟机)解释才能执行。像 C 这样的语言的主要缺点是,当它被编译时,该二进制文件只能在一种特定的体系结构上运行(例如 x86)。

像 PHP 这样的解释性语言实际上是独立于系统的,并且依赖于特定于系统和架构的解释器。这带来了更大的可移植性(相同的 PHP 脚本可以在 Windows 机器和 Linux 机器等上运行)。然而,这种解释会导致性能显着下降。像 PHP 这样的高级语言比可以由硬件执行的机器特定指令需要更多的时间来解释。

Java 寻求在纯编译语言(没有可移植性)和纯解释语言(速度明显慢)之间找到折衷方案。它通过将代码编译成更接近机器语言的形式来实现这一点(实际上,Java 字节代码是一种机器语言,简称为 Java 虚拟机),但仍然可以在体系结构之间轻松传输。因为 Java 仍然需要软件层(JVM)来执行,所以它是一种解释性语言。然而,解释器(JVM)在称为字节码的中间形式上运行,而不是在原始源文件上运行。该字节代码是由 Java 编译器在编译时生成的。因此,Java也是一种编译型语言。通过这种方式操作,Java 获得了编译语言的一些好处,同时也获得了解释语言的一些好处。但是,它也继承了这两种语言的一些限制。

正如 Bozho 指出的那样,有一些策略可以通过使用即时 (JIT) 编译来提高 Java 代码(以及 .Net 等其他字节代码语言)的性能。实际的过程因需求而异,但最终的结果是原始代码在编译时被编译为字节码,然后在运行时经过编译器运行才被执行。通过这样做,代码可以以接近本机的速度执行。有些平台(我相信.Net是这样做的)保存JIT编译的结果,替换字节码。通过这样做,程序的所有未来执行都将像该程序从一开始就被本机编译一样执行。

Java is compiled to an intermediate "byte code" at compilation time. This is in contrast to a language like C that is compiled to machine language at compilation time. The Java byte code cannot be directly executed on hardware the way that compiled C code can. Instead the byte code must be interpreted by the JVM (Java Virtual Machine) at runtime in order to be executed. The primary drawback of a language like C is that when it is compiled, that binary file will only work on one particular architecture (e.g. x86).

Interpreted languages like PHP are effectively system independent and rely on a system and architecture specific interpreter. This leads to much greater portability (the same PHP scripts work on Windows machines and Linux machines, etc.). However, this interpretation leads to a significant performance decrease. High-level languages like PHP require more time to interpret than machine-specific instructions that can be executed by the hardware.

Java seeks to find a compromise between a purely compiled language (with no portability) and a purely interpreted language (that is significantly slower). It accomplishes this by compiling the code into a form that is closer to machine language (actually, Java byte code is a machine language, simply for the Java Virtual Machine), but can still be easily transported between architectures. Because Java still requires a software layer for execution (the JVM) it is an interpreted language. However, the interpreter (the JVM) operates on an intermediate form known as byte code rather than on the raw source files. This byte code is generated at compile time by the Java compiler. Therefore, Java is also a compiled language. By operating this way, Java gets some of the benefits of compiled languages, while also getting some of the benefits of interpreted languages. However, it also inherits some limitations from both of these languages.

As Bozho points out, there are some strategies for increasing the performance of Java code (and other byte code languages like .Net) through the use of Just in Time (JIT) compilation. The actual process varies from implementation to implementation based on the requirements, but the end-result is that the original code is compiled into byte code at compile time, but then it is run through a compiler at runtime before it is executed. By doing this, the code can be executed at near-native speeds. Some platforms (I believe .Net does this) save the result of the JIT compilation, replacing the byte code. By doing this, all future executions of the program will execute as though the program was natively compiled from the beginning.

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