• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String literals and GC

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
What ever i have read abt String literals
and garbage collection it tells that String literals are not garbage collected as these are not created by new operator (so they are not objects).
But what if questions like these come in the exam ???
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

On the exam do they have such questions..without new operator created objects?
what should we consider?? should we ignore the fact that string literals are not Gced and choose
some option?
Thanks
swapna
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say the answer is a) 0, because all of those strings are created on the literal pool which is never available for garbage collection. GC only collects "objects" so, if you don't call new you are not creating an object.
-Matt
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
these are not created by new operator (so they are not objects).
Wrong!!!
String literals are perfectly valid Java objects.
Moreover, Sun will not test on String literals garbage collection since this is 1) tricky business; 2) compiler dependent...
 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I have read, it says specifically that String literal pool is not eligible for GC.
I guess it doesnt matter because it's not on the test, but I am still curious, because the stuff I have read, indicates what I said above, which is in stark contract to what you stated Valentin.
-Just wondering
-Matt
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, String literals are not eligible for garbage collection but it's not because they aren't objects, it's because there is always a reference to them. Even though you might not have a reference to it, one exists within the String Constants Table and will remain there until the application terminates. So, since there is a reference to the object, it can't be garbage collected.
Take a look at this thread for a rather thorough discussion of the topic.
Hope that helps,
Corey
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the replies..this has cleared my
biggest doubt that are String literals objects or not??(thank u Valentin)..
swapna
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and of course...

compiles just fine.
 
reply
    Bookmark Topic Watch Topic
  • New Topic