一、String,StringBuilder,StringBuffer的大概了解
大家知道String,StringBuilder,StringBuffer三个的基本应用场景。
- String会一直创建新对象,因此频繁创建对象的场景不适合用。
- StringBuilder则可以避免这个情况,因此频繁对字符串操作的场景使用StringBuilder比较合适。
- 但是StringBuilder并不是线程安全的,如果要线程安全,需要使用StringBuffer。
二、为什么String会一直创建新对象?
private final char value[];
- String代码中,字符是存在一个 final 的 char value[]里面的。
- 由于final指定了存储的内从是固定的,因此如果有append之类的操作的话要重新开辟新的内存。
- 要注意的是即使 value[]是final的,它也是可以改变里面的内容的。
下面这写法是正确的。不会报错。
final char[] ss = {'a', 'b', 'c'};
ss[0] = 'b';
ss[1] = 'c';
但是String类并没有提供这么一个接口能直接修改value数组。其中的replace是返回了一个新的String 对象。并不是在原先的数组上进行修改。
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */ while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
三、StringBuilder中的主要内容
这个类继承了AbstractStringBuilder。AbstractStringBuilder里面封装了大部分内容。初始化大小为16个字符。
1、相比较String中的value,这个不是final 类型的。
char[] value;
2、newCapacity方法。
- 将原始的capacity*2+2
- 判断新的容量是不是合法
- 最大容量不会超过MAX_ARRAY_SIZE。MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8.
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;
}
3、append方法
其实最终调用了System.arrayCopy将append中的String value copy到了StringBuilder 中的 value。
public AbstractStringBuilder append(StringBuffer sb) {
if (sb == null)
return appendNull();
int len = sb.length();
ensureCapacityInternal(count + len);
sb.getChars(0, len, value, count);
count += len;
return this;
}
最终调用getChars方法。
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
四、StringBuffer中的主要内容
这个类同样继承了AbstractStringBuilder。
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
但是几乎所有的方法都加了同步。因此是线程安全的。
1、append
@Override
public synchronized StringBuffer append(String str) {
toStringCache = null;
super.append(str);
return this;
}
2、chartAt
@Override
public synchronized char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
其中StringBuffer里面有个 char [] toStringCache。当调用toString的时候,这个就会缓存StringBuffer的内容。当对StringBuffer有修改的时候,这个数组就会设定为null。
private transient char[] toStringCache;