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的值,减少数组的扩容次数提高效率。

相关内容

热门资讯

近一年涨364%,近两年468... 来源:今晚吃基 今天前海开源的两则公告引起我的注意。 前海开源沪港深乐享生活、前海开源人工智能主题混...
美伊、霍尔木兹海峡,最新消息!... 特朗普称与伊朗的谈判进展顺利,霍尔木兹海峡通航量上升,油价维持弱势震荡。另外,特朗普要求中东多国与以...
原创 刚... 4月21日下午,当宁德时代超级科技日的大屏幕亮起时,台下不少行业人士都愣了一下。宁德时代宣布,备受瞩...
俄罗斯知名巧克力品牌优化增效 【环球时报综合报道】俄罗斯最大巧克力生产商之一“联合糖果”正优化生产。“联合糖果”公司(旗下品牌包括...
三星半导体员工协商达成年均奖金... 但这份协议对三星而言仍可能是一次胜利,因为其奖金总额低于本土竞争对手SK海力士。 三星与曾威胁发起罢...
Google亲手把搜索框做成了... Google I/O 2026开完了。如果你以为这家公司又在炫酷炫技术,那你猜对了一半——另一半是,...
女子把2万多克黄金存珠宝店,金... 浙江杭州的林女士反映,她是做黄金生意的,从2024年7月开始,分48次陆续将22917.462克黄金...
000638,终止上市!9股获... 今日(5月25日),A股三大指数集体收涨,上证指数报收4152.57点,上涨0.96%;深证成指上涨...
原创 人... 人民币这波行情,最戏剧性的一幕发生在5月13日。当天即期收盘价直接砸到6.7905,正式踏进6.7区...
燕文物流、闪回科技、金龙电机、... 每经记者:李旭馗 每经编辑:袁东 |2026年5月26日 星期二| NO.1燕文物流、闪回科技、金龙...
一代互联网招聘神话,破产了 消费赛道雷声滚滚,招聘赛道也未能幸免。 近日,招聘行业再传重磅消息,曾被无数互联网人视作“跳槽圣地”...
字节反击腾讯称“都是卖猪食的,... 澎湃新闻记者 范佳来 实习生 吴亦菲 抖音副总裁李亮辟谣“反击腾讯”。 近日,有传言称腾讯、字节跳动...
国有大型银行板块5月25日涨0... 证券之星消息,5月25日国有大型银行板块较上一交易日上涨0.02%,中国银行领涨。当日上证指数报收于...
金属包装行业的主流发展趋势 绿色环保、智能化生产、高端化与个性化、行业整合及国际化拓展是当前金属包装行业的主要发展趋势。 绿色...
投资也有流量密码?带你了解自由... 风险提示:基金有风险,投资需谨慎。
美债收益率破5%:全球资产定价... 导读 4月美国通胀数据超预期反弹、美联储新主席沃什近期就任、中东地缘冲突推升油价、美国财政赤字高企与...
烁威光电同步完成两轮Pre-A... 【大河财立方消息】近日,北京烁威光电科技有限公司(以下简称“烁威光电”)同步完成两轮合计金额超亿元融...
库克将迎CEO告别演讲,此后转... 5月25日,知名科技记者马克 · 古尔曼发文称,今年苹果全球开发者大会 (WWDC) 将是库克作为苹...
北京集中约谈17家重点平台企业... 据北京市市场监督管理局5月25日消息,为加强平台经济监管,规范6·18期间平台经营行为,近日,北京市...
原创 日... 你是否听过下面这些管理名言:”永远站在顾客的立场思考问题“、”盯住客户,而不是竞争对手“、”比业绩更...