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

Strings

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic