File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object elligible for garbage collection

 
Isabel Wanderley
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't be after line 3, since line 3 doesn't do anything that would cause an object to become unreachable.
 
Isabel Wanderley
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hahahahahahaha
Sorry I just read the line worng.... Sorry!!!
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can you use the square brackets instead of the pointy ones for your code tags?
 
Matt Kidd
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How? Doesn't [CODE]b1 = null;[/CODE} take the reference away from the Byte object?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but b[1] is still referencing the Byte object even though b1 has no reference to it anymore.
 
Matt Kidd
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic