hello everybody
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 ?
Thank you