This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question in Garbage collection

 
Mathangi Shankar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain me the output of the following snippet(if possible with a diagram).

class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

The answer is None of the above.
 
Premkumar Gopal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since all the objects are created within the heap(in this case , the other happens to be an object of type I, the answer "None of the above is correct".
 
Premkumar Gopal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shankar,
You can refer to KBs book (pp 435 - Section "Isolating a Reference" ) talks in depth

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic