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.
3The 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.
4Simplicity 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.
8Standard Type System
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.
15Full 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.
16No Deterministic Destruction
17Barriers 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.