Java and C++ are two prominent object-oriented programming languages. By many language popularity metrics, the two languages have dominated object-oriented and high-performance software development for much of the 21st century, and are often directly compared and contrasted. Java's syntax was based on C/C++.
Design aims The differences between the programming languages C++ and Java can be traced to their heritage, as they have different design goals. C++ was designed for systems and applications programming (i.e., infrastructure programming), extending the procedural programming language C, which was designed for efficient execution. To C, C++ added support for object-oriented programming, exception handling, lifetime-based resource management (Resource Acquisition Is Initialization (RAII)), generic programming, template metaprogramming, and the C++ Standard Library which includes generic containers and algorithms (the Standard Template Library or STL), and many other general purpose facilities. Java is a general-purpose, concurrent, class-based, object-oriented programming language that is designed to minimize implementation dependencies. It relies on a Java virtual machine to be secure and highly portable. It is bundled with an extensive/massive library designed to provide abstraction of the underlying platform. Java is a statically typed object-oriented language that uses a syntax similar to (but incompatible with) C++. It includes a documentation system called Javadoc. The different goals in the development of C++ and Java resulted in different principles and design trade-offs between the languages. The differences are as follows:
Language features
Syntax
Java syntax has a context-free grammar that can be parsed by a simple LALR parser. Parsing C++ is more complicated. For example, Foo<1>(3); is a sequence of comparisons if Foo is a variable, but creates an object if Foo is the name of a class template. C++ allows namespace-level constants, variables, and functions. In Java, such entities must belong to some given type, and therefore must be defined inside a type definition, either a class or an interface. In Java, all types belong to a namespace (Java package), and there is no such thing as a "global namespace" (even in compact source files where code seemingly does not reside in any class, such code belongs to a final top-level class in the unnamed package), unlike C++ where any code not part of a namespace is part of the global namespace. In Java, the "unnamed package" is not a global namespace, and classes in it cannot be imported by code in named packages. Java has a strict global inheritance tree such that all classes (implicitly) extend the top type, java.lang.Object. C++ does not have any global inheritance tree. It does have a top type std::any, but is a type-erased runtime object rather than an inherited type. In C++, objects are values, while in Java they are not. C++ uses value semantics by default, while Java always uses reference semantics. To opt for reference semantics in C++, either a pointer or a reference can be used.
C++ supports goto statements, which may lead to spaghetti code programming. With the exception of the goto statement (which is very rarely seen in real code and highly discouraged), both Java and C++ have basically the same control flow structures, designed to enforce structured control flow, and relies on break and continue statements to provide some goto-like functions. Some commenters point out that these labelled flow control statements break the single point-of-exit property of structured programming. C++ provides low-level features which Java mostly lacks (one notable exception being the internal sun.misc.Unsafe API for direct memory access and manipulation). In C++, pointers can be used to manipulate specific memory locations, a task necessary for writing low-level operating system components. Similarly, many C++ compilers support an inline assembler, and assembly functions may be linked in C/C++ programs. In Java, such code must reside in external libraries, and can only be accessed via the Java Native Interface, with a significant overhead for each call.
… excerpt ends here. Continue reading the full article.