Java的编译器和解释器(JVM)

huangapple go评论73阅读模式
英文:

Java's compiler and interpreter(JVM)

问题

为什么Java使用编译器将源代码转换为字节码,再使用解释器将字节码转换为机器码,而不是反过来呢?在谷歌上找不到相关的答案。

编辑:抱歉,我是Java的初学者。我所指的另一种方式是,为什么Java不使用解释器将源代码转换为字节码。谢谢答案!

英文:

Why java uses compiler to convert source code into bytecode, Interpreter to convert bytecode to machine code and not the other way round? Couldn't find any relevant answers in google.

Edit: Sorry I'm a beginner in java. By other way round I mean, why java isn't using interpreter to convert source code into bytecode. Thanks for the answer!

答案1

得分: 4

Java字节码被设计为与平台无关,这意味着您可以在支持Java的Linux、Windows、Mac或任何其他平台上运行.class文件。当Java被构想出来时,这被宣传为一个重要特性,可以消除将软件移植到不同系统的巨大障碍。

在运行时,字节码被优化为尽可能快地运行,这是通过“即时编译器”(JIT)完成的。简而言之,虚拟机在代码中找到热点,并用特定于CPU的指令替换它们,以避免解释它们带来的开销。这完全自动进行,对用户是不可见的(除了速度方面)。

之所以会是这样,是因为Java是按这种方式设计的。它也可以以不同的方式设计,但那就不会是Java。

英文:

Java bytecode is designed to be platform-independent, which means you can take a .class file and run it on Linux, Windows, Mac, or any other platform where Java is supported. When Java was conceived, this was advertised as a major feature that would eliminate the huge barrier of porting your software to different systems.

During runtime, bytecode is optimized to run as fast as possible, which is done with a "just in-time compiler" (JIT). In a nutshell, virtual machine finds the hot spots in code and replaces them with CPU-specific instructions to avoid the overhead with interpreting them. This happens completely automatically and is invisible to the user (except for speed).

The reason why it is like that is simply because Java was designed in such way. It could also be designed in a different way, but then this wouldn't be Java.

答案2

得分: 1

Java语言被设计成跨平台的,意味着一个Java程序的二进制代码应该能够在任何处理器架构和操作系统上运行。

机器代码是针对特定处理器架构的,所以为了避免重新编译代码,Java程序永远不会被翻译成真正的机器代码。

相反,Java被编译成字节码,由虚拟机运行。
这个虚拟机针对不同的操作系统和CPU类型创建了许多版本,它将最终用户程序与操作系统和CPU完全隔离开来。

因此,如果你编写了一个纯粹的Java程序并生成了一个jar文件,它将能够在装有JVM的x86 Windows个人电脑和ARM架构的SBC(如树莓派)上运行,只要你安装了JVM。

如果你的Java源代码被编译成了实际的物理CPU机器代码,那是行不通的。

英文:

The Java language was designed to be cross-platform, meaning that a single Java program binary should be able to run on any processor and any OS.

Machine code is specific per processor architecture, so to avoid having to recompile the code, Java programs are never translated to real machine code.

Instead, Java is compiled to byte code which is run by a Virtual Machine.
This virtual machine is created in many versions for different OS and CPU types, and it completely isolates the end user program from that OS and CPU.

Thus, if you write a pure Java program and produce a jar file, it will run on both x86 PC with Windows, and ARM style SBC (like Raspberry PI) with Linux, as ling as you have a JVM installed.

If your Java source was compiled to actual machine code of physical CPU, that would not work.

huangapple
  • 本文由 发表于 2020年10月24日 20:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64513525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定