What is finalize()? Is finalize() similar to a destructor? - Java

What is finalize()? Is finalize() similar to a destructor?

The finalize() method of java is invoked by JVM to reclaim the inaccessible memory location, When the ‘orphan’ object (without reference) is to be released, JVM invokes the finalize() method, when the next garbage collection passes and reclaims the memory space. Choosing to use finalize() provides the ability for performing some important cleanup action at the time of garbage collection.

The destructor of C++ language will always destroy the objects. But Garbage collection is not destruction. In case, some functionality needs to be performed prior to garbage collection. The respective functionality must be hard coded by the developer. As Java has no destructor or similar concept, the process need to be embedded into a method for cleanup. The finalize() method can be overridden to perform this cleanup.

Why does String define the equals( ) method? - Java
The equals() method is defined in String class in order to test whether two strings values are same sequence of characters...
String objects are immutable in java
A string object that is created using “String” class is immutable. The characters of the object can not be changed / modified. There are some methods, such as split(), substring(), beginsWith() etc...
Can we pass a primitive type by reference in java? - Java
Primitive types can be passed by reference. 1. By storing the primitive type value in an array of single element and passing it...
Post your comment