您的位置:首页 > 产品设计 > UI/UE

String、StringBuilder、StringBuffer

2017-05-25 00:00 141 查看

1.不可变字符串

String:java.lang.String

String对象在创建后是不可变的,在字符串缓存区共享。Java语言提供对字符串串联符号(+)以及将其它对象转换为字符串的特殊支持。

/** The value is used for character storage. */
private final char value[];

/** The offset is the first index of the storage that is used. */
private final int offset;

/** The count is the number of characters in the String. */
private final int count;

从String类的实现可以看到,字符串数组的长度是final定义,不可变。

String one = "abc";
String two = "xyz";
String three = one + two;

注意:string的字符串拼接会被java compiler编译成StringBuilder对象的拼接,如下:

String one = "abc";
String two = "xyz";
String three = (new StringBuilder()).append(one).append(two).toString();

如果字符串和其它类型的变量拼接,java compiler并不会自动转换,相应的,速度并不会那么快。

2.可变字符串

StringBuffer:java.lang.StringBuffer

线程安全的可变字符串序列,类似于String的字符串缓冲区,序列的长度和内容可变。StringBuffer主要用到的方法有append和insert,append方法始终将这些字符添加到缓冲区的末端,而insert方法则在指定的点添加字符。

public synchronized StringBuffer append(String str) {
super.append(str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see        #length()
*/
public synchronized StringBuffer insert(int offset, String str) {
super.insert(offset, str);
return this;
}


StringBuilder:java.lang.StringBuilder

非线程安全的可变字符串序列,常用方法append和insert。

public StringBuffer append(String str) {
super.append(str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see        #length()
*/
public StringBuffer insert(int offset, String str) {
super.insert(offset, str);
return this;
}


AbstractStringBuilder:java.lang.AbstractStringBuilder

StringBuffer和StringBuilder都继续该抽象类。StringBuffer和StringBuilder通过super.append()调用父抽象类的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;
}

每个字符串缓冲区都有一定的容量,只要字符串缓冲区所包含的字符序列的长度没有超出此容量,就无需分配新的内部缓冲区数组,如果内部缓冲区溢出,则容量自动增大。

expandCapacity方法实现容量的扩增。

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);
}

System.arraycopy

//从指定源数组中复制一个数组,复制从指定的位置开始,到目标指定的位置结束
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)


3.总结

1.String:少量数据的拼接用
StringBuilder:单线程操作大量数据
StringBuffer:多线程操作大量数据,线程安全

2.不要使用String类的"+"来进行频繁的拼接,因为那样的性能极差的,应该使用StringBuffer或StringBuilder类,这在Java的优化上是一条比较重要的原则。

3.为了获得更好的性能,在构造 StringBuffer 或 StringBuilder 时应尽可能指定它们的容量。当然,如果你操作的字符串长度(length)不超过 16 个字符就不用了,当不指定容量(capacity)时默认构造一个容量为16的对象。不指定容量会显著降低性能。

4.StringBuilder一般使用在方法内部来完成类似"+"功能,因为是线程不安全的,所以用完以后可以丢弃。StringBuffer主要用在全局变量中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息