Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection question" Watch "Garbage collection question" New topic
Author

Garbage collection question

Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Could anybody give answer of this question...

How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4


...........................
I expected it is zero, as string literals do not garbage collected and remain on string pool instead of heap.
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,

I think in Your Code ,
after line
newName="jason",
the Nick Object will be eligible for the garbage collection.Because although
"nick" is a string literal,an "nick" object is also created in the heap.

Please Correct me if iam wrong.

Thanks
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Strings, Literally
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage collection question