Comparison of Java and C++: Difference between revisions

Content deleted Content added
WikiCleanerBot (talk | contribs)
m v2.05b - Bot T20 CW#61 - Fix errors for CW project (Reference before punctuation)
m MOS:FIRSTABBReviation clarify, define before WP:ABBR in parentheses. WP:LINKs: update-standardizes, add, needless WP:PIPE > WP:NOPIPE.
Line 14:
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 ([[RAIIResource 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]].
Line 39:
| 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 [[Reflection (computer programming)|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)).
Line 60:
| 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).
 
Line 300:
* Via stack-allocated objects, C++ supports [[Resource Acquisition Is Initialization|scoped resource management]], a technique used to automatically manage memory and other system resources that supports deterministic object destruction. While scoped resource management in C++ cannot be guaranteed (even objects with proper destructors can be allocated using <code>new</code> and left undeleted) it provides an effective means of resource management. Shared resources can be managed using <code>shared_ptr</code>, along with <code>weak_ptr</code> to break cyclic references. Java supports automatic memory management using [[Garbage collection (computer science)|garbage collection]]{{sfn|Bloch|2018|loc=Chapter §2 Item 8: Avoid finalizers and cleaners|pp=29-33}} which can free unreachable objects even in the presence of cyclic references, but other system resources (files,{{sfn|Bloch|2018|loc=Chapter §2 Item 7: Eliminate obsolete references|pp=123-125}} streams, windows, communication ports, threads, etc.) must be explicitly released because garbage collection is not guaranteed to occur immediately after the last object reference is abandoned.
* C++ features user-defined [[operator overloading]]. Operator overloading allows for user-defined types to support operators (arithmetic, comparisons, etc.) like primitive types via user-defined implementations for these operators. It is generally recommended to preserve the semantics of the operators. Java supports no form of operator overloading (although its library uses the addition operator for string concatenation).
* Java features standard [[application programming interface]] (API) support for [[Reflectionreflective (computer science)|reflectionprogramming]] (reflection) and [[dynamic loading]] of arbitrary new code.
* C++ supports static and dynamic linking of binaries.
* Java has [[Generic programming#Generics in Java|generics]], whosewhich main purpose is to provide type-safe containers. C++ has compile-time [[Generic programming#Templates in C++|templates]], which provide more extensive support for generic programming and metaprogramming. Java has [[Java annotation|annotations]], which allow adding arbitrary custom metadata to classes and metaprogramming via an [[Annotation processing tool#Java|annotation processing tool]].
* Both Java and C++ distinguish between native types (also termed ''fundamental'' or ''built-in'' types) and user-defined types (also termed ''compound'' types). In Java, native types have value semantics only, and compound types have reference semantics only. In C++ all types have value semantics, but a reference can be created to any type, which will allow the object to be manipulated via reference semantics.
* C++ supports [[multiple inheritance]] of arbitrary classes. In Java a class can derive from only one class,{{sfn|Bloch|2018|loc=Foreword|pp=xi-xii}} but a class can implement multiple [[Interface (Java)|interfaces]]{{sfn|Bloch|2018|loc=Chapter §8 Item 8: Favor composition over inheritance|pp=87-92}} (in other words, it supports multiple inheritance of types, but only single inheritance of implementation).