本帖最后由 王得新 于 2012-10-5 20:06 编辑
刚看了下源代码:
//StringBuffer
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
//下面是这个类中的部分方法,看了下,基本上每个方法上都有同步(synchronized,线程安全的) 关键字
public synchronized int length() {
return count;
}
public synchronized int capacity() {
return value.length;
}
public synchronized void ensureCapacity(int minimumCapacity) {
if (minimumCapacity > value.length) {
expandCapacity(minimumCapacity);
}
}
public synchronized StringBuffer append(Object obj) {
super.append(String.valueOf(obj));
return this;
}
}
//StringBuilder类,
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
//下面是这个类中的部分方法,方法都没有synchronized 关键字
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
}
}
|