Showing posts with label Eligibility. Show all posts
Showing posts with label Eligibility. Show all posts

Sunday, 13 November 2011

Sun Certified Java Programmer (SCJP)

How many objects will be eligible for garbage collection after line 7?


public class TutorialGC
{


public static void main(String [] args)
{


Object a = new Integer(100);   // Line1
Object b = new Long(100);       // Line2
Object c = new String("100");  // Line3
a = null;                                        // Line4
a = c;                                             // Line5
c = b;                                             // Line6
b = a;                                             // Line7
// Rest of the code here


}


}



Choices:



  • A. 0

  • B. 1

  • C. 2

  • D. 3

  • E. Code does not compile


Correct choice:



  • B


Explanation:

Sun Certified Java Programmer (SCJP)

Garbage collection


Eligibility for garbage collection


An object is eligible for garbage collection when no live thread can access it.

An object can become eligible for garbage collection in different ways:

  • If the reference variable that refers to the object is set to null, the object becomes eligible for garbage collection, provided that no other reference is referring to it.

  •  If the reference variable that refers to the object is made to refer to some other object, the object becomes eligible