Java 和 C++ 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/387256/
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
What is the difference between Java and C++?
提问by user45161
What is the difference between Java and C++? Are both object-oriented?
Java 和 C++ 有什么区别?两者都是面向对象的吗?
回答by David Thornley
This is far too general a question to be answered here.
这是一个太笼统的问题,无法在这里回答。
Java is an explicitly object-oriented language, with the harder-to-use bits snipped off.
Java 是一种明确的面向对象的语言,删除了较难使用的部分。
C++ is a multi-paradigm language with the safety off. You can do object-oriented programming in it, as well as procedural and generic.
C++ 是一种多范式语言,安全性关闭。您可以在其中进行面向对象的编程,以及过程和泛型。
If you had a more specific question, we could be of more help. Why did you ask? If you want recommendations for a particular platform, or project, or whatever, we could be more responsive.
如果您有更具体的问题,我们可以提供更多帮助。你为什么问?如果您需要针对特定平台、项目或其他任何内容的建议,我们可以做出更快的响应。
回答by Daniel Paull
A C++ programmer will tell you that Java is rubbish. A Java programmer will tell you that C++ is rubbish. Therefore I conclude that they are indeed the same thing.
C++ 程序员会告诉你 Java 是垃圾。Java 程序员会告诉你 C++ 是垃圾。因此我得出结论,它们确实是同一回事。
回答by Josef
Each language designed with different purposes in mind, so IMO it's not fair to compare the two from one perspective, and ignore the other.
每种语言的设计目的都不同,因此 IMO 从一个角度比较两者而忽略另一个是不公平的。
Generally speaking, C++ is an open standard, designed for implementing high performance systems where speed and performance and critical, there are lots of impressing projects designed using this language like Phoenix Lander, Adobe Acrobat Reader and others. C++ gives the developer the ability to program using a very high level abstraction -using generics for example, and, when needed, go down deep to the bare metal of the machine -to handle an interrupt for instance.
一般来说,C++ 是一个开放标准,旨在实现对速度和性能至关重要的高性能系统,有许多使用这种语言设计的令人印象深刻的项目,如 Phoenix Lander、Adobe Acrobat Reader 等。C++ 使开发人员能够使用非常高级的抽象进行编程 - 例如使用泛型,并且在需要时深入到机器的裸机 - 例如处理中断。
Java was designed with other purposes in mind, when Sun was planning Oak (later called Java), it focused on web applications so it supported the language with a bunch of heavy libraries of easy-to-use interfaces considering that. and portability (Compile once, run anywhere) using JVM, which prevents the programmer from coding to specific machine, but instead coding to a sandbox which in turn runs the code on the hosting machine, and this has obviously negative reflections on performance/speed.
Java 的设计考虑了其他目的,当 Sun 计划 Oak(后来称为 Java)时,它专注于 Web 应用程序,因此考虑到这一点,它通过大量易于使用的接口库来支持该语言。和使用 JVM 的可移植性(一次编译,在任何地方运行),这会阻止程序员编码到特定机器,而是编码到沙箱,沙箱又在托管机器上运行代码,这显然对性能/速度有负面影响。
Comparison of those two language is a popular cause of debate between programmers, and this is due to their different working demands and nature, IMO every language has made mistakes in order to mature, for example, C++'s exported templates, and Java's lack of procedural programming (Big Mistake). plus, each one has its pros and cons regarding different aspects, hence the one that balance productivity/performance issue IS the right language.
这两种语言的比较是程序员之间争论的一个流行原因,这是由于他们不同的工作需求和性质,IMO每种语言为了成熟都犯了错误,例如,C++的导出模板,以及Java缺乏程序化编程(大错)。此外,每种语言在不同方面都有其优缺点,因此平衡生产力/性能问题的语言是正确的语言。
For more information Wikipedia's comprehensive article on Comparison of Java and C++
有关更多信息,请参阅维基百科关于 Java 和 C++ 比较的综合文章
It might be interesting to take a look at what languages are used (and being used) to create major systems (like Google) from here.
从这里查看使用(和正在使用)哪些语言来创建主要系统(如 Google)可能会很有趣。
回答by Michael Sharek
One of the most important differences hasn't been mentioned yet - one is compiled to machine code, the other is compiled to bytecode which is interpreted by a virtual machine.
最重要的区别之一尚未提及 - 一个被编译为机器代码,另一个被编译为由虚拟机解释的字节码。
回答by Jared
回答by Dipstick
I love c++ but unless you absolutely need to use c++ then use something else. When you need to use c++ then you will know the difference, Grasshopper.
我喜欢 c++,但除非您绝对需要使用 c++,否则请使用其他东西。当您需要使用 c++ 时,您就会知道其中的区别,Grasshopper。
(hint do not write device drivers, video decoders, encryption libraries, 3-d graphics engines or language run-time engines in java).
(提示不要在 java 中编写设备驱动程序、视频解码器、加密库、3-d 图形引擎或语言运行时引擎)。
回答by OscarRyz
Yes, both are object oriented programming languages.
是的,两者都是面向对象的编程语言。
C++ is an evolution to C. Which was a system programming language. C++ Added many features to the language to make it object oriented. It became the mainstream programming language for that reason.
C++ 是 C 的演变。C 是一种系统编程语言。C++ 为该语言添加了许多特性以使其面向对象。由于这个原因,它成为了主流的编程语言。
Java is an evolution of C++, with different goals ( cross platform for instance ). It remove some of the features that make C++ so hard to learn. Simplify others and remove others.
Java 是 C++ 的演变,具有不同的目标(例如跨平台)。它删除了一些使 C++ 难以学习的特性。简化他人,删除他人。
The main difference is C++ programs are compiled directly to machine code ( understood by the CPU ) while Java programs are compiled to be run in a "Virtual Machine" the JVM most of the cases. For these reasons java programs were interpreted by another program and at the beginning were veeeery slow programs. Nowadays the VM may optimize this code and make it run very very fast.
主要区别在于 C++ 程序直接编译为机器代码(由 CPU 理解),而 Java 程序在大多数情况下编译为在“虚拟机”JVM 中运行。由于这些原因,java 程序被另一个程序解释,并且在开始时是非常缓慢的程序。现在虚拟机可能会优化这段代码,让它运行得非常快。
回答by Jason Coco
Both are object oriented but they are very different languages. This probably isn't the best forum to ask for the differences... I would suggest you look both up on Wikipedia and review the descriptions there. You will be able to see the differences very quickly for yourself.
两者都是面向对象的,但它们是非常不同的语言。这可能不是询问差异的最佳论坛......我建议您在维基百科上查找并查看那里的描述。您将能够很快看到自己的差异。
回答by Astha
- Everything is Object in Java as everything is derived from java.lang.Object But this is not the case in C++
- No pointers in Java whereas C++ has provide support for pointers
- No destructors in java (Java has automatic garbage collection) but C++ has destructors to do that
- Thread support is built in Java but not in C++
- No scope resolution operator in Java
- No Goto statement in Java
- No Multiple Inheritance allowed in Java but C++ allows that
- No operator overloading is allowed in Java but C++ allows that
- Java is interpreted for most part and hence Platform independent
- Java 中的一切都是对象,因为一切都源自 java.lang.Object 但在 C++ 中并非如此
- Java 中没有指针,而 C++ 提供了对指针的支持
- Java 中没有析构函数(Java 具有自动垃圾收集功能)但 C++ 有析构函数来做到这一点
- 线程支持是在 Java 中构建的,但不是在 C++ 中构建的
- Java 中没有作用域解析运算符
- Java 中没有 Goto 语句
- Java 中不允许多重继承,但 C++ 允许
- Java 中不允许运算符重载,但 C++ 允许
- Java 大部分被解释,因此与平台无关
回答by Astha
Gross but accurate oversimplification: Java is easier. C++ is faster.
粗略但准确的过度简化:Java 更容易。C++ 更快。

