当 Java 需要 JVM 运行时,它如何独立于平台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2748910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:25:12  来源:igfitidea点击:

How is Java platform-independent when it needs a JVM to run?

javacross-platform

提问by Serenity

I just started learning Java and I'm confused about the topic of platform independence.

我刚开始学习 Java,我对平台独立性这个话题感到困惑。

Doesn't "independent" imply that Java code should run on any machine and need no special software to be installed? Yet the JVM needs to be present in the machine.

“独立”是否意味着 Java 代码应该在任何机器上运行并且不需要安装特殊软件?然而,JVM 需要存在于机器中。

For example, we need to have the Turbo C Compiler in order to compile C/C++ source code and then execute it. The machine has to have the C compiler.

例如,我们需要有 Turbo C Compiler 才能编译 C/C++ 源代码然后执行它。机器必须有 C 编译器。

Could somebody please what is meant when Java is described as "platform independent"?

有人可以请教 Java 被描述为“平台独立”是什么意思?

采纳答案by Edwin Buck

Typically, the compiled code is the exact set of instructions the CPU requires to "execute" the program. In Java, the compiled code is an exact set of instructions for a "virtual CPU" which is required to work the same on every physical machine.

通常,编译后的代码是 CPU“执行”程序所需的确切指令集。在 Java 中,编译后的代码是一组精确的“虚拟 CPU”指令集,需要在每台物理机器上运行相同的指令。

So, in a sense, the designers of the Java language decided that the language and the compiled code was going to be platform independent, but since the code eventually has to run on a physical platform, they opted to put all the platform dependent code in the JVM.

因此,从某种意义上说,Java 语言的设计者决定语言和编译后的代码将与平台无关,但由于代码最终必须在物理平台上运行,因此他们选择将所有平台相关的代码放在JVM。

This requirement for a JVM is in contrast to your Turbo C example. With Turbo C, the compiler will produce platform dependent code, and there is no need for a JVM work-alike because the compiled Turbo C program can be executed by the CPU directly.

JVM 的这种要求与您的 Turbo C 示例形成对比。使用 Turbo C,编译器将生成平台相关代码,不需要 JVM 类似工作,因为编译后的 Turbo C 程序可以由 CPU 直接执行。

With Java, the CPU executes the JVM, which is platform dependent. This running JVM then executes the Java bytecode which is platform independent, provided that you have a JVM available for it to execute upon. You might say that writing Java code, you don't program for the code to be executed on the physical machine, you write the code to be executed on the Java Virtual Machine.

使用 Java,CPU 执行 JVM,这是平台相关的。这个正在运行的 JVM 然后执行与平台无关的 Java 字节码,前提是您有一个 JVM 可用于它执行。您可能会说编写 Java 代码,不是为要在物理机上执行的代码编写程序,而是编写要在 Java 虚拟机上执行的代码。

The only way that all this Java bytecode works on all Java virtual machines is that a rather strict standard has been written for how Java virtual machines work. This means that no matter what physical platform you are using, the part where the Java bytecode interfaces with the JVM is guaranteed to work only one way. Since all the JVMs work exactly the same, the same code works exactly the same everywhere without recompiling. If you can't pass the tests to make sure it's the same, you're not allowed to call your virtual machine a "Java virtual machine".

所有这些 Java 字节码在所有 Java 虚拟机上工作的唯一方式是为 Java 虚拟机的工作方式编写了相当严格的标准。这意味着无论您使用什么物理平台,Java 字节码与 JVM 接口的部分都保证只能以一种方式工作。由于所有 JVM 的工作方式完全相同,因此相同的代码在任何地方都完全相同,无需重新编译。如果您无法通过测试以确保其相同,则不允许将您的虚拟机称为“Java 虚拟机”。

Of course, there are ways that you can break the portability of a Java program. You could write a program that looks for files only found on one operating system (cmd.exe for example). You could use JNI, which effectively allows you to put compiled C or C++ code into a class. You could use conventions that only work for a certain operating system (like assuming ":" separates directories). But you are guaranteed to never have to recompile your program for a different machine unless you're doing something really special (like JNI).

当然,有一些方法可以破坏 Java 程序的可移植性。您可以编写一个程序来查找仅在一个操作系统(例如 cmd.exe)上找到的文件。您可以使用 JNI,它有效地允许您将已编译的 C 或 C++ 代码放入一个类中。您可以使用仅适用于特定操作系统的约定(例如假设“:”分隔目录)。但是你可以保证永远不必为不同的机器重新编译你的程序,除非你正在做一些非常特别的事情(比如 JNI)。

回答by Matthew Flaschen

It means the Java programmer does not (in theory) need to know machine or OS details. These details do exist and the JVM and class libraries handle them. Further, in sharp contrast to C, Java binaries (bytecode) can often be moved to entirely different systems without modifying or recompiling.

这意味着 Java 程序员(理论上)不需要了解机器或操作系统的详细信息。这些细节确实存在并且 JVM 和类库会处理它们。此外,与 C 形成鲜明对比的是,Java 二进制文件(字节码)通常可以移动到完全不同的系统,而无需修改或重新编译。

回答by Lars Andren

The JVM is a "simulated machine" that can be installed on different systems. In this way, the same Java code can run on different systems, because it relies on the JVM, not on the operational system itself.

JVM 是一个“模拟机器”,可以安装在不同的系统上。这样,相同的 Java 代码可以运行在不同的系统上,因为它依赖于 JVM,而不是操作系统本身。

That is to say, this allows the programmer to communicate with the virtual system (JVM) and utilize its functions, instead of the specific machine and OS functions. Since Java only relies on JVM, it is platform independent (if the platform has JVM installed).

也就是说,这允许程序员与虚拟系统(JVM)进行通信并利用其功能,而不是特定的机器和操作系统功能。由于Java只依赖JVM,所以它是平台无关的(如果平台安装了JVM)。

So in short, Java is not platform independent as such, it requires a JVM-installation for all systems it should run on. However, it will run on all systems that has the JVM installed.

简而言之,Java 不是独立于平台的,它需要为它应该运行的所有系统安装 JVM。但是,它将在所有安装了 JVM 的系统上运行。

回答by Amy B

Edit: Not quite. See comments below.

编辑:不完全是。请参阅下面的评论。

Java doesn't directly run on anything. It needs to be converted to bytecode by a JVM.

Java 不直接在任何东西上运行。它需要由 JVM 转换为字节码。

Because JVMs exist for all major platforms, this makes Java platform-independent THROUGH the JVM.

因为 JVM 存在于所有主要平台上,这使得 Java 平台独立于 JVM。

回答by Guffa

No, it's the other way around. It's because you use the virtual machine that the Java program gets independend.

不,它是相反的。正是因为你使用了虚拟机,Java程序才变得独立。

The virtual machine is not independent, you have to install one that is specifically made for your type of system. The virtual machine creates an independent platform on top of the operating system.

虚拟机不是独立的,您必须安装专为您的系统类型制作的虚拟机。虚拟机在操作系统之上创建了一个独立的平台。

回答by deamon

The JVM abstracts from the concrete platform. Your program relies only upon the JVM and since the JVM is available for different platforms like Windows and Linux, your program is platform independent (but jvm depended).

JVM 从具体平台中抽象出来。您的程序仅依赖于 JVM,并且由于 JVM 可用于 Windows 和 Linux 等不同平台,因此您的程序是独立于平台的(但依赖于 jvm)。

回答by John Percival Hackworth

Java is not platform independent in that it runs on the JVM. Having said that, you gain platform independence via programming against a single abstract machine that has concrete realizations on most common OS platforms (and some embedded appliances).

Java 不是平台独立的,因为它在 JVM 上运行。话虽如此,您可以通过针对单个抽象机器进行编程来获得平台独立性,该机器在大多数常见操作系统平台(和一些嵌入式设备)上具有具体实现。

A related idea is the hardware abstraction layer present in many operating systems that allows the same OS to run on disparate hardware.

一个相关的想法是存在于许多操作系统中的硬件抽象层,它允许相同的操作系统在不同的硬件上运行。

In you original question, Turbo C is analagous to the javac program, and the JVM is the OS/HAL.

在您最初的问题中,Turbo C 类似于 javac 程序,而 JVM 是 OS/HAL。

回答by Pascal Thivent

Doesn't independent means that Java code should be able to run on any machine and would need no special software to be installed (JVM in this case has to be present in the machine)?

是否独立意味着 Java 代码应该能够在任何机器上运行并且不需要安装特殊软件(在这种情况下,JVM 必须存在于机器中)?

With Java, you can compile source code on Windows and the compiled code(bytecode to be precise) can be executed (interpreted) on any platform running a JVM. So yes you need a JVM but the JVM can run any compiled code, the compiled code is platform independent.

使用 Java,您可以在 Windows 上编译源代码,并且编译后的代码(准确地说是字节码)可以在运行 JVM 的任何平台上执行(解释)。所以是的,你需要一个 JVM,但 JVM 可以运行任何编译代码,编译代码是平台无关的

In other words, you have both portability of source code and portability of compiled code.

换句话说,您同时拥有源代码的可移植性和编译代码的可移植性。

Like, for example, we need to have Turbo C Compiler in order to compile C/C++ source code and then execute it.. The machine has to have the C compiler.

例如,我们需要有 Turbo C Compiler 才能编译 C/C++ 源代码然后执行它.. 机器必须有 C 编译器。

The machine doesn't have to have a C compiler, the machine has to use a platform specific binary. With C or C++, the compiled codeis specific to each architecture, it is platform independent.

机器不必有 C 编译器,机器必须使用特定于平台的二进制文件。对于 C 或 C++,编译后的代码特定于每个体系结构,它是平台无关的

In other words, with C / C++ you have portability of source code (with some discipline) but not portability of compiled code: you need to recompile for each architecture into platform specific binaries.

换句话说,使用 C/C++,您具有源代码的可移植性(具有某些纪律),但不具有编译代码的可移植性:您需要为每个架构重新编译为特定于平台的二进制文件。

回答by Nitz

JVM will be platform dependent.
But whatever it will generate that will be platform independent. [which we called as bytecode or simply you can say...the class file]. for that why Java is called Platform independent.
you can run the same class file on Mac as well on Windows but it will require JRE.

JVM 将依赖于平台。
但无论它生成什么,都将是平台独立的。[我们称之为字节码,或者简单地说...类文件]。这就是为什么 Java 被称为平台独立的原因。
您可以在 Mac 上运行相同的类文件,也可以在 Windows 上运行,但它需要 JRE。

回答by PRATHIKSHA JAIN

In c/c++ the source code(c program file) after the compilation using a compiler is directly converted to native machine code(which is understandable to particular machine on which u compiling the code). And hence the compiled code of c/c++ can not run on different OS.

在 c/c++ 中,使用编译器编译后的源代码(c 程序文件)直接转换为本地机器代码(这对于您编译代码的特定机器是可以理解的)。因此 c/c++ 的编译代码不能在不同的操作系统上运行。

But in case of Java : the source file of java(.java) will be compiled using JAVAC compiler(present in JDK) which provides the Byte code(.class file) which is understandable to any JVM installed on any OS(Physical System).

但是在 Java 的情况下:java(.java) 的源文件将使用 JAVAC 编译器(存在于 JDK 中)进行编译,该编译器提供字节代码(.class 文件),这对于安装在任何操作系统(物理系统)上的任何 JVM 都是可以理解的.

Here we need to have different JVM (which is platform dependent) for different operating Systems where we want to run the code, but the .class file(compiled code/Intermediate code)remains same, because it is understandable to any of the JVM installed on any OS.

在这里,我们需要为不同的操作系统使用不同的 JVM(取决于平台)来运行代码,但 .class 文件(编译代码/中间代码)保持不变,因为它可以被任何安装的 JVM 理解在任何操作系统上。

In c/c++ : only source code is machine independent. In Java : both the source code and the compiled code is platform independent.

在 c/c++ 中:只有源代码与机器无关。在 Java 中:源代码和编译代码都是平台无关的。

This makes Java Platform(machine) independent.

这使得 Java 平台(机器)独立。