Sunday, March 19, 2017

The Advantages and Disadvantages of C++ as Compared to Java

The programming language C++ dates back to 1979, when Bjarne Stroustrup, a Danish computer scientist, was researching for his Ph.D. thesis. Stroustrup began working on a programming language he called “C with Classes”; as the name implies, it was intended to be an extension of the C language. The initial idea was to add object-oriented programming to the C language, which was then and remains to be a great language for its portability, low-level functionality, and speed. His language, what would later be called C++, added classes, inheritance, function arguments, and type checking in addition to all the features of the C language.


Java, on the other hand, is a fairly new language. In June 1991, James Gosling, Mike Sheridan, and Patrick Naughton of Sun Microsystems began the Java language project. Initially intended for interactive TV’s, it was too advanced for the cable TV industry at the time. The language was written to be fairly similar to the C and C++ languages, its predecessors, to make it much easier for programmers to learn. Sun Microsystems first released an implementation in 1995, with the promise of it being a “Write once, run anywhere” language. Soon, many web browsers began incorporating Java applets, and the language became hugely popular. So much so that in 2007, Sun Microsystems made all of its Java Virtual Machine’s code available free of cost. Today, Java runs on nearly every device we use, from laptops to gaming consoles, all the way to scientific supercomputers.

Though fairly similar in their syntax and coding style, Java and C++ have some key differences that make each unique and useful for entirely different applications. One of the biggest such differences, perhaps the largest hurdle, is speed. Java runs close to twenty times slower than C++. Java is equipped with just-in-time compilers that convert some of the code to local source code, and these features do offer significant speed ups in the performance. However, its speed will at best come close to equaling that of C++. There are entire classes of issues which Java cannot solve because of this speed issue.  Though there are many applications which can indeed have a small degree of inefficiency, there are also some which cannot tolerate absolutely any slowness.

While not as low-level as assembly languages or Fortran, in which all abstractions were eliminated in the performance-critical code, C++ and its modern compilers certainly do increase the speed of code compilation hugely in comparison to Java. C++ was written explicitly with performance and speed in mind. Its template mechanism is able to do huge and significant computations right at compile time; its results consume almost no run-time and very little compile time. Therefore, C++ with an inventive library and a great compiler can actually beat Fortran performance in its own home game: number and array processing. This cocktail of basic operations, efficiency, and an individually dynamic mechanism make C++ an excellent choice for implementing all performance and time-critical aspects of any application.

Another huge aspect to consider when comparing these two languages is portability. C++ is platform-dependent while Java is platform-independent. One of Java’s main goals is portability; this means that any code written for Java platforms should be able to run on any other combination of operating system and hardware. This was achieved by converting the source code to Java bytecode, which are then to be executed by a virtual machine, the JVM. In comparison, C++ and other languages convert the source code to architecture-specific machine code. For Java, users typically install a Java Runtime Environment on their personal machines for Java applications or in their web browsers for Java applets. Universal bytecode most definitely makes porting the code simple. However, as aforementioned, this makes Java a significantly slower language since the code has to first be implemented. The language itself is platform-independent; the JVM is unique to the system, and is what translates the bytecode to the specific platform’s machine language.

This is in almost stark contrast to C++. The portability of any programming language is dependent on the type of code created by compiler. In the case of C++, the compiler always produces machine code which is directly executed by the central processing unit, or the CPU. Thus, the C++ code is machine dependent and specific to that particular operating system. In order to execute the C++ program on another machine, the program must be recompiled before it is executed. C++ does have compilers available on basically every platform conceivable. It is not a very portable language itself. The standard C++ library only defines an input-output, or IO, library that is suitable for purely text-based console IO. In order to develop a GUI, or a graphical user interface, a GUI framework is required, and those tend to be incredibly specific to the platform.

These two languages also hugely differ in their memory allocation and deallocation services. C++ is equipped with explicit memory management, via constructors, destructors, and pointers. Java, however, has automatic garbage collection. This is actually a strength of the Java platform: developers need not worry about explicitly managing their memory allocation, which is a fairly complex aspect of programming to keep track of. However, C++ does give developers the opportunity to have more control over the allocation of their memory using special pointers, and thus the memory of a system can be used more efficiently and effectively, thus optimizing the performance of a program. In C++, it is possible to allocate arbitrary blocks of memory, meaning a variable need not be instantiated. This does not hold true in Java, where objects must be instantiated and arbitrary memory blocks must be allocated as arrays of bytes.

Modern programming languages like Java have made programming much more easier and powerful. In the case of memory management, Java’s automatic garbage collector seeks to solve the issue of the unreferenced object issue; this is because an object referenced in another part of the program will never be garbage collected and have its memory deallocated. C++ lets a program have allocated memory and an object which is not referenced again in the program. This object cannot be destroyed and thus there would be a memory leak. In contrast, Java programs would not deallocate that object until it becomes entirely unreachable. Thus, the Java garbage collection aims to prevent leaks, since all memory that is no longer referenced can be put into the garbage.  In C++, it is actually more common to allocate objects as local variables that are deallocated when they go out of scope, while in the former they are allocated on the heap, the runtime data area from which a program’s memory is allocated, and then collected by the garbage collector. This feature arguably gives Java a more cohesive and maintainable development environment than that of C++.

Java has finalizers, while C++ has destructors. They are both called upon directly preceding the deallocation of an object, but they are significantly different. In C++, the destructor must be explicitly evoked in order to deallocate that object. The destructor then aligns its execution with the point of the program just before the object’s deallocation. However, in Java, the deallocation of an object is handled implicitly by a garbage collector. Its finalizer is accessed some time later, after it has been accessed for the final time and actually before its deallocation. However, there are not many objects that require finalizers; only objects that need some fixing of the state of that object before its deallocation. C++ features the RAII idiom, or “Resource Acquisition Is Initialization” in which a single type of resource is placed within a class; that class then allocates that resource during and after the construction and releases it upon destruction. Access is provided to that specific resource for the program between those two points. Java offers safe, synchronous allocation and deallocation of the resources using a try/catch/finally algorithm. C++ also offers the possibility to have uninitialized primitive objects, whereas Java actually requires default initialization.

Member access is also quite different between Java and C++. In the former, a protected member access class means that the protected members of a certain class “X” would be accessible in another class “Y” of that same package, without regard for whether class Y actually inherits from class X. This is only true, however, if both classes are in the same package. In C++, private members are only accessible to the specific class that originally defines them, while protected members are in fact accessible to both the class that defines them as well as any classes that inherit from that defining class. Choosing between private and protected depend on the capabilities a programmer wishes to provide another who is working on a derived class of theirs.

Another aspect that also hugely differs between these two programming languages is inheritance. A hugely important aspect of inheritance is that it lessens the amount of duplicated code in a program by allowing common code to be shared amongst several subclasses in a certain program. Rather than having equivalent code existing in related classes, that same code can be shared. This results in better organized and smaller units of code that must be compiled. In this regard, while creating these relationships between blocks of code, C++ always creates a new inheritance tree, whereas Java uses a single inheritance tree because every class in Java is the child of the Object class. The Object class can be considered the root of the inheritance “tree”. All classes in Java inherit from the Object class whether it be directly or indirectly. There is thus always the one tree of classes, in which each class inherits from the Object class, which is at the root. If a programmer created a class in Java without specifying a specific class it would inherit from, that class would automatically inherit from the Object class. However, C++ is similarly analogous to an entire forest. If a programmer did not specify a certain tree a C++ class was a part of, a whole new tree would be created. A huge difference is that, unlike C++, Java does not support multiple inheritance. Any class cannot in fact inherit from more than one other class. A class in Java can, however, implement more than one interface, which is similar to a class but is actually a reference of abstract methods.

Lastly considered are the applications of these two languages. C++, despite its difficulties, is a widely used programming language whose implementations can be seen in applications people use every single day. Big companies like Google and Adobe use C++ for its faster runtime and to save their customers time and money powering their incredibly high-volume websites. Perhaps even more cool than that, however, is the use of C++ in SpaceX, or NASA. In incredibly time sensitive missions to outer-space, such as landing rovers and rockets on Mars or launching satellites into orbit around the Earth, engineers at NASA rely on C++’s speed and low-level programming abilities. In these cases, portability is not actually required, only time and precise planning. While NASA actually still uses what are otherwise considered archaic languages, such as Fortran and other languages close to the machine code, C++ has come to be a huge portion of what their code is written in. Most of NASA’s programming practices and requirements can actually be satisfied perfectly using C++; in fact, their main rules are written using this language in mind, since the C and C++ languages have long been recommended by engineers at NASA for life-dependent code due to their long histories and excellent support.

Java, in contrast, is much more widely employed in application programming, whereas C++ is used for system programming. Java’s main use is in web-based enterprise and mobile applications, where cross-platform support and usability is much more necessary than the fastest possible speed. Java has seen huge success in its application to Android app programming; in fact the official language of android app development is actually Java. The vast majority of android code is in Java, and most of its APIs are written to be called from Java. Since Android has actually passed its Apple rival iOS in sales and downloads, it is not difficult to see the vast applications of this programming language. Large financial institutions are also hugely reliant on Java due to its abilities to analyze, compile, and organize huge swaths of data. Another huge aspect of Java is its scalability, and that is precisely why online merchants like Amazon rely on the language to more easily scale their products and websites as they acquire new customers. Overall, one does not have to look far to find huge and important applications of Java.

Both C++ and Java have their similarities and differences, and make each uniquely suitable to whole different sets of problems. In a world where technology and its applications are becoming hugely necessary in many different types of applications, whether it be economics, education, medicine, or even law, software engineers must have a huge toolset of languages. It is important to analyze and deeply consider the development goals and main desired outcomes of a program in order to accurately gauge and choose a programming language that will best suit those aims. Whether it be that the program should be scalable, cross-platform, or incredibly fast, every programming language has its pros and cons as compared to others. It is difficult to say which of the two between C++ and Java is more advanced, per se, as each of them has their benefits. It goes without doubt, however, that good modern programmers have an excellent grasp of both so as to be able to write applicable and necessary programs for a wide variety of uses.


Sources
  • http://cs-fundamentals.com/tech-interview/java/differences-between-java-and-cpp.php
  • http://www.cplusplus.com/info/history/
  • https://en.wikipedia.org/wiki/C%2B%2B#History
  • http://www.oracle.com/technetwork/java/javase/overview/javahistory-index-198355.html
  • https://en.wikipedia.org/wiki/Java_(programming_language)#Java_platform
  • http://www.cantrip.org/realworld.html
  • http://web.eecs.utk.edu/~bvz/gui/notes/java-versus-c++.html
  • http://www.cs.uni.edu/~fienup/cs151s06_c_cpp/lectures/lec1_java_cpp_comparison.pdf
  • http://sdtimes.com/nasas-10-rules-developing-safety-critical-code/
  • http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf
  • Starting Out with C++ from Control Structures to Objects (8th Edition) by Tony Gaddis

No comments:

Post a Comment