1、String 与 StringBuffer
String类中使用字符数组保存字符串,如下查看String源码时发现,其有“final”修饰符,所以可以知道string对象是不可变的。
String:在String类中没有用来改变已有字符串中的某个字符的方法,由于不能改变一个字符串中的某个单独字符,所以在JDK文档中称String类的对象是不可改变的。然而,不可改变的字符串具有一个很大的优点:编译器可以把字符串设为共享的。
StringBuffer:StringBuffer类属于一种辅助类,可预先分配指定长度的内存块建立一个字符串缓冲区。这样使用StringBuffer类的append方法追加字符 比 String使用 + 操作符添加字符 到 一个已经存在的字符串后面有效率得多。因为使用 + 操作符每一次将字符添加到一个字符串中去时,字符串对象都需要寻找一个新的内存空间来容纳更大的字符串,这无凝是一个非常消耗时间的操作。添加多个字符也就意味着要一次又一次的对字符串重新分配内存。使用StringBuffer类就避免了这个问题。
因此,在对字符串的处理过程中,StringBuffer在内存上要优于String。
下面是分别对用String与StringBuffer拼接字符串的效率做的一个对比
结果很明显表示://40000,stringBuffer use time: 15(毫秒)
//40000,string use time: 2251(毫秒)
@Test
public void testStringBuffer() {
long startTime = System.currentTimeMillis();
StringBuffer sbf = new StringBuffer();
for (int j = 1; j < 40000; j++) {
sbf.append(String.valueOf(j));
}
long entTime = System.currentTimeMillis();
System.out.println("stringBuffer use time: " + (entTime - startTime));//40000,stringBuffer use time: 15
System.out.println(sbf.toString().substring(0, 1000));
}
@Test
public void testString() {
long startTime = System.currentTimeMillis();
String str = new String();
for (int j = 1; j < 40000; j++) {
str += (String.valueOf(j));
}
long entTime = System.currentTimeMillis();
System.out.println("string use time: " + (entTime - startTime));//40000,string use time: 2251
System.out.println(str.substring(0, 1000));
}
2、StringBuffer 的自动扩容机制
当append()字符串小于16的时候,调用str.capacity()方法查看容量大小为16,str.length()大小为你添加字符串的长度
当你添加的字符串超过16的时候,StringBuffer会自动扩容到原来容量的2倍+2
此时StringBuffer的容量不再是扩容2倍+2,而是直接由原来容量(即默认容量)+字符串长度
@Test
public void testSBFAutoIncreate() {
StringBuffer sbf = new StringBuffer();
for (int j = 1; j <= 50; j++) {
sbf.append(String.valueOf(1));
}
System.out.println("sbf 实际长度: " + sbf.length() + " ,sbf容量大小 : " + sbf.capacity());
//sbf 实际长度: 1 ,sbf容量大小 : 16
//sbf 实际长度: 16 ,sbf容量大小 : 16
//sbf 实际长度: 17 ,sbf容量大小 : 34
//sbf 实际长度: 50 ,sbf容量大小 : 70
//sbf 实际长度: 100 ,sbf容量大小 : 142
}
@Test
public void testSBFAutoIncreate2() {
StringBuffer sbf = new StringBuffer(10); // 给容量设定默认值
for (int j = 1; j <= 50; j++) {
sbf.append(String.valueOf(1));
}
System.out.println("sbf 实际长度: " + sbf.length() + " ,sbf容量大小 : " + sbf.capacity());
//sbf 实际长度: 1 ,sbf容量大小 : 10
//sbf 实际长度: 11 ,sbf容量大小 : 22
//sbf 实际长度: 25 ,sbf容量大小 : 46
//sbf 实际长度: 50 ,sbf容量大小 : 94
}
StringBuffer的源码
new 的时候默认容量为16
/**
* Constructs a string buffer with no characters in it and an
* initial capacity of 16 characters.
*/
public StringBuffer() {
super(16);
}
/**
* Constructs a string buffer initialized to the contents of the
* specified string. The initial capacity of the string buffer is
* <code>16</code> plus the length of the string argument.
*
* @param str the initial contents of the buffer.
* @exception NullPointerException if <code>str</code> is <code>null</code>
*/
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}
当进行调用append方法时
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
}
/**
* This implements the expansion semantics of ensureCapacity with no
* size check or synchronization.
*/
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity);
}
尝试将新容量扩为大小变成2倍+2 if 判断一下 容量如果不够,直接扩充到需要的容量大小;
顺便说说:
集合中也有类似情况,ArrayList 和LinkedList也有默认值10 ,也有扩容算法采用的是右偏移1 + 原有长度 ==也就是变为1.5倍大小 ,如果不够用的话,就直接扩充到需要的大小;
由于在不断扩容时会消耗一定的时间与资源,所以当知道StringBuffer的大小时建议在new 的时候给定初始的长度。
String中的对象是不可变的,也就可以理解为常量,显然线程安全。
AbstractStringBuilder是StringBuilder与StringBuffer的公共父类,定义了一些字符串的基本操作,如expandCapacity、append、insert、indexOf等公共方法。
3、线程安全
StringBuffer对方法加了同步锁或者对调用的方法加了同步锁,所以是线程安全的。看如下源码:
复制代码
public synchronized StringBuffer reverse() {
super.reverse();
return this;
}
public int indexOf(String str) {
return indexOf(str, 0);
} //存在
public synchronized int indexOf(String str, int fromIndex) {
...方法
}
复制代码
StringBuilder并没有对方法进行加同步锁,所以是非线程安全的。
4.StringBuilder与StringBuffer共同点
StringBuilder与StringBuffer有公共父类AbstractStringBuilder(抽象类)。
抽象类与接口的其中一个区别是:抽象类中可以定义一些子类的公共方法,子类只需要增加新的功能,不需要重复写已经存在的方法;而接口中只是对方法的申明和常量的定义。
StringBuilder、StringBuffer的方法都会调用AbstractStringBuilder中的公共方法,如super.append(…)。只是StringBuffer会在方法上加synchronized关键字,进行同步。
最后,如果程序不是多线程的,那么使用StringBuilder效率高于StringBuffer。