OCJP(Oracle Certified Java Program)

Totel:38 Click: 1 2 3 4 5 6 7 8
scjp1.5 Interview Questions And Answers

Page 1

We have two concepts for the finalize() method  which is following here:
  1. For any given object, finalize() will be called only once by the garbage collector.
  2. Calling finalize() can actually result in saving an object from deletion.

firstly we know that the when we put any code in the normal method we can put this code in the finalized method.We could write code that passes a reference to the object in question back to another object, effectively unintelligible the object for garbage collection.

The garbage collector, however, will remember that, for this object, finalize() already ran, and it will not run finalize() again.

java provides us to some mechanism for the run some code just before our object is deleted by the garbage collector, which method is use for that. that name is finalize() method,its mainly  for the all classes inherit from class.

Object we have a main prob is that we may have gathered by now, we can't count on the garbage collector to ever delete an object.

So, any code that we put into our class's overridden finalize() method is not guaranteed to run.its method use for the running a any object but its not guaranteed.

firstly we would like to mentioned that contrary to this section's title, garbage collection cannot be forced.

When java provides some ,method that allow us to request that the JVM perform garbage collection, if we are about to perform some time-sensitive operations, we want to probably minimized to the chances which is use for the delay of the garbage collection. but we also know that the methods that Java provides are requests, and not demands.

its a diff way for the objects can become eligible for garbage collection even if they still have valid references!. We also know that the islands of isolation.

We have a code its very simple example for the call instance

public class Island {
Island a;
public static void main(String [] args) {
Island a2 = new Island();
Island a3 = new Island();
Island a4 = new Island();
a2.a = a3; // a2 refers to a3
a3.a = a4; // a3 refers to a4
a4.a = a2; // a4 refers to a2
a2 = null;
a3 = null;
a4 = null;
// do complicated, memory intensive stuff

When the code reaches // do complicated, the three Island objects have instance variables so that they refer to each other, but their links to the outside world have been nulled. These three objects are eligible for garbage collection.

Its also a decouple a reference variable from an object by setting the reference variable to refer to another object.we have a following code : 
 class GarbageProg {
 public static void main(String [] args) {
StringBuffer a1 = new StringBuffer("hello this is my first garbage programe ");
StringBuffer a2 = new StringBuffer("welcome to my progarme ");
// At this point the StringBuffer "hello this is my first grambage progame " is not eligible
a1 = a2; // Redirects a1 to refer to the "welcome to my programe " object
// Now the StringBuffer "hello this is my first garbage programe " is eligible for collection

When we are created an object then we need to the method.When the method is invoked the local variable is automatically created exist only for the duration of the method.Once the method has returned, the objects created in the method are eligible for garbage collection.

Goto Page:

1 2 3 4 5 6 7 8