• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

accessing lost string objects in the "string pool"

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.String s1 = "abcd";//creates one string object.

2.s1="xyz";//another string object get created and previous object is lost.

3.String s2="abcd";

my question is ..

after third line do i have another string object created with value "abcd"?? or ..

since "abcd" was still present in the 'String Pool' the new string s2 points to the already existing and 'abandoned' string "abcd"?

i hope i was clear

anuj
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


2.s1="xyz";//another string object get created and previous object is lost.


No. The object is still available in pool


after third line do i have another string object created with value "abcd"?? or ..


No. The object is available in String pool. compiler reuse the available object from string pool.
IF u want to know more look at the following link.
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

Hope It will useful.
[ April 07, 2005: Message edited by: Raghu Shree ]
 
Anuj Troy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii raghu
i went thru the link you gave me.. according to that if you look at my code after line 2. the string object "abcd" is elligible for garbage collection!

so if the garbage collector runs before line 3. the string "abcd" will not exist any more.. in that case when line 3 executes a new string object has to be created.

now my doubt is that when i isolate the string object"abcd" after line 2. Does it really gets elligible for garbage collection??

because if it does(gets elligible for GC) then it all depends upon whether GC ran or not befor line 3!!

right?

regards
anuj
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Strings in String literal pool never get collected by garbage collector
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi can i say that obj that are created in pool are not eligible for the GC
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, in fact they can be garbage collected, but only under specific unusual conditions. Specifically, the class(es) that use a given literal must first be unloaded. (Or at least, eligible for unloading.) Otherwise that class definition will contain reachable references to the pooled string object. The pool itself doesn't prevent GC of the objects in the pool (that is, the objects on the heap which are referenced by the pool) - but the class(es) which use the pooled instances usually have other references which prevent GC.

As usual when string pooling is mentioned, this is far more information than you need to know for the exam. So if this seems too complicated, don't worry - the exam is not going to ask you whether pooled strings can be garbage collected.
 
Anuj Troy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii all

thanks a lot for all the information it was really useful

regards
anuj
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic