StringBuider 和StringBUffer 區別

我們從2方面考慮:

共同點:

StringBuider 和StringBUffer  都是String的特殊的線性數據結構。

源碼上:

 

 StringBuffer :

1:定義
public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence
2:屬性

 static final long serialVersionUID = 3388685877147921107L;
3:構造函數

(1)構造一個其中不帶字符的字符串緩衝區,初始容量爲 16 個字符。

 public StringBuffer() {
        super(16);
    }
繼承父類的AbstractStringBuilder的構造函數

AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }
(2)構造一個不帶字符,但具有指定初始容量的字符串緩衝區

public StringBuffer(int capacity) {
        super(capacity);
    }
(3)構造一個字符串緩衝區,並將其內容初始化爲指定的字符串內容。該字符串的初始容量爲 16 加上字符串參數的長度

public StringBuffer(String str) {
//這個執行父類的帶參構造函數AbstractStringBuilder(int capacity) 
        super(str.length() + 16);
        append(str);
    }
append 是用 synchronized 修飾的,所以是線程安全的
public synchronized StringBuffer append(String str) {  
        //執行父類的append(str)  
        super.append(str);  
        return this;  

父類 AbstractStringBuilder 的 append 方法

 public AbstractStringBuilder append(String str) {
        if (str == null) str = "null";
        int len = str.length();
        ensureCapacityInternal(count + len);//擴大容量      
        str.getChars(0, len, value, count);
        count += len;
        return this;
    }
//擴大容量

private void ensureCapacityInternal(int minimumCapacity) {  
        // overflow-conscious code:判斷是否需要擴容,也就是說原來的capacity是否足夠大  
        if (minimumCapacity - value.length > 0) //20-19=1,1>0  
            expandCapacity(minimumCapacity);  
    }  
void expandCapacity(int minimumCapacity) {
        //新容量  原始容量 * 2 + 2
        int newCapacity = value.length * 2 + 2;
        if (newCapacity - minimumCapacity < 0)     //擴容後的容量-字符串實際長度<0(就是說如果擴容後還裝不下),
            newCapacity = minimumCapacity;    //則使用字符串實際長度作爲StringBuffer的capacity 
        if (newCapacity < 0) {//擴容後的容量超過integer的最大值
           if (minimumCapacity < 0) // overflow //最終容量超過integer的最大值
               throw new OutOfMemoryError();
            newCapacity = Integer.MAX_VALUE;
        }
        //將舊的值剪切到新的字符數組。
        value = Arrays.copyOf(value, newCapacity);

 

 

StringBuilder :

源碼分析
定義
public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence
{
    ...
}
1
2
3
4
5
6
StringBuilder類被 final 所修飾,因此不能被繼承。

StringBuilder類繼承於 AbstractStringBuilder類。實際上,AbstractStringBuilder類具體實現了可變字符序列的一系列操作,比如:append()、insert()、delete()、replace()、charAt()方法等。值得一提的是,StringBuffer也是繼承於AbstractStringBuilder類。

StringBuilder類實現了2個接口:

Serializable 序列化接口,表示對象可以被序列化。
CharSequence 字符序列接口,提供了幾個對字符序列進行只讀訪問的方法,比如:length()、charAt()、subSequence()、toString()方法等。
主要變量
// AbstractStringBuilder.java

char[] value;

int count;
1
2
3
4
5
這兩個變量是定義在AbstractStringBuilder類中的。其中:

value 用來存儲字符序列中的字符。value是一個動態的數組,當存儲容量不足時,會對它進行擴容。
count 表示value數組中已存儲的字符數。
構造方法
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);
}

// AbstractStringBuilder.java
AbstractStringBuilder(int capacity) {
    value = new char[capacity];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
StringBuilder類提供了4個構造方法。構造方法主要完成了對value數組的初始化。其中:

默認構造方法設置了value數組的初始容量爲16。
第2個構造方法設置了value數組的初始容量爲指定的大小。
第3個構造方法接受一個String對象作爲參數,設置了value數組的初始容量爲String對象的長度+16,並把String對象中的字符添加到value數組中。
第4個構造方法接受一個CharSequence對象作爲參數,設置了value數組的初始容量爲CharSequence對象的長度+16,並把CharSequence對象中的字符添加到value數組中。
append()方法
@Override
public StringBuilder append(boolean b) {
    super.append(b);
    return this;
}

// AbstractStringBuilder.java
public AbstractStringBuilder append(boolean b) {
    if (b) {
        ensureCapacityInternal(count + 4);
        value[count++] = 't';
        value[count++] = 'r';
        value[count++] = 'u';
        value[count++] = 'e';
    } else {
        ensureCapacityInternal(count + 5);
        value[count++] = 'f';
        value[count++] = 'a';
        value[count++] = 'l';
        value[count++] = 's';
        value[count++] = 'e';
    }
    return this;
}    

@Override
public StringBuilder append(String str) {
    super.append(str);
    return this;
}

// AbstractStringBuilder.java
public AbstractStringBuilder append(String str) {
    if (str == null)
        return appendNull();
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
}    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
append()方法將指定參數類型的字符串表示形式追加到字符序列的末尾。StringBuilder類提供了一系列的append()方法,它可以接受boolean、char、char[]、CharSequence、double、float、int、long、Object、String、StringBuffer這些類型的參數。這些方法最終都調用了父類AbstractStringBuilder類中對應的方法。最後,append()方法返回了StringBuilder對象自身,以便用戶可以鏈式調用StringBuilder類中的方法。

AbstractStringBuilder類的各個append()方法大同小異。append()方法在追加字符到value數組中之前都會調用ensureCapacityInternal()方法來確保value數組有足夠的容量,然後才把字符追加到value數組中。

private void ensureCapacityInternal(int minimumCapacity) {
    // overflow-conscious code
    if (minimumCapacity - value.length > 0)
        expandCapacity(minimumCapacity);
}

void expandCapacity(int minimumCapacity) {
    int newCapacity = value.length * 2 + 2;
    if (newCapacity - minimumCapacity < 0)
        newCapacity = minimumCapacity;
    if (newCapacity < 0) {
        if (minimumCapacity < 0) // overflow
            throw new OutOfMemoryError();
        newCapacity = Integer.MAX_VALUE;
    }
    value = Arrays.copyOf(value, newCapacity);
}

// Arrays.java
public static char[] copyOf(char[] original, int newLength) {
    char[] copy = new char[newLength];
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

ensureCapacityInternal()方法判斷value數組的容量是否足夠,如果不夠,那麼調用expandCapacity()方法進行擴容。

expandCapacity()方法默認情況下將數組容量擴大到原數組容量的2倍+2。數組的容量最大隻能擴容到Integer.MAX_VALUE。最後,調用Arrays類的copyOf()靜態方法來創建一個新數組和拷貝原數據到新數組,並將value指向新數組。

delete()方法
@Override
public StringBuilder delete(int start, int end) {
    super.delete(start, end);
    return this;
}

// AbstractStringBuilder.java
public AbstractStringBuilder delete(int start, int end) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (end > count)
        end = count;
    if (start > end)
        throw new StringIndexOutOfBoundsException();
    int len = end - start;
    if (len > 0) {
        System.arraycopy(value, start+len, value, start, count-end);
        count -= len;
    }
    return this;
}    

delete()方法刪除指定位置的字符。刪除的字符從指定的start位置開始,直到end-1位置。delete()方法也調用了父類AbstractStringBuilder類中對應的方法。

delete()方法首先檢查參數的合法性。當end大於value數組中已存儲的字符數count時,end取count值。最後,當需要刪除的字符數大於1的時候,調用System類的arraycopy()靜態方法進行數組拷貝完成刪除字符的操作,並更新count的值。

replace()方法
@Override
public StringBuilder replace(int start, int end, String str) {
    super.replace(start, end, str);
    return this;
}

// AbstractStringBuilder.java
public AbstractStringBuilder replace(int start, int end, String str) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (start > count)
        throw new StringIndexOutOfBoundsException("start > length()");
    if (start > end)
        throw new StringIndexOutOfBoundsException("start > end");

    if (end > count)
        end = count;
    int len = str.length();
    int newCount = count + len - (end - start);
    ensureCapacityInternal(newCount);

    System.arraycopy(value, end, value, start + len, count - end);
    str.getChars(value, start);
    count = newCount;
    return this;
}

// String.java
void getChars(char dst[], int dstBegin) {
    System.arraycopy(value, 0, dst, dstBegin, value.length);
}

replace()方法將指定位置的字符替換成指定字符串中的字符。替換的字符從指定的start位置開始,直到end-1位置。replace()方法也調用了父類AbstractStringBuilder類中對應的方法。

replace()方法首先檢查參數的合法性。當end大於value數組中已存儲的字符數count時,end取count值。然後調用ensureCapacityInternal()方法確保value數組有足夠的容量。接着調用System類的arraycopy()靜態方法進行數組拷貝,主要的作用是從start位置開始空出替換的字符串長度len大小的位置。最後,調用String類的getChars()方法將替換的字符串中的字符拷貝到value數組中。這樣就完成了替換字符的操作。

charAt()方法
// AbstractStringBuilder.java
@Override
public char charAt(int index) {
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    return value[index];
}

charAt()方法返回指定索引處的char字符。索引的範圍從0到count-1。charAt()方法定義在父類AbstractStringBuilder類中。

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

toString()方法返回一個表示該字符序列的字符串。

總結
StringBuilder類使用了一個char數組來存儲字符。該數組是一個動態的數組,當存儲容量不足時,會對它進行擴容。
StringBuilder對象是一個可變的字符序列。
StringBuilder類是非線程安全的。

StringBuffer類是線程安全的。

  • StringBuffer類使用了一個char數組來存儲字符。該數組是一個動態的數組,當存儲容量不足時,會對它進行擴容。
  • StringBuffer對象是一個可變的字符序列。
  • StringBuffer類是線程安全的。

 

 

 

參考資料:

https://blog.csdn.net/u014248032/article/details/90694002

https://blog.csdn.net/wqqqianqian/article/details/80001256

https://blog.csdn.net/u012317510/article/details/83721250

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章