CIS 22 - Data Structures: C++ vs. Java

Similarities and Differences Between C++ and Java


C++ Features Not Found in Java:
    1.    Pointers.
    2.    Global Variables.
    3.    Multiple Inheritance.
    4.    Templates.
    5.    Operator Overloading.
    6.    Preprocessor.

Differences between C++ and Java:
    1.    Object manipulation.

 
Similarities between C++ and Java:
    1.    Local Variable Declarations.
    2.    Method Overloading.
 
 
 
 


C++ Features Not Found in Java.
1.  Java Has No Pointers.
The referencing and dereferencing of objects is handled for you automatically by Java. Java does not allow you to manipulate pointers or memory addresses of any kind: There are two reasons for these restrictions: 2.    Java Has No Global Variables.
In Java, every field and method is declared within a class and forms part of that class. The fields and methods of a class are known as the members of a class.

3.    Java Does Not Support Multiple Inheritance.
C++ supports multiple inheritance of method implementations from more than one superclass at a time. While it seems like a very useful feature, adding it to the language turns out to introduce many complexities. The Java language designers chose to avoid the added complexity by using interfaces instead. Thus, a class in Java can only inherit method implementations from a single superclass, but it can inherit method declarations from any number of interfaces. In practice, this is not any particular hardship.

4.    Java Does Not Support Templates.
C++ supports templates that allow you, for example, to implement a Stack and then instantiate it as Stack <int> or
Stack <double> to produce two separate types: a stack of integers and a stack of floating point values. Java has no such facility. However, the fact that every class in Java is a subclass of Object means that every object can be cast to an instance of Object. Thus, in Java, it is often sufficient to define a data structure (such as a Stack class) that operates on Object values - the objects can be cast back to their actual type whenever necessary.

5.    Java Does Not Allow Operator Overloading.
C++ allows you to define operators that perform arbitrary operations on instances of your class. In effect, it allows to extend the syntax of the language. This is a nifty feature, called operator overloading, tat makes for very elegant examples. In practice however, it tends to make code hard to understand. After much debate, the Java language designers decided to omit such operator overloading from the language. Note, though, that the use of the + operator for string concatenation in Java is at least reminiscent of operator overloading.

6.    No Preprocessor.
Java does not include any kind of preprocessor like the C++ preprocessor. It may seem hard to imagine programming without #define, #include, and #ifdef, but in fact Java really does not require these constructs.


Differences between C++ and Java.

1.    Object manipulation.
    In C++, objects are by default manipulated by value; you must use & to specify a variable or function argument that is automatically manipulated by reference.
    In Java, all objects are manipulated by reference, so there is no need for this & syntax.


Similarities between C++ and Java.
1.    Local object declarations.
A feature that Java has borrowed from C++ is the ability to declare and initialize local variables anywhere in a method body or other block of code. Declarations and their initializers no longer have to be the first statements in any block - you can declare them where it is convenient and fits well with the structure of your code.

2.    Method Overloading.
    A technique that Java borrows from C++ is called method overloading. Overloaded methods are methods that have the same name , but have different signatures. In other words, they take different types of arguments, a different number of arguments, or the same type of arguments in different positions in the argument list. You can not overload a method by changing only its return type. Two methods with the same name may have different return types, but only if the method arguments also differ. Similarly, two overloaded methods may throw different exceptions, but only if their arguments differ as well.
    Method overloading is commonly used in Java to define a number of related functions with the same name, but different arguments. Overloaded methods usually perform the same basic operation, but allow the programmer to specify arguments in different ways depending on what is convenient in a given situation.

Michael Gorenburg - Spring '98


Back to CIS 22 home page