This research paper is a concise comparison between the Java and the C/C++ programming ... language that could give programmers most of the power and speed of machine level code while letting the .... Application Versus Applet. Another ...
Java and C/C++ Language Features in Terms of Network Programming
Matthew Cook
Syed (Shawon) M. Rahman,
B.S. Software Engineering, Student
Ph.D., Assistant Professor
Dept. of Computer Science & Software Engineering University of Wisconsin - Platteville 1 University Plaza, Platteville, WI 53818, USA Phone: (608) 342 1625, Fax: (608) 342-1965 Email: {cookma, rahmans}@uwplatt.edu
ABSTRACT This research paper is a concise comparison between the Java and the C/C++ programming languages with special consider to the functionality of each in networking applications. A brief overview of the origins of each language is given as foundation for this comparison. The features of each language are then analyzed with respect to three categories: ease of use, security, and efficiency.
1. Introduction C++ and Java appear at first to be extremely similar: almost identical programming languages. While there is a lot of common syntax shared between the two languages, there are also some very major differences. C and C++ are very fast and powerful languages. The focus of these languages is building code that will run on a specific machine extremely efficiently. Because of this, in these languages the programmer has a lot of low level controls of what is going on in the computer. The trade off is that the programmer must have considerable knowledge of the system with which they are working with. Java on the other hand puts more emphasis on ease of use for the programmer and transfers this load to the computer. Low level control and overall processing speed are reduced in order to increase the ease of use for the programmer. Java places the focus on increasing the programmer's efficiency, while assuming a good machine to run it on. C/C++ assumes a good programmer, and focuses on the machine's efficiency.
2. Background 2.1. C C is by far the oldest of the languages being compared in this paper. It was developed by Dennis Ritchie of Bell Laboratories in 1972, as an alternative to other very low level programming languages at the time. The language was designed to be a "system implementation language" for the new UNIX operating system that was under production during the same time period. C is based on the type-less language B (developed by Bell Laboratories earlier that decade) and its parent language Basic Combined Programming Language (BCPL) (developed by Martin Richard's at MIT in the mid 60's) [2]. On a broad scope, C, B, and BCPL have many similarities. They are all "low level" languages designed to allow the programmer to interact with standard objects handled by the computer, such as characters, numbers, and addresses [3]. The emphasis in C was the creation of a language that could give programmers most of the power and speed of machine level code while letting the programmer write it at a more understandable level. This close interaction with the machine allows the C language to be converted to machine code by relatively light-weight compilers.
2.2. C++ As we could assume from the name, C++ is based on the earlier language C. Its design was also based on Simula's approach to organize programs, commonly known as "object-oriented" programming. C++ was developed to be a "better C" with support for techniques such as data abstraction and full object-oriented programming. The goal of
2
the new language was to make programming more enjoyable. Like C, C++ was also researched and developed at AT&T Bell Laboratories. Specifically, Bjarne Stroustrup was the lead developer in the creation of C++ in the early 1980s. C++ added many of new features to the C language. Most notably, rather then being an entirely procedural based language such as C, C++ now incorporated both object-oriented programming and procedural programming in its design. The original version of C++, called "C with Classes" was first used in 1980. It was not until 1983 that support for object-oriented programming was added. Two years later, in 1985, the language was made available for commercial use; and by the end of the decade, the language had begun the process of formal standardization by the American National Standards Institute (ANSI) [1].
2.3. Java Java is the newest of the languages discussed in this paper. It was originally developed at Sun Microsystems as a programming language called OAK. The purpose of OAK was to create a programming language that would be a cross platform and allow communication between small off-the-shelf systems (such as VCRs, or other home entertainment systems). As the internet became popular a web browser was developed using the OAK language, called WebRunner. As the popularity of the internet grew, so did the demand for development of applications such as WebRunner. Sun then began promoting their language; changing the name from OAK to Java and the name of the browser from WebRunner to HotJava. Since Java was designed to be a portable and cross platform language. It could not be compiled ahead of time into specific machine code such as C or C++. Instead Java code is compiled into machine independent byte code which can then be compiled into machine code at run time [4]. This is not the only difference between Java and C++. However, Java's focus is much more on ease of use for the developer. As such, the language is entirely object-oriented and includes other developer friendly features such as standard libraries for common tasks, as well as other features not found in C or C++, for example, automatic garbage collection.
3. Ease of Use 3.1. Libraries The availability of libraries of code for common tasks is a major factor which is how easy to use a language. If there is a pre-packaged and thoroughly tested solution to a programming problem, a lot of time and money can be saved by using these standard components instead of reinventing the wheel. Both C++ and Java contain at least some sort of standard libraries. In C++ this is limited to the basic (though extremely well tested) Standard Template Library which is a set of containers and other relatively simple classes. Because C and C++ leave a lot of
3
functionality up to the specific system we are working on, it is very difficult to provide more complex libraries that would be cross-platform compatible. Java, on the other hand, contains many built in libraries for networking, multi-threading, cryptography, xml parsing, database connectivity, graphical user interfaces and much more. This is made possible by design. Java was built from the ground up to be a system independent language and because of that, it is much easier to create standard libraries across different platforms. A good example of the ease of use provided by language libraries is creating a simple HTTP server application. Since C++ is so system dependent, the easiest solution is to use a specific library for that system (in this case Microsoft C++ .NET) or connect to existing system utilities to do the work (as can easily be done on UNIX systems). The same program can be developed quickly in Java and still be cross platform compatible simply by using the included standard networking libraries. Java indisputably wins any debate between it and the standard C++ language as far as networking libraries are concerned
3.2. Garbage Collection If we assume that automated garbage collection is generally a good thing, this is another area Java decidedly comes ahead comparinmg with C/C++. In C++ no garbage collecting is done for us, so all objects that are created must be deleted to avoid memory leaks. Because all objects need to be deleted when they are not being used, all objects must also have constructors written for them. This level of control over an object's memory allows programmers to create highly optimized programs. More often though it seems to make it extremely easy to cause an error by leaving dangling points or unreachable objects and of course memory leaks. Memory leaks are especially important in networking hardware where other systems are depending on as much up-time as possible in the software, and the user cannot afford to restart the system often to clear the memory after leaks. Java reduces the occurrences of such inconveniences by automating garbage collecting. Instead of de-allocating memory by hand when we are finished with the object, the system will do it for us. This is a definite increase in usability as compared with most systems, but does increase the requirements on the system needed to run a Java program.
3.3. Compilation Unlike many newer scripting languages such as Python, Perl, or Ruby; both C/C++ and Java must be compiled before they can be run on a machine. C and C++ source code must be compiled separately for different machines since it compiles straight into machine code. Java on the other hand can be compiled from the source code into byte code once, since the byte code is cross platform compatible. The downside here is that the byte code must still be translated into machine code before it can be run, and this processing work has now been moved to run time. The trade off for this extra portability in Java is the requirement for a Java "virtual machine" to be installed on the system to
4
translate the byte code into machine code at run time. Though this does add extra system requirements and has the potential to decrease the performance of a program at run time, it increases the ease of use for programmers and by a large factor. From a pure usability point of view, Java will again win over C/C++ in the area of compilation simply because of its cross platform compatibility.
3.4. File Structure There are no header files in Java. This can be a blessing or a curse, depending how the programmer is accustomed to writing code. In C++, the header files are often used as a reference for the rest of the code. The single .h file can be opened to view all attributes and functions of an object. When this header information is combined with the main body of code as in Java, the programmer must view all the code in the file instead of only the basic header information. Of course, the advantage is that instead of dealing with two separate files for each object as in C++, we now only need to manage one file per object. This issue of documentation in Java has also been addressed by the standard "javadoc" application. This application reads java class files and automatically produces HTML documentation for them; removing any need for the use of header files for documentation.
4. Security 4.1. Object Handling 4.1.1. Pointers Use of pointers can be a major security concern. Languages that use pointers, such as C++, are vulnerable to pointer hacks. That is, if a user has access to a pointer to one part of the program, and they know where it is stored in memory in relation to the rest of the program, they only need to slightly change the pointer to gain access to potentially private parts of the program. Understanding the proper use of pointers in C and C++ is a large part of the steep learning curve in developing applications in these languages. This is not an issue in Java since instead of passing pointers, all complex objects in Java are passed by reference and all array values must be accessed by index. An added advantage to this method is that, unlike pointers, a reference can be re-bound at any point without being destroyed. 4.1.2. Initialization Un-initialized objects in C++ can also cause program halting errors at run time. Java has addressed this issue by not requiring objects to be initialized. All objects have a default value or will default to null instead of not being defined at all.
5
4.1.3. Type Checking Type checking is another issue that can potentially be another cause of security issues. Allowing certain types of variables where they should not be (or are not expected to be) can cause a program to behave in unexpected ways that could be exploited. C++ has fairly tight type checking, but Java is even stricter. For example, in C++ integers can be used in evaluating Boolean expressions, in Java this is strictly disallowed. In some situations it may be more convenient to allow evaluations of this type, but in a network application the potential for a security risk outweighs the ease of use factor. 4.1.4. Inheritance Another way to gain access to private data in a C++ program is inheritance. In C++ a child class need not have the same security settings as its parent. That is, a parent class may be private, but a child class could be made public. This allows the inherited child (and other classes through the child class) to have access to data and methods in the parent class that were intended to be private. This is prevented in Java by requiring that the child class have the same level of restriction as the parent; so a public parent means a public child, and likewise with private. Multiple inheritances can also be an issue in C++. If two classes derive from a top level class, have a single child class that inherits from both of them, it is possible to have a situation in which (if a function or attribute has the same name in both mid level classes) it will not be clear which should be inherited in the child class [5]. Because of this, a technique known as "virtual" inheritance must be used in C++. Java on the other hand, does not allow for multiple inheritances. Instead it uses a system of implementing "interfaces" which allows it to avoid the issues caused by multiple inheritances in C++.
4.2. Exception Handling The C++ method of exception handling is to catch any exceptions (other than syntax errors that are caught at compile time) that are thrown at run time. At this point, the appropriate error handling function is called. Writing these error handling functions in C++ can even be a bit troublesome due to the nature of its inheritance trees. In C++ not every object is descended from a root object type as with Java, and likewise not every exception need be descended from a root throwable type, as with Java. This means that in C++ we cannot write a single error handling function that will absolutely catch all errors thrown. This issue is fixed in Java by using a single inheritance tree. In addition, Java also includes other helpful exception handling features, such as checking all arrays for out of bounds exceptions automatically when accessing them, and checking for exceptions at compile time instead of just at run time.
6
4.3. Application Versus Applet Another useful feature of Java is the idea of an applet. In C++ and most other programming languages everything is compiled into a single application. This application will essentially have full access to the system, limited only by the code contained in it. Java however gives the option of creating an applet instead of a full application. In an applet, the code is not allowed any permanent access to the system. So the code can be executed but no damage could be done to the system. This has changed with Java 1.1 and above which allows digitally signed Applets. In this case, the system can be accessed by applets which contain security certificates linking them directly to their producer so if any damage is incurred the appropriate people may take responsibility.
5. Speed or Efficiency This is the one area where Java literally has some catching up to do, as compared with C and C++. Traditionally, Java programs have been much slower to execute than their C/C++ counterparts. This should really come as no surprise given the much lower level nature of C++ and especially C. There are a number of reasons C and C++ are quicker than Java programs to execute. The primary reason is that they have already been compiled to machine code. Java intentionally does not compile to machine code until run time to preserve portability. It uses a JIT (Just-In-Time) compiler to translate the universal byte code into machine code, for that specific machine. Large advances have been made in these JIT compilers during recent years however, even to the point that for some applications Java may be up to 2-4 times faster than C++ [8].
5.1. Floating Point Calculations Floating point calculations are another area in which Java understandably lags behind C and C++. In C and C++ floating point calculations, like many other things, are left up to the specific system the program is running on. The precision of the calculation is decided by the machine and not the programmer. Java on the other hand uses what is called strictfp. That is, it restricts its floating point calculations to guarantee the same performance on every system. So there is a compromise in overall speed in floating point calculations for the sake of consistency.
6. Conclusions Obviously both C/C++ and Java have their strengths and weaknesses. C and C++ work extremely well for systems with limited hardware capabilities where the programmer needs precise control over how the system will work and raw speed is of the utmost performance. This could be very critical for some networking applications, especially in embedded systems or other very specific uses.
7
However, hardware is becoming relatively cheap, and in most network applications it can is cheaper in the long run to allow the computer to do the work for us. For example, Google do not have a few super computers doing all the work and then try and maintain these. They instead do what is more convenient and run many cheap systems, using the software instead to compensate when a system goes down. The point is that in networking applications raw performance is not necessarily the best indicator of which is a better language to use. The ease of use of Java leads to a higher reliability, which makes it the superior choice in programming languages for network applications.
References [1] Stroustrup, B. An Overview of the C++ Programming Language The Handbook of Object Technology, CRC Press LLC, Boca Raton, 1999 [2] Ritchie, D. M. The Development of the C Language History of Programming Languages-II ed. Thomas J. Bergin, Jr. and Richard G. Gibson, Jr. ACM Press, New York, NY, and Addison-Wesley, Reading, MA,1996. [3] Kernighan, B.W. and Ritchie, D. M. The C programming Language Prentice-Hall,1988 [4] Gosling, J., Joyn B., Steele, G., and Bracha, G. The Java Language Specification Third Edition Addison-Wesley, Reading, Ma, 2005. [5] Martin, R. C. Java and C++ A critical comparison. Cambridge Sigs Reference Library Series, 1998. [6] Aitken, G. Moving from C++ to Java Dr. Dobb’s Journal of Software Tools http://www.ddj.com/, July 22, 2001. [7] Bull, J. M., Smith, L. A., Pottage, L., and Freeman, R. Benchmarking Java against C and Fortran for Scientific Applications EPCC, University of Edinburgh, Edinburgh, Scotland, U.K., 2001 [8] Walker, W., Lamere, P., and Kwok, P., FreeTTS - A Performance Case Study Sun Microsystems Laboratories, Palo Alto, CA, August 2002
8