概念:StringBuffer本身也屬於一個字符串的操作類,有兩個意義:一個意義是可以快速的修改,另外一個意義是可以快速讀取。String類對象的內容不可改變,而StringBuffer類對象的內容可以改變。
使用方法:StringBuffer需要使用new明確調用構造方法后,使用append()進行連接。
· append():public StringBuffer append(數據類型 s)
代碼實現:
1 package cn.demo; 2 public class Test { 3 public static void main(String[] args) throws Exception { 4 StringBuffer buf = new StringBuffer(); 5 buf.append("hello").append(" word").append("!!!"); 6 fun(buf); 7 System.out.println(buf); 8 } 9 public static void fun(StringBuffer temp){ 10 temp.append("\n").append("hello liyang."); 11 } 12 }
結果:hello world!!!
hello liyang.
總結:String與StringBuffer的最大區別在於String類對象不可改變,而StringBuffer類對象可以改變。
