String、StringBuilder、StringBuffer

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主要用在全局变量中。

转载于:https://my.oschina.net/u/3453392/blog/908742

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值