aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes accessing lost string objects in the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "accessing lost string objects in the "string pool"" Watch "accessing lost string objects in the "string pool"" New topic
Author

accessing lost string objects in the "string pool"

Anuj Troy
Ranch Hand

Joined: Apr 07, 2005
Posts: 30
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


SCJP 1.4, SCWCD 1.4
your eyes cannot see what your mind does not know
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
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 ]

Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Anuj Troy
Ranch Hand

Joined: Apr 07, 2005
Posts: 30
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

Joined: Mar 02, 2005
Posts: 580
hi

Strings in String literal pool never get collected by garbage collector
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi can i say that obj that are created in pool are not eligible for the GC
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Anuj Troy
Ranch Hand

Joined: Apr 07, 2005
Posts: 30
hii all

thanks a lot for all the information it was really useful

regards
anuj
 
jQuery in Action, 2nd edition
 
subject: accessing lost string objects in the "string pool"