Comparison between java and c++ Some key differences

Written by admin. Posted in Basic Skills, C++, C/C++, Ebooks/Notes, JAVA

Tagged: , , ,

Published on March 02, 2013 with No Comments

A table is given below ( SURCE: WIKIPEDIA)

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 const and goto 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 programmingfunctional programmingobject-oriented programming, andtemplate 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 stringcontainers). Normally performs bounds checking. 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 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 type (object) parameters are always passed by value.[1]
Explicit memory management. Supports destructors. C++11 replaces the old standard RAII auto_ptr<T> by unique_ptr<T> and adds shared_ptr<T> (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 like a destructor though should not be used to free resources since its call is uncertain.[2]
Supports classes, structs, and unions, and can allocate them on heap or stack. Only supports classes, and allocates them on the heapJava 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,XSLTMIDI, 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. Only single inheritance is allowed. A class can implement multiple Interfaces, but can inherit neither data members nor function bodies from more than one ancestor.
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 plainconst 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 WindowsBSD,LinuxMac OS XSolaris, 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.

NOTE: SO THE KEY/MAIN DIFFERENCES ARE

  • C++ uses pointers and have memory leaks , where java doesn’t have pointers and there are no memory leaks (although there are logic memory leaks,I think) ..
  • C++ compiles to machine language , when Java compiles to byte code .
  • Java has no virtual , since everything in java is virtual (please correct me if I’m wrong) .
  • In C++ the programmer needs to worry about freeing the allocated memory , where in Java the Garbage Collector (with all its algorithms , I can think of at least 3 algorithms) takes care of the the unneeded / unused variables .

No Comments

Comments for Comparison between java and c++ Some key differences are now closed.