Thursday, October 15, 2009

Differences between Java and C++ in terms of OOP

Most  people start learning Object Oriented Programming from either Java and/or C++.  Although the concept of Object Oriented Programming is the same (in fact, you can even do Object oriented programming without use any OO language), there are subtle difference between Java and C++ from OOP perspective.  I try to list some of the key differences for reference, you are welcome to comment. Please note the grammar differences are not the interest point of this blog.

All stand-alone C++ programs require a function named main and can have numerous other functions. Java does not have stand alone functions, all functions (called methods) are members of a class. All classes in Java ultimately inherit from the Object class, while it is possible to create inheritance trees that are completely unrelated to one another in C++.  In summary, Java is a pure Object oriented language, while C++ is a mixture of Object oriented and structure language.

The interface keyword in Java is used to create the equivalence of an abstract base class containing only method declarations and constants. No variable data members or method definitions are allowed. C++ does not support interface concept. Java does not support multiple inheritance. To some extent, the interface feature provides the desirable features of multiple inheritance to a Java program without some of the underlying problems.

Java is running on a Virtual Machine, which can recollect unused memory to the operating system, so Java does not destructor.  Unlike C++, Java cannot access pointers to do memory operation directly. This leads to a whole host of subtle and extremely important differences between Java and C++. 

Furthermore, the C++ compiler does not check whether all local variables are initialized before they are read. It is quite easy to forget initializing a variable in C++. The value of the variable is then the random bit pattern that happened to be in the memory location that the local variable occupies.

Java does not have global functions and global data. Static in Java is just like global in C++, can be accessed through class name directly, and shared by all instances of the class.  For C++, static data members must be defined out side of class definition, because they don't belong to any specific instance of the class.

Generally Java is more robust than C++ because:

  • Object handles (references) are automatically initialized to null.
  • Handles are checked before accessing, and exceptions are thrown in the event of problems.
  • You cannot access an array out of bounds.
  • Memory leaks are prevented by automatic garbage collection.

While C++ programmer clearly has more flexibility to create high efficient program, also more chance to encounter error.

Monday, October 12, 2009

Difference between Stack memory and Heap Memory.

Recently I came across the difference between stack/heap topic. In simple, Both are dynamic memory allocated for program execution, but not the only 2 memory regions allocated for program execution.

Please Google “stack, heap” for the definition, summary for the difference:

Heap
- free-list - list of free space
- on allocation - memory manager finds space and marks it as used changing free-list
- on de-allocation - memory manager marks space as free changing free-list
- memory fragmentation - memory fragments into small blocks over lifetime of program
- garbage collection - coalesce fragments, possibly moving objects (must be careful of pointers when moving!)
- Concept at Operating system Memory management layer.

Stack
- clean and efficient support for nested functions and recursion
- central concept is stack frame (also called activation record)
- Concept at Micro Processor hardware registers layer.

Simple Example:

void foo()
{
    int x;                     <<< x is on the stack
    char *ptr = new char[255];
<<< 255 characters are allocated in the heap, but ptr object is on the stack
    int array[255];            <<< 255 ints are all on the stack
}