如果 JVM 实现因一台机器而异,字节码生成如何使 Java 平台独立?
How does bytecode generation make Java platform independent if JVM implementation differs from one machine to another?
C++ 不是平台无关的,因为它需要编译成本机代码,并且需要针对不同的 CPU 体系结构实现不同的编译器。
C++ 编译器仅将代码翻译成机器语言或本机代码。此本机代码由处理器执行。
根据我在网上看到的内容,Java 程序最初由 java 编译器 'javac' 编译成字节码。然后 Java 虚拟机逐行解释(并执行)此字节码。
我有几个问题。
1) If C++ is not platform independent because different compilers need to be designed for different CPU architectures, then doesn't JVM differ from one hardware to another as well?
2) If I wrote a C++ code and compiled and executed it on Machine 1 and Machine 2, the output produced will be the same. Similarly, if I write a Java program and execute it on two different machines, the output will still be the same. Why the extra step of bytecode generation?
3) I read somewhere that interpreters, unlike compilers, actually execute a program by emulating a virtual machine. Does it mean that JVM actually executes the bytecode and not just interprets it into native code?
从某种意义上说,您是对的,因为您基本上可以将 C++ 编译器等同于 Java 编译器加上 Java 虚拟机的组合。这两者本身都不是独立于平台的:为 Linux 机器编写的 Java 编译器通常不会 运行 在 Windows 机器上,JVM 也不会。 =10=]
然而,在实践中,Java 字节码的中间语言,即 Java 编译器生成并由虚拟机执行的语言,产生了巨大的差异,因为编译代码正是在这个级别上已发货。所以如果你得到一个 Java 二进制文件,它就是字节码。只要您的机器有 JVM,您就可以 运行 毫不费力地使用它。相比之下,C++ 二进制文件是特定机器的机器代码;你不能 运行 在编译它的机器之外的任何其他机器上。
现在,开始回答您的实际问题:
- 确实,JVM 因硬件平台而异。
- 字节码的编译步骤负责相当多的处理和打包。如果您每次 运行 一个程序都想这样做,启动时间会受到影响。但原则上肯定是可能的,所以是的,如果你围绕调用编译器的 C++ 编译器编写一个包装器,然后立即执行编译后的程序,那么你基本上拥有一个 C++ 虚拟机,它与执行平台无关Java字节码。
- 现代 JVM 非常聪明,可以在字节码执行期间进行相当多的进一步编译(称为即时或 JIT 编译)。这绝不是单纯的字节码解释。
C++是一次编写,随处编译(至少在理论上,说起来容易做起来难,但确实做到了)
Java一次编译,运行任何地方(前提是安装了JVM,同样系统之间存在差异,无法完全隐藏)
If C++ is not platform independent because different compilers need to be designed for different CPU architectures, then doesn't JVM differ from one hardware to another as well?
JVM 不同,但字节码不一定。 C++ 可以编写一次,但通常具有特定于平台的代码。这不一定是个问题,但您可以期望代码必须经过测试,这是为每种系统编写的自定义代码。
If I wrote a C++ code and compiled and executed it on Machine 1 and Machine 2, the output produced will be the same. Similarly, if I write a Java program and execute it on two different machines, the output will still be the same. Why the extra step of bytecode generation?
Java只需要编译一次。你可以在 20 年前的 32 位 windows 平台 运行ning Windows 95 上使用 Java 1.0 编译的 JAR 并在没有源代码或不知道它如何使用的情况下立即使用它在 64 位 ARM 处理器 运行ning Linux 上编译的方式,它将使用该处理器的最新优化和指令集。
对于 C++,如果你拿一个 20 年前在 Windows95 上编写的程序,如果没有 大量工作和测试,它可能无法编译Linux.
的 64 位 ARM 处理器
I read somewhere that interpreters, unlike compilers, actually execute a program by emulating a virtual machine. Does it mean that JVM actually executes the bytecode and not just interprets it into native code?
JVM 最初解释,快速编译,然后变慢(在 OpenJDK 中称为 C1),然后更积极地重新优化(称为 C2)它经历了 4 个不同的编译级别,它可以取消优化代码以根据不断变化的用法甚至更改代码对其进行重新优化。
C++ 不是平台无关的,因为它需要编译成本机代码,并且需要针对不同的 CPU 体系结构实现不同的编译器。
C++ 编译器仅将代码翻译成机器语言或本机代码。此本机代码由处理器执行。
根据我在网上看到的内容,Java 程序最初由 java 编译器 'javac' 编译成字节码。然后 Java 虚拟机逐行解释(并执行)此字节码。
我有几个问题。
1) If C++ is not platform independent because different compilers need to be designed for different CPU architectures, then doesn't JVM differ from one hardware to another as well?
2) If I wrote a C++ code and compiled and executed it on Machine 1 and Machine 2, the output produced will be the same. Similarly, if I write a Java program and execute it on two different machines, the output will still be the same. Why the extra step of bytecode generation?
3) I read somewhere that interpreters, unlike compilers, actually execute a program by emulating a virtual machine. Does it mean that JVM actually executes the bytecode and not just interprets it into native code?
从某种意义上说,您是对的,因为您基本上可以将 C++ 编译器等同于 Java 编译器加上 Java 虚拟机的组合。这两者本身都不是独立于平台的:为 Linux 机器编写的 Java 编译器通常不会 运行 在 Windows 机器上,JVM 也不会。 =10=]
然而,在实践中,Java 字节码的中间语言,即 Java 编译器生成并由虚拟机执行的语言,产生了巨大的差异,因为编译代码正是在这个级别上已发货。所以如果你得到一个 Java 二进制文件,它就是字节码。只要您的机器有 JVM,您就可以 运行 毫不费力地使用它。相比之下,C++ 二进制文件是特定机器的机器代码;你不能 运行 在编译它的机器之外的任何其他机器上。
现在,开始回答您的实际问题:
- 确实,JVM 因硬件平台而异。
- 字节码的编译步骤负责相当多的处理和打包。如果您每次 运行 一个程序都想这样做,启动时间会受到影响。但原则上肯定是可能的,所以是的,如果你围绕调用编译器的 C++ 编译器编写一个包装器,然后立即执行编译后的程序,那么你基本上拥有一个 C++ 虚拟机,它与执行平台无关Java字节码。
- 现代 JVM 非常聪明,可以在字节码执行期间进行相当多的进一步编译(称为即时或 JIT 编译)。这绝不是单纯的字节码解释。
C++是一次编写,随处编译(至少在理论上,说起来容易做起来难,但确实做到了)
Java一次编译,运行任何地方(前提是安装了JVM,同样系统之间存在差异,无法完全隐藏)
If C++ is not platform independent because different compilers need to be designed for different CPU architectures, then doesn't JVM differ from one hardware to another as well?
JVM 不同,但字节码不一定。 C++ 可以编写一次,但通常具有特定于平台的代码。这不一定是个问题,但您可以期望代码必须经过测试,这是为每种系统编写的自定义代码。
If I wrote a C++ code and compiled and executed it on Machine 1 and Machine 2, the output produced will be the same. Similarly, if I write a Java program and execute it on two different machines, the output will still be the same. Why the extra step of bytecode generation?
Java只需要编译一次。你可以在 20 年前的 32 位 windows 平台 运行ning Windows 95 上使用 Java 1.0 编译的 JAR 并在没有源代码或不知道它如何使用的情况下立即使用它在 64 位 ARM 处理器 运行ning Linux 上编译的方式,它将使用该处理器的最新优化和指令集。
对于 C++,如果你拿一个 20 年前在 Windows95 上编写的程序,如果没有 大量工作和测试,它可能无法编译Linux.
的 64 位 ARM 处理器I read somewhere that interpreters, unlike compilers, actually execute a program by emulating a virtual machine. Does it mean that JVM actually executes the bytecode and not just interprets it into native code?
JVM 最初解释,快速编译,然后变慢(在 OpenJDK 中称为 C1),然后更积极地重新优化(称为 C2)它经历了 4 个不同的编译级别,它可以取消优化代码以根据不断变化的用法甚至更改代码对其进行重新优化。