• 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

String pool clarifications

 
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! I have some questions about the String pool in Java. Correct me if I am wrong but to my understanding, string pool is a special space in the heap that belongs to the String class and at the start of the program is empty. Whenever we create a string object without the new keyword, the JVM checks inside the string pool to find if such a string already exists and if not, it adds the string literal inside the pool and returns a reference to that string, otherwise it just returns the reference. If we create a string with the new keyword, a new string object is created outside the string pool as well as inside the string pool if it doesnt already exist and the returned reference is that of the object outside the string pool. The reason of the existance of the string pool is because it is faster to return a reference to a string literal already in the string pool that create a new object each time. Do I have my facts straight guys? Also, we say that the concept of the string pool is possible because strings are immutable in Java, but why wouldn't it be possible if strings were not immutable?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Petros, welcome to the Ranch!

Petros Papatheodoru wrote:to my understanding, string pool is a special space in the heap that belongs to the String class and at the start of the program is empty.



Not really, no. Yes, the string pool is a special place. But the compiler identifies all String literals in the class and, when that class is loaded, all of those String objects are put into the special space by the JVM.

Whenever we create a string object without the new keyword, the JVM checks inside the string pool to find if such a string already exists and if not, it adds the string literal inside the pool and returns a reference to that string, otherwise it just returns the reference.



No. Only String literals go into the string pool. If you create a new String object for example like this: String answer = "Yes, but " + idea then the resulting String object doesn't go into the String pool.

If we create a string with the new keyword, a new string object is created outside the string pool as well as inside the string pool if it doesnt already exist and the returned reference is that of the object outside the string pool.



No. If you create a new String object like this: String badIdea = new String("Don't do this") then yes, a new String object is created. But no, doing that doesn't affect the string pool at all. Even if a String object with the same contents is already in the string pool, it is ignored and a new String object is created.

Incidentally there is basically no reason to use that String constructor. (I suppose there might be some obscure situation where it might be helpful, but I don't know about that.) So avoid using it.

The reason of the existance of the string pool is because it is faster to return a reference to a string literal already in the string pool that create a new object each time.



That's true. It's possible that you might write a class which uses the String literal "Error" in many places, and then yes, it's faster and space-saving to just have one copy of that literal in memory.

Also, we say that the concept of the string pool is possible because strings are immutable in Java, but why wouldn't it be possible if strings were not immutable?



Suppose you used "Error" several times in your code, so they all correspond to a single String in the string pool. Then suppose your class runs this code:



And suppose it later runs this code:



Now because you changed the value in the string pool from "Error" to "Whatever", the value of the response variable is going to be "Whatever 42". You can see that is going to be surprising to many programmers and result in hundreds of questions here on the Ranch. Language designers don't like languages which work like that, either, so they work to avoid that kind of situation. That's why immutability is a good feature for a class to have, if it makes sense.



 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: . . . String literals go into the string pool. . . .

I thought all Strings that are compile‑time constants go into the String pool, and you can add an existing String to the pool with the intern() method. Yes, that is what the intern() link says.

PP: remember you rarely need to know this information. The fact that a String pool is mentioned in the documentation like that commits Oracle to maintaining the String pool for ever.
 
Petros Papatheodoru
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Paul and Cambell! Thanks for your answers. I know that this information isn't super practically useful but I like knowing what is happening under the surface and as long as string pool is concerned a lot of information exists out there but many times it is contradicting. Paul suggests that:

If you create a new String object like this: String badIdea = new String("Don't do this") then yes, a new String object is created. But no, doing that doesn't affect the string pool at all.

but i recently read an article that was suggesting that String str = new String("Cat"); creates either one string object, if "Cat" is already in the string pool, or 2 string objects if "Cat" is not in the string pool. So what is actually the correct answer? haha. I am inclined to believe that Paul is right although I can't think of a way to test this.

If you create a new String object for example like this: String answer = "Yes, but " + idea then the resulting String object doesn't go into the String pool.

Paul, are you trying to say that string objects that are created with string concatenation do not go into the string pool? If yes then it makes sense, since a StringBuilder is used, but if we just had:  String answer = "Yes, but "; then it would go into the string pool, wouldn't it?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Petros Papatheodoru wrote:. . . new String("Cat"); creates either one string object, if "Cat" is already in the string pool, or 2 string objects if "Cat" is not in the string pool. . . .

One new object. The literal is loaded into the String pool when the class is loaded, so it is always “already in the String pool.” The intern() method documentation says to see this Java® Language Specification (=JLS) section.

That JLS part wrote:A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern (§12.5).

 
Petros Papatheodoru
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok, so there is no way to add a string literal in the string pool at runtime because the string pool is already full with all the string literals that your program is going to use, even before it has started executing. So in an expressing like this String s = "Cat" , it doesn't make sense to say that the JVM searches the string pool to find IF the literal "Cat" exists, because it is 100% certain that it is already in there and the JVM just has to return a reference to this object. And in the expression String s = new String("Cat"), "Cat" is already in the string pool but the JVM creates a new object outside the string pool and returns a reference to that object. Did I get it now?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic