Java 常用类 -String VS StringBuffer

时间:2022-12-09 22:50:13

String:对String类型的对象操作,等同于重新生成一个新对象,然后讲引用指向它;

StringBuffer:对StringBuffer类型的对象操作,操作的始终是同一个对象;

package com.java1234.chap05.sec02;
 
public class TestString {
 
    public static void main(String[] args) {
        String str="123";
        str+="abc";
        System.out.println(str);
    }
}
运行输出:

123abc

 Java 常用类 -String VS StringBuffer

 

 

package com.java1234.chap05.sec02;
 
public class TestStringBuffer {
 
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer("123");
        sb.append("abc");
        System.out.println(sb.toString());
    }
}
运行输出:

123abc

 Java 常用类 -String VS StringBuffer