Java字串串連效率

來源:互聯網
上載者:User

     字串操作是編寫程式中最常見的行為,本文對String、StringBuilder、StringBuffer三個類在字串處理方面的效率進行分析。

     Java中最常見也是應用最廣泛的類就是String類。

     String:Strings are constant; their values cannot be changed after they are created.

     這是JDK對String的解釋,意思是:String是常量,一旦建立後它的值不能被修改。

     先看String對象是如何建立的:

     String str1 = “abc”;

     String str2 = new String(“abc”);

     這是我們常見的兩種形式。

     第一種方式建立的字串會放在棧裡,更確切的是常量池中,常量池就是用來儲存在編譯階段確定好了大小的資料,一般我們定義的int等基礎資料型別 (Elementary Data Type)就儲存在這裡。其具體的一個流程就是,編譯器首先檢查常量池,看看有沒有一個“abc”,如果沒有則建立。如果有的話,則則直接把str1指向那個位置。

     第二種建立字串的方法是通過new關鍵字,還是java的記憶體配置,java會將new的對象放在堆中,這一部分對象是在運行時建立的對象。所以我們每一次new的時候,都會建立不同的對象,即便是堆中已經有了一個一模一樣的。

     下面的程式將驗證上面的說法。

 1 String str1 = "abc"; 2 String str2 = new String("abc"); 3  4 String str3 = "abc"; 5 String str4 = new String("abc"); 6  7 System.out.println(str1==str2);//false 8          9 System.out.println(str1 == str3);//true10 System.out.println(str2 == str4);//false11 // When the intern method is invoked, if the pool already contains a12 // string equal to this String object as determined by the13 // equals(Object) method, then the string from the pool is returned.14 // Otherwise, this String object is added to the pool and a reference to15 // this String object is returned.16 str2 = str2.intern();17 System.out.println(str1==str2);//true18 19 false20 true21 false22 true

     以上程式中用兩種方式建立的4個對象,”==”比較的是地址,從結果可以看到str1和str3相同,指向同一個對象。而str2和str4比較返回結果是false,說明str2和str4指向的不是同一個對象。

     (上面用到了一個比較少見的方法:intern。在str2調用了intern方法後對str1和str2進行比較返回true,可以從代碼注釋中看明白,intern方法返回常量池中內容和該對象相同的對象,如果常量池中不存在,則將該對象加入到常量池中。)

     String對象是不變的,那為什麼可以進行str+=”abc”這樣的操作?其實類似這樣的操作是新產生了一個String對象,包含str和abc串連後的字串。

1 package test;2 3 public class StringTest {4     public static void main(String[] args) {5         String str1 = "abc";6         str1 += "123";7     }8 }

     從編譯之後的class可以看出編譯器自動引入了StringBuilder類,通過它的初始化方法建立了StringBuilder對象,通過append方法添加了內容,調用toString返回串連後的對象。以上內容證明了String對象是不可變的,串連操作實際是返回了新的對象。如果是迴圈多次進行串連,將不斷的建立StringBuilder對象,append新內容,toString成String對象。這就帶來了String類處理字串更改操作的效率問題。

 1 public class StringTest { 2     public static void main(String[] args) { 3         long start, end; 4  5         StringBuilder strBuilder = new StringBuilder(" "); 6         start = System.currentTimeMillis(); 7         for (int i = 0; i < 100000; i++) { 8             strBuilder.append(" "); 9         }10         end = System.currentTimeMillis();11         System.out.println("StringBuilder append: " + (end - start) + "ms");12 13         String str = " ";14         start = System.currentTimeMillis();15         for (int i = 0; i < 100000; i++) {16             str += " ";17         }18         end = System.currentTimeMillis();19         System.out.println("String +: " + (end - start) + "ms");20     }21 }

     可以看到String和StringBuilder在效率方便的差距。

     StringBuffer:A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.

     StringBuilder:A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization.

     StringBuffer是可變的、安全執行緒的字串。StringBuilder就是StringBuffer的非線程同步的版本,二者的方法差不多,只是一個安全執行緒(適用於多線程)一個沒有安全執行緒(適用於單線程)。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.