源碼級StringBuffer和StringBuilder以及String區(qū)別
作為一個面試常問的問題:
首先看一下源碼:
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
這是源碼從這里可以看出兩者都可序列化柄错,CharSequence是一個字符序列,從這里我們
看不出來區(qū)別
看下面的代碼可以看出:
1茵瘾、StringBuffe方法前都加了synchronized所以第一個區(qū)別就是StringBuffer是線程安全的饵婆,適合多線程環(huán)境勺馆。
StringBuffer源碼
@Override
public synchronized int length() {
return count;
}
@Override
public synchronized int capacity() {
return value.length;
}
@Override
public synchronized void ensureCapacity(int minimumCapacity) {
super.ensureCapacity(minimumCapacity);
}
/**
* @since 1.5
*/
@Override
public synchronized void trimToSize() {
super.trimToSize();
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
@Override
public synchronized void setLength(int newLength) {
toStringCache = null;
super.setLength(newLength);
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
@Override
public synchronized char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
StringBuilder的源碼
@Override
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}
/**
* Appends the specified {@code StringBuffer} to this sequence.
* <p>
* The characters of the {@code StringBuffer} argument are appended,
* in order, to this sequence, increasing the
* length of this sequence by the length of the argument.
* If {@code sb} is {@code null}, then the four characters
* {@code "null"} are appended to this sequence.
* <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 sb}.
*
* @param sb the {@code StringBuffer} to append.
* @return a reference to this object.
*/
public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}
@Override
public StringBuilder append(CharSequence s) {
super.append(s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder append(CharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}
2、
StringBuilder的包:
package java.lang;
StringBuffer的包:
package java.lang;
import java.util.Arrays;
從這句話可以看出:兩個類都是屬于lang下的侨核,但是StringBuffer中導(dǎo)入了Arrays的
StringBuffer中多出有一個這樣的屬性
/**
* A cache of the last value returned by toString. Cleared
* whenever the StringBuffer is modified.
*/
private transient char[] toStringCache;
解釋一下這個代碼的意思:
注釋的意思是:toString返回的最后一個值的緩沖草穆,當(dāng)StringBuffer被修改時清除
transient修飾的變量值不參與序列化。
這個不重要搓译,既然看到這兒了悲柱,我們在看一下String的特殊之處:
來,上源碼:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
精華全在上邊了:
1些己、首先多實現(xiàn)了Comparable接口诗祸,沒有實現(xiàn)AbstractStringBuilder
2跑芳、低層value數(shù)組不可變,那我們看一下StringBuffer和StringBuilder
abstract class AbstractStringBuilder implements Appendable, CharSequence {
/**
* The value is used for character storage.
*/
char[] value;
這是另一個區(qū)別是String是不可變的直颅。