Comparison of Java and C++ - Design Aims

Design Aims

The differences between the C++ and Java programming languages can be traced to their heritage, as they have different design goals.

  • C++ was designed for systems and applications programming, extending the C programming language. To this procedural programming language designed for efficient execution, C++ has added support for statically typed object-oriented programming, exception handling, scoped resource management, and generic programming, in particular. It also added a standard library which includes generic containers and algorithms.
  • Java was created initially as an interpreter for printing systems but grew to support network computing. It is good for beginner programmers because it is easier to see how things work, with the already made methods(functions) being more like real words and it being very object-oriented. Sun Microsystems used to use it for the basis of their "HotJava" thin client system. It relies on a virtual machine to be secure and highly portable. It is bundled with an extensive library designed to provide a complete abstraction of the underlying platform. Java is a statically typed object-oriented language that uses a syntax similar to C++, but is not compatible with it. It was designed from scratch with the goal of being easy to use and accessible to a wider audience. It includes an extensive documentation 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 :

C++ Java
Compatible with C source code, except for a few corner cases. No backward compatibility with any previous language. The syntax is, however, strongly influenced by C/C++. There are things such as reserved keywords, such as xor and const that don't do anything so that people who write on C++ don't get confused.
Write once, compile anywhere (WOCA). Write once, run anywhere / everywhere (WORA / WORE).
Allows procedural programming, functional programming, object-oriented programming, and template metaprogramming. Strongly encourages an object-oriented programming paradigm.
Allows direct calls to native system libraries. Call through the Java Native Interface and recently Java Native Access
Exposes low-level system facilities. Runs in a virtual machine.
Only provides object types and type names. Is reflective, allowing metaprogramming and dynamic code generation at runtime.
Has multiple binary compatibility standards (commonly Microsoft and Itanium/GNU). Has a binary compatibility standard, allowing runtime check of correctness of libraries.
Optional automated bounds checking (e.g., the at method in vector and string containers). Normally performs bounds checking. HotSpot can remove bounds checking.
Supports native unsigned arithmetic. No native support for unsigned arithmetic.(Explain)
Standardized minimum limits for all numerical types, but the actual sizes are implementation-defined. Standardized types are available as typedefs (uint8_t, ..., uintptr_t, ...). Standardized limits and sizes of all primitive types on all platforms.
Pointers, references, and pass-by-value are supported. Primitive and reference data types always passed by value.
Explicit memory management. Supports destructors. C++11 replaces the old standard RAII auto_ptr by unique_ptr and adds shared_ptr (smart pointer with reference counter), though third party frameworks exist to provide better garbage collection. Automatic garbage collection (can be triggered manually). Has a finalize method that works as a destructor.
Supports classes, structs, and unions, and can allocate them on heap or stack. Only supports classes, and allocates them on the heap. Java SE 6 optimizes with escape analysis to allocate some objects on the stack.
Allows explicitly overriding types. Rigid type safety except for widening conversions. Autoboxing/unboxing added in Java 1.5.
The C++ Standard Library has a much more limited scope and functionality than the Java standard library but includes language support, diagnostics, general utilities, strings, locales, containers, algorithms, iterators, numerics, input/output, and Standard C Library. The Boost library offers more functionality, including threads and network I/O. Users must choose from a plethora of (mostly mutually incompatible) third-party libraries for GUI and other functionality. The standard library has grown with each release. By version 1.6, the library included support for locales, logging, containers and iterators, algorithms, GUI programming (but not using the system GUI), graphics, multi-threading, networking, platform security, introspection, dynamic class loading, blocking and non-blocking I/O. It provided interfaces or support classes for XML, XSLT, MIDI, database connectivity, naming services (e.g. LDAP), cryptography, security services (e.g. Kerberos), print services, and web services. SWT offers an abstraction for platform-specific GUIs.
Operator overloading for most operators. The meaning of operators is generally immutable, but the + and += operators have been overloaded for Strings.
Full Multiple inheritance, including virtual inheritance. From classes, only single inheritance is allowed. From Interfaces, Multiple inheritance is allowed.
Compile-time templates. Generics are used to achieve an analogous effect to C++ templates, but they do not translate from source code to byte code due to the use of type erasure by the compiler.
Function pointers, function objects, lambdas (in C++11), and interfaces. No function pointer mechanism. Instead, idioms such as Interface, Adapter, and Listener are extensively used.
No standard inline documentation mechanism. Third-party software (e.g. Doxygen) exists. Javadoc standard documentation.
const keyword for defining immutable variables and member functions that do not change the object. final provides a version of const, equivalent to type* const pointers for objects and plain const for primitive types only. No const member functions, nor any equivalent to const type* pointers.
Supports the goto statement. It may cause ( Spaghetti Programming). Supports labels with loops and statement blocks.
Source code can be written to be platform-independent (can be compiled for Windows, BSD, Linux, Mac OS X, Solaris, etc., without modification) and written to take advantage of platform-specific features. Typically compiled into native machine code. Compiled into byte code for the JVM. Byte code is dependent on the Java platform, but is typically independent of operating system specific features.

Read more about this topic:  Comparison Of Java And C++

Famous quotes containing the words design and/or aims:

    Humility is often only the putting on of a submissiveness by which men hope to bring other people to submit to them; it is a more calculated sort of pride, which debases itself with a design of being exalted; and though this vice transform itself into a thousand several shapes, yet the disguise is never more effectual nor more capable of deceiving the world than when concealed under a form of humility.
    François, Duc De La Rochefoucauld (1613–1680)

    We are often reminded that if there were bestowed on us the wealth of Crsus, our aims must still be the same, and our means essentially the same.
    Henry David Thoreau (1817–1862)