前言
2022/10/24
路漫漫其修远兮,吾将上下而求索
本文是根据jdk学习所做笔记
仅供学习交流使用,转载注明出处
推荐
JDK API 1.6 中文版
说明
以下内容是结合很多资料进行编写的
源码为jdk1.8的
斜体样式 为自己的思考
下划线为自己所画的重点
AbstractStringBuilder类
基本信息
package java.lang; abstract class AbstractStringBuilder implements Appendable, CharSequence { }
属性
/** * The value is used for character storage. */ char[] value; /** * The count is the number of characters used. */ int count;
构造器
/** * This no-arg constructor is necessary for serialization of subclasses. */ AbstractStringBuilder() { } /** * Creates an AbstractStringBuilder of the specified capacity. */ AbstractStringBuilder(int capacity) { value = new char[capacity]; }
方法
length
/** * Returns the length (character count). * * @return the length of the sequence of characters currently * represented by this object */ @Override public int length() { return count; }
capacity
/** * Returns the current capacity. The capacity is the amount of storage * available for newly inserted characters, beyond which an allocation * will occur. * * @return the current capacity */ public int capacity() { return value.length; }
ensureCapacityInternal
/** * This method has the same contract as ensureCapacity, but is * never synchronized. */ private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0)//value.length容量 expandCapacity(minimumCapacity); }
expandCapacity
默认扩容: int newCapacity = value.length * 2 + 2;
/** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. */ 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.copyOf实现扩容 }
Arrays
public static char[] copyOf(char[] original, int newLength) { char[] copy = new char[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
/* Params: src – the source array. srcPos – starting position in the source array. dest – the destination array. destPos – starting position in the destination data. length – the number of array elements to be copied. */ public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
append
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; }
insert
public AbstractStringBuilder insert(int offset, String str) { if ((offset < 0) || (offset > length())) throw new StringIndexOutOfBoundsException(offset); if (str == null) str = "null"; int len = str.length(); ensureCapacityInternal(count + len); System.arraycopy(value, offset, value, offset + len, count - offset); str.getChars(value, offset); count += len; return this; }
reverse
public AbstractStringBuilder reverse() { boolean hasSurrogates = false; int n = count - 1; for (int j = (n-1) >> 1; j >= 0; j--) { int k = n - j; char cj = value[j]; char ck = value[k]; value[j] = ck; value[k] = cj; if (Character.isSurrogate(cj) || Character.isSurrogate(ck)) { hasSurrogates = true; } } if (hasSurrogates) { reverseAllValidSurrogatePairs(); } return this; }
String
@Override public abstract String toString();//抽象的
总结
关键词:
- 初始容量为16的char数组
- 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。
- 默认情况下,扩容为原来的2倍+2,同时将原有的数组中的元素复制到新的数组中
- 线程安全
- append
- resverse
- toString
最后
开源=为爱发电