Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object elligible for garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object elligible for garbage collection" Watch "object elligible for garbage collection" New topic
Author

object elligible for garbage collection

Isabel Wanderley
Ranch Hand

Joined: Aug 24, 2002
Posts: 42
The question is : What is the earliest line after which the object created on //A is
eligible for garbage collection?
(Select one correct answer)
I thought was after line 3 but the answer is after line 2 anyone can explain me that ? Thanks!
<code>
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
}
}
</code>
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Can't be after line 3, since line 3 doesn't do anything that would cause an object to become unreachable.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Isabel Wanderley
Ranch Hand

Joined: Aug 24, 2002
Posts: 42
hahahahahahaha
Sorry I just read the line worng.... Sorry!!!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi, can you use the square brackets instead of the pointy ones for your code tags?
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
For my own clarification, wouldn't the object declared in the //A line be ready for garbage collection after line //1 beginning with //2?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Matt Kidd:
For my own clarification, wouldn't the object declared in the //A line be ready for garbage collection after line //1 beginning with //2?

b[1] is still referring to the object ("127") so at that point in time, that object is still not eligible for garbage collection...
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
How? Doesn't [CODE]b1 = null;[/CODE} take the reference away from the Byte object?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Yes but b[1] is still referencing the Byte object even though b1 has no reference to it anymore.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
*grumble, grumble, grumble*
ebil twick question...juuuust ebil.
(After I posted I looked at the code again and I got that feeling in my gut that was the case)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Search your feelings Matt... you know them to be true
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: object elligible for garbage collection
 
Similar Threads
GC
garbage collection
Garbage Collection from Sun Guogiao's exam 1
Garbage collection easy question
garbage collection