• 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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
Integer x = new Integer(10);
findOut(x);
Integer y = new Integer(99);
Integer z = y;
z = null;
findOut(y);
//here
}
public void findOut(Integer y){
y = null;

}
}

how many objects are garbage collected..ans=0..please explain
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what do you think the answer should be? And why do you think that? The best way to learn this stuff is to take a stab at writing down an answer- writing an answer for public review often leads to a sudden clearing of the mind
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
Integer x = new Integer(10);// An Integer object x is created
findOut(x); // A copy of the reference is passed to findOut method
// here x, y (formal parameter) are pointing to same object
// y=null statement is executed, now y is not pointing the
//Integer object, only x has the reference of Integer Object.
// Here Integer Object has one reference (reachable) so , it is
//not eligible for garbage collection


Integer y = new Integer(99); // An Integer object 'y' is created

Integer z = y;// here references z, y both are pointing to same object

z = null; // now z is not pointing to Integer object, only 'y' has the
//reference of the second Integer object.

findOut(y); // A copy of the reference is passed to findOut method
// here second Integer object is having two references
// y (local to constructor), y ( local to findOut method)
// y=null statement is executed. now 'y'(local to findOut method
// is not pointing to second IntegerObject, only 'y' ( local to
// construtor )is pointing to second Integer object.

// Two objects are created in the contrustor, first object has
// one reference ( x), second object has one reference (y). So objects are
//reachable, No object is eligible for garbage collection here


//here
}
public void findOut(Integer y){
y = null;

}
}

Hope this helps you

Regards
Naresh
(Nareshg2k5@yahoo.com)
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic