I reached the String Chapter in the K/B book, and I have some questions: -I assume that "String literal pool" and "String constant pool" are the same thing, because the K/B book talks about "String constant pool" and SCJP Tip Line talks about "String constant pool".
- the String pool is part of the stack ?
- taking into account the following example:
String s = "abc"
In the book it is said that "abc" will be stored in the pool, and s reference will refer to it. Does that mean that "abc" will not be stored in the heap, only in the pool ? In the article from the SCJP Tip Line it says that the String pool "is a collection of references to the String objects". Does that mean that there is a special place (the pool) where there are stored the references to the string "abc", which referes to the object "abc" stored in the heap ? So, which one is true, because I see a contradiction between the two statemnts.
- The next question is about strings created using "new":
String ss = new String ("abc");
In the book it says "In this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and ss will refer to it. In addition, the literal "abc" will be placed in the pool". What that mean ? Are there are 3 steps in this process ? 1. create the object in the heap 2. create the s reference in the stack that refers to the object 3. create in the pool the string "abc" which also refers to the object from the heap ? If this is the case, I don't see the efficiet use of memory (the reason that String pool was invented), because there is stored twice the string "abc" (one in heap and one in the pool).
Please someone tell me what is actually stored in the pool: the object itself , or just the reference to the object from the heap ? And the reference to the object (s and ss) are created in the stack ?
Java creates a Sting Pool where it stors all the Strings created. A string literal always refers to the same instance of class String. Check the intern().
However when a new String("abc") is called. it creats a new object in the heap and is not recommended unless explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.
"The pool" is not a special place separate from the stack or heap. It's just a collection of String objects somewhere behind the scenes. Those String objects are on the heap, just like any other Java object. About the difference between these two lines:
1. String s = "abc"; 2. String s = new String("abc");
In case 1, there is one String object with the value "abc". That String object is on the heap and it is in the pool. The variable s points to that String object.
In case 2, there is the same String object with the value "abc" which is on the heap and in the pool, but you also create a new String object explicitly which will contain a copy of the pooled String object. The explicitly created String object is not in the pool. The variable s points to the explicitly created String. So there will be two String objects. You should never use new String("..."), with a String literal as the argument, because that is inefficient and unnecessary.
The string pool is an optimalisation. If you use the same string literal multiple times in the same program, there will be just 1 String object created which will be shared at all the places you use it. This optimalisation is possible because class String is immutable, which means that the value of a String object never changes after it is created. [ July 03, 2007: Message edited by: Jesper Young ]