Java

Differences between Java and C++

Design enhancements - Implementation detail enhancements - Reduced error proneness - Enhanced portability - Extended functionality - Conclusions - Notes - References

This article is intended to provide a technical overview over the differences between the Java and C++ programming languages. The article lists aspects that are present in Java and absent in C++. The list does not claim to be complete. Knowledge of either Java or C++ is assumed. Aspects of performance or of protection of implementation secrets are not covered here. The status of this article is ongoing.

Design enhancements

* single inheritance enforced
Strict single inheritance is enforced, which makes the design clearer. Instead of multiple inheritance the more controlled interface construct is supported.
* single-rooted classes
All classes are single-rooted by the class Object. This enables such things as the use of mixed-type containers.
* concept of packages
The concept of packages is used, i.e. a large, hierarchical namespace is provided (1). This prevents name overlaps in libraries.
* guidelines for in-source documentation
In-source code documentation comments are provided. Documentation keywords are supported as well: @author, @version, @param, etc.

Implementation detail enhancements

* no global data
Global data (outside class blocks) is not supported. This makes the interfaces less complex. However, static class data in (possibly uninstanceable) classes is supported.
* no operator overloading
Overloaded operators, which are hard to read, are not supported.
* explicit boolean type supported
boolean is an explicit type, different from int. Also the constants true and false are provided. This feature disables integer assignments in conditionals.
* useful root class methods supported
There are some methods, that are of importance to building data structures, predefined for each class (as inherited by Object): toString, equals, hashCode.
* array length accessible
The length of an array object is accessible through length.
* no goto
goto is not supported. However, multilevel break and continue is supported.
* multilevel break and continue
The control flow in nested loops is enhanced with multilevel break and continue.
* better syntax with superclass referencing
The syntax with superclass referencing is more readable: extends, super.

Reduced error proneness

* no dangling pointers
There are no dangling pointers since variables are initialized and only freed (eventually) if they come out of scope.
* nullpointers reasonable caught
Null pointers are caught by a NullPointerException. This enhances the error diagnostics.
* no explicit destructor code needed
Explicit destructor code is not needed. The use of garbage collection, the missing explicit memory freeing and the omission of explicit destructor code prevents memory leaks and referencing freed memory.
* automatic variable initialization
Variables are automatically initialized (see dangling pointers).
* runtime container bounds checks
The bounds of containers (arrays, strings, etc.) are checked at runtime and an IndexOutOfBoundsException is thrown if necessary.
* unchecked error conditions prevented through exceptions
Since error conditions are propagated through exceptions, they cannot be easily ignored.
* all methods and fields carry explicit access modifiers
Methods and fields carry explicitly one of the access modifiers. This prevents the missing overview in large class definitions.

Enhanced portability

* sizes of the integer types defined
The sizes of the integer types byte, short, int and long are defined to be 1, 2, 4 and 8 bytes.
* network byte order used
The portable network byte order is used to save the primitive types to file.
* no structs used
Structs, which pose problems with undefined alignment when saved to file or written over network, are not supported.
* extended class libraries provided
The functionality provided by the standard (extended) class libraries makes it unnecessary to implement own or use proprietary classes, which may be difficult to port across platforms (see Extended functionality).
* unicode provided
Unicode is used as the character type and as base for strings.

Extended functionality

* string class
A string class is provided.
* stream extensions
Extensions to the input/output streams are among others: StringBufferStream (strings useable as input to stream reading methods), mark() (an undo mechanism), readFully(), SequenceInputStream (concating of input streams).
* extended class libraries: package java.util
Supported among others: Enumeration (an iterator interface), Hashtable, Vector. (2)
* multithreading support
Multithreading is supported by the Thread class.
* support for critical section handling in multithreading
synchronized blocks or methods can be used to handle critical sections.

Conclusions

Within a software engineering point of view, Java seems to be the more robust programming language than C++.
The language catches some of the ugly design and implementation features that C++ offers.
The memory management based on garbage collection makes code more robust and might allow faster development.

Notes

(1) The ANSI C++ comitee is also working on name space support.

(2) STL provides some of the java.util functionality.

References

[1] The Java Language Specification, J. Gosling, B. Joy, G. Steele, Addison-Wesley, 1996

[2] The C++ Programming Language, B. Stroustrup, Addison-Wesley, 2nd Ed. 1991

Keywords: Java vs. C++, Java versus C++, differences between Java and C++, differences between C++ and Java


lr / Thu Mar 27 1997 - back to the Java page