StringBuilder的底层实现原理整理笔记
admin
2024-03-20 03:24:56
0

目录

1、StringBuilder的结构

2、创建StringBuilder对象,看下它的构造函数

3、再看下StringBuilder的拼接方法append,部分源码如下:

4、总结


tringBuilder简介
StringBuilder 最早出现在JDK1.5,是一个字符拼接的工具类,它和StringBuffer一样都继承自父类AbstractStringBuilder,在AbstractStringBuilder中使用char[] value字符数组保存字符串,但是没有用final关键字修饰,所以StringBuilder是可变的。

性能
StringBuilder 对字符串的操作是直接改变字符串对象本身,而不是生成新的对象,所以新能开销小。
与StringBuffer相比StringBuilder的性能略高(15%~30%),StringBuffer为保证多线程情况下的安全性(synchronize加锁)而牺牲了性能,以时间来换取安全。而StringBuilder则没有保证线程的安全,从而性能略高于StringBuffer。

使用场景
频繁使用字符串拼接的时候可以用StringBuilder(推荐)或者StringBuffer。

StringBuilder是我们常用来动态拼接字符串的一个类,通常在面试中被问到这个可能就离不开这个问题“String、StringBuffer和StringBuilder的区别?”。这个问题也是老生常谈的,相信大家都不陌生。一个最明显的区别就是String是不可变的,在动态拼接字符串时会产生很多新的对象,StringBuffer和StringBuilder就是用来解决这个问题的,它们继承了 AbstractStringBuilder ,底层基于可修改的char数组(JDK8),而这两个的区别是StringBuffer加了synchronized关键字,是线程安全的,而StringBuilder是非线程安全的。

知道它们的区别,那么StringBuilder的底层到底是如何实现修改的呢?分析部分源码如下:

1、StringBuilder的结构

public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, CharSequence{....
}~~~~~~~~~~~~~~~~~~~~~~~萌萌哒分割线~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~abstract class AbstractStringBuilder implements Appendable, CharSequence {/*** The value is used for character storage.*/char[] value;/*** The count is the number of characters used.*/int count;
}

可以看到StringBuilder继承自AbstractStringBuilder,底层有一个char数组存放字符,count记录字符个数。

2、创建StringBuilder对象,看下它的构造函数

	/*** Constructs a string builder with no characters in it and an* initial capacity of 16 characters.*/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);}
    /*** Creates an AbstractStringBuilder of the specified capacity.*/AbstractStringBuilder(int capacity) {value = new char[capacity];}

3、再看下StringBuilder的拼接方法append,部分源码如下:

	@Overridepublic StringBuilder append(String str) {super.append(str);return this;}~~~~~~~~~~~~~~~~~萌萌哒分割线~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~父类AbstractStringBuilder的appeng方法实现:/*** Appends the specified string to this character sequence.* 

* 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.*

* Let n be the length of this character sequence just prior to* execution of the {@code append} method. Then the character at* index k in the new character sequence is equal to the character* at index k in the old character sequence, if k is less* than n; otherwise, it is equal to the character at index* k-n in the argument {@code str}.** @param str a string.* @return a reference to this object.*/public AbstractStringBuilder append(String str) {if (str == null)return appendNull();//@1int len = str.length();//@2ensureCapacityInternal(count + len);//@3str.getChars(0, len, value, count);//@4count += len;@5return this;//@6}

逐个分析上面的@1~@6,看看具体每步做了什么。
@1:appentNull()

private AbstractStringBuilder appendNull() {int c = count;ensureCapacityInternal(c + 4);final char[] value = this.value;value[c++] = 'n';value[c++] = 'u';value[c++] = 'l';value[c++] = 'l';count = c;return this;}

@2:int len = str.length();待添加的字符串长度
@3:ensureCapacityInternal(count + len);将当前char数组中字符个数加上待添加的字符串长度,也调用的这个方法和拼接null时一样,只不过拼接null时待添加的字符串长度等于4,看下这个方法做了什么

/*** 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 codeif (minimumCapacity - value.length > 0) {value = Arrays.copyOf(value,newCapacity(minimumCapacity));}}

看代码结合方法注释就很清楚,就是将字符总数和char数组的长度比对,确保char数组长度是否足够,如果不够就需要扩容,copy一个新的数组,将旧数组的数据复制到新数组中,然后将引用赋值给value,同时这里newCapacity里面对于新数组的长度也有一些判断逻辑,源码如下:

    /*** 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 codeint newCapacity = (value.length << 1) + 2;if (newCapacity - minCapacity < 0) {newCapacity = minCapacity;}return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)? hugeCapacity(minCapacity): newCapacity;}private int hugeCapacity(int minCapacity) {if (Integer.MAX_VALUE - minCapacity < 0) { // overflowthrow new OutOfMemoryError();}return (minCapacity > MAX_ARRAY_SIZE)? minCapacity : MAX_ARRAY_SIZE;}

@4:str.getChars(0, len, value, count);将待添加的字符添加到char数组
@5:count += len;将count记录的字符个数加上新加的
@6:return this;返回当前对象

4、总结

通过上面的分析总结下StringBuilder的底层实现原理:
(1)new StringBuilder()默认创建一个容量大小=16的char数组
(2)调用append方法拼接字符串首先会判断char数组容量是否组足够,如果不够需要扩容,按原来数组大小的2倍+2扩容,将原来char数组的元素赋值到新扩容的数组中,并将新数组引用赋值给原来的value,
(3)将待拼接的字符也添加到新数组中,将记录字符个数的count更新
(4)返回当前对象

分析StringBuilder其他更多的方法会发现每次操作返回的都是当前对象,这也是为什么在动态拼接字符串的时候推荐使用它而非String的原因。另一个点就是由于 StringBuilder 底层是基于 char 数组存放字符,而数组是连续内存结构,为了防止频繁地复制和申请内存,在预先知道字符串大概长度的情况下,new StringBuilder()时可以先指定capacity的值,减少数组的扩容次数提高效率。

相关内容

热门资讯

港股全线回调,关注恒生科技ET... 截至收盘,中证港股通消费主题指数下跌1.9%,中证港股通互联网指数下跌2.6%,恒生科技指数下跌2....
瑞幸2025年四季度营收增长利... 界面新闻记者 | 宋佳楠 2月26日,瑞幸咖啡发布2025年第四季度及全年未经审计财务业绩。财报显...
上市券商新年定增“第一单”!西... 来源:21世纪经济报道 21世纪经济报道记者 易妍君 在沪深北交易所优化再融资一揽子措施背景下,20...
河南牧原离职员工家属发文,吐槽... 近日,一篇名为《一位牧原离职员工家属的自述》的文章引起网友关注。 作者称,丈夫原在牧原从事养猪工作,...
【西街观察】理性看待高位金银:... 春节回来,黄金白银又涨了。 尽管价格尚未突破前期高点,但在大类资产中,累计涨幅依旧一骑绝尘,让金银的...
安徽春节观察:从“打卡”到“入... 这个春节,是丙午马年的开场,也是“史上最长假期”的落幕。 当9天的时光画卷缓缓收起,人们回味的,不只...
杭州银行原监事长王立雄再获任副... 2月26日,杭州银行公告称,王立雄杭州银行副行长任职资格已获浙江金融监管局核准。据公告,王立雄此前曾...
郑华国:白癜风患者的情绪调节要... 情绪波动是白癜风病情反复的重要诱因之一,长期焦虑、抑郁、压力过大,会导致内分泌失调、免疫力下降,损伤...
原创 3... 国内能精准预测房地产市场趋势的人并不多,王健林就是其中之一。早在2017年,王健林就宣布万达集团将走...
裁判太偏了?!赵睿10分+犯满... 北京时间2月26日消息,作为中国男篮队长,今晚在冲绳,赵睿尽了全力。身陷犯规困扰的他利用自己的冲击力...
山东城商行“一哥”之争升温:青... 作为山东地区的两家头部城商行,青岛银行和齐鲁银行近年来的发展呈现“你追我赶”的胶着态势。 据最新披露...
特斯拉,又放大招!汽车市场降价... 汽车市场的降价步伐仍未停歇。 2月26日,特斯拉中国官宣新一轮购车金融政策:3月31日前下单,全系车...
超6亿和解金,欣旺达亏了还是赚... 二线品牌备胎难当。 作者|景行 编辑|古廿 “当年在汽车领域,吉利是最早和欣旺达合作的,就是看中欣旺...
机器人产业指数低开低走,机器人... 截至收盘,中证消费电子主题指数上涨1.3%,中证物联网主题指数上涨0.6%,国证机器人产业指数下跌0...
德邦股份,向上交所提出终止上市... 2月26日晚间,德邦股份(603056,股价18.85元,市值190.68亿元)发布公告称,公司拟以...
帝亚吉欧:从未讨论过出售水井坊... 蓝鲸新闻2月26日讯(记者 朱欣悦)水井坊(600779.SH)控股股东帝亚吉欧拟出售其股权的传闻,...
保持银行体系流动性充裕 央行加... 每经记者|张寿林 每经编辑|张益铭 为保持银行体系流动性充裕,2月25日,央行以固定数量、利率招标...
又有外资理财公司总经理,将离任... 【导读】贝莱德建信理财总经理张鹏军将离任 中国基金报记者 吴娟娟 中国基金报记者获悉,贝莱德建信理财...
CBAM豁免政策深度剖析:哪些... 欧盟碳边境调节机制(CBAM)的豁免政策,是缓解企业合规压力的重要保障,尤其惠及中小出口企业。厘清豁...
私有数据,是AI应用唯一的“护... 文|数据猿 “旧的护城河正在瓦解,AI时代的生存法则,才刚刚开始。 这个春节,你用AI点奶茶、买门...