Extrait du course Java and C a critical comparison
Software engineers are being forced to become multi-lingual. Any of you who have had the pleasure of creating a web site know this. Reading this article will be something like that. I freely swap between C++ and Java throughout. Those of you who are not familiar with Java need not fear, you will find the syntax to be very similar to C++. However, for serious readers, a Java reference guide might be a good thing to have nearby. Fortunately, the entire API for the current release of Java is on-line at www.javasoft.com/products/JDK/CurrentRelease/api/
Multiple Inheritance
The designers of Java avoided multiple inheritance. Replacing it is multiple conformance to interfaces. In Java, there is a structure called an « Interface ». A Java interface is almost identical to a C++ class that has nothing but pure virtual functions. In Java, you cannot inherit from more than one base class; even if the base classes have nothing but abstract methods (pure virtual functions). However you can“implement” more than one “interface”; which amounts to the same thing.
For example. In Java you can create the interface for a Stack as follows:
public interface Stack
{
public void Push(Object o);
public Object Pop();
};
This structure is roughly identical to the following C++ code:
class Stack
{
public:
virtual void Push(Object&) = 0;
virtual Object& Pop() = 0;
};
However, a Java interface is nota class. The functions declared within a Java interface cannot be implemented within that interface. Moreover, a Java interface cannot have any member variables.
Because interfaces cannot have function implementations or data members, multiple implementation of interfaces does not lead to the problems that caused “virtual” inheritance to be added to C++. That is, in Java there is no need for virtual inheritance since it is impossible to inherit the same member variable from more than one path.
Memory Management
Java uses garbage collection. Garbage collection is a scheme of memory management that automatically frees blocks of memory sometime after all references to that memory have been redirected. For example, consider the following snippet of Java:
Clock c = new Clock(); // c refers to the new clock.
// … use c for awhile.
c = null; // done with that clock. System will clean up later.
In this example, we create a new Clockobject using the keyword: new. The new object is referred to by the variable ’c’. Note that ’c’ is rather like a reference variable in C++; however in Java it is possible to reassign references. We use the new Clockobject through its reference variable ’c’ for awhile. Then, when we are done with it, we redirect ’c’ to null. When the Java runtime system detects that there are no more reference variables referring to the Clockobject, it classifies that object as “garbage”. At some later time, the Java runtime system will clean up that “garbage” returning its memory to the heap.
Garbage collection makes certain kinds of applications much easier to program. The designers of those programs need not worry as much about cleaning up after “dead” memory. As a result,C++ is often criticized for its lack of GC. However, many people have added garbage collectors to C++. Some of these are available as third party products, or as shareware on the net. These collectors are far from perfect, but they can be used when convenient.
The corresponding statement cannot be made for Java. There is no waythat this humble writer could discover to manage memory manually. Apparently, you cannot write your own memory manager and construct objects within the memory that it controls.
……….
Course Java and C++ a critical comparison (105 KO) (Cours PDF)