• 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

Confusion in String Constant Pool

 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

from the K and B book

When the
compiler encounters a String literal, it checks the pool to see if an identical String
already exists. If a match is found, the reference to the new literal is directed to the
existing String, and no new String literal object is created. (The existing String simply
has an additional reference.
)

does this mean that only those strings are checked which hava reference ? what about the lost objects which they dont have nay
reference pointing to them?


String s = new String("abc"); // creates two objects,
// and one reference variable
In this case, because we used the new keyword, Java will create a new String object
in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will
be placed in the pool.

>> this is alittle bit confusing could somebody please clear it up for me? does this mean s refers to two objects? why do we need to create two objects ? so what happens to the object which is in normal (nonpool) memory when we want to alter the objec refered to by s ? will it be aaboned as well? at last isn't it redundancy?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great article at our very own JavaRanch:

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

...Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.



Since Strings are immutable, multiple references can share the same actual value. This is done to save resources. However, using the new keyword manually creates unique objects and is redundant like you say.

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think creating String s = new String("abc") should not put put String "abc" into String pool , otherwise why do we need intern() ? Please correct me if I am wrong.
 
Cole Tarbet
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been reading about intern(). Mostly confusing.

http://weblogs.java.net/blog/enicholas/archive/2006/06/all_about_inter.html

Sounds like intern() is used for specialized comparison of Strings. I must not be using intern() properly because it looks to me like it slows things down.

 
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are perform intern() on String object jvm checks that String object content is available in any literal object. if it present means return that literal object reference otherwise it create one literal object and that reference will return.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in the above example is the object eligible for garbage collection before the main completes? i think it shouldn't be because the literal "abc" will be placed in the pool and hence it will alwyas has a reference, am i right?
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below line 1 creates 2 objects: One in the heap(the non-pool memory) and one in the String Constant pool.

The line 2 makes only the object created in the heap eligible for gc.
 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
objects are always created in heap memory only. String constant pool doesn't contain objects it contain literal object references only so one object is eligible for GC. literal object present in heap and it's reference is in string constant pool memory.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fwiw, intern() is not on the real exam, and on the real exam GC questions never use objects of type String.

hth,

Bert
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic