• 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
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

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
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic