When you move from C++ to Java, one of the more subtle, yet
important issues you will face is the difference between a C++ destructor and a Java finalize() method. Although
similar in many respects, their actual operation is
distinctively different. Let's begin by reviewing the purpose and effect of a C++ destructor and the
Java finalize() method. In C++, when an object goes out of scope, it is destroyed. Just
prior to its destruction, its destructor function is called (if it has one). This is a
hard-and-fast rule. There are no exceptions. Let's look more closely at each part of this rule:

Every object is destroyed when it goes out of scope. Thus,
if you declare a local object inside a function, when that function returns, that local
object is automatically destroyed. The same goes for function parameters and for objects
returned by functions.

Just before destruction, the object's destructor is called. This
happens immediately, and before any other program statements will
execute. Thus, a C++ destructor will always execute in a deterministic fashion. You
can always know when and where a destructor will be executed.

In Java, the tight linkage of the destruction of an object and
the calling of its finalize()
method does not exist. In Java, objects are not explicitly
destroyed when they go out of scope. Rather, an object is marked as unused when there are no
longer any references pointing to it. Even then, the finalize() method will
not be called until the garbage collector runs. Thus, you cannot know precisely when or where a
call to finalize( ) will occur. Even if you execute a call to gc( ) (the garbage
collector), there is no guarantee that finalize( ) will immediately be executed.

While the deterministic behavior of a C++ constructor and the
somewhat probabilistic aspect of finalization are of little concern in most cases, they
will have an impact on others. For example, consider the following C++ program:

Look carefully at the constructor and destructor for X.
The constructor increments the value of count as long as count is less than MAX.
The destructor decrements count. Thus, count is incremented when an X object is
created and decremented when an X
object is destroyed. But no more than MAX objects can
exist at any one time. However, in main( ), f( ) is called MAX*2 times
without causing an error! Here is why. Inside f( ), an object of type X is created, causing count to be
incremented, and then the function returns. This causes the object to immediately go out of scope
and its destructor to be called, which decrements count. Thus, calling f( ) has
no net effect on the value of
count. This means that it can be called indefinitely.
However, this is not the case when this program is converted to Java.

The reason the program fails is that garbage collection does not
occur each time f( )
returns. Thus, finalize( ) is not invoked, and the value
of count is not decremented. After five calls to the method, count reaches its maximum value
and the program fails. It is important to emphasize that precisely when garbage
collection occurs is implementation dependent. It is possible that for some
implementation of Java, on some platform, the preceding program will function similarly to its
C++ version. However, the point of the example remains: In C++, you know when and where a
destructor will be called. In Java, you do not know when or where finalize( ) will
be executed. Therefore, when porting code from C++ to Java, you will need to watch for
instances in which the precise timing of the execution of a destructor is relied upon.