File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection from Sun Guogiao's exam 1

 
MONZY THARIAN
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one explain me how it will work
===============================================================
What is the earliest line after which the object created on //A is eligilble for garbage collection? Select one correct answer)
public class Test059
{
public static void main(String args[])
{
Byte b1 = new Byte("127"); //A
Byte b[] = new Byte[2];
b[1] = b1;
b1 = null; //1
b[1] = null; //2
System.out.println(b1); //3
System.out.println(b[1]); //4
}
}
Thanks
Monzy
 
Mariusz Szurnacki
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!!
I think that after the line with number 2.
Have a nice day,
Mariusz
 
Mariusz Szurnacki
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, you wanted the explanation and I gave you only the answer.
Here the object is referenced by two references: b1 and b[1],
so this object is eligible for garbage collection after these two refereces have null values.
Mariusz
 
Roopa Bagur
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think b1 will be eligible for garbage collection after line 3.Because the system.println statement still references b1 in line 3.
Somebody correct me if I am wrong.
Originally posted by MONZY THARIAN:
can any one explain me how it will work
===============================================================
What is the earliest line after which the object created on //A is eligilble for garbage collection? Select one correct answer)
public class Test059
{
public static void main(String args[])
{
Byte b1 = new Byte("127"); //A
Byte b[] = new Byte[2];
b[1] = b1;
b1 = null; //1
b[1] = null; //2
System.out.println(b1); //3
System.out.println(b[1]); //4
}
}
Thanks
Monzy

 
Guoqiao Sun
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Roopa, I think Mariusz is right. The object will be eligible for GC after all the references to it are set to null.
Regards,
------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹²³ at my homepage.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi agree with Sun and Mariusz...
We are are talking about an "object" and b1 is a reference...
So the object created on line A will be eligible for GC as soon as no references is pointing to it, i.e. afeter line 2.
Val
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic