|
This is a comparison of the Java programming language to the C++ programming language. Java is a reflective, object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. ...
C++ (pronounced see plus plus, IPA: /siË plÉs plÉs/) is a general-purpose computer programming language. ...
Design aims
Both Java and C++ were designed to support general-purpose application programming. However, unlike Java, C++ was also designed to support systems programming. Because of this, C++ has several design principles that Java does not share, such as: Computer programming (often simply programming) is the craft of implementing one or more interrelated abstract algorithms using a particular programming language to produce a concrete computer program. ...
System programming (or systems programming) is the activity of building and maintaining, low-level, system software for computers, including operating systems, system utilities, compilers, and so forth. ...
- the programmer should not have to pay (efficiency-wise) for language features he or she does not use;
- the language should trust the programmer to the extent that it should allow the programmer to override the type system, perform manual memory management, perform pointer manipulation, and even execute arbitrary assembly language code.
These principles combined with C++'s C heritage are responsible for most concrete language differences between Java and C++. Memory management is the act of managing computer memory. ...
Assembly language or simply assembly is a human-readable notation for the machine language that a specific computer architecture uses. ...
The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a standardized imperative computer programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the...
Language features - Java has automatic garbage collection. Memory management in C++ is usually done by hand, or through Smart pointers. The C++ standard permits garbage collection, but does not require it. Garbage collection is rarely used in practice.
- Java has a somewhat simpler syntax than C++.
- C++ is normally compiled directly to machine code which is then executed directly by the operating system. Java is normally compiled to byte-code which is then interpreted by the Java virtual machine.
- Due to the lack of constraints in the use of some C++ language features (eg. unchecked array access, raw pointers), programming errors can lead to low-level buffer overflows, page faults, and segmentation faults. However, these errors can be avoided by adopting good development practices or using the C++ Standard Template Library containers (like vector, list and map). In Java, such errors either simply cannot occur or are detected by the Java virtual machine and reported to the application in the form of an exception.
- C++ is mostly backward compatible with C, and C libraries (such as the operating system API) are usually directly accessible from C++. In Java, access to native operating system and hardware functions requires the use of the Java Native Interface.
- Java has a considerably larger standard library than C++. The C++ standard library only provides components that are relatively general purpose, such as strings, containers, and I/O streams. The Java standard library includes components for networking, graphical user interfaces, XML processing, logging, database access, cryptography, and many other areas. This additional functionality is available for C++ by third party libraries.
- Java has generics. C++ has templates. See Comparison of generics to templates.
- C++ is generally more time and space efficient than Java, especially in digital signal processing and other low-level or arithmetic-heavy code.
- C++ features operator overloading. Java does not, although certain operators were overloaded for standard library classes such as String.
- Both Java and C++ distinguish between native types (these are also known as "fundamental" or "built-in" types) and user-defined types (these are also known as "compound" types). However, in C++ this distinction is far less extensive.
- C++ supports multiple inheritance of arbitrary classes. In Java a class can derive from only one class, but in addition a class can implement multiple interfaces.
- Java explicitly distinguishes between interfaces and classes. C++ does not have interfaces, but has a more general notion of classes which makes it possible to define classes with semantics equivalent to Java interfaces.
- Java and C++ use different idioms for resource management. Java relies mainly on garbage collection, while C++ relies mainly on the Resource Acquisition Is Initialization idiom. This is reflected in several differences between the two languages:
- In C++ it's common to allocate objects of compound types as local stack-bound variables which are destructed when they go out of scope. In Java compound types are always allocated on the heap and collected by the garbage collector.
- C++ has destructors, while Java has finalizers. C++ destructors are invoked anytime an object is explicitly deallocated or goes out of scope. In Java, finalizers are only run when objects are garbage collected, which is not guaranteed to happen before the program terminates.
- Java features a
finally clause in try statements, C++ does not. - The C++ type system incorporates the notion of const-ness. Java has no equivalent concept.
- In Java, whether a method parameter is passed by value or by reference is implicitly determined by whether or not the type of the parameter is native. In C++ whether a function parameter is passed by value or by reference is up to the writer of the function and becomes part of the function's type signature.
- C++ allows namespace level constants, variables, and functions. Java does not.
- C++ tends to adapt more to the environment and machine it runs on:
- The ranges of C++ fundamental types is platform dependent. Java native types are fixed.
- The encoding of string and character literals in C++ is platform dependent. The encoding of string and character literals in Java is fixed.
- In C++, pointers can be manipulated directly as memory address values. Java references on the other hand can only refer to objects or "null". Another way to look at this is that in C++ pointers are actual first-class objects, while in Java references are merely syntactic entities. An illustrative example of this is that in C++ one can construct pointers to pointers, which have no direct Java equivalent. One can, however, create references to objects that in turn contain references to the desired target objects.
- C++ features goto. In Java "goto" is a reserved keyword that is barred from use.
- In Java, bounds checking is implicitly performed for all array access operations. In C++, array access operations on native arrays are not bounds-checked, and bounds checking for random-access element access on standard library collections like std::vector and std::deque is optional.
- C++ features inline assembler. Java has no equivalent feature.
- Java features "static initializers"; blocks of code that are executed when a class is loaded. C++ has no equivalent feature.
- Java and C++ use different techniques for splitting up code in multiple source files. Java uses an advanced package system, C++ uses a header file inclusion system.
- C++ has a preprocessor. Java has no equivalent feature.
See also: Comparison of C# to Java In computing, garbage collection (also known as GC) is a form of automatic memory management. ...
A smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. ...
The first meaning of the term syntax, originating from the Greek words ÏÏ
ν (sun, meaning âtogetherâ) and ÏÎ±Î¾Î¹Ï (taxis, meaning sequence/order), can be described as the study of the rules, or patterned relations that govern the way the words in a sentence come together. ...
A system of codes directly understandable by a computers CPU is termed this CPUs native or machine language. ...
In computing, an operating system (OS) is the system software responsible for the direct control and management of hardware and basic system operations. ...
Byte-code is a sort of intermediate code that is more abstract than machine code. ...
An interpreter is a computer program that executes other programs. ...
A Java Virtual Machine or JVM is a virtual machine that runs Java byte code. ...
This article is about the computer data type. ...
In computer programming, a buffer overflow is an anomalous condition where a program somehow writes data beyond the allocated end of a buffer in memory. ...
In computer storage technology, a page fault is an exception which is raised by the memory management unit when a requested page is not mapped in physical memory. ...
A segmentation fault (sometimes referred to as segfault for short) is a particular error condition that can occur during the operation of computer software. ...
The Standard Template Library (STL) is a software library. ...
A Java Virtual Machine or JVM is a virtual machine that runs Java byte code. ...
Exception handling is a programming language construct or computer hardware mechanism designed to handle runtime errors or other problems (exceptions) which occur during the execution of a computer program. ...
In technology (especially computing), backward compatibility has several related but differing meanings: A system is backward compatible if it is compatible with earlier versions of itself, or sometimes other earlier systems, particularly systems it intends to supplant. ...
In computing, an operating system (OS) is the system software responsible for the direct control and management of hardware and basic system operations. ...
API redirects here. ...
The Java Native Interface (JNI) is a programming framework that allows Java code running in the Java virtual machine (VM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages, such as C, C++ and assembly. ...
In C++, the Standard Library is a collection of classes and functions, which are written in the core language. ...
In C++, the Standard Library is a collection of classes and functions, which are written in the core language. ...
Java 2 Platform, Standard Edition or J2SE is a collection of Java Application Programming Interfaces useful to any Java platform programs. ...
A computer network is a system for communication among two or more computers. ...
A graphical user interface (or GUI, sometimes pronounced gooey) is a method of interacting with a computer through a metaphor of direct manipulation of graphical images and widgets in addition to text. ...
The Extensible Markup Language (XML) is a W3C-recommended general-purpose markup language for creating special-purpose markup languages. ...
A database is an organized collection of data. ...
Cryptography has had a long and colourful history. ...
This is a comparison of generics (a type of generic programming) as implemented in the Java programming language (and proposed for the C# programming language) to templates in C++ and D. A quick note on terminology: the term generics can refer to a number of related generic programming techniques, but...
Digital signal processing (DSP) is the study of signals in a digital representation and the processing methods of these signals. ...
Arithmetic or arithmetics (from the Greek word αÏιθμÏÏ = number) in common usage is a branch of (or the forerunner of) mathematics which records elementary properties of certain operations on numerals, though in usage by professional mathematicians, it often is treated as a synonym for number theory. ...
In computer programming, operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, = or == are treated as polymorphic functions and as such have different behaviours depending on the types of their arguments. ...
Some programming languages allow multiple inheritance, in which a class can inherit behaviors and features from more than one superclass. ...
Java is a reflective, object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. ...
Resource Acquisition Is Initialization (often referred to by the acronym RAII) is a popular programming technique in C++ and D. The technique combines acquisition and release of resources with initialization and uninitialization of variables. ...
In object-oriented programming languages that use automatic garbage collection, a finalizer is a special method that is executed when an object is garbage collected. ...
On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ...
Const-correctness is a programming language feature that allows the programmer to indicate when an object can and cannot change by declaring the object const (or non-const). ...
Parameters are a way of allowing the same sequence of commands to operate on different data without re-specifying the instructions. ...
A type signature defines the inputs and outputs for a function or method. ...
This article is about the computer data type. ...
This page is about the programming command. ...
In computer programming, bounds checking is the name given to any method of detecting whether or not an index given lies within the limits of an array. ...
Inline assembler is a feature of programming languages, that enables very low level code written in assembler to be embedded in a high level language like C. This allows programmers to optimise a very performance-sensitive algorithm by sending individual commands to the computers CPU. This example of inline...
In computer programming, especially in the C programming language or C++, a header file is a text file containing small bits of program code, which is used to describe the contents of the main body of code to other modules. ...
A preprocessor is a program that takes text and performs lexical conversions on it. ...
This is a comparison of the C# programming language to the Java programming language. ...
External references - How Java Differs from C -- excerpt from Java in a Nutshell by David Flanagan
|