1. public class Foo {
     2. public static void main (String [] args) {
     3. StringBuffer a = new StringBuffer (“A”);
     4. StringBuffer b = new StringBuffer (“B”);
     5. operate (a,b);
     6. system.out.printIn{a + “,” +b};
     7. )
     8. static void operate (StringBuffer x, StringBuffer y) {
     9. x.append {y};
     10. y = x;
     11. )
     12. }     What is the result?     A. The code compiles and prints “A,B”.
     B. The code compiles and prints “A,A”.
     C. The code compiles and prints “B,B”.
     D. The code compiles and prints “AB,B”.
     E. The code compiles and prints “AB,AB”.
     F. The code does not compile because “+” cannot be overloaded for StringBuffer.

解决方案 »

  1.   

    我觉得是D,等答案和讲解
    UP
      

  2.   

    answer  = D
    但是为什么Y不是AB啊
      

  3.   

    开始 a,x-->"A"
         b,y-->"B"
    然后 a,x-->"AB"
         y-->"AB",
         b-->"B"
        就是a,x,y-->"AB"
            b-->"B"
    所以打印出的a,b 就是AB,B
      

  4.   

    9. x.append {y};
    执行完9为什么a,x-->"AB"
    10. y = x;
    而执行完10为什么
    y-->"AB",
    b-->"B"
      

  5.   

    9. x.append {y};
    执行完9为什么a,x-->"AB"
    10. y = x;
    而执行完10为什么
    y-->"AB",
    b-->"B"
    ~~~~~~~~~~~~~`
    x.append {y};就是x所指向的“A” appen y所指向的 “B” 就是变成了 “AB",但其地址并没改变
    而a跟x指向是一样的。
    接下来y=x,就是y指向改成了x所指向的”AB“ 就是 y-->"AB",而b的指向不便仍然是”B“