StringBuffer可变
String不可变

解决方案 »

  1.   

    String对象不能变,Stringbuffer可直接改变。当正在改变字符串时用StringBuffer,如果只是偶尔需要连接静态字符串,用string。
      

  2.   

    public class StringTest{
    String s="asdf";
    String s1=s;   //s1 and s have the same object handle
    s1=s1+"world";
    System.out.println(s);
    }
    the result is: asdf;
    public class StringBuffer{
    StringBuffer sb=new StringBuffer("nihao");
    StringBuffer sb1=sb;
    sb.append("world");
    System.out.println(sb1);
    }
    the result is:nihaoworld