{3} Differences Between Java and C++
Java and C++: Similar or Different?
At first glance Java and C++ are strikingly similar. They both come from the same C family of languages. In fact, Java started as an extension to C++. However, under closer examination the languages bear some important differences. This chapter concentrates on the specific, usually subtle, differences between the languages. In the end you will come to know that Java and C++ are deceivingly dissimilar, and you will see why a well-thought-out library is necessary in order to reconcile the differences.
Similar Features But Different Implementations
It is true that Java and C++ source code bear strong resemblance to one another. However, nearly identical source code in Java and C++ often behaves quite differently. If the goal were simply to produce source code that compiles in both languages, that could be accomplished with a minimum amount of effort, albeit with limited benefits too. It is a much loftier goal to produce source code in C++ that behaves like it was written in the Java language. That is why the behavioral differences between Java and C++ are so important and why the Pie Run-time Library is necessary.
Practically every aspect of the Java language behaves or is implemented slightly different than C++. Let us consider some of these differences.
Fundamental Types. Although Java and C++ share many like-named fundamental types, the storage size of the Java versions are fixed, while the size of the C++ versions may be defined differently by each compiler.
Objects. All Java objects descend from the a common base class but this is not necessarily true in C++. Objects in Java are always created using the new keyword, but C++ provides various ways of instantiating new objects. Objects in Java are automatically deleted when they are no longer referenced. C++ requires that objects (created using the new keyword) be explicitly deleted, either directly using the delete keyword or indirectly using a “smart” pointer object.
References. Java supports object references which may or may not be null and also manage the lifetime of the objects they reference. C++ references are simply thinly veiled pointers and are almost always prevented from being null.
Run-time Type Information (RTTI). Java supports much richer information about types at run-time than C++ does. C++ only provide run-time type information for classes with at least one virtual method, which excludes most Standard Template Library classes like string.
Strings. In Java, strings—including string literals—store Unicode characters, are always immutable objects, and may not be extended (subclassed). In C++, strings may either be character arrays or string objects, which may or may not store Unicode characters. String literals are character arrays not string objects. String objects are not inherently immutable and may be subclassed. Additionally, the Standard Template Library (STL) in C++ does not limit strings to strings of characters but allows for strings of other types.
Arrays. In Java, arrays are “real” objects and benefit from object lifetime management and bounds checking of array indexes. Arrays in C++ are basically direct memory pointers. If they are allocated using the new keyword, they must be properly deleted.
Exceptions. Exceptions in Java are always passed as object references, while exceptions in C++ can be practically any type and be passed in a variety of ways.
This is just a sample of the behavioral and implementation differences between the two languages. For a more in-depth discussion of the differences see one of the sources listed at the end of this chapter.
As similar as the two languages look, their respective features are implemented quite differently. These differences are not irreconcilable but overcoming them requires ingenious, unobvious solutions. These solutions are provided by the Pie Run-time Library and are detailed in the next chapter, Pie Run-time Library Reference.
References
C Family of Languages “State of the Union” Interview, by Herb Sutter; C++ Report, July-August 2000; pp. 7-24, 47.
Java Primer Plus, by Paul M. Tyma, et. al.; Waite Group, 1996; pp. 179-180, 195, 243-260.
The Java/C++: Cross-Reference Handbook, by Frederick F. Chew; Prentice Hall PTR, 1997.
C++ for Java Programmers, by Timothy Budd; Addison Wesley, 1999.
Copyright
© 2003 PureNative Software Corporation.
All rights reserved.