• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing lost string objects in the "string pool"

 
Anuj Troy
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Raghu Shree
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
vidya sagar
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Strings in String literal pool never get collected by garbage collector
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi can i say that obj that are created in pool are not eligible for the GC
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii all

thanks a lot for all the information it was really useful

regards
anuj
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic