Programming: Book: Java: Difference Between C++ and Java.




What is the Different Between C++ & Java?

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 (a.k.a. infrastructure 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, lifetime-based resource management (RAII), generic programming, and template meta programming, in particular. It also added a standard library which includes generic containers and algorithms (STL), as well as many other general purpose facilities.
  • Java is a general-purpose, concurrent, class-based, object-oriented computer programming language that is specifically designed to have as few implementation dependencies as possible. It relies on a Java 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 similar (but incompatible) syntax to C++. It includes a documentation system called Javadoc.
The different goals in the development of C++ and Java resulted in different principles and design tradeoffs between the languages. The differences are as follows :
C++Java
Extension of C with object-oriented programming, still able to run C code.Strongly influenced by C++/C syntax.
Compatible with C source code, except for a few corner cases.Provides the Java Native Interface and recently Java Native Access as a way to directly call C/C++ code.
Write once, compile anywhere (WOCA).Write once, run anywhere / everywhere (WORA / WORE).
Allows procedural programmingfunctional programming,object-oriented programminggeneric programming, andtemplate metaprogramming. Favors a mix of paradigms.Allows procedural programmingfunctional programming(since Java 8) and generic programming (since Java 5), but strongly encourages the object-oriented programming paradigm. Includes support for the creation of scripting languages.
Runs as native executable machine code for the targetinstruction set(s).Runs in a virtual machine.
Provides object types and type names. Allows reflection through RTTI.Is reflective, allowing metaprogramming and dynamic code generation at runtime.
Has multiple binary compatibility standards (commonly Microsoft (for MSVC compiler) and Itanium/GNU (for virtually all other compilers)).Has a single, OS- and compiler-independent binary compatibility standard.
Optional automated bounds checking (e.g., the at()method in vector and string containers).All operations are required to be Bound-checked by all compliant distributions of Java. HotSpot can remove bounds checking.
Supports native unsigned arithmetic.No native support for unsigned arithmetic.
Standardized minimum limits for all numerical types, but the actual sizes are implementation-defined. Standardized types are available through the standard library<cstdint>.Standardized limits and sizes of all primitive types on all platforms.
Pointers, references, and pass-by-value are supported for all types (primitive or user-defined).All types (primitive types and reference types) are always passed by value.[1]
Memory management can be done manually through new / delete, automatically by scope, or by smart pointers. Supports deterministic destruction of objects. Garbage collection ABI standardized in C++11, though compilers are not required to implement garbage collection.Automatic garbage collection. Supports a non-deterministic finalize() method whose use is not recommended.[2]
Resource management can be done manually or by automatic lifetime-based resource management (RAII).Resource management must be done manually, or automatically via finalizers, though this is generally discouraged. Has try-with-resources for automatic scope-based resource management (version 7 onwards).
Supports classes, structs (POD-types), and unions, and can allocate them on the heap or the stack.Classes are allocated on the heapJava SE 6 optimizes with escape analysis to allocate some objects on the stack.
Allows explicitly overriding types as well as some implicit narrowing conversions (for compatibility with C).Rigid type safety except for widening conversions.
The C++ Standard Library was designed to have a limited scope and functionality but includes language support, diagnostics, general utilities, strings, locales, containers, algorithms, iterators, numerics, input/output, random number generators, regular expression parsing, threading facilities, type traits (for static type introspection) and Standard C Library. The Boost library offers more functionality including network I/O.
A rich amount of third-party libraries exist for GUI and other functionalities like: ACECrypto++, various XMPP Instant Messaging (IM) libraries,[3] OpenLDAPQtgtkmm.
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 XMLXSLTMIDI, 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. Preserving meaning (semantics) is highly recommended.Operators are not overridable. The language overrides + and += for the String class.
Single and Multiple inheritance of classes, including virtual inheritance.Single inheritance of classes. Supports multiple inheritance via the Interfaces construct, which is equivalent to a C++ class composed of abstract methods.
Compile-time templates. Allows for Turing complete meta-programming.Generics are used to achieve basic type-parametrization, 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.References to functions achieved via the reflection API. OOP idioms using Interfaces, such as Adapter, Observer, and Listener are generally preferred over direct references to methods.
No standard inline documentation mechanism. Third-party software (e.g. Doxygen) exists.Extensive Javadoc documentation standard on all system classes and methods.
const keyword for defining immutable variables and member functions that do not change the object. Const-ness is propagated as a means to enforce, at compile-time, correctness of the code with respect to mutability of objects (see const-correctness).final provides a version of const, equivalent to type* const pointers for objects and const for primitive types. Immutability of object members achieved through read-only interfaces and object encapsulation.
Supports the goto statement.Supports labels with loops and statement blocks.
Source code can be written to be platform-independent (can be compiled for WindowsBSDLinuxMac OS X,Solaris, etc., without modification) and written to take advantage of platform-specific features. Typically compiled into native machine code, must be re-compiled for each target platform.Compiled into byte code for the JVM. Byte code is dependent on the Java platform, but is typically independent of operating system specific features.

Language features

Syntax

See also: Java syntax and C++ 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 C++, objects are values, while in Java they are not. C++ uses value semantics by default, while Java always usesreference semantics. To opt for reference semantics in C++, either a pointer or a reference can be used.
C++Java

Comments