Sunday 26 February 2017

Java vs C++



Java
C++
Java does not support pointers, templates, unions, operator overloading, structures etc.
The Java language promoters initially said "No pointers!", but when many programmers questioned how you can work without pointers, the promoters began saying "Restricted pointers." Java supports what it calls "references". References act a lot like pointers in C++ languages but you cannot perform arithmetic on pointers in Java. References have types, and they're type-safe. These references cannot be interpreted as raw address and unsafe conversion is not allowed.
C++ supports structures, unions, templates, operator overloading, pointers and pointer arithmetic.
Java support automatic garbage collection. It does not support destructors as C++ does. There is a finalize( ) method that’s a member of each class, something like a C++ destructor, but finalize( ) is called by the garbage collector and is supposed to be responsible only for releasing "resources"
C++ support destructors, which is automatically invoked when the object is destroyed.
Java does not support conditional compilation and inclusion.
Conditional inclusion (#ifdef #ifndef type) is one of the main features of C++.
Java has built in support for threads. In Java, there is a Thread class that you inherit to create a new thread and override the run() method.
C++ has no built in support for threads. C++ relies on non-standard third-party libraries for thread support.
Java does not support default arguments. There is no scope resolution operator (::) in Java. The method definitions must always occur within a class, so there is no need for scope resolution there either.
C++ supports default arguments. C++ has scope resolution operator (::) which is used to to define a method outside a class and to access a global variable within from the scope where a local variable also exists with the same name.
There is no goto statement in Java. The keywords const and goto are reserved, even though they are not used.
C++ has goto statement. However, it is not considered good practice to use of goto statement.
Java doesn't provide multiple inheritance, at least not in the same sense that C++ does.
C++ does support multiple inheritance. The keyword virtual is used to resolve ambiguities during multiple inheritance if there is any.
Exception handling in Java is different because there are no destructors. Also, in Java, try/catch must be defined if the function declares that it may throw an exception.
While in C++, you may not include the try/catch even if the function throws an exception.
Java has method overloading, but no operator overloading. The String class does use the + and += operators to concatenate strings and String expressions use automatic type conversion, but that's a special built-in case.
C++ supports both method overloading and operator overloading.
Java has built-in support for documentation comments (/** ... */); therefore, Java source files can contain their own documentation, which is read by a separate tool usually javadoc and reformatted into HTML. This helps keeping documentation maintained in easy way.
C++ does not support documentation comments.
Java is interpreted for the most part and hence platform independent.
C++ generates object code and the same code may not run on different platforms

Main Features in Java:

1) All objects of non-primitive types can be created only via new. There’s no equivalent to creating non-primitive objects "on the stack" as in C++. All primitive types can be created only on the stack, without new. There are wrapper classes for all primitive classes so that you can create equivalent heap-based objects via new. (Arrays of primitives are a special case: they can be allocated via aggregate initialization as in C++, or by using new.)
2) Java uses packages in place of namespaces. The name issue is taken care of by putting everything into a class and by using a facility called "packages" that performs the equivalent namespace breakup for class names. Packages also collect library components under a single library name. You simply import a package and the compiler takes care of the rest.
3) There are no Java pointers in the sense of C and C++. When you create an object with new , For example:
String s = new String("howdy");

However, unlike C++ references that must be initialized when created and cannot be rebound to a different location, Java references don’t have to be bound at the point of creation. They can also be rebound at will, which eliminates part of the need for pointers.
4)Java uses a singly-rooted hierarchy, so all objects are ultimately inherited from the root class Object. In C++, you can start a new inheritance tree anywhere.
5)Java has no templates or other implementation of parameterized types. There is a set of collections: Vector, Stack, and Hashtable that hold Object references, and through which you can satisfy your collection needs.
6)Java provides the interface keyword, which creates the equivalent of an abstract base class filled with abstract methods and with no data members. This makes a clear distinction between something designed to be just an interface and an extension of existing functionality via the extends keyword.
7)There’s no virtual keyword in Java because all non-static methods always use dynamic binding.

8)Java has method overloading, but no operator overloading. The String class does use the + and += operators to concatenate strings and String expressions use automatic type conversion, but that’s a special built-in case.

No comments:

Post a Comment