Posted By:
5 Feb, 2014 10:04 am PST

Detailed Comparison of Java against C/C++

Detailed Comparison of Java against C/C++

 

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 supports special methods to convert values between class objects and primitive types.

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

·  Java does not support typedef and enum keywords.

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

· Java does 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 zeros at the top end.

· The + operator can be used to concatenate strings.

·  Operator overloading is not possible in Java.

· The , 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 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 functions.

· There are no inline functions in Java.

· Java does not support variable-length argument lists to functions. All method definitions must have a specific number of arguments.

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

 

5. Preprocessor:

 

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

· Constants can be created 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.

· There are no destructors in Java.

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

· Java does not provide direct support for 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 if it is visible outside the class.

· There is no virtual keyword in Java. All non-static methods always use 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++.

7. Strings:

 

· Strings in C and C++ are arrays of characters, terminated by a null character. But strings in Java are objects. They are not terminated by a null. Therefore, strings are treated differently in C++ and Java.

· Strings can be concatenated using + operator.

8. Arrays:

 

·     Arrays are quite different in Java. Array boundaries are strictly enforced. Attempting to read past the end of an array produces an error.

· One array can be assigned to another in Java.

· Java does not support multidimensional arrays as in C and C++. However, it is possible to create arrays of arrays to represent multidimensional arrays.

9. Control Flow:

 

· There is no goto in Java. We can, however, use labelled break and continue.

· The test expressions for control flow constructs return a Boolean value (true or false) in Java. In C and C++, they return an integer value.

· The control variable declared in for loop is not available after the loop is exited in Java.

10. Command-line Arguments:

· The command line arguments passed from the system into a Java program differ in a couple of ways, compared to that of C++ program.

· In C and C++, two arguments are passed. One specifies the number of arguments and the other is a pointer to an array of characters containing the actual arguments. In Java, a single argument containing an array of strings is passed.

·The first element in the arguments vector in C and C++ is the name of the program itself. In Java, we do not pass the name of the program as an argument. We already know the name of the program because it is the same name as the class.

 

11. Other Differences:

 

· Java supports multithreading.

· Java supports automatic garbage collection and makes a lot of programming problems simply vanish.

· The destructor function is replaced with a finalize function.

· Exception handling in Java is different because there are no destructors. A finally clause is always executed to perform necessary clean up.

· Java has built-in support for comment documentation, so the source code file can also contain its own documentation.

Attachment:None


Video is posted for better understanding of the concept.

Was it useful?  




Please login to reply to this problem.