• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage Collection from Sun Guogiao's exam 1

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic