10 Reasons Java Has Supplanted C++ (and 5 Reasons It Hasn't)
Ten Advantages Java Has Over C++
Java is known for its garbage collection. Garbage collection (GC) is a form of automatic memory management. The garbage collector attempts to reclaim garbage, or memory, occupied by objects that are no longer in use by the program. A significant portion of C++ code is dedicated to memory management. Cross-component memory management does not exist in C++. Libraries and components are harder to build and have less natural APIs. Garbage collection leads to faster time to market and lower bug count.
The Build Process
When compared with Java, C++ builds are slow and complicated. Developers have noted that a full build in C++ might take up to 20 hours, while the same build might take only seven minutes in Java. For debugging C++, developers need a second build. Java has more approachable build tools like Ant and Maven, while C++'s Make and NMake are considered less so.
Simplicity of Source Code and Artifacts
C++ splits source into header and implementation files. It requires a big monitor to see .hpp and .cpp header file extensions and file name extensions at the same time. With C++, there is code in multiple places: some inlined in the header, some in the .cpp. Artifacts are compiler-specific, but there are many of them. Yet, with Java, there is just one .java, one .class extension.
Java has a binary standard. In addition to being loadable as a class by a Java Virtual Machine, a Java Classfile can be used to compile against. C++ has no binary standard. C++ precompiled headers are compiler- and platform-specific. And C++ requires a large amount of source to be shipped in order to compile against it. Java defers platform-specific stuff to the runtime.
There is no standard way to dynamically link to C++ classes. Java allows arbitrary collections of classes to be packaged together and dynamically loaded and linked as needed. With Java, there are no dynamic link library fiascos, also known as DLL hell.
As Java's mantra has been "Write once, run anywhere," Java is portable with very little effort. C++ is portable in theory, but in practice, you have to build another language (#ifdef'd types, etc.) on top of it. And C++ has significant differences from vendor to vendor, e.g., standards support.
Standard Type System
Java has:• specified, portable primitive types;• a built-in, specified, portable runtime library;• rich support for I/O, networking, XML/HTML, database connectivity;C++ does not.
Reflection is the ability of a program to examine and modify the structure and behavior (specifically the values, metadata, properties and functions) of an object at runtime. Java has full runtime capability to look at the runtime. C++ has optional Run-time type information (RTTI) but no reflection. Reflection enables extremely powerful generic frameworks and provides the ability to learn about, access and manipulate any object.
Although performance is typically not considered one of the benefits Java has over C++, Purdy argues that garbage collection can make memory management much more efficient, thereby impacting performance. In addition, Java is multi-threaded while C++ does not support multi-threading. C++'s thread safe smart pointers are three times slower than Java references. And Java has HotSpot Java Virtual Machine (JVM), which features just-in-time (JIT) compilation for better performance.
Java eliminates pointers, which can allow arbitrary memory access and the ability to easily crash the process (core dump). There are no buffer overruns in Java, and code and data cannot be accidentally mixed. And Java includes bounds-checking. Bounds-checking is any method of detecting whether a variable is within some bounds before its use.
Five Reasons Why Java Has Not Been Able to Fully Supplant C++
Java is not good for "instant" and short-running processes. The graph of initially loaded classes is pretty large. The code is either getting JITed to native code each time the JVM starts, or it's initially being interpreted (HotSpot).
Java uses significantly more memory than C++, particularly for "small" applicationseasily two orders of magnitude more at the extreme end of the scale. Memory requirements limit Java adoption on some devices.
Full Garbage Collection (GC) Pauses
Sooner or later, there is a part of GC that can't be run in the background and can't be avoided, which results in a temporary halt and makes real-time processing impossible. This can be havoc for distributed systems. However, there is possible partial amelioration by increasing the memory footprint.
No Deterministic Destruction
C++ has deterministic destruction; Java does not. Deterministic destruction is useful for managing resources. In C++, when objects are deleted, their destructors are executed immediately. This allows system resources to be released when an object is no longer needed.
Barriers to Native Integration
Operating systems are built in C/C++. APIs are typically C. Native GUIs and other OS-level functionality often require C callbacks. However, the Java Native Interface only allows Java to call native code that was written explicitly to be called by JNI.