考虑到 Java(语言)是平台独立的,为什么 JVM 不是平台独立的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2120654/
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
Why is the JVM not platform independent, given that Java (the language) is platform independent?
提问by giri
Just curious to know when java is made platform independent then are there any specific reasons JVM is made platform dependent..
只是想知道什么时候 java 是平台独立的,然后有什么特定的原因 JVM 是平台相关的..
采纳答案by TofuBeer
Unless you have a CPU that can directly execute Java bytecode (there are such things) you need to be able to interact with the OS (for things like reading files, connecting to the network, displaying to the screen, etc...).
除非您有一个可以直接执行 Java 字节码的 CPU(有这样的东西),否则您需要能够与操作系统交互(例如读取文件、连接到网络、显示到屏幕等)。
You can write a JVM in other languages (such as Javaor JavaScript) but ultimately there needs to be something that can interact with the underlying OS.
您可以使用其他语言(例如Java或JavaScript)编写 JVM,但最终需要一些可以与底层操作系统交互的东西。
回答by Chris Kannon
The JVM executes Java code, but is written in platform specific languages such as C/C++/ASM etc. The JVM is not written in Java and hence cannot be platform independent.
JVM 执行 Java 代码,但使用特定于平台的语言编写,例如 C/C++/ASM 等。JVM 不是用 Java 编写的,因此不能独立于平台。
回答by FrustratedWithFormsDesigner
The JVM must be platform dependent to allow your Java to run on the specific platform. A JVM for Windows will translate your Java into different system calls than a JVM for OS X.
JVM 必须依赖于平台,以允许您的 Java 在特定平台上运行。Windows 的 JVM 会将您的 Java 转换为与 OS X 的 JVM 不同的系统调用。
回答by Neville Flynn
Because there needs to be some way to convert the platform-independent application's Java calls to calls that are compatible with the underlying OS.
因为需要某种方式将独立于平台的应用程序的 Java 调用转换为与底层操作系统兼容的调用。
回答by aashish
I found that this was a great answer to the question:
我发现这是对这个问题的一个很好的回答:
JVM translates bytecode into machine language
Every Java program is compiled into an intermediate language called Java bytecode. The JVM is used to both translate the bytecode into the machine language for a particular computer, and actually execute the corresponding machine-language instructions as well. The JVM and bytecode combined give Java its status as a "portable" language.
Machine language is OS dependent
Given the previous information, it should be easier to deduce an answer to the question. Since the JVM must translate the bytecode into machine language, and since the machine language depends on the operating system being used, it is clear that the JVM is platform (operating system) dependent. This fact can be verified by trying to download the JVM – you will be given a list of JVM's corresponding to different operating systems, and you will obviously pick whichever JVM is targeted for the operating system that you are running.
JVM 将字节码翻译成机器语言
每个 Java 程序都被编译成一种称为 Java 字节码的中间语言。JVM 用于将字节码转换为特定计算机的机器语言,并实际执行相应的机器语言指令。JVM 和字节码的结合使 Java 具有“可移植”语言的地位。
机器语言取决于操作系统
鉴于先前的信息,应该更容易推断出问题的答案。由于 JVM 必须将字节码转换为机器语言,而且机器语言取决于所使用的操作系统,因此很明显 JVM 是平台(操作系统)相关的。可以通过尝试下载 JVM 来验证这一事实——您将获得一个对应于不同操作系统的 JVM 列表,您显然会选择针对您正在运行的操作系统的 JVM。
Quoted from Is the JVM Platform Dependent?
引用自JVM 平台依赖吗?
回答by black.marque
No, JVMs are not platform independent. In fact they are platform specific run time environment provided by the vendor. Each platform (Windows, UNIX, Mac etc) has its own JVM to run Java applications. Although the byte code supports connection to multiple databases..
不,JVM 不是平台独立的。实际上,它们是供应商提供的特定于平台的运行时环境。每个平台(Windows、UNIX、Mac 等)都有自己的 JVM 来运行 Java 应用程序。虽然字节码支持连接到多个数据库..
Think of Music being played in a MP3 player, CD player and old faithful cassette players(Boom Box). The output is always the same, ie music. But the input (media ie .mp3 files for MP3 Players, CDs for CD Players and cassettes for Cassette Players) vary depending on the system [here the systems will be the various Operating Systems like Windows, UNIX, Mac etc..]. Hope i was able to solve your problem..
想想在 MP3 播放器、CD 播放器和老式忠实卡带播放器(Boom Box)中播放的音乐。输出总是相同的,即音乐。但是输入(媒体,即 MP3 播放器的 .mp3 文件,CD 播放器的 CD 和盒式播放器的盒式磁带)因系统而异[这里的系统将是各种操作系统,如 Windows、UNIX、Mac 等。]。希望我能解决你的问题..
回答by Srichandar Karpuram
simply like - * - makes a +.
就像 - * - 做一个 +。
We all know Java is platform independent
我们都知道 Java 是平台无关的
but OS where we write the code is platform dependant
但是我们编写代码的操作系统是平台相关的
and Output should be platform independent so, we make jvm (which is in-between and installed with jre) platform dependent so that the output is independent.
和输出应该是平台无关的,所以我们使 jvm(它位于中间并与 jre 一起安装)平台相关,以便输出是独立的。
回答by PeGiannOS
I think that it could be platform independent if many different languages (each one written in specific different platform )where combined to make it change its own code dynamically.But all these take some great effort to happen and maybe destroy its portability.
我认为如果许多不同的语言(每种语言都在特定的不同平台上编写)结合起来使其动态更改自己的代码,它可能是平台独立的。但是所有这些都需要付出很大的努力才能实现,并且可能会破坏其可移植性。
回答by Beena
JVM translate the byte code which is universal to machine code which is machine dependent hence JVM is platform dependent. Due to this byte code java is platform independent.
JVM 将通用的字节码转换为机器相关的机器码,因此 JVM 是平台相关的。由于这个字节码,java 是平台无关的。
回答by Yirga
No, JVM is platform dependent. The code written in Java is not platform dependent. If you write Java code on a Windows operating system you can run that code on Linux or other operating Systems. But the JVM for Windows and Linux is different. JVM are found in JRE and when you download JRE from Oracle it says JRE for Window, Linux and other operating systems
不,JVM 是平台相关的。用 Java 编写的代码与平台无关。如果您在 Windows 操作系统上编写 Java 代码,则可以在 Linux 或其他操作系统上运行该代码。但是用于 Windows 和 Linux 的 JVM 是不同的。JVM 位于 JRE 中,当您从 Oracle 下载 JRE 时,它会显示 JRE for Window、Linux 和其他操作系统