代码如下:
public class stringBuilderDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuilder sb1 = new StringBuilder("AAA");
StringBuilder sb2 = new StringBuilder("AAA");
String s1 = "111";
String s2 = "111";
System.out.println("---------------------------------");
System.out.println(sb1 + " " + sb2 + " " + s1 + " " + s2);
System.out.println("---------------------------------");
replace(sb1, sb2, s1, s2);
System.out.println("---------------------------------");
System.out.println(sb1 + " " + sb2 + " " + s1 + " " + s2);
}
public static void replace(StringBuilder sb1, StringBuilder sb2, String s1,
String s2) {
sb1 = new StringBuilder("BBB");
sb2.append("CCC");
s1 = new String("222");
s2.concat("333");
System.out.println(sb1 + " " + sb2 + " " + s1 + " " + s2);
}
}
我的问题是:
s2在replace方法中有这么一个操作s2.concat("333");,因此,在常量池中多了个"111333"的数据,但是这个数据没人接收,s2还是指向常量池中的数据“111”;这个我能理解
但sb2,在replace方法中有追加的操作sb2.append("CCC");追加后在常量池中应该也多了个“AAACCC”的数据,但是为什么sb2就指向了"AAACCC",而不是像s2一样,新生成的“AAACCC”无人接收呢? |
|