University of Virginia, Department of Computer Science
CS655: Programming Languages
Spring 2000
Position Paper 5
Yves LEPOUCHARD
17 April 2000
Why I prefer C++ to Java
Motivation
Java applets are revolutionizing the Internet by providing a tool to add interactivity to web pages. Java is becoming a standard of the programming languages by extending its scope to other fields than internet applications. On the occasion of the mock trial, I concluded my verdict by claiming that the future belonged to overly complex programming languages. A question was asked by the grader: "Do you count Java in that category?" It is also to answer this question that I wanted to write this paper.
Brief similarities
As a beginner could note, a source code in Java looks like a source code in C++. These two languages definitely have some similarities. Let's give some examples from [1]:
- they both can be used to develop standalone applications,
- they both provide support for object-oriented programming,
- they share many keywords like public and private (which also have the same functions in this example),
- the packages of Java provide the same functionalities as the name spaces of C++.
However, it is much more what is different which interests us in this discussion.
Run-time checking
Java has an intense use of run-time checks to ensure the safety of a program. For example, Java takes advantage of these checks to verify the bounds of an array before doing any operation with it, while C++ may generate a segmentation fault if a misuse occurs. Even though these precautions may appear wise and useful, I would object for two reasons: these systematic checks do affect the performance of an application and, as illustrated by the problem set 3, run-time checking is not a panacea and is subject to security flaws. I do no think that giving the illusion of safety by trading performance is a good solution.
Inheritances
If both C++ and Java provide object-oriented programming, their interpretation of this hazy concept is different. Their divergence is particularly clear when we examine their respective solutions for inheritance. While Java only supports single inheritance considering multiple inheritance as an uncertain matter, C++ does provide the latter. Multiple inheritance can lead to tricky cases leaving most researchers of the field perplex. Analyzing these problems and providing the solutions of C++ is not in the scope of this paper. I would just note that the designers of Java have chosen to evade this issue preferring to deliver a clearly defined language. However, it seems that these designers traded expressiveness for simplicity. [2] provides an example where the user would appreciate multiple inheritance to be able to implement his specifications.
Memory management
In Java, memory management is reduced to garbage collection. Java programs are not allowed to forge a pointer to a function or a data and to access it directly. In C++, the situation is inverse. C++ has often been criticized for its lack of garbage collecting. However, many people have added garbage collectors to C++. Because designed on top of C, C++ provides the same mechanisms of pointer as its ancestor. Regarding to memory management, Java suffers from a lack of expressiveness by timorously avoiding accessing memory directly.
Exception handling
Both Java and C++ support exception handling mechanisms. For further details, [3] gives a study of the exception structures of these languages as well as a comparison between them. C++ provides more functions than Java by allowing the user to manage memory exhaustion for example. However, [2] argues that if both languages use a system of try and catch blocks, C++ unwinds the stack by calling destructors when an exception is thrown. If a user wants to clean up, he needs to put some code into these destructors. Java uses a more elegant solution by providing a finally clause which can be included in the try clause. This finally clause is executed whether the completion is achieved normally , through an exception or, through a control flow statement. [2] concludes that this system is much easier to use than the complex exception handling of C++. However, some users ([4] gives a good example) have designed efficient exception handling with C++.
Concurrency
As pointed out by [2], one clear advantage of Java is the support of threads when C++ provides nothing (as far as I know) to implement concurrency control.
Standardization
While Java has been standardized from its very beginning, programmers had to wait about 7 years to be provided a C++ standard. During this lapse of time, C++ applications proliferated. This trend has led to a situation where many C++ compilers and source codes are not fully compatible with each other. The early standardization of Java is considered as an advantage by most people.
My opinion is slightly different. I admit that the consistency of a language can be at stake when there exist too many different interpretations of some hazy specifications. However, I would tend to support the idea of an open language where specifications are much looser than those of any product of a committee of experts who actually represent only a small part (research, industry or military domain) of the potential users of a programming language. In my opinion, one major reason for the success of a language like C++ has been the emergence of a new class of "programming language consumers" based on cheap home PCs and, interconnected through the Internet. It is just normal that these consumers can gather and express their needs through a flexible programming language. My belief is that the time of Algol-like design is over.
Trade-offs and conclusion
The sections above have brought a comparative summary of relevant features provided by Java or C++. [5] offers a much more thorough comparison between C++, Java and also, Ada and C. From [5], I found interesting to notice that if Java does better for reliability and maintainability than C++, it was the only language of the set to only ensure a "partial" generality (to the extent necessary to satisfy the needs of embedded computer applications) as well as a partial efficiency. This evaluation is motivated by the fact that Java supports neither enumerated types nor generics (however, this weakness is well-known and "Pizza" is referred as a superset of Java that adds templates). Moreover, "Java's garbage collection raises questions about efficiency and guaranteed timing, especially in real-time systems".
The advantages and disadvantages of Java have to be related to the initial purpose of the language. The history of Java, developed by Sun, has been driven by the intent of providing mobile code. Java could be considered as a successful attempt to fulfill the demands of web developers to get efficient tools for the implementation of new functions. Java applets can be downloaded from a server and then be used on the machine of any user through a virtual machine implemented to be platform-independent. Therefore, it is logical to avoid any function that could be dependent from the hardware such as memory management.
As explained above, C++ has a more general background and is intended to be widely used in the industry, by PC owners and, actually anyone who happens to choose it. For expressiveness purposes, C++ is not afraid of being close to the hardware.
If Java is good programming language to enforce mobile code, very useful for internet applications, I would state that C++ should be preferred for any other kind of application. Its greater expressiveness and the constant development of new libraries or add-ons (that some people could call "hacks") by a dynamic community of programmers offer a very powerful and evolving tool. When I said that the future belonged to complex programming languages, I was considering the view of one programming language able to be used for any kind of application in the line of the Ada project. I was perhaps forgetting the possibility that this future could belong to specific programming languages, each adapted to a particular job. That's, in my opinion, what Java tends to promote and this is why I prefer C++.
References
- Java security: from Hotjava to Netscape and beyond, Felton E.W., Dean D., Wallach D.S. (1996), Princeton University, IEEE Symposium on Security and Privacy
- Java and C++ - A critical comparison, Martin R. (1996), http://idm.internet.com/opinion/java_v_cpp.shtml
- Exception mechanisms, De A., Harry J., Lepouchard Y., Li P. and Lu P. (2000), http://www.cs.virginia.edu/~yel4j/cs655/, University of Virginia, Department of Computer Science, project report
- Integrating Structured Exception Handling in the C++ exception mechanism, Pels R. (2000), http://www.drbob42.com/cbuilder/structur.htm
- Ada, C, C++, and Java vs. The Steelman, Wheeler D. A. (1996), http://www.adahome.com/History/Steelman/steeltab.htm