Comparison of Java to C Plus Plus
|
This is a comparison of the Java programming language to the C++ 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:
- the programmer should not have to pay (efficiency-wise) for language features he 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++.
Language features
- Java has automatic garbage collection. C++ does not. Automatic garbage collection is available via various non-standard libraries in C++, but it is not part of the language.
- Java has considerably 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 executed by the Java virtual machine.
- Due to the crudeness 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. 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 backwards 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.
- 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, Java does not.
- Java has finalizers, C++ does not.
- Java features a
finally
clause intry
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 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 C++ one can construct pointers to pointers, which have no Java equivalent.
- 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
External references
- How Java Differs from C (http://www.ora.com/catalog/javanut/excerpt/index.html#except) -- excerpt from Java in a Nutshell by David Flanagan