• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

class Garbage
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("hello");
StringBuffer s2=new StringBuffer("goodbye");
System.out.println(s1);
s1=s2;
}
}
in this pgm first we r printing s1 value.after we are assigning s2 value to s1.my doubt is after assgining the s2 value to s1 it is eligible for garbage collection r after making s1 to null.

suhita
 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how many objects r garbage collected?ans is 1.i can't understood how answer is one.


public class X {
2. public static void main(String [] args) {
3. X x = new X();
4. X x2 = m1(x);
5. X x4 = new X();
6. x2 = x4;
7. doComplexStuff();
8. }
9. static X m1(X mx) {
10. mx = new X();
11. return mx;
12. }
13. }
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this. you're much more likely to get help if your posts contain real words, and not things like "plz" and "r".
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please read this. you're much more likely to get help if your posts contain real words, and not things like "plz" and "r".



Well said.

in this pgm first we r printing s1 value.after we are assigning s2 value to s1.my doubt is after assgining the s2 value to s1 it is eligible for garbage collection r after making s1 to null.



Yes. Object referenced by s1 will be eligible for Garbage Collection after the assignment.


how many objects r garbage collected?ans is 1.i can't understood how answer is one.


public class X {
2. public static void main(String [] args) {
3. X x = new X();
4. X x2 = m1(x);
5. X x4 = new X();
6. x2 = x4;
7. doComplexStuff();
8. }
9. static X m1(X mx) {
10. mx = new X();
11. return mx;
12. }
13. }



Only the object referenced by x2 will be garbage collected, since it has no reference to it after the x2=x4 statement.
reply
    Bookmark Topic Watch Topic
  • New Topic