• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!