首页 > 代码库 > StringBuffer和String 的例子

StringBuffer和String 的例子

public class Example {  String str = new String("good");  static StringBuffer sbf=new StringBuffer();  char[] ch = {‘a‘,‘b‘,‘c‘};  public static void main(String[] args) {     Example ex = new Example();     ex.change(ex.str, ex.ch);     System.out.print(ex.str +"and"); //string是不变的,goodand     System.out.print(ex.ch);//gbc     //参考答案输出结果:goodandgbc     change2(sbf);     System.out.println(sbf); //sbf改变了  }   public void change(String str, char ch[]){     str= "test ok";     ch[0]= ‘g‘;  }  public static void change2(StringBuffer sbf)  {      sbf.append("333");  }}

 

StringBuffer和String 的例子