编程语言
首页 > 编程语言> > Java的StringBuffer类详解

Java的StringBuffer类详解

作者:互联网

StringBuffer类详解---Java

1. String、StringBuffer和StringBuilder区别

String:不可变的字符序列;底层以char[]存储
StringBuffer:可变的字符序列;线程安全的,效率低;底层以char[]存储
StringBuilder:可变的字符序列;线程不安全的,效率高;底层以char[]存储

[注]:StringBuffer和StringBuilder本质是一样的,只是StringBuffer是线程安全的,而StringBuilder是线程不安全的;两者基本操作都一样

2. 源码分析

		// String和StringBuffer的操作对应底层源码操作
        String str = new String(); // char[] value = new char[0];
        String str1 = new String("abc"); // char[] value = new char[]{'a','b','c'};

        StringBuffer sb1 = new StringBuffer(); // char[] value = new char[16];
        StringBuffer sb2 = new StringBuffer("abc"); // char[] value = new char["abc".length()+16];

        System.out.println(sb1.length()); // 0
        sb1.append("a"); // value[0] = 'a';
        sb2.append('b'); // value[sb2.length()] = 'b';

3. StringBuffer方法

3.1 StringBuffer append ( xxx):提供了很多的 append() 方法 用于进行字符串拼接

        StringBuffer sb1 = new StringBuffer("abc");
        sb1.append('a');
        sb1.append(3);
        sb1.append("def");
        System.out.println(sb1);

可以添加任何方式的数据,详情见构造器:
在这里插入图片描述

3.2 StringBuffer delete (int start,int end):删除指定位置的内容

        StringBuffer sb1 = new StringBuffer("abca3def");
        sb1.delete(2, 4);
        System.out.println(sb1);

输出:
ab3def

3.3 StringBuffer replace (int start, int end, String str):把 [start,end] 位置替换为 str

        StringBuffer sb1 = new StringBuffer("ab3def");
        sb1.replace(2, 4, "123");
        System.out.println(sb1);

ab123ef

3.4 StringBuffer insert (int offset, xxx):在指定位置插入 xxx

        StringBuffer sb1 = new StringBuffer("ab123ef");
        sb1.insert(0, 2.3);
        System.out.println(sb1);

2.3ab123ef

3.5 StringBuffer reverse ():把当前字符序列逆转

        StringBuffer sb1 = new StringBuffer("2.3ab123ef");
        sb1.reverse();
        System.out.println(sb1);

fe321ba3.2

3.6 StringBuffer也包含String的方法

使用详情见Java的String类方法详解

4. String、StringBuffer和StringBuilder的效率对比

public class StringBufferTest {
    /**
     * String StringBuffer StringBuilder效率对比
     *
     */
    @Test
    public void test2() {
        long startTime = 0L;
        long endTime = 0L;
        String str = "";
        StringBuffer buffer = new StringBuffer("");
        StringBuilder builder = new StringBuilder("");

        startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            str += i;
        }
        endTime = System.currentTimeMillis();
        System.out.println("String执行时间:" + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            buffer.append(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer执行时间:" + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            builder.append(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuilder执行时间:" + (endTime - startTime));
    }
}

String执行时间:3603
StringBuffer执行时间:6
StringBuilder执行时间:3

标签:sb1,Java,String,int,StringBuffer,System,详解,new
来源: https://blog.csdn.net/E_chos/article/details/113403680