Graeme's Wiki

java vs cpp or Students.cpp?
A review of C++ versus Java, and which should be used in classrooms

Sean Enck
Kettering University
Computer Science Undergraduate
enck6535 at kettering dot edu


This paper aims to compare C++ and Java in a university setting. The question posed among staff and students at some universities is: should we be teaching Java or C++ as the current programming standard? Both are viable options, but would students benefit more from either language? The following discussions of both C++ and Java will give a little background or history of the language. The pros and cons of both Java and C++ will be weighed against each other in a classroom setting, what is good for students when it comes to programming languages and what would make a student struggle? The next step involves an implementation of algorithms and coding which will be used to show the amount of iterations C++ and Java can do within a 1000 millisecond time interval. Several experiments have been designed to test different code in each language. These experiments include an integer assignment, an array filling assignment, a recursive method, generic system output, a bubble sort, and finally retrieval of the current directory. This same code will be reviewed to see how many lines of code it takes for Java and C++ to have done the same amount of work. These results will be interpreted as to which language is the better language as far as speed and ease of use. Once both Java and C++ have been covered the future languages that are available for teaching courses in the next several years will be reviewed briefly. Finally, the better teaching language will be discussed and the reasons for the choice will be given.


Executables – Programs compiled into files that are attributed with the .exe file tag. These programs are mostly developed in accordance to Microsoft’s .NET Framework.
JVM – The Java Virtual Machine. Required to run any Java program. It is a code interpreter to run Java code on any machine acting as a gateway.
Multithreading– Sharing a single CPU between multiple tasks in order to minimize the time required switching threads. 5
JRE – Java Runtime Environment. The basic runtime libraries for a Java program. The JRE libraries are required to run several Java programs.
JDK – Java Development Kit. The standard download at Sun Microsystems’ website to start creating Java applications or applets. Includes the standardized libraries.
OOP – Object Oriented Program. The use of objects as a major part of a programming language’s program development. The best example would be Java.
Procedural Languages – a programming language in which the programmer specifies an explicit sequence of steps that will be followed to produce a result.
Programming Languages – In this paper refers to a plethora of different languages, ranging from C++ to Python to Pascal


Throughout the past several decades as computers have developed, so have the computer sciences. The computer sciences have changed with the times to create an easier development of applications on newer technology ranging from applications to hardware.

Whether it is Fortran, Pascal, Basic, C++ or Java, there is a good chance any programming language’s usefulness will end if it has not already. This leads universities to adopt a new programming language in order to keep up with the times. Some students feel cheated after being taught a dead language and all those courses they took have no practical use. So what language should a university choose? Today’s more likely candidates are C++ and Java. Both C++ and Java have proven they have the backing from a strong community and a good company. Neither language will die out any time soon. This paper will discuss the pros and cons of C++ and Java, implement algorithms in both C++ and Java to show differences in time complexity and the amount of coding required, and finally what may be coming up to replace C++ or Java as the new hot language.

2. The Opposing Languages: C++

First, we will start with a little history of C++. Comparing C++ and C shows that C++ adds a new programming philosophy to C 7. The development of C++ started in the early 1980’s. Though some of the basics of C have remained in C++, it is a whole different programming language as it is an Object Oriented language and C is a procedural language. Object Oriented Programming in C++ is more about the data then it is about the algorithms 7. This gives C++ a different look and feel then its predecessors.

Pros of C++
List 1 is a list of a few pros for C++ that will now be discussed.
The compilation of C++ is the first pro that will be looked at here. In C++, the program can be simply compiled and then run as an executable (though it depends on the platform). In Java this is not the case, compilation and interpretation slow down because of the Java Virtual Machine (JVM). The JVM ironically enough is not written in Java and thusly requires the code to be compiled and then interpreted by JVM prior to be run, which is three steps to run versus C++ with the two steps. Global variables in C++ allow for any easier pass of information across the program. In Java, no global variables requires that the variable is passed through a method, constructor or some other indirect path in order to move from one location to another. This requires some extra coding and more tracking of different variables. This can lead to more lines of code in Java for the same amount of work than C++. In C++, there is a combination of OOP and procedural where as Java requires all parts to be OOP based (or so it seems). There are no structs, unions or any of these programming concepts to be used in Java. Once again, this leads to more coding for the same amount of work in java versus C++. Developing a six-line struct in C++ would require another class in Java.

List 1 - Characteristics regarding C++ and Java4
Combination of Compilation and interpretation
Slow execution when interpreted
No Global Variables
Pure Object-Oriented

Fast Execution
Global Variables
Hybrid between procedural and Object-Oriented

C++ in the Real World
What about C++ use in real life situations?
C++ can be compiled into an executable and used mostly on a Window’s Platform. This is noticed by the heavy push of Microsoft’s Visual Studio 2005 and the .NET Framework. “Microsoft .NET is the Microsoft strategy for connecting systems, information, and devices through Web services so people can collaborate and communicate more effectively” 6. It is developed by Microsoft along with other languages like C#, J#, and Visual Basic. The aim is that C++ applications will be developed more for the Windows Platform and not as much for platforms like Unix or different distributions of Linux. Though this does not prevent C++ applications from being developed for Linux, Mac OS X, or Unix it does create a noticeable bias. Since Windows is run on a large amount of computers, this increases the usability of C++ programs and executables. With so many people using Windows, C++ is very useful for certain Windows developers.

3. The Opposing Languages: Java

Java is a relatively new language started and initially developed during the 1990’s. Developed by Sun Microsystems to power consumer appliances, it has grown into a totally different language 1. Though Java is based off of C and C++ it has grown into it’s own powerful language based on pure OOP. Unlike Sun Microsystems’ plan, Java has shown that system independence can be a key for programmers on any Operating System.

Pros of Java
Found in List 2 are some of the things Java has done better then C++ that will now be reviewed more in-depth.
Garbage collection isn’t really that key until dealing with very large amounts of data. However, when programming (unless in a classroom) the amount of data and algorithms used is extensive and thusly will almost assuredly require some amount of garbage collection. Forgetting to call garbage collection could lead to a slower program, but in Java garbage collection will be called if necessary, in C++ this call has to be made by hand.
Multithreading or a form of CPU sharing can truly increase the speed of a program. Where Java allows instructions to multithread and thusly creates a faster threading process, C++ doesn’t allow such CPU occurrences and thread tasking is left to a single and slower threading situation. Creating documentation, the key to future maintenance. In Java, documentation has one basic standard in Javadoc, not to mention HTML format documentation can be generated. In C++, though certain documentation standards do exist, there is no one creator of C++ and so any commenting style could be expected. This can lead to sloppy/a lack of commenting, which for any computer scientist can lead to confusion down the road when performing routine maintenance.

Characteristics regarding Java and C++4
Automatic garbage collection
Supports multithreading
Automatic generation of documentation in HTML format

No automatic garbage collection
No multithreading
No automatic documentation

Java in the Real World
Now for Java in a real life situation.
Java is developed by Sun Microsystems and Sun Microsystems only (with 3rd party libraries open to the public). This helps maintain something Java can hold over C++ indefinitely, which is that Java is system independent. Any program written Java can be moved from one Operating System to another (assuming the JRE is installed if necessary) with no hassle. GUI’s programmed in Linux will look the same in Windows or Mac OS X (assuming the correct libraries are used to achieve this). Though Java programs are .java files, they can be compiled to self running .jar files to allow for clickable running in a given Operating System (similar to an executable file in Windows). This concept allows for a Operating System migration with Java programs to require little to no hassle compared to a C++ change.

4. Java and C++ in the classroom

Now that there is a little more of a background about each language, the student aspect can be discussed.

Students and C++
Why C++ could be used in classrooms.
- C++ is faster than Java. It doesn’t require a Virtual Machine to run and is very fast as a native program. Students using C++ would see a much faster program when compared to Java.
- It is highly useable on Windows, the more popular Operating System today. Even if Linux distributions are found on a campus, most students are likely going to be using a Windows Platform
- The tools are top-notch. Visual Studio 2005 makes programming in C++ a much easier task then before for anyone
- C++ is a closer relation to C. C was truly the gateway to current languages and system level operations. For Computer Engineers this sort of programming is very important.
- It is a proven language. C++ has been around for quite some time and has much support from big names like Microsoft but also a highly developed community. Finding help online would be much easier when working with a solid community

Students and Java
What about Java?
- Java has a very standardized library. Downloading the Java Development Kit provides the complete Java library for use in programming. This makes teaching Java much more standardized when it comes to libraries as only the IDE will change.
- It is system independent. The program can be written on a Windows Platform and moved to a Linux/Unix machine with little if any change in coding required for very complex or simple programs
- Java is the "safer" language. Instead of allowing a user to allocate memory, Java handles all pointers and only gives the user the chance to reference to objects. In C++ a miss allocation of memory can lead to a system failure, loss of work and reboot.
- Close resemblance to newer languages. Many Java programmers can easily shift away from Java and to a language like C# that is up and coming with a very similar syntax to Java.
- API Support. With each new release of a Java version, the information regarding each part of Java is updated and accessible at Sun’s website. This creates a very easy reference for new programmers.

5. Evaluation of C++ versus Java

In order to show the differences of C++ and Java, two programming experiments have been designed from the ground-up specifically for this paper:
- A Speed Analysis
- A Length Analysis
- A quick explanation of each program may be given in pseudo-code to clarify.

Speed Analysis
The first experiment deals with the speed of C++ and the speed of Java. There will be five different test programs; each program will run for a 1000 millisecond duration. During this time, the given assignment will be run and the number of runs will be calculated. The total amount of runs in 1000ms is the data that will be used. Each program will be given five runs and the average will be used as the data reference. The programs will be the same except for the specific syntax each language will require.

Integer assignment
The integer assignment will be very basic, something to the extent of: Integer x = 5

Array assignment
The array assignment will assign a value to 5 array slots. A for loop will be used to fill each slot in the array.
For(int y = 0; y < 5; y++)
X[y] = y * 5;
X[y] = y * 5;
X[y] = y * 5;
And so on for X[0,1,2,3,4,5]

Recursive Method
A basic recursive method will be written to count from 10 to 0
if (start is 0)
start = start – 1

System output
The system output will be very simple:
Output(“Hello World”);

Bubble Sort
A simple bubble sort will sort an array of 20 items in unsorted order. It will go through the list and switch an element if the current value is greater then next.

Write to file
A file will be created and the line “Hello World” will be entered into the file.
Open File
Write Line “Hello World!”
Close File

Return the current working directory
The program will find the current working directory of the computer and output the directory. This program includes two parts, finding the directory and then outputting the directory to the console.
Find the directory

Length Analysis
The second experiment is not completing or running a program but determining how many lines of code were required to complete all the projects from the Speed Analysis.
- Lines with only brackets “{ }”, white space, or comments are not counted in the total amount of lines.

Example (in Java):
Class example{
Public static void main(String[] args){
Int five = 5;
//Comment or white space

This example would result in a line count of 4.

6. Results

The results of each experiment are shown in List 3 and List 4.

Speed Results
The results shown in List 3 show that C++ is in fact faster.
In a 1000ms interval, C++ executed nearly twice as many times as Java did when dealing with Array Assignment, Recursive calls, and Bubble Sorts.
The closest Java came to matching C++ was when writing to a file. In that circumstance Java was able to execute about 80% of what C++ was able to execute.
When accessing system information like the current directory, Java was only 65% capable when compared to C++.
All and all Java proved to be much less capable over 1000ms when compared to C++.

List 3 - Java and C++ Speed Results 1000ms (Averages)
Integer Assignment
Java: 10982272
C++: 19737397

Array Assignment
Java: 7964085
C++: 19524412

Recursive Call
Java: 7848842
C++: 19870911

System Output
Java: 2163
C++: 3443

Bubble Sort
Java: 460193
C++: 1021053

Write to file
Java: 3598
C++: 4455

Current Directory
Java: 319

Length Results
The length results prove yet another interesting fact. In most cases C++ programs required a fewer number of lines for a program then Java did. Even when Java matched the same amount of lines, it still ran slower as shown above. Array assignment for both Java and C++ required 13 lines of code; Java failed to run the assignment even half as many times. For write to file (Java’s most comparable speed to C++) the amount of coding required 4 more lines then C++ did. So even when Java is moving close to C++ speed, the amount of coding seems to have gone up.

Java and C++ Code Length Results
Integer Assignment
Java Lines: 11
C++ Lines: 10

Array Assignment
Java Lines: 13
C++ Lines: 13

Recursive Call
Java Lines: 16
C++ Lines: 15

System Output
Java Lines: 11
C++ Lines: 10

Bubble Sort
Java Lines: 40
C++ Lines: 39

Write to file
Java Lines: 19
C++ Lines: 15

Current Directory
Java Lines: 17
C++ Lines: 17

7. Other Languages

There are plenty of other languages currently supported and used. Whether it is Visual Basic or C# they are viable options that could some day replace C++ or Java in the struggle for teaching dominance.

C# is a newer .NET based programming language. In the near future it could replace C++ as the keystone language but it still has to overcome its infancy stages and a stronger community before it can really take off. C# is a resemblance of Java and C++ widely developed in Microsoft’s Visual Studio 2005 as a major part in the renovations of .NET.

Python is another newer language. It has some similar syntax to BASIC yet is considered more powerful then BASIC. In many ways Python could be compared to Perl or Java with very extensible modules to increase its usefulness 3. Python is an easier language to teach because of the syntax; however, its use still is questionable in “real world” applications.

Visual Basic
Visual Basic is a much simpler and visual-based language. Though its background may not show as much power as other .NET languages, it has its uses when dealing with creating applications where Visual themes are key. For GUIs and the like VB is the best call.

An older and still used language, C is not gone. Though some may say it is going to be phased out, it has still survived being “replaced” by other languages. For system programming, very few (if any) languages can match C’s best abilities.

The follow-up to J++, it is a language with an amazingly similar syntax/library/ideology as Java. J# is Microsoft’s response to Java. It is basically an integration of Java into Visual Studio and the .NET framework without forcing Microsoft and Sun Microsystems to enter into a legal contract. Though J# may end in the same manner as its big brother J++ 2, it could also become something bigger in the future.

Web-based (Perl, PHP…)
Whether we discuss Perl, PHP or any other mostly web-based language, the question of portability begins. Though they are often fast and require only a server, they are also not as easy to set-up for single users without server access. Yes, Perl or PHP can be run on a single computer but this is no easy task. Often times this sort of task will require an Internet server to be installed along with other packages for a specific language.

8. or Students.cpp?

After reviewing the difference in Java and C++, which should “win” in the classroom? Currently, Java should be the winner overall, especially for students with little to no prior programming experience. Even after reviewing other articles, Java still maintains a few better attributes then C++ as shown in the paper:
- Java allows for less freedom, which is good for novices. Memory allocation, garbage collection, pointers are not left up to the programmer - thus it gets a little harder to cause a complete system failure
- The Java library. The JDK comes with a complete library that should easily cover the first several programming courses and the subjects those courses would cover
- Develop anywhere. Java is much more portable and the transfer of code from one machine to another will require no change in the coding for the simpler concepts taught throughout lower lever programming classes. C++ is portable but is more likely going to require a few changes to insure the code will still run
- Java coding length is comparable to C++ length. Though Java executes slower than C++ for several assignments, it still has the benefit of being a safer language. The JVM may hinder Java’s speed, but it is also the reason Java truly could beat C++. In several cases, a bad segment of code in C++ could affect the whole computer, however Java’s JVM would be the only thing truly affected (minus lag for the computer from memory for the JVM). In the end killing the JVM is much better then rebooting the computer.

There are plenty of places C++ could be better for teaching. An engineering school, for example, would probably benefit from using C++ as there is a good chance a Computer Engineer would need to do system level programming which Java would not be as “friendly” to use for such operations. The transfer between C++ and Java is not seamless however it is not a complete change in mindset. Teaching Java could easily lead to moving towards C++ in later courses and vice versa. Using C++ in later courses would help when programs become more complex and timing algorithms may be key.

Finally, in the future, perhaps one of the discussed languages will take the throne as the best language to teach. As of Summer 2006, none of these languages have yet to prove themselves better then the current offerings, but only the future will show what is next.


1 Java Technology. Java Technology: Brief History of Java Technology. Retrieved September 7, 2006, from website (2005):
2 Kanellos, M., Shankland, S., & Wong, W. Sun, Microsoft settle Java suit. Retrieved September 5, 2006, from website (2001):,+Microsoft+settl...251401.h...
3 Kuchling, A. BeginnersGuide/Overview – PythonInfo wiki. Retrieved September 6, 2006, from website (2006):
4 Levine, D. L. Java vs. C++. Retrieved September 3, 2006, from Washington University in St. Louis website (2000):
5 multithreading. The Free On-line Dictionary of Computing. Retrieved September 6, 2006, from website: 0&y=0
6 .NET FAQ. .NET Frequently Asked Questions. Retrieved September 5, 2006, from website (2005):
7 Prata, S. C++ Primer Plus (5th ed.) Sams, Indiananpolis, IN, 2005.