Sagewire Logo

Class destructors

9 Message(s) by 5 Author(s) originally posted in java programming


From: mlw Date:   Thursday, April 19, 2007
Could someone explain why there is no destructor in JAVA classes?

There are many times you need to be called WHEN an object goes out of scope
and not when it'll eventally be freed.


From: ~kurt Date:   Thursday, April 19, 2007
wrote in message:
Could someone explain why there is no destructor in JAVA classes?



The explanation I remember is destructors are generally used to free
memory, and JAVA has garbage collection. Does not make it right, but that
is the way it is.

- Kurt


From: mlw Date:   Thursday, April 19, 2007
wrote in message:

wrote in message:
Could someone explain why there is no destructor in JAVA classes?

The explanation I remember is destructors are generally used to free
memory, and JAVA has garbage collection. Does not make it right, but that
is the way it is.



That's one of the things that bothers me about JAVA. So many newbe CompSci
people think it is wonderful, but it has so many glaring omissions and all
too often it falls short of a real and useful programing language . Like
Windows, skill in JAVA comes from knowing the trivia of JAVA, and not from
the theory and science of your algorithms.


From: ~kurt Date:   Friday, April 20, 2007
wrote in message:
That's one of the things that bothers me about JAVA. So many newbe CompSci
people think it is wonderful, but it has so many glaring omissions and all
too often it falls short of a real and useful programing language. Like
Windows, skill in JAVA comes from knowing the trivia of JAVA, and not from
the theory and science of your algorithms.



Most of my experience is with structured programming - OOD is relatively new
to me. I have to admit, I did not start "getting it" until I started working
with JAVA. Most of the things you "can not" do, you can not do for a reason.
While I like C for structured programming, I just never liked C++ - seems
kind of like a kludge to me. Most of the C++ code I have been forced to work
with is just crap (fault of the programmer , not the language). Overall, I
have to admit I enjoy programming in JAVA. It seems to encourage better OOP.
You do need to have an idea as to what is going on under the hood to avoid
making bloated and inefficient code.

I have been coding up some engineering related algorithms to gain the
experience (not work related). I guess through using it I will see if it is
really of any real value for scientific work. I did see a demo for a JAVA3D
based application similar to Google Earth (but with an astrodynamic slant).
It was very slick - excellent graphics and performance. I have also been
messing around with JAVA3D myself - so far I'm impressed.

- Kurt


From: efriedNoSpam Date:   Saturday, April 21, 2007
wrote in message:
wrote in message:
wrote in message:
Could someone explain why there is no destructor in JAVA classes?
The explanation I remember is destructors are generally used to free
memory, and JAVA has garbage collection. Does not make it right, but that
is the way it is.
That's one of the things that bothers me about JAVA. So many newbe CompSci
people think it is wonderful, but it has so many glaring omissions and all
too often it falls short of a real and useful programing language. Like
Windows, skill in JAVA comes from knowing the trivia of JAVA, and not from
the theory and science of your algorithms.


There are a large number of places running real and useful enterprise
applications that were written in JAVA.

Being skilled in algorithms makes you a good developer . JAVA doesn'thing to
take away from that. Being skilled in JAVA "trivia" - aka the language and
"libraries" - makes you a better developer, but that's true of any language.

JAVA has its warts. Also true of all other languages I have seen.

Eric


From: Silvio Bierman Date:   Tuesday, May 01, 2007
wrote in message:
wrote in message:
Could someone explain why there is no destructor in JAVA classes?
The explanation I remember is destructors are generally used to free
memory, and JAVA has garbage collection. Does not make it right, but that
is the way it is.
That's one of the things that bothers me about JAVA. So many newbe CompSci
people think it is wonderful, but it has so many glaring omissions and all
too often it falls short of a real and useful programing language. Like
Windows, skill in JAVA comes from knowing the trivia of JAVA, and not from
the theory and science of your algorithms.



Total nonsense. Destructors haven'thing whatever to do with general theory
and science of algorithms.

In the C++ object model where objects (class instance s) and the lexical
reference to them have a one-to-one relationship destructors make sense. The
fact that C++ doesn't offer GC makes them even essential. And yes, many
other usefull constructs can be expressed using destructors.

In JAVA (and most other contemporary programming/scripting languages) the
object instance and the syntactical construct referencing it are loosely
coupled. Objects can have zero or more references pointing to them, not
unlike C++ pointer s (or C++ references, for that matter). Such an object
model doesn't allow the definition of a destructor like construct because
the lexical scope of an object reference and the lifetime of the actual
object instance are at best indirectly related.

If you value a theoretical approach to programming than you should be able
to distinguish abstract algorithms from language constructs that allow you
to implement them. If you find yourself unable to implement your abstract
algorithm in a language without destructors (which wouldn't leave you many
options anyway) then you obviously are unable to make that distinction.

Silvio Bierman


From: ~kurt Date:   Tuesday, May 01, 2007
wrote in message:
In the C++ object model where objects (class instances) and the lexical
reference to them have a one-to-one relationship destructors make sense. The
fact that C++ doesn't offer GC makes them even essential. And yes, many
other usefull constructs can be expressed using destructors.
In JAVA (and most other contemporary programming/scripting languages) the
object instance and the syntactical construct referencing it are loosely
coupled. Objects can have zero or more references pointing to them, not
unlike C++ pointers (or C++ references, for that matter). Such an object
model doesn't allow the definition of a destructor like construct because
the lexical scope of an object reference and the lifetime of the actual
object instance are at best indirectly related.



Thanks for the writeup.

- Kurt


From: Faton Berisha Date:   Wednesday, May 09, 2007
wrote in message:
Total nonsense. Destructors haven'thing whatever to do with general theory
and science of algorithms.



Well, for that matter, neither do constructors.

The reason for not implementing destructors in JAVA is,
I think, that many found it difficult to properly use them
(if not completely omit them), and this is compatible with not
implementing pointers.

Personally, the thing I miss the most from C++ is operator overloading.
(Readability is to blame here, I guess.)

Obviously, the designers had in mind things other than
developing a scientific computation or a system programming language
when designing JAVA.

Faton Berisha


From: Silvio Bierman Date:   Thursday, May 10, 2007
wrote in message:

Total nonsense. Destructors haven'thing whatever to do with general
theory and science of algorithms.

Well, for that matter, neither do constructors.



Off course. Only a language as a whole represents a view on implementing
algorithms.

The reason for not implementing destructors in JAVA is,
I think, that many found it difficult to properly use them
(if not completely omit them), and this is compatible with not
implementing pointers.


I already stated why a destructor is in contradiction with the JAVA language
logic. JAVA object references are quite close to pointers in C(++). Only the
concept-mix of array s and pointers that C++ inherited from C (as much as
Stroustrup said to regret that) is totally missing which means no pointer
arithmetic.

Being an old time C (and since its early conception, C++) programmer I kind
of liked pointer arithmetic and I must admit I sometimes miss it when doing
some array fiddling in JAVA.

Personally, the thing I miss the most from C++ is operator overloading.
(Readability is to blame here, I guess.)



I welcomed it when it was added to C++ and used it enthousiasticly. In
retrospect I find it is only suitable when implementing very generic classes
(like strings, collection classes etc). I always found application level
logic to be expressed best with descriptively named operations
(functions/methods).

Obviously, the designers had in mind things other than
developing a scientific computation or a system programming language
when designing JAVA.



My guess is they started with C++ but wanted something managed (Virtual
Machine based) and simpler (quirk-free as opposed to C++ which is full of
quirks that only language theorists like to play with). They made some good
design decisions, some bad. The resultng JAVA environment just works,
despite some things I'd like to have seen different about the language.
The cross-platformness and the enormous amount of freely availably libraries
for almost anything a developer could need in combination with an OK
language has effectively won me over.

I find it pointless to discuss isolated language details or doing
apples/oranges comparisons between programming languages. In the end the
quality and timely availability of the resulting software systems is all
that counts, whichever languages where used to implement them.

Silvio Bierman

Faton Berisha






Next Message: Applets not being garbage collected


Blogs related to Class destructors

Java/J2ee Interview Companion
C++ requires explicit memory management, while Java includes automatic garbage collection. [Refer Q32 in Java section]. Q 04: Explain Java class loaders? Explain dynamic class loading? LF A 04: Class loaders are hierarchical. ...

Top 100 Java / J2EE Questions Asked in Actual Interviews
Name four methods every Java class will have. http://www.coolinterview.com/interview/382 * What does the "abstract" keyword mean in front of a method? A class? http://www.coolinterview.com/interview/383 * Does Java have destructors? ...

Why I should learn Lisp
Java was the language that took away from me the pride of writing destructors and allocators. But in course of this sense of loss, I realized that I was now programming at a higher level of abstraction with the entire memory management ...

Questions About Exception Specifications
Required: In the declaration of a virtual function that overrides a base class virtual function that has an exception specification. Implicit and automatic: In the declaration of the constructors, assignment operators, and destructors ...

Using marshal_as for mixed-mode string conversions
While I did realize that I had misspelled converter as convertor I decided to leave it like that so I got a unique class-name and that way I could avoid Google dilution (there are dozens of other StringConverter classes, specially Java ...

errors trying to install mplayerplug-in from source
class ns ISupports’ has virtual functions but non-virtual destructor Source/nsIScriptableMplayerPlugin.h:25: warning: ‘class nsIScriptableWMPPlugin ’ has virtual functions but non-virtual ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional