File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection question" Watch "Garbage collection question" New topic

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

I think in Your Code ,
after line
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.

wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Strings, Literally
I agree. Here's the link:
subject: Garbage collection question
It's not a secret anymore!