posted 16 years ago
From my understanding, a new object is created for every string literal, and added to the string pool. These are the compile-time constants. Every time you type a string in quotes, that is considered a string literal, and if it doesn't match a string already in the string pool when you compile, it is added. So, first of all, based on this understanding,
creates FOUR objects (not just one as you had claimed). The three string literals, "hello friend", "hello", and "friend" are added to the pool during compilation. The fourth object, created and referenced by a3, is the string "hellofriend" which is not in the string pool, because it's created at runtime. Let's say you meant to say
(which is what I'm guessing), then there would still be FOUR objects created, because the assignment
is at runtime, so the compiler will not check the string pool for any matches.
Correct me if I'm wrong, but this is my understanding, from the K&B book.