Because when looking at the source code of StringBuffer and StringBuilder, it was found that both inherited the AbstractStringBuilder, and many methods are methods of the parent class AbstractStringBuilder directly, so I decided to look at the source code of AbstractStringBuilder first, and then look at StringBuffer and StringBuilder.
Location: in java.lang package
Statement: abstract class AbstractStringBuilderimplements Appendable, CharSequence
The AbstractStringBuilder class has abstract modification, which shows that it cannot be instantiated.
The AbstractStringBuilder class has two subclasses: StringBuilder and StringBuffer.
Fields
/** * The value is used for character storage. */ char value[]; /** * The count is the number of characters used. */ int count;
Constructor
1. Parameterless constructor
AbstractStringBuilder() { }2. When creating an object of the abstractstringbuilder implementation class, specify the buffer size to capacity.
AbstractStringBuilder(int capacity) { value = new char[capacity]; }When the subclass StringBuilder or StringBuffer is instantiated, this constructor is called in the constructor.
Expand capacity
void expandCapacity(int minimumCapacity)
This method has package access permissions, and multiple methods in the class will call this method to expand capacity when the capacity is insufficient.
Source code:
void expandCapacity(int minimumCapacity) { int newCapacity = (value.length + 1) * 2; if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; } else if (minimumCapacity > newCapacity) { newCapacity = minimumCapacity; } value = Arrays.copyOf(value, newCapacity); }Add the buffer length by 1 by 2 to the variable newCapacity, then compare this value with the specified value, and determine the larger value as the new capacity of the buffer; then call the copyof method of the Arrays class, which creates a new array and then copy all the characters in the original array into the new array.
ensureCapacity(int minimumCapacity)
public void ensureCapacity(int minimumCapacity)
Make sure that the capacity is at least equal to the specified minimum value. If the current capacity is less than the specified value, a new array is created, and the capacity of the new array is twice the specified value plus 2; if the current capacity is not less than the specified value, no processing is done directly.
Source code:
public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > value.length) { expandCapacity(minimumCapacity); } }test:
StringBuffer s = new StringBuffer(); System.out.println("Capacity: " + s.capacity());// Capacity: 16 s.ensureCapacity(10); System.out.println("Capacity: " + s.capacity());// Capacity: 16 s.ensureCapacity(30); System.out.println("Capacity: " + s.capacity());// Capacity: 34 s.ensureCapacity(80); System.out.println("Capacity: " + s.capacity());// Capacity: 80method
The codePointAt method is implemented using Character.codePointAtImpl(value, index, count)
public int codePointAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return Character.codePointAtImpl(value, index, count); }The getChars method is implemented using the System.arraycopy() method
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { if (srcBegin < 0) throw new StringIndexOutOfBoundsException(srcBegin); if ((srcEnd < 0) || (srcEnd > count)) throw new StringIndexOutOfBoundsException(srcEnd); if (srcBegin > srcEnd) throw new StringIndexOutOfBoundsException("srcBegin > srcEnd"); System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); }The append method involves the ensureCapacityInternal() method and the getChars() method to implement it
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; }Arrays.copyOf() is used to implement
void expandCapacity(int minimumCapacity) { int newCapacity = value.length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); }Arrays.fill(value, count, newLength, '/0'); Copy between strings
public void setLength(int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException(newLength); ensureCapacityInternal(newLength); if (count < newLength) { Arrays.fill(value, count, newLength, '/0'); } count = newLength; }delete() only changes the size of the string and does not really delete the string
public AbstractStringBuilder delete(int start, int end) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (end > count) end = count; if (start > end) throw new StringIndexOutOfBoundsException(); int len = end - start; if (len > 0) { System.arraycopy(value, start+len, value, start, count-end); count -= len; } return this; }Learn to flexibly use the System.arraycopy() method
public AbstractStringBuilder insert(int index, char[] str, int offset, int len) { if ((index < 0) || (index > length())) throw new StringIndexOutOfBoundsException(index); if ((offset < 0) || (len < 0) || (offset > str.length - len)) throw new StringIndexOutOfBoundsException( "offset " + offset + ", len " + len + ", str.length " + str.length); ensureCapacityInternal(count + len); System.arraycopy(value, index, value, index + len, count - index); System.arraycopy(str, offset, value, index, len); count += len; return this; }Summarize
The above is all the content of this article about the detailed interpretation of the source code of the AbstractStringBuilder class source code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!