当我们拥有JVM时,为什么还需要Java编译器?

时间:2022-01-19 00:10:32

Just a question of curiosity that I came up with when I was reading the JVM introduction.

我在阅读JVM简介时提出的好奇心问题。

Why do we need the Java compiler when we have the universal, platform-independent Java Virtual Machine? I mean, consider Python, which has an interactive shell that reads the source code line by line and then execute it without having to compile the source code beforehand, why can't JVM be designed to be able to read .java files directly like that of Python and then execute it?

当我们拥有通用,独立于平台的Java虚拟机时,为什么还需要Java编译器?我的意思是,考虑Python,它有一个交互式shell,逐行读取源代码然后执行它而不必事先编译源代码,为什么JVM不能设计为能够直接读取.java文件Python然后执行它?

If that's not the case, can someone please explain the significance of Java compiler?

如果不是这样,有人可以解释一下Java编译器的意义吗?

2 个解决方案

#1


3  

Java could indeed have been created where you ship source everywhere (as one typically does with JavaScript). But as a design choice, Gosling et. al. decided to ship bytecode instead, which is created from the source by the Java compiler. There are several objective reasons for doing so:

确实可以创建Java,您可以在任何地方发送源代码(通常使用JavaScript)。但作为设计选择,Gosling等。人。决定使用字节码来代替,这是由Java编译器从源代码创建的。这样做有几个客观原因:

  • Bytecode is smaller than source code.
  • 字节码小于源代码。

  • Bytecode is harder to reverse-engineer than source code (though only slightly).
  • Bytecode比源代码更难逆向工程(虽然只是略微)。

  • Bytecode is harder to modify than source code.
  • 字节码比源代码更难修改。

  • Using bytecode means the JVM doesn't have to have the Java compiler in it (reducing footprint, which was more important in ~1995 than it is now)
  • 使用字节码意味着JVM不必拥有Java编译器(减少占用空间,这在1995年比现在更重要)

  • Compiling takes non-trivial time (and non-trivial memory use). Compiling to bytecode preserves Run Everywhere(tm) without that startup impact.
  • 编译需要非常重要的时间(以及非平凡的内存使用)。编译为字节码会保留Run Everywhere(tm)而不会影响启动。

Again, though, it was just a design decision they took. Microsoft took the same decision with .Net.

尽管如此,这只是他们采取的一项设计决定。微软对.Net采取了同样的决定。

#2


0  

The java compiler converts the java source to bytecode.

java编译器将java源转换为字节码。

The JVM only executes bytecode. JVMs on different operating systems can run the same bytecode.

JVM只执行字节码。不同操作系统上的JVM可以运行相同的字节码。

Other languages like Scala can also be compiled to bytecode and run on a JVM.

其他语言(如Scala)也可以编译为字节码并在JVM上运行。

The compilation improves execution efficiency and security.

编译提高了执行效率和安全性。

#1


3  

Java could indeed have been created where you ship source everywhere (as one typically does with JavaScript). But as a design choice, Gosling et. al. decided to ship bytecode instead, which is created from the source by the Java compiler. There are several objective reasons for doing so:

确实可以创建Java,您可以在任何地方发送源代码(通常使用JavaScript)。但作为设计选择,Gosling等。人。决定使用字节码来代替,这是由Java编译器从源代码创建的。这样做有几个客观原因:

  • Bytecode is smaller than source code.
  • 字节码小于源代码。

  • Bytecode is harder to reverse-engineer than source code (though only slightly).
  • Bytecode比源代码更难逆向工程(虽然只是略微)。

  • Bytecode is harder to modify than source code.
  • 字节码比源代码更难修改。

  • Using bytecode means the JVM doesn't have to have the Java compiler in it (reducing footprint, which was more important in ~1995 than it is now)
  • 使用字节码意味着JVM不必拥有Java编译器(减少占用空间,这在1995年比现在更重要)

  • Compiling takes non-trivial time (and non-trivial memory use). Compiling to bytecode preserves Run Everywhere(tm) without that startup impact.
  • 编译需要非常重要的时间(以及非平凡的内存使用)。编译为字节码会保留Run Everywhere(tm)而不会影响启动。

Again, though, it was just a design decision they took. Microsoft took the same decision with .Net.

尽管如此,这只是他们采取的一项设计决定。微软对.Net采取了同样的决定。

#2


0  

The java compiler converts the java source to bytecode.

java编译器将java源转换为字节码。

The JVM only executes bytecode. JVMs on different operating systems can run the same bytecode.

JVM只执行字节码。不同操作系统上的JVM可以运行相同的字节码。

Other languages like Scala can also be compiled to bytecode and run on a JVM.

其他语言(如Scala)也可以编译为字节码并在JVM上运行。

The compilation improves execution efficiency and security.

编译提高了执行效率和安全性。