目录
一、StringBuilder类
为了能高效拼接拼接字符串,Java标准库提供了StringBuilder,它是一个可变对象,可以预分配缓冲区,这样,往StringBuilder添加字符时,不会创建新的临时对象.
扩容机制
原理:
StringBuffer、StringBuilder和String类似,底层也是用一个数组来存储字符串的值,并且数组的默认长度为16,即一个空的StringBuffer对象数组长度为16。实例化一个StringBuffer对象即创建了一个大小为16个字符的字符串缓冲区。但是当我们调用有参构造函数创建一个StringBuffer对象时,数组长度就不再是16了,而是根据当前对象的值来决定数组的长度,数组的长度为“当前对象的值的长+16”。所以一个 StringBuffer 创建完成之后,有16个字符的空间可以对其值进行修改。如果修改的值范围超出了16个字符,会先检查StringBuffer对象的原char数组的容量能不能装下新的字符串,如果装不下则会对 char 数组进行扩容。
源码分析
StringBufferStringBufferStringBuffer和StringBuilder都继承自AbstractStringBuilder.jdk1.8中源码位于
rt.jar/java/lang/
路径下.
无参构造
无参数构造时,默认容量大小为16.
StringBuilder str = new StringBuilder() // 默认初始大小16
/** * Constructs a string builder with no characters in it and an * initial capacity of 16 characters. */ public StringBuilder() { super(16); }
其调用了父类(即AbstractStringBuilder类)的构造器,当调用这个构造器时,会生成容量为capacity的char数组,如果StringBuilder没有传入capacity,则会调用super(16),即默认构造一个容量为16的字符数组
/** * Creates an AbstractStringBuilder of the specified capacity. */ AbstractStringBuilder(int capacity) { value = new char[capacity]; }
有参构造
StringBuilder str = new StringBuilder("abc");
如果是使用带参数的构造器,则底层构建的数组的长度为:16+参数字符串的长度(16+"abc".length() = 19)
/** * 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); }
/** * Creates an AbstractStringBuilder of the specified capacity. */ AbstractStringBuilder(int capacity) { value = new char[capacity]; }
扩容机制
不断的使用append(str)完字符串中添加字符的时候, 当然底层的数组长度不够用的时候就会触发扩容机制,从源码可以看出扩容为:2倍+2
1.调用append()方法
@Override public StringBuilder append(String str) { super.append(str); return this; }
2.判断追加字符串之后会不会操作底层数组的长度
public AbstractStringBuilder append(String str) { if (str == null) return appendNull(); int len = str.length(); ensureCapacityInternal(count + len);//count为本身长度 len为追加长度 str.getChars(0, len, value, count); count += len; return this; }
3.调用ensureCapacityInternal()方法 进行判断是否超出底层数组长度
private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) { value = Arrays.copyOf(value, newCapacity(minimumCapacity)); } }
此方法可以看出当追加后超出容量会触发扩容,通过
newCapacity
获得新容量。4.newCapacity()方法
超过了底层数组的长度,那么就创建新的数组长度为原来长度右移加2,也就是2倍+2,并将数组进行重新复制
private int newCapacity(int minCapacity) { // overflow-conscious code int newCapacity = (value.length << 1) + 2;//右移+2 即2n+2 if (newCapacity - minCapacity < 0) { newCapacity = minCapacity; } return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0) ? hugeCapacity(minCapacity) : newCapacity; }
总结
扩容的逻辑就是创建一个新的 char 数组,将现有容量扩大一倍再加上2,如果还是不够大则直接等于需要的容量大小。扩容完成之后,将原数组的内容复制到新数组,最后将指针指向新的 char 数组。
二、StringBuffer类
其余扩容原理与StringBuilder类相同,见上面
区别
1.StringBuffer的继承关系和StringBuilder完全一致,因此StringBuffer内部也是调用的AbstractStringBuilder的方法,所以很多方法的理解都和StringBuilder相差无几
2.但和StringBuilder不同的是,StringBuffer的每个方法上都用了synchronized修饰,当涉及到多线程调用时StringBuffer是线程安全的,与之相对的StringBuilder便线程不安全了,但因此StringBuilder的效率要比StringBuffer高
3.所以,当我们在多线程环境下应当使用StringBuffer以保证线程安全,而在单线程环境下使用StringBuilder以提高效率
#java