Differences between Java and C++

  Java and C++ 

     Java is true Object-Oriented language while C++ is basically C with object-oriented extension. That is what exactly the increment operator ++ indicates. C++ has maintained backward compatibility with C. It is therefore possible to write an old style C program and run it successfully under C++. Java appears to be similar to C++ when we consider only the “extensions” part of C++. However, some object-oriented features of C++ make the C++ code extremely difficult to follow and maintain.


          Listed below are major C++ features that were intentionally omitted from java or significantly modified.

1. Java Does not support operator overloading.

2. Java does not have template classes as in C++.

3. Java does not support multiple inheritance of classes. This is    

    accomplished using a new feature called “interface”.

4. Java does not support global variables. Every variables and   

     methods   is declared within a class and forms part of the class.

5. Java does not use pointers.

6. Java has replace the destructor function with a finalize() function.

7. There are no Header files in Java.

          Java also add some new features. While C++ is a superset of C, Java is neither superset nor a subset of C or C++. Java may be considered as a first cousin of C++ and a second cousin of C.   

 

 

 

Differences between Java and C++ 

    Some major difference are as follows

1. Data Types:

All Java primitive data types(char, int, short, long, byte, float, double, and boolean) have specified sizes and behaviour that are machine-independent.

 

Conditional expressions can only be Boolean , not integral.

 

Casting between data types is much more controlled in java. Automatic conversion occurs only when there is no loss of information. All other casts must be explicit.

 

Java support special method to convert values between class objects and primitive types.

 

Composite data types are accomplished in Java using only classes. Structures and union are not supported.

 

Java does not support typedef keyword.

 

All no-primitive types can only be created using new operator.

 

Java dos not define the type modifiers auto, extern, registor, signed, and unsigned.

2. Pointers

Java does not support Pointers. Similar functionality is accomplished by using implicit references to objects. Pointer arithmetic is not possible in Java.

3. Operators

Java adds a new right shift operator >>> which inserts Zero at the top end.

The + operator can be used to concatenate strings.

Operators overloading is not possible in Java.

The . (dot) operator of C has been deleted.

Java adds another operator instanceof to identify objects.

The modulo division may be applied to float values in Java which is not permitted in C/C++.

4. Functions and Methods

All functions are defined in the body of the class. There are no independent functions.

The functions are defined inside a class are known as methods.

Although function overloading in Java works virtually identical to C++ function overloading, there are no default arguments to function.

No inline functions in Java.

Java requires that methods with no arguments must be declared with empty parenthesis, (not with void keyword).

5. Preprocessor

Java does not have pre-processor, and as such, does not support #define or macros.

Constants can be create using the final modifier when declaring class and instance variables.

Java programs do not use header files.

6. Classes

Class definitions take the similar form in Java as in C++, but there is no closing semicolon.

There is no scope resolution operator :: in Java.

No forward references of classes are necessary in Java.

No destructor in Java.

Java has no template.

No nested classes in Java.

Inheritance in Java has the same effect as in C++, but the syntax is different.

Java does not provide direct support to multiple inheritance. We can accomplish multiple inheritance by using interfaces.

Access specifiers (public, private, protected and private protected) are placed on each definition for each member of a class.

A class in Java can have an access specifier to determine whether it is visible outside the file.

There is no virtual keyword in Java. All not-static methods always dynamic binding.

Initialization of primitive class data member is guaranteed in Java. We can initialize them directly when we define them in the class, or we can do it in the constructor.

We need not externally define storage for static members like we do in C++.

 

Comments

Popular posts from this blog

Getting Started With Object Oriented Programming in Java

Common Java Practice Problems