0
  1. Does the String pool reside on the heap? If yes, are String literals eligible for garbage collection?

When using new String("abc"), we know that it creates an object on the heap and places the String literal in the String pool. So my 2nd question is:

  1. Does new StringBuilder("abc") behave the same way as new String("abc") ? If yes, how does StringBuilder manipulate the String literal inside the String pool?
Hoopje
  • 12,677
  • 8
  • 34
  • 50
Ryan
  • 156
  • 2
  • 18
  • "*how does StringBuilder manipulates the String literal inside the String pool*" StringBuilder doesn't manipulate String literal from pool, but it copies all character from literal to its own buffer which can then be manipulated. – Pshemo Jan 25 '15 at 15:16
  • When you do `new String("abc")` the string literal was already in the interned string pool. – Hot Licks Jan 25 '15 at 15:19

2 Answers2

3

You are confusing compile time, load time, and runtime.

A string literal is added to the constant pool at class loading time. Just a mention of a literal anywhere in the class code is enough; you don't even have to execute any line of code in that class.

On the other hand, the expression new String("literal") yields a new String instance each time it is evaluated. That instance is distinct from the one in the constant pool and has a copy of the string value.

StringBuilder acts exactly the same way as String in this respect: it is initialized with a copy of the string literal's value.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • The last paragraph could be misunderstood. What actually happens is that the characters of the constructor's argument are copied into a char[] used for storing the characters of the "build". – laune Jan 25 '15 at 17:17
  • @laune What I am interested to hear is the misunderstood version. What you wrote sounds to me like just a more implementation-oriented retelling of my sentence. – Marko Topolnik Jan 25 '15 at 18:23
  • Maybe I'm over-sensitive here, but I thought that someone might think that StringBuilder actually stores String objects. "initialized from the string's literal value", maybe. But, as I wrote, maybe... – laune Jan 25 '15 at 18:30
  • @laune I see what you mean. What I consider as "value" there may be confused. – Marko Topolnik Jan 25 '15 at 18:48
2

First, yes, the string pool and the strings it contains are on the heap. Once a string literal is in the string pool it will never be removed from it. Thus, all string literals in the string pool are reachable until the program is terminated and thus not eligible for garbage collection. (Strings added to the string pool by other means might be eligible for garbage collection.)

If we create a new String object by new String("abc"), then two things happen: first, because of the String literal "abc", a new String object with contents "abc" is created an added to the string pool (if it is not already there). Then, because of the new String(...) constructor, a new String object is created which is a copy of the string literal. This new string is not placed in the string pool. Thus, new String("abc") == "abc" does not hold.

The code new StringBuilder("abc") does not do the same thing as new String("abc"), because it creates a StringBuilder object rather than a String. However, because of the String literal "abc", it does make sure that a String object with contents "abc" is in the string pool.

Hoopje
  • 12,677
  • 8
  • 34
  • 50
  • `However, once a string is in the string pool it will never be removed from it.`---not true. Even string literals can become unreachable if a class is unloaded. Strings explicitly `intern`ed at runtime become garbage the same way as "regular" strings (the reference to them inside the pool is a weak one). – Marko Topolnik Jan 25 '15 at 15:26