Sunday, September 25, 2011

java vs c++

Java is different from C++ because

  • They eliminated manual memory allocation and deallocation and added automatic GarbageCollection
  • They eliminated MultipleInheritance, replacing it with a notion of interface derived from ObjectiveCee
  • An object must be dynamically allocated on TheHeap
  • Language support for thread protection with the synchronized keyword
  • Java forces you to deal with exceptions (see JavaExceptionHandling).
  • Write once run anywhere / everywhere
  • Primitive and reference data types always passed by value
  • Automatic garbage collection (can be triggered manually)
  • Supports only class and allocates them on the heap. Java SE 6 optimizes with escape analysis to allocate some objects on the stack.
  • Single inheritance only from classes, multiple from interfaces.
  • Generics are used to achieve an analogous effect to C++ templates, however they do not translate from source code to byte code due to the use of Type Erasure by the compiler.
  • Supports labels with loops and statement blocks.
  • Is compiled into byte code for the JVM. Is dependent on the Java platform but the source code is typically written not to be dependent on operating system specific features.





C++ is different from Java because



  • C++ makes at least an attempt to provide genericity with its "templates", which are, although horribly difficult to use, at least better than casting from "Object" all the time (see UsingTemplates). This has been improved in Java 1.5 with it's generic/parameterized types.
  • On a related vein, C++'s collection classes work at least with all basic types, no need for silly wrappers around "int" as if any half-semi-intelligent compiler couldn't provide them automatically. This has been improved in Java 1.5 with auto-boxing...
  • Write once compile anywhere
  • Pointers, References, and pass by value are supported
  • Explicit memory management
  • Supports class, struct, and union and can allocate them on heap or stack
  • Full multiple inheritance, including virtual inheritance.
  • Compile time Templates
  • Supports the goto statement.
  • Source code can be written to be platform independent (can be compiled for Windows, BSD, Linux, Mac OS X, Solaris etc. without needing modification) and written to take advantage of platform specific features. Is typically compiled into native machine code.
C++ was designed for systems and applications programming, extending the C programming language
To this procedural programming language(C 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 relies on a virtual machine to be secure and highly portable.
Java is a statically-typed object-oriented language that uses a syntax similar to C++

C++
Explicit memory management
Java
Automatic garbage collection

Java supports classes, but does not support structures or unions.
All classes in Java ultimately inherit from the Object class. This is significantly different from C++ where it is possible to create inheritance trees that are completely unrelated to one another.
Both C++ and Java support class (static) methods or functions that can be called without the requirement to instantiate an object of the class.
Java does not support operator overloading.

Java, like C++, has primitive types for efficient access.
In Java, these are boolean, char, byte, short, int, long, float, and double.
All the primitive types have specified sizes that are machine independent for portability



References
http://c2.com/cgi/wiki?JavaVsCpp
http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B
http://www.dickbaldwin.com/java/Java008.htm
http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html

No comments:

Post a Comment