This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Kathy Sierra book doubt  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!