News Projects Gallery Reviews Links About Personal Server Status
Java vs C++


Table of Contents

Preface

As you have noticed, this is an article comparing the differences between two of the most widely used programming languages in the world today; Java and C++. A lot of what's here had already been said by others (and probably better), but with a touch of personal experience here and there.

Introduction

To those who haven't taken Computer Science (but are computer literate), the very mention of Java probably (I know i did!) generates thoughts of a slow incoherent, and inferior programming language, much like javascript, cobol, or even HTML. You are probably thinking that Java is for "newbies" and not for hardcore "hackers" such as yourself.

And in a way, you are absolutely right!
Java was not created with the same intentions in mind as C++. C(++) was created as a high level language that could harness the power of assembly without the extreme complexities of having to learn assembly language. It is very much like a sandbox where you are the bully who gets what he/she wants when he/she wants it. However, inherent problems arise when you go play in someone else's sandbox. There are things that you can do, that you simply should not be able to. Key features present in almost every new programming language such as security and simplicity of operations is not natively not present in C++. And thus, Java was born. Java was created as a programming language that could adapt to the needs of advancing technology, such as the internet, and address the needs of those who implement this technology. However, such changes come at a price, Java is inherently slower than C++ in almost all operations, less fluent in precedural programming structure, and largely restrictive in control over system resources. While some would view this as a step backwards, many have accepted Java as a viable solution to next generation programming.

Similarities between Java and C++

At first glance, the Java programming language is almost identical to the C++ programming language. Both consist of recognizable keywords, such as public, or void, and the basic primitive types, such as int, boolean, char. Code is structured similarly, considering that both languages allow for object abstraction, and object oriented programming. Where as C++ has the standard library, from which programmers can implement commonly used functions, Java has its own well defined API for all its methods. Like C++, Java must be compiled before runtime, but into bytecode rather than machine specific code (see below). More specific and complete comparisons can be found on the net, so I'll just move on.

Differences between Java and C++

When getting technical, Java is profoundly different than C++. Java code is compiled into bytecode which is fed to a Virtual Machine that interprets the code in almost real time, which accounts for much of the differences in speed between operations in both languages. C++ code is compiled into machine specific (ie. x86) assembly code (hence the speed), which limits its program base only to those which have the same hardware. There are numerous language specific differences like the lack of global variables (not static!), operator overloading, and multiple inheritance in Java. C++ on the other hand, lacks automatic garbage collection, strict interfaces, Strings, etc. As mentioned, speed is a major factor in the two languages, due to overhead in Java objects, and Java being interpreted. Such differences are a result of choices made by Sun when designing the language, to better balance the need of the client and the flexibility of the programmer. Where C++ strives to be a programmer's language of choice, Java strives to be a language tool that supports programming well defined, object oriented programs for the next generation of technology.

So Why Java?

So far, I haven't made any references to personal experience, so I will do it now. Java is the programming language taught in almost all first year Computer Science courses. There are many reasons for which I believe Java is taught in the first year. Firstly, Java syntax is notably simpler than their C++ counterparts (ie. printf/iostream::cout vs System.out.println), and the fact that Java takes care of much of the internal workings of the program, students need only concentrate on the cause and effect of their programming. Secondly, not all students are using Pc's, yes, there are those who enjoy using Mac or otherwise, and having a language that is unified in almost every which way makes teaching and learning both that much easier. Finally, Java is a platform that can produce almost instant results, for example, in the first week, we were already working with JFrames. To even think about attempting that on C++, either through Win32 GDI or DirectX, after the first week of classes would be absurd. This way, a student's ability is only limited by their creativity and determination in learning the simple syntax of the language. Also, the Java API Documentation definately a step in the right direction. No more having to dig around reference books to get the syntax of a particular statement, one can just load up the API and get all the information they need. This allows and promotes programmers to follow the trend by writing concise and well structured documentation in their code.

Although there are numerous differences between the two languages, I must admit that I have never run into things that can be done in Java that can't be replicated in C++ (short of GUI development!!) Edit: 21/12/03 (My bad, I've actually found somethings that would make Java programming a lot simpler, most notably, overloading math operators (ie. a+b), and pointers to methods, both of which are available in python, but missing in Java). Having said that, if you already know C++, that will be handy when picking up the Java language. However, having such knowledge beforehand may be a hinderance when trying to adapt use new concepts such as multiple inheritance and strict interfaces.

Conclusion

I must admit, although Java is a great language, those who are hard-coded (no pun intended) into C++ may fail to realize it's potential. I know an old friend that laughed when I told him I was learning Java.

Leaving it at that, I do realize that people come from all programming backgrounds, from C++, VB (like me!), perl, python, html, pencil&paper, etc. Java is a language worth learning, if not just for the sake of being able to say that you can make an updated Java version of pong. With the way the internet is growing, and the astronomical advances in computing power, (as Bartosz Milewski states) "If it's not Java, there will come another language to finish off C++ [for good]."