• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection from Sun Guogiao's exam 1

 
MONZY THARIAN
Ranch Hand
Posts: 63
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!