Java数据类型—StringBuilder与StringBuffer「建议收藏」

Java数据类型—StringBuilder与StringBuffer「建议收藏」StringBuilder与StringBuffer作用就是用来处理字符串,但String类本身也具备很多方法可以用来处理字符串,那么为什么还要引入这两个类呢?前面我们讲解到String对象的不可变性,以及它的不足那就是创建新的对象,因为它是不可变的,所以你对它的操作逻辑就体现在另外一个对象里,那就是你的操作新创建的对象。…

大家好,又见面了,我是你们的朋友全栈君。

StringBuilder与StringBuffer

StringBuilder与StringBuffer作用就是用来处理字符串,但String类本身也具备很多方法可以用来处理字符串,那么为什么还要引入这两个类呢?前面我们讲解到String 对象的不可变性,以及它的不足那就是创建新的对象,具体你可以查看文章String进阶之不可变性,因为它是不可变的,所以你对它的操作逻辑就体现在另外一个对象里,那就是你的操作新创建的对象。

这种操作最常见的就是字符串的拼接,所以我们几天学习的这两个类都是为了解决这个问题的,那既然都是为了解决这个问题的,为什么会有两个类的,我们后面慢慢分析

初识

首先看下面的例子

    @Test
    public void testPerformance(){ 
   
        String str0 = "hello,world";

        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) { 
   
            str0 += i;
        }
        System.out.println(System.currentTimeMillis() - start);

        StringBuilder sb = new StringBuilder("hello,world");
        long start1 = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) { 
   
            sb.append(i);
        }
        System.out.println(System.currentTimeMillis() - start1);

        StringBuffer sbf = new StringBuffer("hello,world");
        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) { 
   
            sbf.append(i);
        }
        System.out.println(System.currentTimeMillis() - start2);
    }

上述代码中3处循环完成了同样的功能,字符串拼接,执行的结果如下:

38833
4
4

可以看出执行时间差别很大,为了解决String不擅长的大量字符串拼接这种业务场景,所以我们引入了StringBuffer和StringBuilder.

首先我们分析一下为什么String在大量字符串拼接这种场景下这么慢?其实前面我们已经说到了,这我我们再解释一下

因为String本身不可变,我们对String的任何操作都会返回一个新的对象,然后当前String变量指向新的对象,而原来的String对象就会被GC回收,那么在循环中就会大量快速的创建新的对象,大量原来的对象会不断的被GC回收,消耗的时间是非常恐怖的,而且内存占用非常大。

但是我们从输出结果看到另外一个问题,那就是StringBuffer与StringBuilder的允许时间基本一致,那为什么需要定义两个功能相似的类呢?

接下来我们再看一段代码

    @Test
    public void testSafe() throws InterruptedException { 
   
        String str0 = "hello,world";

        StringBuilder sb = new StringBuilder(str0);
        for (int i = 0; i < 100; i++) { 
   
            new Thread(() -> { 
   
                try { 
   
                    TimeUnit.MILLISECONDS.sleep(1);
                } catch (InterruptedException e) { 
   
                    e.printStackTrace();
                }
                sb.append("a");
            }).start();
        }


        StringBuffer sbf = new StringBuffer(str0);
        for (int i = 0; i < 100; i++) { 
   
            new Thread(() -> { 
   
                try { 
   
                    TimeUnit.MILLISECONDS.sleep(1);
                } catch (InterruptedException e) { 
   
                    e.printStackTrace();
                }
                sbf.append("a");
            }).start();
        }
        // 等待工作线程运行结束
        while (Thread.activeCount()>2){ 
   

        }
        System.out.println("StringBuilder:"+sb.toString().length());
        System.out.println("StringBuffer:"+sbf.toString().length());
    }

输出结果

StringBuilder:109
StringBuffer:111

看出什么了吗,本来拼接完程度应该是111的,但是现在呢,StringBuilder是109,说明了StringBuilder不是安全的,也就是说再多线程的环境下,我们应该使用StringBuffer

下面我们对比了String、StringBuffer与StringBuilder的区别

String StringBuffer StringBuilder
final修饰,不可继承 final修饰,不可继承 final修饰,不可继承
字符串常量,创建后不可变 字符串变量,可动态修改 字符串变量,可动态修改
不存在线程安全问题 线程安全,所有public方法由synchronized修改 线程不安全
大量字符串拼接效率最低 大量字符串拼接效率非常高 大量字符串拼接效率最高

StringBuffer与StringBuilder实现非常类似,下面以StringBuilder简单说明一下append()方法基本原理

源码剖析

这里我们以StringBuilder为例,

1. StringBuilder的构造方法

StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder(100);

StringBuilder对字符串的操作是通过char[]来实现的,通过默认构造器创建的StringBuilder,其内部创建的char[]的默认长度为16,当然可以调用重载的构造器传递初始长度(推荐这样,因为这样可以减少数组扩容次数,提高效率)。

     */
    public StringBuilder() { 
   
        super(16);
    }

    /** * Constructs a string builder with no characters in it and an * initial capacity specified by the {@code capacity} argument. * * @param capacity the initial capacity. * @throws NegativeArraySizeException if the {@code capacity} * argument is less than {@code 0}. */
    public StringBuilder(int capacity) { 
   
        super(capacity);
    }

    /** * Constructs a string builder initialized to the contents of the * specified string. The initial capacity of the string builder is * {@code 16} plus the length of the string argument. * * @param str the initial contents of the buffer. */
    public StringBuilder(String str) { 
   
        super(str.length() + 16);
        append(str);
    }

    /** * Constructs a string builder that contains the same characters * as the specified {@code CharSequence}. The initial capacity of * the string builder is {@code 16} plus the length of the * {@code CharSequence} argument. * * @param seq the sequence to copy. */
    public StringBuilder(CharSequence seq) { 
   
        this(seq.length() + 16);
        append(seq);
    }

2. StringBuilder的append()方法

每次调用append(str)方法时,会首先判断数组长度是否足以添加传递来的字符串

/** * Appends the specified string to this character sequence. * <p> * The characters of the {@code String} argument are appended, in * order, increasing the length of this sequence by the length of the * argument. If {@code str} is {@code null}, then the four * characters {@code "null"} are appended. * * @param str a string. * @return a reference to this object. */
public AbstractStringBuilder append(String str) { 
   
    if (str == null)
        return appendNull();
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
}
/** * For positive values of {@code minimumCapacity}, this method * behaves like {@code ensureCapacity}, however it is never * synchronized. * If {@code minimumCapacity} is non positive due to numeric * overflow, this method throws {@code OutOfMemoryError}. */
private void ensureCapacityInternal(int minimumCapacity) { 
   
    // overflow-conscious code
    if (minimumCapacity - value.length > 0) { 
   
        value = Arrays.copyOf(value,
                newCapacity(minimumCapacity));
    }
}

如果传递的字符串长度 + 数组已存放的字符的长度 > 数组的长度,这时就需要进行数据扩容了

/** * Returns a capacity at least as large as the given minimum capacity. * Returns the current capacity increased by the same amount + 2 if * that suffices. * Will not return a capacity greater than {@code MAX_ARRAY_SIZE} * unless the given minimum capacity is greater than that. * * @param minCapacity the desired minimum capacity * @throws OutOfMemoryError if minCapacity is less than zero or * greater than Integer.MAX_VALUE */
private int newCapacity(int minCapacity) { 
   
    // overflow-conscious code
    int newCapacity = (value.length << 1) + 2;
    if (newCapacity - minCapacity < 0) { 
   
        newCapacity = minCapacity;
    }
    return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
        ? hugeCapacity(minCapacity)
        : newCapacity;
}

扩容规则如下:默认将数组长度设置为“ (当前数组长度 * 2) + 2”,但如果按此规则扩容后的数组也不足以添加新的字符串,就需要将数组长度设置为“数组内字符长度 + 传递的字符串长度”。

因此假如我们知道拼接的字符串大概长度有100多字符,我们就可以设置初始长度150或200,这样就可以避免或减少数组扩容的次数,从而提高效率。

总结

  1. StringBuilder与StringBuffer 都是为了解决大量字符串拼接时的性能问题,其实就是为了解决String类在拼接过程中产生的大量对象的问题,因为这会导致大量内内存分配和GC 问题
  2. StringBuilder与StringBuffer 二者之间的主要区别是线程是否安全,StringBuffer 通过对方法添加synchronized关键字保证了线程安全。
  3. StringBuilder与StringBuffer底层都是依赖字符数组实现的,不同于String 的是String 底层的字符数组是不可变的,也就是final 修饰的,其实这就是StringBuilder与StringBuffer可变的原因,动态扩展字符数组来实现的。
  4. 为了提高StringBuilder与StringBuffer的性能我们可以通过设置合适的容量来避免数组库容。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153409.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java数组截取[通俗易懂]

    java数组截取[通俗易懂]JAVA数组截取publicclassDay12_2{publicstaticvoidmain(String[]args){int[]A=newint[]{1,3,5,7,9};intstart=1;intend=3;arraySub(A,start,end);}public…

    2022年6月11日
    30
  • 浅谈 HTTP 和 HTTPS[通俗易懂]

    浅谈 HTTP 和 HTTPS[通俗易懂]HTTP作为我们浏览器与服务器之间通讯协议,目前这块知识成为了面试的高频率题,也是我们前后端同学必须掌握的部分,一起来看看吧!

    2022年10月16日
    6
  • ScriptManager.RegisterStartupScript()方法

    ScriptManager.RegisterStartupScript()方法如果页面中不用Ajaxcs中运行某段js代码方式可以是:Page.ClientScript.RegisterStartupScript(Page.GetType(),””,”window.open(‘default2.aspx’)”);如果页面中使用了Ajax则上述代码即使执行也无效果。对这种情况我们通常采用:ScriptManager.RegisterStartupScr

    2022年7月20日
    16
  • opencv人脸识别训练「建议收藏」

    opencv人脸识别训练「建议收藏」一开始我运用人脸库训练出一个xml文件,后来新增人脸时候需要将新人继续拿来训练,需要将训练的特征信息加在原来xml文件里面。如果程序不关闭是可以将新人特征信息加到xml文件里的。比如,原来的xml文件为face.xml;现在采集了一些人脸,需要将这些人脸的特征信息提取加入到face.cml;然后下次load这个face.xml进行人脸识别。现在就是怎么将这些人脸加入原来的xml文件呢?这个是全

    2022年6月7日
    40
  • 我的世界显示服务器领地指令,我的世界领地指令介绍 我的世界领地指令怎么设置…

    我的世界显示服务器领地指令,我的世界领地指令介绍 我的世界领地指令怎么设置…在我的世界这款经典有趣的建造类游戏中,为了让自己的领地不然其他玩家占用,我们可以设置一下领地。那我的世界领地怎么设置呢?下面是小编给大家分享的我的世界领地指令大全,大家赶紧来了解一下吧!一、我的世界设置领地:先用一块木头斧子左键敲击一方块设置点A,右键敲击一方块设置点B(可以输入“/resselectsize”查看所选区域的大小);之后输入“/rescreate123”(例)这样设置后,就…

    2022年9月23日
    2
  • 真封神之天尊地魔引擎修改系列教程

    真封神之天尊地魔引擎修改系列教程真封神引擎技术研究QQ交流群:157754180真封神之天尊地魔SF引擎研修目录:本套教程分为三个部分:客户端部分、服务端部分  、以及引擎部分!引擎部分:1.真封神GC去MD5教程      2.引擎修改之突破仓库金钱上限20亿                           3.引擎修改之多次转职            

    2022年7月24日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号