java.lang.*中StringBuffer 源代码详解

java.lang.*中StringBuffer 源代码详解

    • 核心方法
    • StringBuffer()构造方法
    • synchronized int length()
    • synchronized int capacity()
    • synchronized char charAt()
    • synchronized void setCharAt()
    • synchronized StringBuffer insert()
    • int indexOf()
    • int lastIndexOf()
    • synchronized String toString()
    • synchronized void writeObject()
    • void readObject()

核心方法

StringBuffer()构造方法[OK]
synchronized int length() 实际存在的字符长度[OK]
synchronized int capacity() 容量的长度[OK]
synchronized char charAt() 返回指定索引位置的字符[OK]
synchronized void setCharAt() 设置指定索引位置的字符[OK]
synchronized StringBuffer insert() 往指定索引位置插入对象
int indexOf()
int lastIndexOf()
synchronized String toString()
synchronized void writeObject()
void readObject()

StringBuffer()构造方法

public StringBuffer() {
    super(16);//默认容量大小为16
}

public StringBuffer(int capacity) {
    super(capacity);//指定容量大小
}

public StringBuffer(String str) {
    super(str.length() + 16);//默认初始化容量str.length + 16
    append(str);
}

public StringBuffer(CharSequence seq) {
    this(seq.length() + 16);//默认初始化容量str.length + 16
    append(seq);
}

synchronized int length()

public synchronized int length() {
    return count;//实际存在的字符长度
}

synchronized int capacity()

public synchronized int capacity() {
    return value.length;//容量的长度
}

synchronized char charAt()

/**
 * 返回指定索引位置的字符
 * @param index 索引位置
 */
public synchronized char charAt(int index) {
	//判断角标是否越界
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    return value[index];
}

synchronized void setCharAt()

/**
 * 设置某个索引位置的字符
 * @param index 索引位置
 * @param ch 替换的字符
 */
public synchronized void setCharAt(int index, char ch) {
	//判断角标是否越界
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    toStringCache = null;
    value[index] = ch;
}

synchronized StringBuffer insert()

public synchronized StringBuffer insert(int index, char[] str, int offset,
                                            int len)
{
    toStringCache = null;
    super.insert(index, str, offset, len);
    return this;
}

public synchronized StringBuffer insert(int offset, Object obj) {
    toStringCache = null;
    super.insert(offset, String.valueOf(obj));
    return this;
}

public synchronized StringBuffer insert(int offset, String str) {
    toStringCache = null;
    super.insert(offset, str);
    return this;
}

public synchronized StringBuffer insert(int offset, char[] str) {
    toStringCache = null;
    super.insert(offset, str);
    return this;
}

public StringBuffer insert(int dstOffset, CharSequence s) {
    // Note, synchronization achieved via invocations of other StringBuffer methods
    // after narrowing of s to specific type
    // Ditto for toStringCache clearing
    super.insert(dstOffset, s);
    return this;
}

public synchronized StringBuffer insert(int dstOffset, CharSequence s,
            int start, int end)
{
    toStringCache = null;
    super.insert(dstOffset, s, start, end);
    return this;
}

public  StringBuffer insert(int offset, boolean b) {
    // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
    // after conversion of b to String by super class method
    // Ditto for toStringCache clearing
    super.insert(offset, b);
    return this;
}

public synchronized StringBuffer insert(int offset, char c) {
    toStringCache = null;
    super.insert(offset, c);
    return this;
}

public StringBuffer insert(int offset, int i) {
    // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
    // after conversion of i to String by super class method
    // Ditto for toStringCache clearing
    super.insert(offset, i);
    return this;
}

public StringBuffer insert(int offset, long l) {
    // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
    // after conversion of l to String by super class method
    // Ditto for toStringCache clearing
    super.insert(offset, l);
    return this;
}

public StringBuffer insert(int offset, float f) {
    // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
    // after conversion of f to String by super class method
    // Ditto for toStringCache clearing
    super.insert(offset, f);
    return this;
}

public StringBuffer insert(int offset, double d) {
    // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
    // after conversion of d to String by super class method
    // Ditto for toStringCache clearing
    super.insert(offset, d);
    return this;
}

int indexOf()

int lastIndexOf()

synchronized String toString()

synchronized void writeObject()

void readObject()

你可能感兴趣的:(源码解读,java)