java-编程题求解

what is the result when you compile and run the following code?
class Example
{ public void myBuf(StringBuffer s,StringBuffer s1)
{
s.append("how are you");
s=s1;
}
public static void main(String [] args)
{
Example tb=new Example();
StringBuffer s=new StringBuffer("hello');
StringBuffer s1=new StringBuffer("doing');
tb.myBuf(s,s1);
System.out.println(s);
}
}
Select all right answer?

A:hello how are you
B:hello
C:hello how are you doing
D:compile time error

答案选A,myBuf(StringBuffer s,StringBuffer s1)传递的参数是对象引用的拷贝。
s.append("how are you")执行后s指向的对象内容变为hello how are you;
s=s1执行后s的拷贝指向s1,但是s仍指向hello how are you这个对象。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-12-26
D, 原因是
StringBuffer s=new StringBuffer("hello');
StringBuffer s1=new StringBuffer("doing');
字符串必须是双引号本回答被提问者和网友采纳