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()函数的。