Carinha, seu post também me despertou essa dúvida, fiz uma classe pra ver se possuem a mesma referencia:
package string;
public class PoolStringsTest {
public static void main(String[] args) {
String abc = ("abc");
final String abc2 = ("abc");
String concat1 = abc.concat("def");
String concat2 = abc.concat("def");
String abcdef1 = "abcdef";
String abcdef2 = getAbcdef();
String abcdef3 = "abc" + "def";
String abcdef4 = abc + "def";
String abcdef5 = abc2 + "def";
if(concat1 == concat2){
System.out.println("As string concatenas com concat compartilham a mesma referencia");
}else{
System.out.println("As string concatenas com concat nao compartilham a mesma referencia");
}
if(abcdef1 == abcdef2){
System.out.println("As variaveis com abcdef, sem concatenar, compartilham a mesma referencia");
}else{
System.out.println("As variaveis com abcdef, sem concatenar, compartilham a mesma referencia");
}
if(concat1 == abcdef1 || concat1 == abcdef2){
System.out.println("As variaveis concatenadas compartilham a mesma referencia que as criadas diretamente com abcdef");
}else{
System.out.println("As variaveis concatenadas nao compartilham a mesma referencia que as criadas diretamente com abcdef");
}
if(abcdef3 == abcdef1){
System.out.println("Constantes utilizando o operador de soma, em tempo de compilacao sao avaliadas e declaradas como uma so, ou seja, em tempo de compilacao \"abc\" + \"def\" vira \"abcdef\"");
}
System.out.println("Ou seja:");
if(abcdef4 != abcdef3){
System.out.println("Variaveis concatenadas nao compartilham a mesma referencia");
}
if(abcdef3 == abcdef5){
System.out.println("Constantes concatenadas apenas com o operador de soma compartilham a mesma referencia");
}
}
private static String getAbcdef(){
return "abcdef";
}
}
Saída:
As string concatenas com concat nao compartilham a mesma referencia
As variaveis com abcdef, sem concatenar, compartilham a mesma referencia
As variaveis concatenadas nao compartilham a mesma referencia que as criadas diretamente com abcdef
Constantes utilizando o operador de soma, em tempo de compilacao sao avaliadas e declaradas como uma so, ou seja, em tempo de compilacao “abc” + “def” vira "abcdef"
Ou seja:
Variaveis concatenadas nao compartilham a mesma referencia
Constantes concatenadas apenas com o operador de soma compartilham a mesma referencia.
Código do concat: (cria uma nova string na mão)
/**
* Concatenates the specified string to the end of this string.
* <p>
* If the length of the argument string is <code>0</code>, then this
* <code>String</code> object is returned. Otherwise, a new
* <code>String</code> object is created, representing a character
* sequence that is the concatenation of the character sequence
* represented by this <code>String</code> object and the character
* sequence represented by the argument string.<p>
* Examples:
* <blockquote><pre>
* "cares".concat("s") returns "caress"
* "to".concat("get").concat("her") returns "together"
* </pre></blockquote>
*
* @param str the <code>String</code> that is concatenated to the end
* of this <code>String</code>.
* @return a string that represents the concatenation of this object's
* characters followed by the string argument's characters.
*/
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
char buf[] = new char[count + otherLen];
getChars(0, count, buf, 0);
str.getChars(0, otherLen, buf, count);
return new String(0, count + otherLen, buf);
}