Java has its own garbage collection implementation so it does not require any destructor like C++ . This makes Java developer lazy in implementing memory management.
Still we can have destructor along with garbage collector where developer can free resources and which can save garbage collector's work. This might improves the performance of application. Why does Java not provide any destructor kind of mechanism?
Developer does not have control over GC but he/she can control or create object. Then why not give them ability to destruct the objects?
I think you are confusing terminology. Here is how I see it:
create object = first allocate memory, then construct via constructor
destroy object = first destruct via destructor, then deallocate memory
How the memory is allocated and deallocated depends. If you use
new
anddelete
, the memory management is done byvoid* operator new(size_t)
andvoid operator delete(void*)
.If you know you don't some big objects anymore just set the references to them to null. This could maybe speed up the garbage collection of these objects.
Destructors are called when the object is destroyed in C++, not to destroy the object. If you want to guarantee cleanup, make the user call a Destroy method or similar.
No, java does not support destructors. All freeing the memory task is done by GARBAGE COLLECTOR.
Java has it's own memory management feature using garbage collector. When you use finalize() the object becomes available for garbage collection and you don't need to explicitly call for the destructor. C# and Java don't want you to worry about destructor as they have feature of garbage collection.
Java is a bytecode language, it has very strong garbage detection. If you were to allow people to define their own destructors it is likely that they might make some mistakes. By automating the process, Java intends to prevent those mistakes.