Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

String Objects

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends
Is it correct that String objects created by using the string literals ( as shown below ) are not garbage collected.
String s = "XYZ";
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects are created on the heap. Garbage collection is done to keep the heap clean.
Variables are created in the stack. Each method has it's own stack. If it is a local variable, when the method completes, the stack for that method is discarded. The garbage collector does not do this. The variable s will be discarded whenever the variable goes out of scope.
However the String literal "XYZ" will get created at class load time in the Constant Pool, the garbage collector will not get rid of that.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rakesh,
I think you are incorrect. The only thing that happens with literal strings is that they save on object creation:
String s1 = "First";
String s2 = "Frist";
Both will point to only one String object. If I perform the following:
String s1 = new String( "First" );
String s2 = new String( "First" );
I will get two Strings in memory.
If I perform the following:
String s = "First";
s = "Second";
I would think that the JVM would clear the memory location of "First" because I no longer have any live reference to it.
Manfred.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = "First";
This does NOT point to an object. It points to a literal in the constant pool. There is no object created at all.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Check out this earlier post http://www.javaranch.com/ubb/Forum24/HTML/007315.html.
Basically, the way in which gc occurs is implementation dependent. The JLS does not require String literals to be gc'd however it doesn't say they can't be either! In real terms, it's not something you need to worry about for the exam.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,
I can't seem to find that post. Do you know where it went??
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends
String s = "First" + "Second" + "Third";
3 String objects are created, assigning
s =null;
How many objects are eligible for garbage collection?
Swati

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi swati..
as far as i think.. no object will be available for GC.. coz none was created in the first place..there were three literals which are placed in the constant pool and not on the heap..
------------------
Hima
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic