Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection question

 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings, Literally
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic