什么是便携性?如何比Java等语言更加便携? [英] What is portability? How is java more portable than other languages?

查看:278
本文介绍了什么是便携性?如何比Java等语言更加便携?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道怎么的Java比C,C ++和.NET和其他语言更加便于携带。我已经由于跨preTER和JVM了解Java的可移植了很多次,但是JVM只是隐藏在硬件体系结构的差异,对不对?我们还是需要对不同的计算机体系结构不同的JVM。我缺少的是在这里吗?因此,如果有人对C最常见的架构写一个抽象层,让我们说的CVM,那么一旦被安装CVM的C程序将这些架构上运行,不是吗?

I wonder how Java is more portable than C, C++ and .NET and any other language. I have read many times about java being portable due to the interpreter and JVM, but the JVM just hides the architectural differences in the hardware, right? We'd still need different JVMs for different machine architectures. What am I missing here? So if someone writes an abstraction layer for C for the most common architectures, let's say the CVM, then any C program will run on those architectures once CVM is installed, isn't it?

到底是什么这个便携性? .NET可以被称为便携式?

What exactly is this portability? Can .NET be called portable?

推荐答案

便携性不是一个黑与白,是或否之类的话。便携性是一个人如何可以轻松地我拿一个程序,对所有人问津的平台上运行。

Portability isn't a black and white, yes or no kind of thing. Portability is how easily one can I take a program and run it on all of the platforms one cares about.

有会影响这几件事情。一个是语言本身。 Java语言规范一般留给更达人实施。例如,I = I + +未定义的C和C ++,但在Java中定义的含义。更实际地说,类型,如INT有在Java中的特定尺寸(例如:int是总是32位),而在C和C ++的大小取决于平台和编译器。单单这些分歧并不prevent您从C和C ++编写便携式code,但你需要有很多更勤奋。

There are a few things that affect this. One is the language itself. The Java language spec generally leaves much less up to "the implementation". For example, "i = i++" is undefined in C and C++, but has a defined meaning in Java. More practically speaking, types like "int" have a specific size in Java (eg: int is always 32-bits), while in C and C++ the size varies depending on platform and compiler. These differences alone don't prevent you from writing portable code in C and C++, but you need to be a lot more diligent.

另外一个是库。 Java有一大堆的C和C ++没有标准库。例如,线程,网络和GUI库。这些类型的图书馆为C和C ++的存在,但它们不是标准的一部分,并提供相应的库可以有很大的不同,从平台到平台。

Another is the libraries. Java has a bunch of standard libraries that C and C++ don't have. For example, threading, networking and GUI libraries. Libraries of these sorts exist for C and C++, but they aren't part of the standard and the corresponding libraries available can vary widely from platform to platform.

最后,还有你是否可以只取一个可执行文件,并把它的其他平台上,并在那里工作的整个问题。这通常与Java的作品,假设有目标平台JVM。 (也有对人关心多/大多数平台的JVM)这通常是不与C和C ++实现。你通常会在至少的需要进行重新编译,这是假设你已经采取了previous照顾两个点。

Finally, there's the whole question of whether you can just take an executable and drop it on the other platform and have it work there. This generally works with Java, assuming there's a JVM for the target platform. (and there are JVMs for many/most platforms people care about) This is generally not true with C and C++. You're typically going to at least need a recompile, and that's assuming you've already taken care of the previous two points.

是的,如果一个CVM存在多个平台,这将使C和C ++更便携 - 样的。你仍然需要编写你的C code无论是在便携的方式(例如:假设一无所知比标准说什么一个int其他的大小),或者你会写信给CVM(假设它已经取得了所有在所有目标平台这类事情的统一决定)。你还就需要放弃使用非标准库(没有联网,或线程GUI)或写为这些目的的具体CVM的库。于是,我们并不是真的在谈论这使得C和C ++更便携,而是一种特殊CVM-C / C ++这就是便携。

Yes, if a "CVM " existed for multiple platforms, that would make C and C++ more portable -- sort of. You'd still need to write your C code either in a portable way (eg: assuming nothing about the size of an int other than what the standard says) or you'd write to the CVM (assuming it has made a uniform decision for all of these sorts of things across all target platforms). You'd also need to forgo the use of non-standard libraries (no networking, threading or GUI) or write to the CVM-specific libraries for those purposes. So then we're not really talking about making C and C++ more portable, but a special CVM-C/C++ that's portable.

再次,便携性不是黑色和白色的东西。即使与Java也可以采取不兼容。该GUI库(尤其是AWT)是具有不一致的行为那种臭名昭著,和任何涉及线程可以不同的行为,如果你得到马虎。但一般情况下,这是一个更容易采取写在一个平台上不平凡的Java程序,并在另一个运行它比它是做同样的用C或C ++程序。

Once again, portability isn't a black and white thing. Even with Java there can still be incompatibilities. The GUI libraries (especially AWT) were kind of notorious for having inconsistent behavior, and anything involving threads can behave differently if you get sloppy. In general, however, it's a lot easier to take a non-trivial Java program written on one platform and run it on another than it is to do the same with a program written in C or C++.

这篇关于什么是便携性?如何比Java等语言更加便携?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆