首页 > 代码库 > JDK源码学习----StringBuffer+StringBuilder

JDK源码学习----StringBuffer+StringBuilder

      由于前面学习了StringBuffer和StringBuilder的父类AbstractStringBuilder,他们俩的很多方法都是直接super了父类的,也为了较好的比较StringBuffer和StringBuilder,所以把二者放在同一博文中。

一.StringBuffer

1.静态类,不能被继承,实现了Serializable和CharSequence接口。

public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence
2.构造函数

public StringBuffer() {//StringBuffer的默认 容量 是16,注意是容量不是长度
	super(16);
    }
 
public StringBuffer(int capacity) {//也可以在初始化StringBuffer时为它指定容量,为了减少不必要的扩容,尽量在初始化StringBuffer时指定 容量
	super(capacity);
    }
 
 public StringBuffer(String str) {//若初始化StringBuffer时传入字符串,则 容量 为字符串长度+默认容量16	super(str.length() + 16);
	append(str);
    }

 public StringBuffer(CharSequence seq) {
        this(seq.length() + 16);
        append(seq);
    }
3.成员函数

方法大部分都定义为synchronized,这是StringBuffer和StringBuilder的最大区别,也就是StringBuffer是同步的,线程安全的,而StringBuilder是非线程安全的。当然嘛,上帝给你关了一扇门总是会为你开一扇窗的,StringBuffer是线程安全的但是效率上去略逊与StringBuilder.

由于几乎都是super的父类的,故不再累述,见我的前一篇JDK源码学习----AbstractStringBuilder   http://blog.csdn.net/sheepmu/article/details/26095203

 public synchronized int length() {
	return count;
    }

    public synchronized int capacity() {
	return value.length;
    }


    public synchronized void ensureCapacity(int minimumCapacity) {
	if (minimumCapacity > value.length) {
	    expandCapacity(minimumCapacity);
	}
    }

    public synchronized void trimToSize() {
        super.trimToSize();
    }
 
    public synchronized void setLength(int newLength) {
	super.setLength(newLength);
    }
 
    public synchronized char charAt(int index) {
	if ((index < 0) || (index >= count))
	    throw new StringIndexOutOfBoundsException(index);
	return value[index];
    }
  
    public synchronized StringBuffer append(Object obj) {
	super.append(String.valueOf(obj));
        return this;
    }

    public synchronized StringBuffer append(String str) {
	super.append(str);
        return this;
    }
 
    public synchronized StringBuffer append(StringBuffer sb) {
        super.append(sb);
        return this;
    }
 
    public StringBuffer append(CharSequence s) {
        // Note, synchronization achieved via other invocations
        if (s == null)
            s = "null";
        if (s instanceof String)
            return this.append((String)s);
        if (s instanceof StringBuffer)
            return this.append((StringBuffer)s);
        return this.append(s, 0, s.length());
    }
 
    public synchronized StringBuffer delete(int start, int end) {
        super.delete(start, end);
        return this;
    }

   
    public synchronized StringBuffer deleteCharAt(int index) {
        super.deleteCharAt(index);
        return this;
    }
 
    public synchronized String substring(int start) {
        return substring(start, count);
    }
  
    public int indexOf(String str) {
	return indexOf(str, 0);
    }
 
    public synchronized int indexOf(String str, int fromIndex) {
        return String.indexOf(value, 0, count,
                              str.toCharArray(), 0, str.length(), fromIndex);
    }
 
    public synchronized StringBuffer reverse() {
	super.reverse();
	return this;
    }

    public synchronized String toString() {
	return new String(value, 0, count);
    }
 
    private static final java.io.ObjectStreamField[] serialPersistentFields = 
    { 
        new java.io.ObjectStreamField("value", char[].class), 
        new java.io.ObjectStreamField("count", Integer.TYPE),
        new java.io.ObjectStreamField("shared", Boolean.TYPE),
    };

    
    private synchronized void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException {
        java.io.ObjectOutputStream.PutField fields = s.putFields();
        fields.put("value", value);
        fields.put("count", count);
        fields.put("shared", false);
        s.writeFields();
    }

   
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        java.io.ObjectInputStream.GetField fields = s.readFields();
        value = http://www.mamicode.com/(char[])fields.get("value", null);>二.StringBuilder

1.静态类,不能被继承,实现了Serializable和CharSequence接口。  (同StringBuffer)

public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, CharSequence
2.构造函数  (同StringBuffer)
 public StringBuilder() {
	super(16);
    }
 
    public StringBuilder(int capacity) {
	super(capacity);
    }
 
    public StringBuilder(String str) {
	super(str.length() + 16);
	append(str);
    }
 
    public StringBuilder(CharSequence seq) {
        this(seq.length() + 16);
        append(seq);
    }
3.成员函数  (与StringBuffer最大的区别就是非同步的)
public StringBuilder append(Object obj) {
	return append(String.valueOf(obj));
    }

    public StringBuilder append(String str) {
	super.append(str);
        return this;
    }
 
    private StringBuilder append(StringBuilder sb) {
	if (sb == null)
            return append("null");
	int len = sb.length();
	int newcount = count + len;
	if (newcount > value.length)
	    expandCapacity(newcount);
	sb.getChars(0, len, value, count);
	count = newcount;
        return this;
    }
 
    public StringBuilder append(StringBuffer sb) {
        super.append(sb);
        return this;
    }
 
    public StringBuilder append(CharSequence s) {
        if (s == null)
            s = "null";
        if (s instanceof String)
            return this.append((String)s);
        if (s instanceof StringBuffer)
            return this.append((StringBuffer)s);
        if (s instanceof StringBuilder)
            return this.append((StringBuilder)s);
        return this.append(s, 0, s.length());
    }
 
    public StringBuilder delete(int start, int end) {
	super.delete(start, end);
        return this;
    }
 
    public StringBuilder deleteCharAt(int index) {
        super.deleteCharAt(index);
        return this;
    }
 
    public StringBuilder insert(int index, char str[], int offset,
                                int len) 
    {
        super.insert(index, str, offset, len);
	return this;
    }
   
    public int indexOf(String str) {
	return indexOf(str, 0);
    } 
    public int indexOf(String str, int fromIndex) {
        return String.indexOf(value, 0, count,
                              str.toCharArray(), 0, str.length(), fromIndex);
    }
 
    public int lastIndexOf(String str) {
        return lastIndexOf(str, count);
    } 
    public StringBuilder reverse() {
	super.reverse();
	return this;
    }

    public String toString() {
        // Create a copy, don't share the array
	return new String(value, 0, count);
    }