Java之String和StringBuffer堆疊圖分析

鄭清發表於2018-08-25
public class Demo {
	
	public static void main(String[] args) {
		String s1="AAA";
		String s2="KKK";
		StringBuffer sb1=new StringBuffer("QQQ");
		StringBuffer sb2=new StringBuffer("JJJ");
		
		change(s1,s2,sb1,sb2);
		System.out.println("改變後s1="+s1);	//AAA
		System.out.println("改變後s2="+s2);	//KKK
		System.out.println("改變後sb1="+sb1);//QQQ999
		System.out.println("改變後sb2="+sb2);//JJJ777
	}
	private static void change(String s1, String s2, StringBuffer sb1,StringBuffer sb2) {
		s1="XXX";
		s2=s2.concat("666");
		sb1.append("999");
		sb2=sb2.append("777");
		System.out.println("改變中s1="+s1);		//XXX
		System.out.println("改變中s2="+s2);		//KKK666
		System.out.println("改變中sb1="+sb1);	//QQQ999
		System.out.println("改變中sb2="+sb2);	//JJJ777
	}

}

堆疊圖分析:(注意:String-->不可變字串      StringBuffer-->可變字串)

相關文章