JavaSE基础(82) String和StringBuffer堆栈图分析

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-->可变字符串)

JavaSE基础(82) String和StringBuffer堆栈图分析_第1张图片

你可能感兴趣的:(-----❶,JavaSE基础)