java学习日记 StringBuffer类

1、与String区别1

String用+连接字符

StringBuffer用append()方法

String不能修改

StringBuffer可修改

public class StringBufferDemo1 {
    public static void main(String[] args) {
        StringBuffer sbu = new StringBuffer();
        sbu.append("Hello").append("World").append("!!!");
        System.out.println("修改之前:"+sbu);
        System.out.println("修改之后:"+change(sbu));

    }
    public static StringBuffer change(StringBuffer temp){
        return temp.append("jxqwc").append("gaile");
    }
}

运行结果:

修改之前:HelloWorld!!!
修改之后:HelloWorld!!!jxqwcgaile

2、两种方法将String类转换成StringBuffer类

public class StringBufferDemo1 {
    public static void main(String[] args) {
        StringBuffer sbu = new StringBuffer("Hello");
        System.out.println(sbu);
       }
}
public class StringBufferDemo1 {
    public static void main(String[] args) {
        StringBuffer sbu = new StringBuffer();
        sbu.append("Hello");
        System.out.println(sbu);
    }
}

3、将StringBuffer类转换成String类

public class StringBufferDemo1 {
    public static void main(String[] args) {
        StringBuffer sbu = new StringBuffer("Hello");
        String str = sbu.toString();
        System.out.println(sbu);
    }
}

java学习日记 StringBuffer类_第1张图片

 

你可能感兴趣的:(java学习日记 StringBuffer类)