String str="abc"
str.concat("efg"); //这里没有改变str的引用,只是连接产生了一个字符串。但并未复制给任何引用。
str=str.concat("efg"); //这句话和str=str+"efg"的作用是一样的,这是“abc”就没有引用指向它,成为了垃圾,所以不要在字符串的连接外面加循环,那样会产生很多垃圾。(尽管Java有垃圾回收机制)
//上面三句话产生了三个字符串对象:“abc”,"efg","abcefg",以及两个引用。
String str1="abc";
String str2="abc";
System.out.println(str1==str2); //true
String str3=new String("abc");
String str4=new String("abc");
System.out.println(str3==str4); //false
String str5="abc";
String str6="efg";
String str7=str5+str6;
System.out.println(str7=="abcefg"); //false
String str8="abc"+"efg";
String str9="abcefg";
System.out.println(str8==str9); //true
String str10="abc";
String str11=str10+"efg";
String str12="abcefg";
System.out.println(str11==str12); //false,变量就不相等了。