aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection from Sun Guogiao's exam 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection from Sun Guogiao Watch "Garbage Collection from Sun Guogiao New topic
Author

Garbage Collection from Sun Guogiao's exam 1

MONZY THARIAN
Ranch Hand

Joined: Feb 21, 2001
Posts: 63
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

Think Big . So shall you become big.
Mariusz Szurnacki
Ranch Hand

Joined: Sep 12, 2001
Posts: 44
Hi!!!
I think that after the line with number 2.
Have a nice day,
Mariusz

<BR>
Mariusz Szurnacki
Ranch Hand

Joined: Sep 12, 2001
Posts: 44
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

Joined: Nov 03, 2000
Posts: 267
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

Joined: Jul 18, 2001
Posts: 317
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.


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection from Sun Guogiao's exam 1
 
Similar Threads
object elligible for garbage collection
help scjp5.0 question
Eligible for garbage collection
garbage collection
strange results