• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Kathy Sierra book doubt

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on page 259 chapter 3 selftest question #2, the given answer is C. 2, but when i try to run the code it seems that c1 c1.story c3 c3.story are all null so there should be 4 objects eligible for gc? i used this piece of code i wrote myself is there a mistake that i made? thanks

public class CardBoard {
Short story = 200;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}

public static void main (String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;

System.out.println("C1: " + c1);
try {
System.out.println("C1 Short: " + c1.story);
}
catch (NullPointerException e) {
System.out.println("C1 Short is null");
}

System.out.println("C2: " + c2);
try {
System.out.println("C2 Short: " + c2.story);
}
catch (NullPointerException e) {
System.out.println("C2 Short is null");
}

System.out.println("C3: " + c3);
try {
System.out.println("C3 Short: " + c3.story);
}
catch (NullPointerException e) {
System.out.println("C3 Short is null");
}}}
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of c3 no new objects have been created. So though you are getting null for both c1 and c3 actually there are only two objects (of c1) eligible for garbage collection.

If you can make a diagram step by step then I think it would be clear to you.

Murali...
 
reply
    Bookmark Topic Watch Topic
  • New Topic