Java编译器/解释器 [英] Java compiler/interpreter

查看:122
本文介绍了Java编译器/解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

为什么我们说java是编译和解释语言。 Java在编译时被编译为中间字节代码。这与在编译时编译为机器语言的C语言形成对比。 Java字节代码不能在编译C代码的方式上直接在硬件上执行。相反,字节代码必须在运行时由JVM(Java虚拟机)解释以便被执行。像C这样的语言的主要缺点是当它被编译时,该二进制文件将只在一个特定的体系结构(例如x86)上工作。



PHP之类的解释型语言与系统无关,并且依赖于系统和架构特定的解释器。这导致更大的可移植性(相同的PHP脚本工作在Windows机器和Linux机器等)。然而,这种解释导致显着的性能降低。像PHP这样的高级语言需要更多的时间来解释,而不是由硬件执行的特定于机器的指令。



Java寻求在纯编译语言(没有可移植性)和纯解释型语言(显着更慢)之间找到一个妥协。它通过将代码编译为更接近机器语言的形式(实际上,Java字节代码是机器语言,仅用于Java虚拟机)来实现,但是仍然可以在架构之间容易地传输。因为Java仍然需要一个软件层来执行(JVM),它是一种解释型语言。然而,解释器(JVM)在称为字节码的中间形式而不是原始源文件上操作。此字节代码在编译时由Java编译器生成。因此,Java也是一种编译语言。通过这种方式,Java获得了编译语言的一些好处,同时也获得了解释语言的一些好处。然而,它也继承了这两种语言的一些限制。



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


Why we say java is compiled and interpreted language.What is the advantage over this(being compiled/interpreted)

解决方案

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

这篇关于Java编译器/解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆