StringBuilder源码浅析(基于jdk1.8.0_231)
StringBuilder 简介
- StringBuilder是可变的字符长类,在单线程中基本等价于StringBuffer,但相比较于StringBuffer,StringBuilder是线程不安全的;
- StringBuilder 同StringBuffer一样,也直接继承自AbstractStringBuilder,二者的扩容机制都是使用父类实现的扩容机制,扩容新容量= (原容量<<1) +2;
StringBuild UML
StringBuild API
所有字段
-----继承自AbstractStringBuilder--------------
char[] value;
int count;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8
---------------StringBuilder自定义的字段--------
static final long serialVersionUID = 4383685877147921099L;
这里StringBuilder和StringBuffer有些区别,StringBuilder类中并没有重新定义一个数组,指向value数组的最后一次修改;
所有API
- 构造函数
public StringBuilder()
public StringBuilder(int capacity)
public StringBuilder(String str)
public StringBuilder(CharSequence seq)
- 其他所有函数
-----------------append方法---------------------------
public StringBuilder append(Object obj)
public StringBuilder append(String str)
public StringBuilder append(StringBuffer sb)
public StringBuilder append(CharSequence s)
public StringBuilder append(CharSequence s, int start, int end)
public StringBuilder append(char[] str)
public StringBuilder append(char[] str, int offset, int len)
public StringBuilder append(boolean b)
public StringBuilder append(char c)
public StringBuilder append(int i)
public StringBuilder append(long lng)
public StringBuilder append(float f)
public StringBuilder append(double d)
public StringBuilder appendCodePoint(int codePoint)
-------------------删除 替代 -------------------------------
public StringBuilder delete(int start, int end)
public StringBuilder deleteCharAt(int index)
public StringBuilder replace(int start, int end, String str)
--------------------insert方法------------------------
public StringBuilder insert(int index, char[] str, int offset, int len)
public StringBuilder insert(int offset, Object obj)
public StringBuilder insert(int offset, String str)
public StringBuilder insert(int offset, char[] str)
public StringBuilder insert(int dstOffset, CharSequence s)
public StringBuilder insert(int dstOffset, CharSequence s,int start, int end)
public StringBuilder insert(int offset, boolean b)
public StringBuilder insert(int offset, char c)
public StringBuilder insert(int offset, int i)
public StringBuilder insert(int offset, long l)
public StringBuilder insert(int offset, float f)
public StringBuilder insert(int offset, double d)
------------------查改方法--------------------------------
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
---------------------------------------------------------
public StringBuilder reverse() //逆序
public String toString()
private void writeObject(java.io.ObjectOutputStream s)
private void readObject(java.io.ObjectInputStream s)
- 直接继承或来及接口default方法
---------------直接从AbstractStringBuilder继承来的方法---------------
public int length()
public int capacity()
public void ensureCapacity(int minimumCapacity)
public void trimToSize()
public char charAt(int index)
public void setLength(int newLength)
public void setCharAt(int index, char ch)
public int codePointAt(int index)
public int codePointBefore(int index)
public int codePointCount(int beginIndex, int endIndex)
public default IntStream chars()
public default IntStream codePoints()
StringBuild 源码浅析
package java.lang;
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
static final long serialVersionUID = 4383685877147921099L;
public StringBuilder() {
super(16);
}
public StringBuilder(int capacity) {
super(capacity);
}
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
@Override
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}
public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}
@Override
public StringBuilder append(CharSequence s) {
super.append(s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder append(CharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}
@Override
public StringBuilder append(char[] str) {
super.append(str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder append(char[] str, int offset, int len) {
super.append(str, offset, len);
return this;
}
@Override
public StringBuilder append(boolean b) {
super.append(b);
return this;
}
@Override
public StringBuilder append(char c) {
super.append(c);
return this;
}
@Override
public StringBuilder append(int i) {
super.append(i);
return this;
}
@Override
public StringBuilder append(long lng) {
super.append(lng);
return this;
}
@Override
public StringBuilder append(float f) {
super.append(f);
return this;
}
@Override
public StringBuilder append(double d) {
super.append(d);
return this;
}
/**
* @since 1.5
*/
@Override
public StringBuilder appendCodePoint(int codePoint) {
super.appendCodePoint(codePoint);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int index, char[] str, int offset,
int len)
{
super.insert(index, str, offset, len);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, Object obj) {
super.insert(offset, obj);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, String str) {
super.insert(offset, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, char[] str) {
super.insert(offset, str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int dstOffset, CharSequence s) {
super.insert(dstOffset, s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int dstOffset, CharSequence s,
int start, int end)
{
super.insert(dstOffset, s, start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, boolean b) {
super.insert(offset, b);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, char c) {
super.insert(offset, c);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, int i) {
super.insert(offset, i);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, long l) {
super.insert(offset, l);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, float f) {
super.insert(offset, f);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
public StringBuilder insert(int offset, double d) {
super.insert(offset, d);
return this;
}
@Override
public int indexOf(String str) {
return super.indexOf(str);
}
@Override
public int indexOf(String str, int fromIndex) {
return super.indexOf(str, fromIndex);
}
@Override
public int lastIndexOf(String str) {
return super.lastIndexOf(str);
}
@Override
public int lastIndexOf(String str, int fromIndex) {
return super.lastIndexOf(str, fromIndex);
}
@Override
public StringBuilder reverse() {
super.reverse();
return this;
}
@Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
s.defaultWriteObject();
s.writeInt(count);
s.writeObject(value);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
count = s.readInt();
value = (char[]) s.readObject();
}
}
StringBuilder示例
单线程中与StringBuffer效率的测试(基于core i7-9750H cpu@2.60GHz 2.59GHz GTX1660Ti)
//一次性插入1千万个int型数字花费的时间比较
package commonclass;
import java.util.Timer;
public class StringBuilderTest {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
StringBuffer stringBuffer = new StringBuffer();
long startTime1 , endTime1,startTime2,endTime2;
startTime1 = System.currentTimeMillis();
for (int i = 0; i<= 10000000; i++){
stringBuilder.append(i);
}
endTime1 = System.currentTimeMillis();
System.out.println("StringBuilder用时: " + (endTime1 -startTime1));
startTime2 = System.currentTimeMillis();
for (int i = 0; i<= 10000000; i++){
stringBuffer.append(i);
}
endTime2 = System.currentTimeMillis();
System.out.println("StringBuffer用时: " + (endTime2 -startTime2));
}
}
- 结果
StringBuilder用时: 300
StringBuffer用时: 414
可以看出单线程中,StringBuild效率更好;
面试session
- StringBuilder StringBuffer 与 String 的异同与使用场景?
StringBuilder StringBuffer String 都用于字符串类,StringBuilder StringBuffer是可变的字符串类,而String是不可变的字符串类;
StringBuffer 是线程安全的,而StringBuilder是现场不安全的。单线程中,若存在对一个字符串大量的修改的,优先使用StringBuilder类,多线程中,使用StringBuffer.