This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Hi All,

Can a string object with no reference, residing in a string pool be got back? That is, i understand that each time a new literal is creatd, JVM searches for a same string in the string pool anf if it finds one it refers to that one. So if there is string which has no existing references, can it be referred by a new reference variable?


Cheers!
RSR
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Oh yeah, the constants pool will be in the memory till the class definition is loaded in the JVM. Using String.intern() you can refer to an already existing string in the pool. It does not matter whether the string object has any references or not.


apigee, a better way to API!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
However, if there are no other references to the pooled String (other than the pool itself, which acts as a WeakReference) then the String is eligible for garbage collection. This generally will not happen right away, especially since pooled Strings are stored in a part of the heap reserved for objects which rarely get garbage collected (the so-called "permanent" generation of the heap). But it can happen eventually, and if it does, then that particular instance is lost.


"I'm not back." - Bill Harding, Twister
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Strings