前言
月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)
央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)
泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)
月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容
希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
一.StringBuffer的作用:
直接看源码注释(我的翻译可能不太准,如果道友们有更棒的理解,可以留言或者私信)
/**
* 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.
* 1.线程安全的、可变的字符序列。字符串缓冲区类似于String,但可以修改。
* 在任何时候,它都包含一些特定的字符序列,但是可以通过某些方法调用来更改序列的长度和内容
* <p>
* 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.
* 2.字符串缓冲区可供多个线程安全使用。这些方法在必要时被同步,以便任何特定实例上的所有操作都表现得好像它们以某种串行顺序发生,
* 该顺序与所涉及的每个单独线程进行的方法调用的顺序一致
* <p>
* The principal operations on a {@code StringBuffer} are the
* {@code append} and {@code 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
* {@code append} method always adds these characters at the end
* of the buffer; the {@code insert} method adds the characters at
* a specified point.
* 3.StringBuffer上的主要操作是append和insert方法,它们被重载以接受任何类型的数据。
* 每个有效地将给定的数据转换为字符串,然后将该字符串的字符附加或插入到字符串缓冲区中。
* append方法总是在缓冲区的末尾添加这些字符; insert方法在指定点添加字符。
* <p>
* For example, if {@code z} refers to a string buffer object
* whose current contents are {@code "start"}, then
* the method call {@code z.append("le")} would cause the string
* buffer to contain {@code "startle"}, whereas
* {@code z.insert(4, "le")} would alter the string buffer to
* contain {@code "starlet"}.
* 4.例如,如果z引用当前内容为"start"的字符串缓冲区对象,则方法调用z.append("le")
* 将导致字符串缓冲区包含"startle",而 z.insert(4, "le")会将字符串缓冲区更改为包含 {@code "starlet"}。
* <p>
* In general, if sb refers to an instance of a {@code StringBuffer},
* then {@code sb.append(x)} has the same effect as
* {@code sb.insert(sb.length(), x)}.
* 5.一般来说,如果 sb 引用了一个StringBuffer的实例,
* 那么sb.append(x)和sb.insert(sb.length(), x)的效果是一样的。
* <p>
* 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.
* Note that while {@code StringBuffer} is designed to be safe to use
* concurrently from multiple threads, if the constructor or the
* {@code append} or {@code insert} operation is passed a source sequence
* that is shared across threads, the calling code must ensure
* that the operation has a consistent and unchanging view of the source
* sequence for the duration of the operation.
* This could be satisfied by the caller holding a lock during the
* operation's call, by using an immutable source sequence, or by not
* sharing the source sequence across threads.
* 6.每当发生涉及源序列的操作(例如从源序列追加或插入)时,此类仅在执行操作的字符串缓冲区上同步,
* 而不是在源上同步。请注意,虽然StringBuffer被设计为可以安全地从多个线程并发使用,
* 但如果构造函数或append或insert操作传递了一个跨线程共享的源序列,
* 则调用代码必须确保操作在操作期间具有一致且不变的源序列视图。
* 这可以通过调用者在操作调用期间持有锁、使用不可变源序列或不跨线程共享源序列来满足。
* <p>
* 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.
* 7.每个字符串缓冲区都有一个容量。只要字符串缓冲区中包含的字符序列的长度不超过容量,
* 就不需要分配新的内部缓冲区数组。如果内部缓冲区溢出,它会自动变大。
* <p>
* Unless otherwise noted, passing a {@code null} argument to a constructor
* or method in this class will cause a {@link NullPointerException} to be
* thrown.
* 8.除非另有说明,否则将null参数传递给此类中的构造函数或方法将导致抛出NullPointerException
* <p>
* As of release JDK 5, this class has been supplemented with an equivalent
* class designed for use by a single thread, {@link StringBuilder}. The
* {@code 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.
* 9从 JDK 5 版本开始,这个类已经补充了一个设计用于单线程的等效类,StringBuilder。
* StringBuilder类通常应该优先于这个类使用,因为它支持所有相同的操作,但速度更快,因为它不执行同步
*
* @author Arthur van Hoff
* @see java.lang.StringBuilder
* @see java.lang.String
* @since JDK1.0
*/
二.类图:
三.成员变量:
/**
* StringBuffer 的可序列化字段。
* @serialField value char[]
* The backing character array of this StringBuffer.
* 此 StringBuffer 的支持字符数组。
* @serialField count int
* The number of characters in this StringBuffer.
* 此 StringBuffer 中的字符数。
* @serialField shared boolean
* A flag indicating whether the backing array is shared.
* The value is ignored upon deserialization.
* 指示后备数组是否共享的标志。反序列化时忽略该值。
*/
private static final java.io.ObjectStreamField[] serialPersistentFields =
{
new java.io.ObjectStreamField("value", char[].class),
new java.io.ObjectStreamField("count", Integer.TYPE),
new java.io.ObjectStreamField("shared", Boolean.TYPE),
};
/**
* toString 返回的最后一个值的缓存。每当修改 StringBuffer 时清除
*/
private transient char[] toStringCache;
四.构造方法:
/**
* 构造一个没有字符的字符串缓冲区,初始容量为 16 个字符。
*/
public StringBuffer() {
super(16);
}
/**
* 构造一个字符串缓冲区,其中没有字符且具有指定的初始容量。
*/
public StringBuffer(int capacity) {
super(capacity);
}
/**
* 构造一个初始化为指定字符串内容的字符串缓冲区。字符串缓冲区的初始容量是16加上字符串参数的长度
*/
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}
/**
* 1.构造一个字符串缓冲区,其中包含与指定的CharSequence相同的字符。
* 字符串缓冲区的初始容量是16加上CharSequence参数的长度。
* 2.如果指定的CharSequence的长度小于或等于 0,则返回容量为16的空缓冲区
*/
public StringBuffer(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
五.内部方法:
length
@Override
public synchronized int length() {
return count;
}
capacity
@Override
public synchronized int capacity() {
return value.length;
}
@Override
public synchronized void ensureCapacity(int minimumCapacity) {
super.ensureCapacity(minimumCapacity);
}
trimToSize
@Override
public synchronized void trimToSize() {
super.trimToSize();
}
setLength
@Override
public synchronized void setLength(int newLength) {
toStringCache = null;
super.setLength(newLength);
}
charAt
public synchronized char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
codePoint
@Override
public synchronized int codePointBefore(int index) {
return super.codePointBefore(index);
}
@Override
public synchronized int codePointCount(int beginIndex, int endIndex) {
return super.codePointCount(beginIndex, endIndex);
}
@Override
public synchronized int offsetByCodePoints(int index, int codePointOffset) {
return super.offsetByCodePoints(index, codePointOffset);
}
getChars
public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
int dstBegin)
{
super.getChars(srcBegin, srcEnd, dst, dstBegin);
}
setCharAt
public synchronized void setCharAt(int index, char ch) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
toStringCache = null;
value[index] = ch;
}
append
@Override
public synchronized StringBuffer append(Object obj) {
toStringCache = null;
super.append(String.valueOf(obj));
return this;
}
@Override
public synchronized StringBuffer append(String str) {
toStringCache = null;
super.append(str);
return this;
}
/**
* 1. 将指定的StringBuffer附加到此序列。
* 2.StringBuffer参数的字符按顺序附加到此StringBuffer的内容中,将此StringBuffer的长度增加参数的长度。
* 如果 sb为 null,则将四个字符 "null"附加到此StringBuffer。
* 3.设n是旧字符序列的长度,即在append方法执行之前包含在StringBuffer中的字符序列。
* 那么新字符序列中索引k处的字符等于旧字符序列中索引k处的字符,如果k小于n;否则,
* 它等于参数 sb中索引k-n处的字符。
*4.此方法在目标对象this上同步,但不会在源 sb上同步
*/
public synchronized StringBuffer append(StringBuffer sb) {
toStringCache = null;
super.append(sb);
return this;
}
synchronized StringBuffer append(AbstractStringBuilder asb) {
toStringCache = null;
super.append(asb);
return this;
}
/**
* 1.将指定的 CharSequence附加到此序列。
* 2.CharSequence参数的字符按顺序附加,通过参数的长度增加此序列的长度。
* 3.此方法的结果与调用 this.append(s, 0, s.length()); 完全相同
* 4.此方法在目标对象this上同步,但不会在源 (s) 上同步。
*5.如果 s为 null,则附加四个字符 "null"。
*/
@Override
public synchronized StringBuffer append(CharSequence s) {
toStringCache = null;
super.append(s);
return this;
}
@Override
public synchronized StringBuffer append(CharSequence s, int start, int end)
{
toStringCache = null;
super.append(s, start, end);
return this;
}
@Override
public synchronized StringBuffer append(char[] str) {
toStringCache = null;
super.append(str);
return this;
}
@Override
public synchronized StringBuffer append(char[] str, int offset, int len) {
toStringCache = null;
super.append(str, offset, len);
return this;
}
@Override
public synchronized StringBuffer append(boolean b) {
toStringCache = null;
super.append(b);
return this;
}
@Override
public synchronized StringBuffer append(char c) {
toStringCache = null;
super.append(c);
return this;
}
@Override
public synchronized StringBuffer append(int i) {
toStringCache = null;
super.append(i);
return this;
}
/**
* @since 1.5
*/
@Override
public synchronized StringBuffer appendCodePoint(int codePoint) {
toStringCache = null;
super.appendCodePoint(codePoint);
return this;
}
@Override
public synchronized StringBuffer append(long lng) {
toStringCache = null;
super.append(lng);
return this;
}
@Override
public synchronized StringBuffer append(float f) {
toStringCache = null;
super.append(f);
return this;
}
@Override
public synchronized StringBuffer append(double d) {
toStringCache = null;
super.append(d);
return this;
}
delete
@Override
public synchronized StringBuffer delete(int start, int end) {
toStringCache = null;
super.delete(start, end);
return this;
}
@Override
public synchronized StringBuffer deleteCharAt(int index) {
toStringCache = null;
super.deleteCharAt(index);
return this;
}
replace
public synchronized StringBuffer replace(int start, int end, String str) {
toStringCache = null;
super.replace(start, end, str);
return this;
}
subString
@Override
public synchronized String substring(int start) {
return substring(start, count);
}
@Override
public synchronized CharSequence subSequence(int start, int end) {
return super.substring(start, end);
}
@Override
public synchronized String substring(int start, int end) {
return super.substring(start, end);
}
insert
@Override
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) {
//注意,在将 s 缩小到特定类型 Ditto 以清除 toStringCache 之后,通过调用其他 StringBuffer 方法实现同步
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) {
//注意,通过超类方法 Ditto 将 b 转换为 String 后调用 StringBuffer insert(int, String) 实现同步以清除 toStringCache
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) {
super.insert(offset, i);
return this;
}
public StringBuffer insert(int offset, long l) {
super.insert(offset, l);
return this;
}
public StringBuffer insert(int offset, float f) {
super.insert(offset, f);
return this;
}
public StringBuffer insert(int offset, double d) {
super.insert(offset, d);
return this;
}
indexOf
public int indexOf(String str) {
// Note, synchronization achieved via invocations of other StringBuffer methods
return super.indexOf(str);
}
public synchronized int indexOf(String str, int fromIndex) {
return super.indexOf(str, fromIndex);
}
public int lastIndexOf(String str) {
// Note, synchronization achieved via invocations of other StringBuffer methods
return lastIndexOf(str, count);
}
public synchronized int lastIndexOf(String str, int fromIndex) {
return super.lastIndexOf(str, fromIndex);
}
reverse
public synchronized StringBuffer reverse() {
toStringCache = null;
super.reverse();
return this;
}
toString
public synchronized String toString() {
if (toStringCache == null) {
toStringCache = Arrays.copyOfRange(value, 0, count);
}
return new String(toStringCache, true);
}
writeObject/readObject
/**
* 调用 readObject 以从流中恢复 StringBuffer 的状态。
*/
private synchronized void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
java.io.ObjectOutputStream.PutField fields = s.putFields();
fields.put("value", value);
fields.put("count", count);
fields.put("shared", false);
s.writeFields();
}
/**
* 调用 readObject 以从流中恢复 StringBuffer 的状态
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
java.io.ObjectInputStream.GetField fields = s.readFields();
value = (char[])fields.get("value", null);
count = fields.get("count", 0);
}
六.总结:
内部重要方法全是调用的父类.....