java 为什么Java在理论上独立于平台而在实践中依赖于平台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4139980/
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 Java platform independent in theory and platform dependent in practice?
提问by Luis Miguel Serrano
I know that one of the big things about Java is that it is platform independent in the sense that you can make a Java application and have it run in Windows, Linux, Mac, and so forth, as long as you don't use libraries specific to one OS, and as long as you have a JVM installed for the appropriate OS to interpret things correctly...
我知道 Java 的一大特点是它是平台独立的,因为您可以制作 Java 应用程序并让它在 Windows、Linux、Mac 等系统中运行,只要您不使用库特定于一个操作系统,并且只要您为适当的操作系统安装了 JVM 以正确解释事物...
However, why can't a normal computer Java program (as in a simple Hello World in Java, for Windows or Linux for example) run just the same in a mobile phone, when mobile phones also have their specific JVM installed to interpret things correctly?
但是,为什么一个普通的计算机 Java 程序(例如在 Java 中的简单 Hello World,对于 Windows 或 Linux)不能在手机中运行相同,当手机还安装了特定的 JVM 以正确解释事物时?
Why is it necessary to change the architecture of the program in some cases, such as Android development, or use Java ME to make applications specific for some general mobile phones?
为什么在某些情况下需要改变程序的架构,比如Android开发,或者使用Java ME来制作一些通用手机专用的应用程序?
I know that there are some functions that are related to certain functionalities of the OS, that might not apply in the mobile platforms for example, such as some things related with console, input methods, and so forth, but is this really the only reason that makes things not compatible? If so, why won't a simple application that just declares and initializes an integer variable be able to run across all non-mobile and mobile platforms that have a JVM available?
我知道有些功能与操作系统的某些功能有关,例如可能不适用于移动平台,例如一些与控制台、输入法等相关的东西,但这真的是唯一的原因吗?这使事情不兼容?如果是这样,为什么仅声明和初始化整数变量的简单应用程序不能在所有具有可用 JVM 的非移动和移动平台上运行?
I am aware of other questions that have been posted before, such as this, but that do not focus the exact point I am aiming for here.
我知道之前发布的其他问题,例如this,但并没有关注我在这里的确切目标。
回答by SimonJ
The unit of portability to look at is a classrather than an application. A class that declares and initialises an integer variable willrun on all the platforms you describe, and many others too. They understand the same bytecode, even if they do execute it using different mechanisms ranging from bytecode interpreters, to JIT compilation, to Android's Dalvik (which translates JVM bytecode into its own instruction set).
要查看的可移植性单位是类而不是应用程序。声明和初始化整数变量的类将在您描述的所有平台以及许多其他平台上运行。他们理解相同的字节码,即使他们确实使用不同的机制来执行它,从字节码解释器到 JIT 编译,再到 Android 的 Dalvik(将 JVM 字节码转换成自己的指令集)。
Even if we look beyond a single integer variable, Java that uses "core" functionality willwork on most of these devices. There's a great deal of common ground between J2ME, Android and J2SE (and particularly the latter two - J2ME was intended as a cut-down version of the standard Java APIs for devices with limited resources, so less of the standard API is available).
即使我们超越了单个整数变量,使用“核心”功能的 Java也可以在大多数这些设备上运行。J2ME、Android 和 J2SE 之间有很多共同点(尤其是后两者 - J2ME 旨在为资源有限的设备提供标准 Java API 的精简版本,因此可用的标准 API 较少)。
On a Windows/Mac/Linux system, an application is usually something that you explicitly start, use, and - when you're done - tell it to exit. Compare this with, say, an Android phone: the application might be started in response to an event occurring (perhaps an incoming SMS, or a specific type of file downloaded from the web) in which case it needs to know howand whyit was started - a simple public static main(String[] args)
just doesn't cut it. Once started, the app needs to be aware of events such as "low battery" or "entering standby mode" in order to free up resources or disable CPU-intensive features (such as GPS) that might otherwise drain the battery.
在 Windows/Mac/Linux 系统上,应用程序通常是您明确启动、使用和 - 完成后 - 告诉它退出的东西。将其与 Android 手机进行比较:应用程序可能会响应发生的事件(可能是传入的 SMS,或从网络下载的特定类型的文件)而启动,在这种情况下,它需要知道它是如何以及为什么发生的开始 - 一个简单的public static main(String[] args)
就不会削减它。启动后,应用程序需要了解诸如“低电量”或“进入待机模式”等事件,以释放资源或禁用 CPU 密集型功能(如 GPS),否则可能会耗尽电池电量。
These aren't obscure functions - they're essential to a phone being useful as a phone - so all native applications must handle them.
这些并不是晦涩的功能——它们对于作为手机有用的手机必不可少——因此所有本机应用程序都必须处理它们。
回答by pravin penshanwar
When Java Code is compiled a byte code(class file) is generated which is independent of the system. This byte code is fed to the JVM (Java Virtual Machine) which resides in the system. Since every system has its own JVM, it doesn't matter where you compile the source code. The byte code generated by the compiler can be interpreted by any JVM of any machine. Hence it is called Platform independent Language.
当 Java 代码被编译时,会生成一个独立于系统的字节码(类文件)。该字节码被馈送到驻留在系统中的 JVM(Java 虚拟机)。由于每个系统都有自己的 JVM,因此在何处编译源代码并不重要。编译器生成的字节码可以被任何机器的任何JVM解释。因此它被称为平台无关语言。
thanks
谢谢
回答by Bharat360
Why is Java platform independent in theory and platform dependent in practice?
为什么Java在理论上独立于平台而在实践中依赖于平台?
Remember and clear one thing that only the Java language is platform independent and try to understand the meaning of the sentence. Java is platform independent means the code you have developed using Java can be run on any machine.
记住并明确一件事,只有 Java 语言是平台无关的,并尝试理解这句话的含义。Java 是平台无关的,这意味着您使用 Java 开发的代码可以在任何机器上运行。
When you compile the .java
file it generates .class
file and it contains bytecode and this bytecode is platform independent you can run it on any machine this is what about platform independency of Java language.
当您编译.java
文件时,它会生成.class
文件并且它包含字节码,并且该字节码是平台无关的,您可以在任何机器上运行它,这就是 Java 语言的平台无关性。
Now you said that it is not in practice so the reason is only Java language is platform independent, but its runtime enviroment, or JVM, is platform dependent, it is written separately for each OS. So we can say the Java language is platform independent, but its runtime environment is platform dependent.
现在你说它不是在实践中所以原因只是Java语言是平台无关的,但是它的运行时环境或JVM是平台相关的,它是为每个操作系统单独编写的。所以我们可以说Java语言是平台无关的,但它的运行环境是平台相关的。
回答by kakarot
Sun micro systems has released different versions of jdk. one for windows based and another for linux/unix based. When we installed jdk with that we get jvm,jre and javac. Suppose if we write a java program in windows with intel processor which is installed with windows jdk, then the java compiler of that jdk will convert .java file to .class file which contains byte code instructions similar to assembly language code these byte code instructions can be understandable only by jvm. If we take the .class file which is generated in windows o.s and if we run in linux then the jvm of that linux machine internally rewrites your java program by using the approximate 200+ instruction sets which are developed by javasoft people and placed as part of jvm. and the .class file gets executed. So here the point is to concentrate that jdk is platform dependent but .class is not platform dependent it is platform independent because jvm only responsible for running any .class files. Every jdk's jvm internally has predefined instructions sets i.e., approx. 200+.
Sun微系统已经发布了不同版本的jdk。一个用于基于 Windows 的,另一个用于基于 linux/unix 的。当我们用它安装 jdk 时,我们得到 jvm、jre 和 javac。假设我们在装有windows jdk 的intel 处理器的windows 中编写java 程序,那么jdk 的java 编译器会将.java 文件转换为.class 文件,其中包含类似于汇编语言代码的字节码指令,这些字节码指令可以只有 jvm 才能理解。如果我们采用在 windows os 中生成的 .class 文件,如果我们在 linux 中运行,那么该 linux 机器的 jvm 会使用大约 200 多个指令集在内部重写您的 java 程序,这些指令集由 javasoft 人员开发并放置在虚拟机。并且 .class 文件被执行。所以这里的重点是集中在 jdk 是平台相关的,但 .class 不是平台相关的,它是平台无关的,因为 jvm 只负责运行任何 .class 文件。每个 jdk 的 jvm 内部都有预定义的指令集,即大约。200+。
回答by Mitchell
The java language is just one thing but then many other devices such as mobile phones run their own version which is usually a trimmed down version to fit on the device. THese can also occasionally have other proprietary classes to help access the hardware (ie touchscreen). By making separate platforms based off a main one you get much support and a tighter more efficient programming language.
Java 语言只是一回事,但随后许多其他设备(例如手机)运行自己的版本,该版本通常是精简版以适合设备。这些有时也可以有其他专有类来帮助访问硬件(即触摸屏)。通过基于主要平台制作单独的平台,您可以获得更多支持和更紧密更高效的编程语言。