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.
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.
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?
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.Paul Clapham wrote: . . . String literals go into the string pool. . . .
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 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.
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?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.
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.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. . . .
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).
Consider Paul's rocket mass heater. |