public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a,b);
System.out.println(a + "," + b);
}
public static void operate(StringBuffer x,StringBuffer y){
x.append(y);
y = x;
y.append('x');
}
答案为:
ABx,B
解析:y=x之后y指向a的地址,即xy都指向a
public static void main(String[] args) {
String textString = new String("java");
StringBuffer textBuffer = new StringBuffer("java");
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println(textString + textBuffer);
}
public static void bufferReplace(StringBuffer text){
text.append("C");
text = new StringBuffer("Hello");
text.append("World!");
}
public static void stringReplace(String text){
text = text.replace('j','i');
}
答案为:
javajavaC
public static void main(String[] args) {
String s = "bbbb";
StringBuffer sb = new StringBuffer("bbbb");
change(s,sb);
System.out.println(s + sb);
}
public static void change(String s,StringBuffer sb){
s = "aaaa";
sb.setLength(0);
sb.append("aaaa");
}
答案为:
bbbbaaaa
public static void main(String[] args) {
String str = null;
StringBuffer sb = new StringBuffer();
sb.append(str);
System.out.println(sb.length());
System.out.println(sb);
StringBuffer sb1 = new StringBuffer(str);
System.out.println(sb1);
}
答案为:
4
null
Exception in thread "main" java.lang.NullPointerException
解析:查看源码