Tuesday 2 April 2013

Difference between C,C++,JAVA




Complex Data Types
There are two types of complex data types in C: structures and unions. C++ adds classes to this list. Java only implements one of these data types: classes.
A structure can be emulated by a class – simply write a class without any methods and make all the fields public. However, emulating a union is not always possible in Java, and the memory saving advantages unions hold in C may not carry accross. Java presents a simpler model but at the cost of not being able to save a little memory. For many applications this will be a non-issue.

Strings
C has no built-in string data type. The standard technique adopted among C programmers is that of using null-terminated arrays of characters to represent strings. This practice if often seen in C++ programs too.
Neither C++ or Java have string as a primitive type, but they do both have string objects that are a standard part of the language. In Java this type is called String, and in C++ it is called CString.

Multiple Inheritance
Multiple inheritance is a feature of some object oriented languages that allows you to derive a class from multiple parent classes. Although multiple inheritance is indeed powerful (and sometimes the logical way to define a class hierachy), it is complicated to use correctly and can create situations where it’s uncertain which method will be executed. For example, if each of the parent classes provide a method X and the derived class does not, it is unclear which X should be invoked. It is also complicated to implement from the compiler perspective.
C++ supports multiple inheritance. Java provides no direct support for multiple inheritance, but you can implement functionality similar to multiple inheritance by using interfaces in Java. Java interfaces provide method descriptions but contain no implementations. Therefore implementations can only be inherited from one class, so there is no ambiguity over which method to invoke.

Operator Overloading
Operator overloading enables a class to define special behaviour for built-in operators when they are applied to objects of that class. For example, if the * (multiply) operator was to be used on two objects of type Matrix, then matrix multiplication could be implemented. This allows object types to feel much more tightly integrated into the language and can deliver much clearer code. However, sometimes it is not clear what a particular operator would sensibly do for a particular type, whereas a well-named method call would be clear.
Operator overloading is considered a prominent feature in C++. It is not supported in Java, probably in an effort to keep the language as simple as possible and help ensure it is obvious what code does, even though it may take longer to type and read.

Automatic Coercions
Automatic coercion refers to the implicit casting of data types that sometimes occurs in C and C++. For example, in C++ you can assign a float value to an int variable, which can result in a loss of information, although a compiler warning will be given about this. Java does not support C++ style automatic coercions. In Java, if coercion will result in a loss of data, you must always explicitly cast the data element to the new type.

Goto Statement
The goto statement is rarely used these days in C and C++, but it is a standard part of the language. The goto statement has historically been cited as the cause for messy, difficult to understand, and sometimes near impossible to predict code known as “spaghetti code.” The primary bad usage of the goto statement has merely been as a convenience to substitute not thinking through an alternative, more structured branching technique. Very occasionally, it can lead to clearer code.
To avoid the potential for “spaghetti code”, Java does not provide a goto statement. The Java language specifies goto as a keyword, but its usage is not supported. This is consistent with Java’s desire to make programmers write clear, non-messy code.

Variadic Arguments
C and C++ let you declare functions, such as printf, that take a variable number of arguments. Although this is a convenient feature, it is impossible for the compiler to thoroughly type check the arguments, which means problems can arise at runtime without you knowing. Java doesn’t support variable arguments at all, though if it did it would likely be able to handle subsequent runtime problems better than C or C++.

Command-line Arguments
The command-line arguments passed from the system into a Java program differ in a couple of ways from the command-line arguments passed into a C++ program. First, the number of parameters passed differs between the two languages.
In C and C++, the system passes two arguments to a program: argc and argv. argc specifies the number of arguments stored in argv. argv is a pointer to an array of characters containing the actual arguments. In Java, the system passes a single value to a program: args. ‘args’ is an array of Strings that contains the command-line arguments. 

Feature
C
C++
Java
Paradigms
Procedural
Procedural, OOP, Generic Programming
OOP, Generic Programming (from Java 5)
Form of Compiled Source Code
Executable Native Code
Executable Native Code
Java bytecode
Memory management
Manual
Manual
Managed, using a garbage collector
Pointers
Yes, very commonly used.
Yes, very commonly used, but some form of references available too.
No pointers; references are used instead.
Preprocessor
Yes
Yes
No
String Type
Character arrays
Character arrays, objects
Objects
Complex Data Types
Structures, unions
Structures, unions, classes
Classes
Inheritance
N/A
Multiple class inheritance
Single class inheritance, multiple interface implementation
Operator Overloading
N/A
Yes
No
Automatic coercions
Yes, with warnings if loss could occur
Yes, with warnings if loss could occur
Not at all if loss could occur; must cast explicitly
Variadic Parameters
Yes
Yes
No
Goto Statement
Yes
Yes
No

No comments:

Post a Comment