We need to understand the difference between the concat() function and String concatenation operator +.
1. concat() function:
concat() function uses new String(int offset, int count, char value[]) constructor to create the new concatenated String. (source : java.lang.String source code)
so new String will not be pulled from the String literal pool.
See this:
2. String concatenation operator +
there are two cases here:
2.1. Both the operands are String literals:
If both the operands are String literals then the resulting String object is resolved at compile-time.
From Java Language Specification:
Therefore:
2.2. one or both the operands are reference variables:
If one or both of the operands are reference variables (other than String <<edit: one reference variable must be of String type>>) then at run-time
String Conversion takes place.
If one or both of the operands are references of String type, then String conversion is not required for that variable.
Thereafter: "String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method". (Source
Javadocs for String class )
So in this case a new String object is created instead of pulled out from the pool.
therefore: