One obvious advantage is a runtime environment that provides platform independence: you can use the same code on Windows, Solaris, Linux, Macintosh, and so on. This is certainly necessary when programs are downloaded over the internet to run on a variety of platforms.
· Garbage Collection:
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. This eliminates the problem of manual memory allocation. Thus, Garbage collection leads to lower bug count and faster execution time. Whereas in C++, 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.
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 7 minutes in Java.For debugging C++, developers need a second build. Java has more approachable build tools than C++.
· Safety: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.
Although performance is typically not considered one of the benefits Java has over C++, 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.
Reflection is the ability of a program to examine and modify the structure and behaviour (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.
· Standard System Type: 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.
· Portability: 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.
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.
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.