Reputation: 429
If there are two strings string1
and string2
contained in string pool referenced to String literals, will System.out.println(string1 + string2)
generate additional string object in String pool as string3=string1+string2;
? If not, why?
Like to add for more clarity how many objects are allocated in pool in such case?
Upvotes: 0
Views: 123
Reputation: 200168
The expression
"string1" + "string2"
will result in a string constant
"string1string2"
created at compile time and added to the enclosing class's constant pool.
The expression
string1 + string2
where string1
and string2
are arbitrary String
-typed variables will compile into executable code which concatenates two strings. The result is not committed to the pool.
Upvotes: 2