程序如下:
public class Test {
public static void main(String[] args) {
StringBuffer stra = new StringBuffer("Hello");
StringBuffer strb = stra;
strb.append(" World!");
System.out.println("StringBuffer字符串a是:"+stra);
System.out.println("StringBuffer字符串b是:"+strb);
System.out.println("-------------------");
String sa = new String("Hello");
String sb = sa;
sb += " World!";
System.out.println("String字符串a是:"+sa);
System.out.println("String字符串b是:"+sb);
}
}
得到的结果是:
StringBuffer字符串a是:Hello World!
StringBuffer字符串b是:Hello World!
-------------------
String字符串a是:Hello
String字符串b是:Hello World!
哪位高手能帮我解释一下为什么StringBuffer字符串a与StringBuffer字符串b的结果相同,而String字符串a与String字符串b结果不同,其实也就是String类型与StringBuffer类型的区别
关于string和stringbuffer的一些问题
最新推荐文章于 2024-10-19 14:20:38 发布