很多人都知道String是不可變的荚虚,StringBuffer和StringBuilder是可變的,那么為什么呢籍茧?
首先我們確定一個(gè)概念性問題版述,什么是不可變對象!
什么是不可變對象:如果一個(gè)對象寞冯,在它創(chuàng)建完成之后渴析,不能再改變它的狀態(tài)晚伙,那么這個(gè)對象就是不可變的。不能改變狀態(tài)的意思是俭茧,不能改變對象內(nèi)的成員變量咆疗,包括基本數(shù)據(jù)類型的值不能改變,引用類型的變量不能指向其他的對象母债,引用類型指向的對象的狀態(tài)也不能改變午磁。
String
以下是String的源碼截取
<pre>
** * The {@code String} class represents character strings. All * string literals in Java programs, such as {@code "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);
</pre>
從上面我們可以知道每次對String對象的賦值,都是已經(jīng)改變了String指向的對象毡们!所以String是不可變的迅皇!
再深層點(diǎn),我們會(huì)發(fā)現(xiàn)里面的data對象是final衙熔,所以呢登颓。。呵呵呵
我們也可以很容易理解為什么當(dāng)用戶調(diào)用以下語句的時(shí)候红氯,會(huì)生成了兩個(gè)對象框咙。<pre>String s = new String("abc");</pre>
那么我們可以推出實(shí)際編程中String類型的使用時(shí)機(jī):常量,數(shù)據(jù)不會(huì)發(fā)生改變狀態(tài)下
StringBuffer和StringBuilder
很多文章都是把StringBuffer和StringBuilder分開來講解痢甘!我覺得這樣其實(shí)不好扁耐,他們區(qū)別其實(shí)就在于一個(gè)關(guān)鍵字:synchronized,這代表著使用StringBuffer是線程安全的产阱,這就決定了他們之間的使用場景,在于多線程和單線程块仆!所以构蹬,很簡單,如果從使用效率上看悔据,在單線程上跑庄敛,使用StringBuilder效率高于StringBuffer,多線程操作(例如網(wǎng)絡(luò)操作)就用StringBuffer吧科汗!如果考慮到以后擴(kuò)展的可能性藻烤,則更難確定,所以我更愿意使用StringBuffer头滔。
下面我們分析下StringBuffer和String的區(qū)別~
<pre>
- A thread-safe, mutable sequence of characters.* A string buffer is like a {@link 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.
</pre>
<pre>public final class StringBuffer extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
public final class StringBuilder extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
</pre>
說明StringBuffer是一個(gè)線程安全的可變序列怖亭!和StringBuilder一樣繼承了AbstractStringBuilder類,所以StringBuffer和StringBuilder作為Object對象是不能直接比較值的坤检,不管你是用equals還是==兴猩,當(dāng)然==是用來比較內(nèi)存地址的,如果兩個(gè)對象引用的是同一個(gè)對象早歇,會(huì)返回true倾芝;
繼承了AbstractStringBuilder的可變字符串序列
AbstractStringBuilder提供了對字符串的處理機(jī)制讨勤,同樣是將數(shù)據(jù)用char數(shù)組的類型保存:
<pre>
/** * Appends the specified string to this character sequence. * <p> * The characters of the {@code String} argument are appended, in * order, increasing the length of this sequence by the
length of the * argument. If {@code str} is {@code null}, then the four * characters {@code "null"} are appended. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the {@code append} method. Then the character
at * index <i>k</i> in the new character sequence is equal to the character * at index <i>k</i> in the old character sequence, if <i>k</i>
is less * than <i>n</i>; otherwise, it is equal to the character at index * <i>k-n</i> in the argument {@code str}. * *
@param str a string. * @return a reference to this object. */
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;
}
</pre>
在append(str)函數(shù)調(diào)用的時(shí)候,首先會(huì)判斷原來用于存儲(chǔ)字符串的values的字符串?dāng)?shù)組有沒有足夠的大小來存儲(chǔ)將要新添加入StringBuilder的字符串晨另。如果不夠用潭千,那么就調(diào)用ensureCapacityInternal判斷是否有足夠的存儲(chǔ)空間,如果夠用借尿,那么就直接添加進(jìn)去刨晴,如果不夠,那就調(diào)用 expandCapacity進(jìn)行字符串的擴(kuò)展操作垛玻。這是StringBuffer和StringBuilder可變的一個(gè)重要原因割捅。
關(guān)于字符串更改用+還是append
結(jié)果是很明顯的!有一篇文章寫得不錯(cuò)<a >在Java中連接字符串時(shí)是使用+號還是使用StringBuilder</a>
喜歡就給我點(diǎn)個(gè)贊唄帚桩!