public class Inc {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a, b);
System.out.println(a +","+ b);
}
static void operate(StringBuffer x, StringBuffer y) {
y.append(x);
y = x;
}
}
What is the result?
A.The code compiles and prints “A,B”.
B.The code compiles and prints “A, BA”.
C.The code compiles and prints “AB, B”.
D.The code compiles and prints “AB, AB”.
E.The code compiles and prints “BA, BA”.
F.The code does not compile because “+” cannot be overloaded for stringBuffer.
ans:B
if: System.out.println(a + b); then ans:F
StringBuffer与","(""也可)相加时,会自动调用StringBuffer.toString()函数的。
本文探讨了Java中StringBuffer作为方法参数时的行为特点,特别是当方法内部修改StringBuffer对象时对外部调用的影响。通过一个具体示例,解释了为何在方法中改变StringBuffer引用不会影响原始对象,但对其内容的修改则会体现出来。
1396

被折叠的 条评论
为什么被折叠?



