Java vs跨平台C ++:有什么区别? [英] Java vs cross platform C++: What is difference?

查看:65
本文介绍了Java vs跨平台C ++:有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个区别:

Java在任何地方都可以写入。

C ++编写一次编译。



但是还有其他差异吗?

我可以在跨平台Java中做些什么,而不是在跨平台C ++中做什么?

I know one difference:
Java is write once run anywhere.
C++ is write once compile anywhere.

But are there other differences?
Is there something i can make in cross platform Java, but not in cross platform C++?

推荐答案

请看我最后对这个问题的评论,一个说我觉得我理解这个混乱。



要理解事情,你需要了解所有的从CPU到应用程序的操作。你不这样做,所以很难解释,需要时间和耐心。



你认为这些应用程序如何与OS配合使用? C ++和所有寻址本机平台的语言,编译为本机代码,编写本机CPI指令。此代码与启动应用程序时进程执行代码的代码不完全相同。代码由编译器创建,并使用相对寻址并引用代码的其他部分,这些代码部分以二进制obj或lib文件中的名称(字符串)的形式规定。这些文件由链接器放在一个可执行代码(DLL或EXE,无关紧要,这只是PE文件)中。链接器采用符号名称并按地址链接它们。那些地址仍然是相对的,只有符号被消除。然后第三个玩家出现了 loader ,这是最不为人知和最复杂的部分。它实际上在内存中创建代码。每个进程使用虚拟内存获取其单独的虚拟地址空间。代码块,所有调用,对象与引用它们的地址同步移位,以适应OS提供的内存,设置代码和数据描述符。这里的问题是:操作系统调用在哪里?回到编译和链接之间的阶段:它们是OS DLL(共享对象或其他东西)和函数名称的符号名称。这一切都在PE文件中得到解决。



现在,所有操作系统都不同,因此编译后的代码不同。不仅仅是名称:OS调用没有一对一的对应关系。除了常见的标准库事物(例如控制台和文件I / O)之外,没有什么是跨平台的。它不是跨平台,它是便携式的。这意味着存在将OS与那些标准便携式调用绑定的库。源代码是相同的,但实现方面的库是不同的。代码的其他方面,尤其是UI,是不可移植的。它比UI更多:串行和并行I / O,同步原语,系统特定的设施,很多东西。请注意,相同的库具有每个平台和每个CPU 指令集架构的单独版本。比如,Windows for x86是一回事,Windows for x86-64是另一回事,Windows for IE-64则不同,所以这些平台的C ++库也是如此 - 它们都是不同的版本。还有更多的平台,非微软一次。



如何使复杂的C ++应用程序可移植?有UI和其他功能?只有一种方法:只有当它们带有扮演 OS和硬件抽象层角色的库时。一个例子是QT: http://en.wikipedia.org/wiki/Qt_%28software%29 [ ^ ],https://qt-project.org/ [ ^ ]。在这个例子中,相同的QT应用程序如何在不同的平台上运行?首先,它不能:首先,它需要为不同的平台重新构建。据说每个平台都有一个版本的QT,它应该安装在开发机器上,只有你可以构建它。但是,在构建解决方案时,您需要将其与应用程序一起部署到由一个或另一个QT版本支持的平台。如果此类版本不存在,则您的应用程序将无法运行。如果用户在被要求确认时说QT是什么?谁知道?我不想安装它,您的应用程序将无法运行。它是便携式的吗?是的,可能吗?它是跨平台吗?哈哈,只是在我刚才解释的意义上。



使用Java,情况有所不同。这种操作系统和硬件抽象的作用由其虚拟机,JVM扮演。这不是一个图书馆。这是一个在本机操作系统之上工作的整个平台。有针对不同操作系统的JVM实现。它们必须有表面:后端面向本机操作系统,所以它们都是不同的,与我上面提到的C ++库一样。在顶部,前端大小,它面向Java应用程序,这个界面是统一的。 Java不在CPU指令中编译,它编译为虚拟机的虚构指令,字节码。使用即时编译(JIT)即时编译字节代码。虚拟机将字节码指令转换为本机代码并执行它。



使用.NET,操作系统和硬件抽象部分是实现的.NET Framework CLR 的。 .NET应用程序也编译了一些字节码,CIL代码,使用JIT编译,但编译后的CIL代码被打包成常规PE文件,但文件很特殊,只有安装了CLR才能工作。有针对不同操作系统的CLR实现,例如Mono。相同的PE文件可用于不同的CPU架构和不同的平台,不进行重新编译,但有一些例外:应用程序可以针对具体的CPU指令集架构,通常是为了与某些本机代码模块兼容(在.NET中它可以使用C ++ / CLI或P / Invoke完成,Java也可以通过JNI使用本机代码)。这些应用程序失去了部分可移植性,它们只能与与目标CPU兼容的CPI工作。



请参阅:

http://en.wikipedia.org/wiki/Java_virtual_machine [ ^ ],

http://en.wikipedia.org/wiki/Bytecode [ ^ ],

http://en.wikipedia.org/wiki/Virtual_machine [ ^ ],

http://en.wikipedia.org/wiki/Portable_Executable [ ^ ],

http://en.wikipedia.org/wiki / Just-in-time_compilation [ ^ ],

http://en.wikipedia.org/wiki/Compiler [< a href =http://en.wikipedia.org/wiki/Compilertarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Linker_%28computing%29 [ ^ ],

http://en.wikipedia.org/wiki/Loader_%28computing%29 [ ^ ],

http://en.wikipedia.org/wi ki / Instruction_set [ ^ ],

http://en.wikipedia.org/wiki/Common_Language_Runtime [ ^ ],

http://en.wikipedia.org/wiki/Platform_Invocation_Services [ ^ ],

http: //en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ],

http:// en.wikipedia.org/wiki/Cross-platform [ ^ ],

http://en.wikipedia.org/ wiki / Operating_system_abstraction_layer [ ^ ],

http://en.wikipedia.org/wiki/Hardware_abstraction [ ^ ]。



- SA
Please see my last comment to the question, the one saying "I think I understand the confusion".

To understand things, you need to understand all the operation from CPU to the application. You don't, so it's hard to explain, needs time and patience.

How do you think the applications work with OS? C++ and all languages addressing native platforms, compile to native code, writing native CPI instructions. This code is not exactly the same as the code when it is executed by the process when an application is started. The code is created by a compiler and uses relative addressing and referenced to other parts of code, which are prescribed in the form of names (strings) in the binary obj or lib file. Those files are put together in one executable code (DLL or EXE, it does not matter, this is just the PE file) by the linker. The linker takes the symbolic names and links them by addresses. Those addresses are still relative, only the symbols are eliminated. Then the third player comes up, the loader, the least known and most sophisticated part. It actually creates code in memory. Each process gets its separate virtual address space using virtual memory. The code blocks, all calls, objects are shifted in sync with addresses referencing them, to fit in the memory the OS provides, code and data descriptors are set up. The problem here: where are the OS calls? Get back to the stage between compilation and linkage: they are the symbolic names of OS DLLs (shared objects, or something else) and function names. This is all resolved in the PE file.

Now, all OS are different, so the compiled code is different. Not just in names: OS calls do not have one-to-one correspondence. Nothing is cross-platform, except common standard library things, such as console and file I/O. It's not "cross-platform", it's portable. It means that there are libraries that bind OS with those standard portable calls. The source code is the same, but the libraries are different on implementation side. Other aspects of code, notably, UI, is not portable. It's a lot more than UI: serial and parallel I/O, synchronization primitives, system-specific facilities, a lot of things. Note that the same library has separate version of every platform and every CPU instruction set architecture. Say, Windows for x86 is one thing, Windows for x86-64 is another one, and Windows for IE-64 is different, so same goes for the C++ libraries for these platforms — they are all different version. And there are a lot more platforms, non-Microsoft once.

How more complex C++ application can be made portable? With UI and other features? There is only one way: only if they come with the libraries playing the role of OS and hardware abstraction layers. One example is QT: http://en.wikipedia.org/wiki/Qt_%28software%29[^], https://qt-project.org/[^]. On this example, how the same QT application can work on different platforms? First of all, it cannot: first, it needs to be re-build for different platform. Each platform supposedly has its one version of QT, which should be installed on development machine, only them you can build it. But when your solution is built, you need to deploy it with the application to the platform supported by one or another QT version. If such version does not exist, your application won't work. If the user, when asked for confirmation, says "What is that QT? Who knows? I don't want to install it", your application won't work. Is it portable? Yes, potentially? Is it "cross-platform"? Ha-ha, only in the sense I just explained.

With Java, things are different. The role of such OS and hardware abstraction is played by its virtual machine, JVM. This is not a library. This is a whole platform working on top of native OS. There are JVM implementations for different OS. They have to surfaces: the backs ends face native OS, so they are all different, in the same sense as the C++ libraries I mentioned above. On the top, front-end size, it faces Java application, and this interface is unified. Java does not compile in CPU instructions, it compiles to the "fictional" instructions of the virtual machine, byte-code. The byte code is compiled on the fly using Just-In-Time compilation (JIT). The virtual machine translates byte-code instruction into native code and executes it.

With .NET, the OS and hardware abstraction part is the .NET Framework implementing CLR. .NET applications are also compiled kind of byte-code, CIL code, JIT compilation is used, but the compiled CIL code is packed into regular PE files, but the files are special, then can only work if CLR is installed. There are CLR implementation for different OS, such as Mono. The same PE file can be used for different CPU architectures and different platforms, no recompilation is done, but there are exclusions: application can target concrete CPU instruction-set architectures, usually for compatibility with some native-code modules (in .NET it can be done with C++/CLI or P/Invoke, Java also can use native code via JNI). Such applications looses part of its portability, they can work only with the CPIs compatible with the target CPU.

Please see:
http://en.wikipedia.org/wiki/Java_virtual_machine[^],
http://en.wikipedia.org/wiki/Bytecode[^],
http://en.wikipedia.org/wiki/Virtual_machine[^],
http://en.wikipedia.org/wiki/Portable_Executable[^],
http://en.wikipedia.org/wiki/Just-in-time_compilation[^],
http://en.wikipedia.org/wiki/Compiler[^],
http://en.wikipedia.org/wiki/Linker_%28computing%29[^],
http://en.wikipedia.org/wiki/Loader_%28computing%29[^],
http://en.wikipedia.org/wiki/Instruction_set[^],
http://en.wikipedia.org/wiki/Common_Language_Runtime[^],
http://en.wikipedia.org/wiki/Platform_Invocation_Services[^],
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://en.wikipedia.org/wiki/Cross-platform[^],
http://en.wikipedia.org/wiki/Operating_system_abstraction_layer[^],
http://en.wikipedia.org/wiki/Hardware_abstraction[^].

—SA


java是独立于平台的,C ++是平台相关的



java是安全的而C ++不是安全的有指针。



在Java中你可以没有课,但是在C ++中你可以编写没有课程的程序。



在java中,有内存管理和健壮,C ++没有这些功能。



java是多道程序设计分布式,这些都找不到C ++。



有关C ++和JAVA之间的更多差异:



C ++ vs JAVA



显示C ++与JAVA的视频
java is Platform independent and C++ is platform dependent

java is secure whereas C++ is not secure as there are pointers.

In Java you can have class without class,but in C++ one can write program without class.

In java,there is memory management and robust,C++ doesn't have these features.

java is multiprogramming and distributed,these can not be found of C++.

For more Differences between C++ and JAVA:

C++ vs JAVA

Video showing C++ vs JAVA


这篇关于Java vs跨平台C ++:有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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