Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Java中String、StringBuffer及StringBuilder的區別

Java中String、StringBuffer及StringBuilder的區別

編輯:關於Android編程

一、String介紹

先看下官方文檔對String的介紹:

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

     String str = "abc";

is equivalent to:

     char data[] = {'a', 'b', 'c'};
     String str = new String(data);

Here are some more examples of how strings can be used:

     System.out.println("abc");
     String cde = "cde";
     System.out.println("abc" + cde);
     String c = "abc".substring(2,3);
     String d = cde.substring(1, 2);

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class.

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.

The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).

從介紹中可看到String是字符串常量,字符串長度不變,它是Immutable的。從String類的實現也可以看到它加了final標識符。

使用String主要注意的點:
1. String類初始化後是不可變的(immutable);

String的實例一旦生成就不會再改變了,比如說:String str=”kv”+”ill”+” “+”ans”;就有四個字符串常量,最終由於String的不可變導致通過“+”產生了很多不必要的臨時變量,這種情況下使用StringBuffer或StringBuilder更好。
2. 使用String不一定創建對象;

在執行到雙引號包含字符串的語句時,如String a = “123”,JVM會先到常量池裡查找,如果有的話返回常量池裡的這個實例的引用,否則的話創建一個新實例並置入常量池裡。只有通過new()方法才能保證每次都創建一個新的對象。
3. 使用new String,一定創建對象;

在執行String a = new String(“123”)的時候,首先走常量池的路線取到一個實例的引用(若沒有,則創建一個”abc”對象),然後再通過new關鍵字創建一個新的String實例,實際上有可能創建了兩個String對象。
4. String.intern()方法;

tring的 intern()方法就是擴充常量池的 一個方法;當一個String實例str調用intern()方法時,Java 查找常量池中 是否有相同Unicode的字符串常量,如果有,則返回其的引用,如果沒有,則在常量池中增加一個Unicode等於str的字符串並返回它的引用。比如:

String s0 = "kvill"; 
String s1 = new String("kvill"); 
String s2 = new String("kvill"); 
System.out.println( s0 == s1 ); //false
System.out.println( "**********" ); 
s1.intern(); //雖然執行了s1.intern(),但它的返回值沒有賦給s1
s2 = s2.intern(); //把常量池中"kvill"的引用賦給s2 
System.out.println( s0 == s1); //flase
System.out.println( s0 == s1.intern() ); //true//說明s1.intern()返回的是常量池中"kvill"的引用
System.out.println( s0 == s2 ); //true

二、StringBuffer介紹

官方對StringBuffer的定義:

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.

For example, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would cause the string buffer to contain "startle", whereas z.insert(4, "le") would alter the string buffer to contain "starlet".

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

從定義也可知:StringBuffer是線程安全的可變字符序列。在任意時間點上它都包含某種特定的字符序列,但通過某些方法調用可以改變該序列的長度和內容。可將字符串緩沖區安全地用於多個線程。

StringBuffer 上的主要操作是 append 和 insert 方法,可重載這些方法,以接受任意類型的數據。每個方法都能有效地將給定的數據轉換成字符串,然後將該字符串的字符追加或插入到字符串緩沖區中。append 方法始終將這些字符添加到緩沖區的末端;而 insert 方法則在指定的點添加字符。例如,如果 z 引用一個當前內容是“start”的字符串緩沖區對象,則此方法調用 z.append(“le”) 會使字符串緩沖區包含“startle”,而 z.insert(4, “le”) 將更改字符串緩沖區,使之包含“starlet”。

使用StringBuffer類時,每次都會對 StringBuffer 對象本身進行操作,而不是生成新的對象並改變對象引用。

三、StringBuilder介紹

Java中對StringBuider的定義:

A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

介紹上也說了,StringBuilder是一個可變的字符序列,是JDK5.0新增的。此類提供一個與 StringBuffer 兼容的 API,但不保證同步。該類被設計用作 StringBuffer 的一個簡易替換,用在字符串緩沖區被單個線程使用的時候(這種情況很普遍)。速度相較於StringBuffer要更快。

四、String、StringBuffer及StringBuilder使用

如果要操作少量的數據,用String; 單線程操作大量數據,用StringBuilder; 多線程操作大量數據,用StringBuffer; 不要使用String類的”+”來進行頻繁的拼接,因為性能很差,應該使用StringBuffer或StringBuilder類; 為了獲得更好的性能,在構造StringBuffer或StringBuilder時應盡可能指定它們的容量,默認構造的容量為16個字符; StringBuilder一般使用在方法內部來完成類似”+”功能,因為是線程不安全的,所以用完以後可以丟棄。StringBuffer主要用在全局變量中;

StringBuffer和StringBuilder都是繼承自AbstractStringBuilder的,AbstractStringBuilder原理:AbstractStringBuilder中采用一個char數組來保存需要append的字符串,char數組有一個初始大小,當append的字符串長度超過當前char數組容量時,則對char數組進行動態擴展,也即重新申請一段更大的內存空間,然後將當前char數組拷貝到新的位置,因為重新分配內存並拷貝的開銷比較大,所以每次重新申請內存空間都是采用申請大於當前需要的內存空間的方式。

從JDK 1.5開始,帶有字符串變量的連接操作(+),JVM內部采用的是StringBuilder來實現的,而之前這個操作是采用StringBuffer實現的。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved