String, StringBuffer, StringBuilder

轉自:https://segmentfault.com/a/1190000002683782

我們先要記住三者的特徵:

  • String 字符串常量
  • StringBuffer 字符串變量(線程安全)
  • StringBuilder 字符串變量(非線程安全)

一、定義


查看 API 會發現,String、StringBuffer、StringBuilder 都實現了 CharSequence 接口,內部都是用一個char數組實現,雖然它們都與字符串相關,但是其處理機制不同。

  • String:是不可改變的量,也就是創建後就不能在修改了。
  • StringBuffer:是一個可變字符串序列,它與 String 一樣,在內存中保存的都是一個有序的字符串序列(char 類型的數組),不同點是 StringBuffer 對象的值都是可變的。
  • StringBuilder:與 StringBuffer 類基本相同,都是可變字符換字符串序列,不同點是 StringBuffer 是線程安全的,StringBuilder 是線程不安全的。

使用場景

使用 String 類的場景:在字符串不經常變化的場景中可以使用 String 類,例如常量的聲明、少量的變量運算。

使用 StringBuffer 類的場景:在頻繁進行字符串運算(如拼接、替換、刪除等),並且運行在多線程環境中,則可以考慮使用 StringBuffer,例如 XML 解析、HTTP 參數解析和封裝。

使用 StringBuilder 類的場景:在頻繁進行字符串運算(如拼接、替換、和刪除等),並且運行在單線程的環境中,則可以考慮使用 StringBuilder,如 SQL 語句的拼裝、JSON 封裝等。


分析

在性能方面,由於 String 類的操作是產生新的 String 對象,而 StringBuilder 和 StringBuffer 只是一個字符數組的擴容而已,所以 String 類的操作要遠慢於 StringBuffer 和 StringBuilder。

簡要的說, String 類型和 StringBuffer 類型的主要性能區別其實在於 String 是不可變的對象, 因此在每次對 String 類型進行改變的時候其實都等同於生成了一個新的 String 對象然後將指針指向新的 String 對象。所以經常改變內容的字符串最好不要用 String ,因爲每次生成對象都會對系統性能產生影響,特別當內存中無引用對象多了以後, JVM 的 GC 就會開始工作,那速度是一定會相當慢的

而如果是使用 StringBuffer 類則結果就不一樣了,每次結果都會對 StringBuffer 對象本身進行操作,而不是生成新的對象,再改變對象引用。所以在一般情況下我們推薦使用 StringBuffer ,特別是字符串對象經常改變的情況下。

而在某些特別情況下, String 對象的字符串拼接其實是被 JVM 解釋成了 StringBuffer 對象的拼接,所以這些時候 String 對象的速度並不會比 StringBuffer 對象慢,而特別是以下的字符串對象生成中, String 效率是遠要比 StringBuffer 快的:

    String S1 = “This is only a" + “ simple" + “ test";
    StringBuffer Sb = new StringBuilder(“This is only a").append(“ simple").append(“ test");

你會很驚訝的發現,生成 String S1 對象的速度簡直太快了,而這個時候 StringBuffer 居然速度上根本一點都不佔優勢。其實這是 JVM 的一個把戲,在 JVM 眼裏,這個

String S1 = “This is only a" + “ simple" + “test";
其實就是:
String S1 = “This is only a simple test";
所以當然不需要太多的時間了。但大家這裏要注意的是,如果你的字符串是來自另外的 String 對象的話,速度就沒那麼快了,譬如:

1 String S2 = "This is only a";
2 String S3 = "simple";
3 String S4 = "test";
4 String S1 = S2 +S3 + S4;
這時候 JVM 會規規矩矩的按照原來的方式去做。


又及:

關於 equal 和 ==

  == 用於比較兩個對象的時候,是來check 是否兩個引用指向了同一塊內存。

這個輸出就是false


這個輸出是true


一個特殊情況 :

這是因爲:
字符串緩衝池:程序在運行的時候會創建一個字符串緩衝池。
當使用 String s1 = "xyz"; 這樣的表達是創建字符串的時候(非new這種方式),程序首先會在這個 String 緩衝池中尋找相同值的對象,
在 String str1 = "xyz"; 中,s1 先被放到了池中,所以在 s2 被創建的時候,程序找到了具有相同值的 str1
並將 s2 引用 s1 所引用的對象 "xyz"



 equals()
equals() 是object的方法,默認情況下,它與== 一樣,比較的地址。
但是當equal被重載之後,根據設計,equal 會比較對象的value。而這個是java希望有的功能。String 類就重寫了這個方法

結果返回true

總的說,String 有個特點: 如果程序中有多個String對象,都包含相同的字符串序列,那麼這些String對象都映射到同一塊內存區域,所以兩次new String("hello")生成的兩個實例,雖然是相互獨立的,但是對它們使用hashCode()應該是同樣的結果。Note: 字符串數組並非這樣,只有String是這樣。即hashCode對於String,是基於其內容的。

public class StringHashCode {
       public static void main(String[] args) {
            \\輸出結果相同
            String[] hellos = "Hello Hello".split(" " );
            System.out.println(""+hellos[0].hashCode());
            System.out.println(""+hellos[1].hashCode());
            \\輸出結果相同
            String a = new String("hello");
            String b = new String("hello");
            System.out.println(""+a.hashCode());
            System.out.println(""+b.hashCode());
      }
}

結論

String 類是final類,不可以繼承。對String類型最好的重用方式是組合 而不是繼承。
String 有length()方法,數組有length屬性

String s = new String("xyz"); 創建了幾個字符串對象?
兩個對象,一個靜態存儲區“xyz”, 一個用new創建在堆上的對象。

String 和 StringBuffer,String Builder區別?

在大部分情況下 StringBuffer > String

Java.lang.StringBuffer 是線程安全的可變字符序列。一個類似於 String 的字符串緩衝區,但不能修改。雖然在任意時間點上它都包含某種特定的字符序列,但通過某些方法調用可以改變該序列的長度和內容。在程序中可將字符串緩衝區安全地用於多線程。而且在必要時可以對這些方法進行同步,因此任意特定實例上的所有操作就好像是以串行順序發生的,該順序與所涉及的每個線程進行的方法調用順序一致。

StringBuffer 上的主要操作是 append 和 insert 方法,可重載這些方法,以接受任意類型的數據。每個方法都能有效地將給定的數據轉換成字符串,然後將該字符串的字符追加或插入到字符串緩衝區中。append 方法始終將這些字符添加到緩衝區的末端;而 insert 方法則在指定的點添加字符。

例如,如果 z 引用一個當前內容是 “start"的字符串緩衝區對象,則此方法調用 z.append("le") 會使字符串緩衝區包含 “startle"( 累加); 而 z.insert(4, "le") 將更改字符串緩衝區,使之包含 “starlet"。

在大部分情況下 StringBuilder > StringBuffer

java.lang.StringBuilder 一個可變的字符序列是 JAVA 5.0 新增的。此類提供一個與 StringBuffer 兼容的 API,但不保證同步,所以使用場景是單線程。該類被設計用作 StringBuffer 的一個簡易替換,用在字符串緩衝區被單個線程使用的時候(這種情況很普遍)。如果可能,建議優先採用該類,因爲在大多數實現中,它比 StringBuffer 要快。兩者的使用方法基本相同。



源碼

String,StringBuffer,StringBuilder都實現了CharSequence接口。

public interface CharSequence
{
    int length();
    // return the char value at the specified index
    char charAt(int index);
    // return a new CharSequence that is a subsequence
    // of this sequence.
    CharSequence subSequence(int start, int end);
    public String toString();
}

String的源碼

public final class String{
    private final char value[]; // used for character storage
    private int the hash; // cache the hash code for the string
}

成員變量只有兩個:
final的char類型數組
int類型的hashcode

構造函數

public String()
public String(String original){
    this.value = original.value;
    this.hash = original.hash;
}
public String(char value[]){
    this.value = Arrays.copyOf(value, value.length);
}
public String(char value[], int offset, int count){
    // 判斷offset,count,offset+count是否越界之後
    this.value = Arrays.copyOfRange(value, offset, offset+count);
}

這裏用到了一些工具函數
copyOf(source[],length); 從源數組的0位置拷貝length個;
這個函數是用System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength))實現的。

copyOfRange(T[] original, int from, int to)

構造函數還可以用StringBuffer/StringBuilder類型初始化String,

    public String(StringBuffer buffer) {
        synchronized(buffer) {
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
        }
    }
   public String(StringBuilder builder) {
        this.value = Arrays.copyOf(builder.getValue(), builder.length());
    }

除了構造方法,String類的方法有很多,
length,isEmpty,可以通過操作value.length來實現。
charAt(int index):
通過操作value數組得到。注意先判斷index的邊界條件

    public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }

getChars方法

public void getChars(int srcBegin, int srcEnd,
     char dst[], int dstBegin)
     {
     \\邊界檢測
     System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
     }

equals方法,根據語義相等(內容相等,而非指向同一塊內存),重新定義了equals

   public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

如果比較的雙方指向同一塊內存,自然相等;(比較==即可)
如果內容相等,也相等,比較方法如下:
首先anObject得是String類型(用關鍵字instanceof)
然後再比較長度是否相等;
如果長度相等,則挨個元素進行比較,如果每個都相等,則返回true.

還有現成安全的與StringBuffer內容比較
contentEquals(StringBuffer sb),實現是在sb上使用同步。

compareTo():
如果A大於B,則返回大於0的數;
A小於B,則返回小於0的數;
A=B,則返回0

    public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }

regionMatches:如果兩個字符串的區域都是平等的,

    public boolean regionMatches(int toffset, String other, int ooffset,
            int len)
   {
    //判斷邊界條件
            while (len-- > 0) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }
            }
 public boolean regionMatches(boolean ignoreCase, int toffset,
            String other, int ooffset, int len) 
{    
    while (len-- > 0) {
            char c1 = ta[to++];
            char c2 = pa[po++];
            if (c1 == c2) {
                continue;
            }
            if (ignoreCase) {
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                char u2 = Character.toUpperCase(c2);
                if (u1 == u2) {
                    continue;
                }
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
                    continue;
                }
            }
            return false;
        }
        return true;
}

startsWith(String prefix, int toffset)
startsWith(String prefix)
endsWith(String suffix)

{
    return startsWith(suffix, value.length 
    - suffix.value.length);
    }

substring(int beginIndex,int endIndex)
除了條件判斷:

 return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);

字符串連接concat(String str)

        int otherLen = str.length();
        if (otherLen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = Arrays.copyOf(value, len + otherLen);
        str.getChars(buf, len);
        return new String(buf, true);

對於StringBuffer和StringBuilder
StringBuffer 和 StringBuilder 都是繼承於 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);//getChars就是利用native的array copy,性能高效
        count += len;
        return this;
    }

StringBuffer 底層也是 char[], 數組初始化的時候就定下了大小, 如果不斷的 append 肯定有超過數組大小的時候,我們是不是定義一個超大容量的數組,太浪費空間了。就像 ArrayList 的實現,採用動態擴展,每次 append 首先檢查容量,容量不夠就先擴展,然後複製原數組的內容到擴展以後的數組中




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