Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of Java and C++
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Design aims == The differences between the programming languages C++ and Java can be traced to their [[Virtual heritage|heritage]], as they have different design goals. C++ was designed for systems and applications programming (i.e., infrastructure programming), extending the [[procedural programming]] language [[C (programming language)|C]], which was designed for efficient execution. To C, C++ added support for [[object-oriented programming]], [[exception handling]], lifetime-based resource management ([[Resource Acquisition Is Initialization]] (RAII)), [[generic programming]], [[template metaprogramming]], and the [[C++ Standard Library]] which includes generic containers and algorithms (the [[Standard Template Library]] or STL), and many other general purpose facilities. Java is a general-purpose, concurrent, class-based, object-oriented{{sfn|Bloch|2018|loc=Foreword|pp=xi-xii}} programming language that is designed to minimize implementation dependencies. It relies on a [[Java virtual machine]] to be [[computer security|secure]] and highly [[porting|portable]]. It is bundled with an extensive library designed to provide abstraction of the underlying platform. Java is a statically typed object-oriented language that uses a syntax similar to (but incompatible with) C++. It includes a documentation system called [[Javadoc]]. The different goals in the development of C++ and Java resulted in different principles and design trade-offs between the languages. The differences are as follows: {| class="wikitable" ! style="width:50%;"| C++ ! style="width:50%;"| Java |- | Extends [[C (programming language)|C]] with [[object-oriented programming]] and [[generic programming]]. C code can most properly be used. | Strongly influenced by C++/C syntax. |- | Compatible with [[C (programming language)|C]] source code, except for a few [[corner case]]s. | Provides the [[Java Native Interface]] and recently [[Java Native Access]] as a way to directly call C/C++ code. However, native languages are not safe and applications using native methods are susceptible to memory corruption.{{sfn|Bloch|2018|loc=Chapter §11 Item 66: Use native methods judiciously|p=285}} If the code is not carefully written, native methods can lower the performance of the system because the garbage collector is incapable of monitoring or maintaining native memory usage, and there is a cost context-switching between native and non-native code.{{sfn|Bloch|2018|loc=Chapter §11 Item 66: Use native methods judiciously|p=285}} |- | [[Write once, compile anywhere]] (WOCA). | [[Write once, run anywhere]]/everywhere (WORA/WORE). |- | Allows [[procedural programming]], [[functional programming]], [[object-oriented programming]], [[generic programming]], and [[template metaprogramming]]. Favors a mix of paradigms. | Allows [[procedural programming]], [[functional programming]] (since Java 8) and [[generic programming]] (since Java 5), but strongly encourages the [[object-oriented programming|object-oriented]] [[programming paradigm]]. Includes support for creating [[scripting language]]s. |- | Runs as native executable machine code for the target [[instruction set]](s). | Runs on a [[Java virtual machine|virtual machine]]. |- | Provides object types and type names. Allows [[Reflective programming|reflection]] via [[run-time type information]] (RTTI). | Is reflective, allowing [[metaprogramming]] and dynamic code generation at runtime. |- | Has multiple binary compatibility standards (commonly Microsoft (for MSVC compiler) and Itanium/GNU (for almost all other compilers)). | Has one binary compatibility standard, [[cross-platform]] for OS and compiler. |- | Optional automated [[bounds checking]] (e.g., the <code>at()</code> method in <code>vector</code> and <code>string</code> containers). | All operations are required to be bound-checked by all compliant distributions of Java. [[HotSpot (virtual machine)|HotSpot]] can remove bounds checking. |- | Native [[Unsigned (arithmetic)|unsigned arithmetic]] support. | Native unsigned arithmetic unsupported. Java 8 changes some of this, but aspects are unclear.<ref>{{Cite web | url=https://blogs.oracle.com/darcy/entry/unsigned_api | title=Unsigned Integer Arithmetic API now in JDK 8 | access-date=17 March 2014 | archive-date=25 February 2017 | archive-url=https://web.archive.org/web/20170225213350/https://blogs.oracle.com/darcy/entry/unsigned_api | url-status=dead }}</ref> |- | Standardized minimum limits for all numerical types, but the actual sizes are implementation-defined. Standardized types are available via the standard library <code><cstdint></code>. | Standardized limits and sizes of all primitive types on all platforms. |- | Pointers, references, and pass-by-value are supported for all types (primitive or user-defined). | All types (primitive types and [[reference type]]s) are always passed by value.<ref>{{cite web |url= http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html |title= The Java Tutorials: Passing Information to a Method or a Constructor |publisher= Oracle |access-date=17 February 2013}}</ref> |- | [[Memory management]] can be done [[manual memory management|manually]] via <code>new / delete</code>, automatically by scope, or by smart pointers. Supports deterministic destruction of objects. [[Garbage collection (computer science)|Garbage collection]] ABI standardized in C++11, though compilers are not required to implement garbage collection. | Automatic [[Garbage collection (computer science)|garbage collection]].{{sfn|Bloch|2018|loc=Chapter §2 Item 7: Eliminate obsolete references|pp=123-125}} Supports a non-deterministic finalize() method, use of which is not recommended.<ref>{{cite web |url= http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html |title= The Java Tutorials: Object as a Superclass |publisher= Oracle |access-date=17 February 2013}}.</ref>{{sfn|Bloch|2018|loc=Chapter §2 Item 8: Avoid finalizers and cleaners|pp=29-33}} |- | [[Resource management (computing)|Resource management]] can be done manually or by automatic lifetime-based resource management ([[Resource Acquisition Is Initialization|RAII]]). | Resource management must generally be done manually, or automatically via finalizers, though this is generally discouraged. Has try-with-resources for automatic scope-based resource management (version 7 onwards). It can also be done using the internal API <code>sun.misc.Unsafe</code> but that usage is highly discouraged and will be replaced by a public API in an upcoming Java version. |- | Supports classes, structs ([[passive data structure]] (PDS) types), and unions, and can allocate them on the [[Dynamic memory allocation|heap]] or the [[Stack-based memory allocation|stack]]. | Classes are allocated on the [[Dynamic memory allocation|heap]]. [[Java version history#Java SE 6|Java SE 6]] optimizes with [[escape analysis]] to allocate some objects on the [[Stack-based memory allocation|stack]]. |- | Allows explicitly overriding types, and some implicit narrowing conversions (for compatibility with C). | Rigid [[type safety]] except for widening conversions. |- | The [[C++ Standard Library]] was designed to have a limited scope and functions, but includes language support, diagnostics, general utilities, strings, locales, containers, algorithms, [[Iterator#C++|iterators]], numerics, input/output, random number generators, regular expression parsing, threading facilities, type traits (for static type introspection) and Standard C Library. The [[Boost (C++ libraries)|Boost library]] offers more functions including network I/O. A rich amount of third-party libraries exist for GUI and other functions like: [[Adaptive Communication Environment]] (ACE), [[Crypto++]], various [[XMPP]] [[Instant Messaging]] (IM) libraries,<ref name="XMPP Software » Libraries">{{cite web|title=XMPP Software » Libraries|url=http://xmpp.org/xmpp-software/libraries/|publisher=xmpp.org|access-date=13 June 2013}}</ref> [[OpenLDAP]], [[Qt (software)|Qt]], [[gtkmm]]. | The standard library has grown with each release. By version 1.6, the library included support for locales, logging, containers and iterators, algorithms, GUI programming (but not using the system GUI), graphics, multi-threading, networking, platform security, introspection, dynamic class loading, blocking and non-blocking I/O. It provided interfaces or support classes for [[XML]], [[XSLT]], [[MIDI]], database connectivity, naming services (e.g. [[LDAP]]), cryptography, security services (e.g. [[Kerberos (protocol)|Kerberos]]), print services, and web services. SWT offered an abstraction for platform-specific GUIs, but was superseded by [[JavaFX]] in the latest releases; allowing for graphics acceleration and CSS-themable UIs. Although it doesn't support any kind of "native platform look" support. |- | [[Operator overloading]] for most operators. Preserving meaning (semantics) is highly recommended. | Operators are not overridable. The language overrides + and += for the String class. |- | Single and [[multiple inheritance]] of classes, including virtual inheritance. | Only supports single inheritance of classes,{{sfn|Bloch|2018|loc=Foreword|pp=xi-xii}} however interfaces may multiply extend other interfaces and classes may implement multiple interfaces. |- | Compile-time templates. Allows for [[Turing complete]] meta-programming. | [[Generics in Java|Generics]] are used to achieve basic type-parametrization, but they do not translate from source code to byte code due to the use of [[type erasure]] by the compiler. |- | Function pointers, function objects, lambdas (in [[C++11]]), and interfaces (using abstract classes). | Functions references, function objects and lambdas were added in [[Java 8]]. Classes (and interfaces, which are classes) can be passed as references as well through <code>SomeClass.class</code> and <code>someObject.getClass()</code>. |- | No standard inline documentation mechanism. Third-party software (e.g. [[Doxygen]]) exists. | Extensive [[Javadoc]] documentation standard on all system classes and methods. |- | <code>const</code> keyword for defining immutable variables and member functions that do not change the object. Const-ness is propagated as a means to enforce, at compile-time, correctness of the code with respect to mutability of objects (see [[const-correctness]]). | <code>final</code> provides a version of <code>const</code>,{{sfn|Goetz|Peierls|Bloch|Bowbeer|2006|loc=§3.4.1 Final fields|p=48}} equivalent to <code>type* const</code> pointers for objects and <code>const</code> for primitive types. Immutability of object members achieved via read-only interfaces and object encapsulation. |- | Supports the <code>[[goto]]</code> statement. | Supports labels with loops and statement blocks. <code>goto</code> is a reserved keyword but is marked as "unused" in the [https://docs.oracle.com/javase/specs/ Java specification.] |- | Source code can be written to be [[cross-platform]] (can be compiled for [[BSD]], [[Linux|GNU/Linux]], [[macOS]], [[Solaris (operating system)|Solaris]], [[Microsoft Windows|Windows]], etc., without modification) and written to use platform-specific features. Typically compiled into native machine code, must be recompiled for each target platform. | Compiled into [[Java bytecode]] for the [[JVM]]. Byte code is dependent on the Java platform, but is typically independent of [[operating system]] specific features. |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)