Raymond said:
The context of the original quote (which I inadvisable snipped) did have
the connotation of "only",
That connotation is only in your imagination because it was not there in
what I wrote.
in that you were comparing them to a scheme
in Java where close() or release() type methods were invoked. The only
way to do this in Java is manually and you said this "not that different
from C++". It is very different in that it is possible to write C++
programs in which the destructors are never invoked manually but do run.
This is not possible in Java.
You are still missing my point. In Java, all objects are on the heap. In
C++ you can put objects on the heap as well and when you do the
destructor is called manually using the delete statement. You may not do
it yourself (e.g. using an auto pointer) but it still exists in some
piece of code. You cannot write code for objects on the heap in C++ in
which the destructor for those objects run without there being a manual
invocation of delete somewhere in somebody's code. Therefore for heap
based objects there is little difference between C++ and Java.
In any case, I do not want to get into a word parsing you said/I said
match. I just want to make it clear that C++ destructors are not solely
invoked manually and have no comparable mechanism in Java. My apologies
if I misunderstood your post.
And it is not the lack of destructors you are talking about but the lack
of stack-based objects. For stack-based objects, yes the destructor is
invoked automatically when the object goes out of scope. All of the
cases where the destructor (really the delete operator) is not invoked
manually involve a stack based object of some sort. Either the object
itself is stack based or you use a stack based object to invoke the
delete on a heap based object (e.g. auto_ptr).
There is no automatic calling of destructors without stack based
objects. Therefore the issue is not destructors vs. dispose methods but
the lack of stack-based objects (or variables that hold objects).
Java has very good reasons for not having stack-based objects that far
outweigh the slight inconvenience of manually invoking a dispose method
on those objects that deal with non-memory resources.
There is a much bigger difference than you represent. In C++ you *must*
manually delete objects created via new or you get a memory leak.
And when I said that you have to manually delete objects above you told
me I was wrong! Java only has "objects created via new". And just like
in C++ you have to manually do something to dispose of it. In Java it is
only necessary for non-memory resources, but in C++ you have to do it
for every object.
Objects in Java do not *necessarily* have a method that must be called
to release resources.
Right. You only have to do it for non-memory resouces. But the point is
that just like C++ it is a manual process.
Right; the real point however is that creating local variables is
standard operating procedure in C++ while it is not possible in Java. So
in C++ developers have the option of using local variables and getting
the destructors invoked "for free". This option is not available in
Java, as you note below.
And it is this feature you are asking for, not destructors. Destructors
are meaningless in Java without this feature.
You'll have to be more clear here. I'm not even certain which language
you are referring to.
I was referring to C++. Consider this expanded version of the example:
class A
{
public:
virtual int foo() { return 5; }
};
class B : public A
{
public:
virtual int foo() { return 10; }
};
and this code to call it:
B b;
A a = b;
A *pa = &b;
cout << a.foo() << endl;
cout << pa.foo() << endl;
The fact that this prints:
5
10
is a point of confusion for many newbies.
I do not see how your conclusion follows. Destructors could be added to
Java with benefits. All that would really be necessary would be that
the JVM should guarantee that finalize() is called. Then
implementations of java.sql.Connection could close() themselves, etc.
There are many pitfalls there however and the designers of Java chose
not to include this benefit. To say it buys you nothing doesn't wash
however.
And when will they be called? Is it OK if they are never called until
the program shuts down? If you want it to be guaranteed to be some time
sooner than that you'll have to explain how the JVM can guarantee that.