Wednesday 24 August 2016

JAVA vs C++

How JAVA is different than C++??



1) Java has no preprocessor. If you want to use classes in another library, you say import and the name of the library. There are no preprocessor-like macros.

2) Java uses packages in place of namespaces. 

3) There are no Java pointers in the sense of C and C++. When you create an object with new, you get back a reference .

example:
String s = new String("cpreddy");

4) There are no destructors in Java. 

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"

5) Java uses a singly-rooted hierarchy, so all objects are ultimately inherited from the root class Object
6) Java has no templates or other implementation of parameterized types. There is a set of collections: Vector, Stack, and Hashtable that hold Object references.
7)  Java has built-in multithreading support. There’s a Thread class that you inherit to create a new thread (you override the run( ) method). Mutual exclusion occurs at the level of objects using the synchronized keyword as a type qualifier for methods.
8)  No inline methods. The Java compiler might decide on its own to inline a method, but you don’t have much control over this.
9)  Java provides the interface keyword, which creates the equivalent of an abstract base class filled with abstract methods and with no data members.
10) There’s no virtual keyword in Java because all non-static methods always use dynamic binding
11)   Java doesn’t provide multiple inheritance (MI).
The interface keyword takes care of combining multiple interfaces.
12) Exception handling in Java is different because there are no destructors. A finally clause can be added to force execution of statements that perform necessary cleanup.
13) Java has method overloading, but no operator overloading. The String class does use the + and += operators to concatenate strings .
14) Java solves this with native methods that allow you to call a function written in another language (currently only C and C++ are supported).
15) Java has built-in support for comment documentation, so the source code file can also contain its own documentation, which is stripped out and reformatted into HTML via a separate program.
16) Java contains standard libraries for solving specific tasks. C++ relies on non-standard third-party libraries. These tasks include (or will soon include):
   Networking, Database Connection (via JDBC), Multithreading, Distributed Objects (via RMI and CORBA).

17) Everything must be in a class. There are no global functions or global data. If you want the equivalent of globals, make static methods and static data within a class. There are no structs or enumerations or unions, only classes .

18)  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 .

19) Java is interpreted for the most part and hence platform independent.
20) Java does not support pointers, templates, unions, operator overloading, structures etc.

Java supports what it calls "references". References act a lot like pointers in C++ languages but you cannot perform arithmetic on pointers in Java.






No comments:

Post a Comment