Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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...
 
Happily living in the valley of the dried frogs with a few tiny ads.
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!