Saturday, June 19, 2010

C++ & Java Comparison

Here are some of the main differences between C++ and Java. This will be useful when people knowing C++, start learning Java, as I did.

1) Platform Independence

C++ is platform dependent. Programs are compiled and linked for a specific platform. Whereas, Java is platform independent. Java compiles the code into a byte-code, which is platform independent. Each platform has a Java runtime VM, which on-the-fly interprets the byte-code into native machine-code.

2) The main() entry point

Typically C++ programs begin with the main function (or WinMain etc). However, in Java, since all functions are inside classes only, the main function is a static function in one of the classes inside the Java program.

3) Function Declaration
C++ requires function prototypes to be declared before they are called. No such requirement in Java.

4) typedef, #define
In C++, typedef lets you define new data types, #define lets you create constant values/macros. However, in Java, no typedef or #define is supported. Named constants are supported using the final keyword in Java.

5) Variables and Objects
In C++ Variables and Objects can be on the stack or on the heap. For eg if Fruit is a class, the following are acceptable.

a) Fruit apple;
b) Fruit apple = new Fruit();

Also, variables and objects can be global.

In Java, Variables can be on the stack or on the heap. However, Objects can ONLY be on the heap. For eg if Fruit is a class, ONLY the following is acceptable.

a) Fruit apple = new Fruit();

No global variables or objects allowed in Java. However, something similar can be achieved using the static keyword.

6) Primitive types
In C++, int, char, long, double and float supported. In addition to this, Java supports the byte and boolean types. Further, Java provides very useful wrapper classes for all primitive types. Also char is 8-bit in C++, whereas 16-bits in Java, as it supports Unicode.

7) Enumerated types
Simple C-style enumerated types present in C++. The same is available in Java. In addition, Type-safe and much enhanced enumerated types are added in Java. After knowing them, you will discontinue using the simpler enum type.

8) struct and union
In addition to classes, struct and unions are supported in C++. No support for struct or union in Java.

9) Instance variables
In C++, No instance variable can be initialized during the class definition. In Java, instance variables can be initialized during class definition.

10) static variables
In C++ static variables need to be initialized outside the class definition using the :: operator. However in Java, static variables can be initialized inside the class definition itself.

11) Access specifiers
In C++, public, protected and private access speficiers are present. In Java, in addition to public, protected and private, the default access specifier that provides package level access is present.

12) goto
In C++, usage of goto allowed. No such keyword exists in Java. However, limited usage of labels is allowed inside loops.

13) Pointers, this & scope resolution
In C++, Pointers are allowed. The this pointer is used with -> to access members. The :: operator, helps in accessing variables and methods outside the class definition

In Java, Pointers are NOT allowed. No program can specify a memory address explicitly and try accessing the location.Also all variable are passed by values. No :: operator in Java.

14) Destructors, Garbage collection
In C++, Destructors are implemented and any memory allocated from heap by class methods, have to be freed during destruction. No destructors in Java. No need to free memory allocated using the new operator as Java automatically frees up unused memory using Garbage collection. This ensures that there are no memory leaks.

15) Polymorphism
In C++, Polymorphism is implemented through the following:
a) Static Polymorphism
    a.1) Function overloading
    a.2) Operator overloading

b) Dynamic Polymorphism

In Java, Polymorphism is implemented through the following:

a) Static Polymorphism
a.1). Function overloading

b) Dynamic Polymorphism

Yes, no operator overloading in Java!

16) Inheritance
In C++, Single and multiple inheritances are supported. In Java, only single inheritance is possible. However, 'interface' in Java provides something equivalent to what multiple inheritance does in C++. Also, all user defined classes derive from the Object class in Java.

17) Virtual functions
In C++, explicit usage of the virtual keyword is required to make a method virtual. However, in Java all methods default to the virtual behaviour. Due to this, Java does not implement the virtual keyword.

18) Virtual Inheritance
In C++, using the virtual keyword in the base class makes it possible to have a diamond shaped inheritance relationship possible. As multiple inheritance is not supported, there is no question of virtual inheritance in Java.

19) namespace
In C++ namespace definitions help avoid collision of variable and class names. However, in Java, all variables are inside classes and packages are implemented to avoid collision. No namespace in Java.

20) Templates
In C++, template functions and classes are supported. This is to support generic-coding. No templates in Java. However, generics supported.

21) Collections API
In C++, the Standard Template Library (STL) implements quite a few features like Vector and List. In Java, the Collections API implements Vector, List, Set and Map. Also, as Java is a open source project, innumerable APIs keep getting added by the day. Also there are many more APIs for Cryptography, Internet programming, apart from the Collections API.

22) RTTI
In C++, RTTI is implemented with the typeId and dynamic_cast keywords. In Java, the instanceOf keyword serves the purpose.

-oOo-

1 comment:

  1. Nice content presentation! Thanks for putting the efforts on gathering useful content and sharing here. You can find more Programming languages related question and answers in the below forum.

    Programming language question and answers

    ReplyDelete