詳細(xì)解讀AbstractStringBuilder類源碼
因?yàn)榭碨tringBuffer 和 StringBuilder 的源碼時(shí)發(fā)現(xiàn)兩者都繼承了AbstractStringBuilder,并且很多方法都是直接super的父類AbstractStringBuilder的方法,所以還是決定先看AbstractStringBuilder的源碼,然后再看StringBuffer 和 StringBuilder.
位置:java.lang包中
聲明: abstract class AbstractStringBuilderimplements Appendable, CharSequence
AbstractStringBuilder 類有abstract 修飾,可知它不能被實(shí)例化。
AbstractStringBuilder 類有兩個(gè)子類:StringBuilder和StringBuffer。
字段
/** * The value is used for character storage. */ char value[]; /** * The count is the number of characters used. */ int count;
構(gòu)造器
1、無(wú)參構(gòu)造器
AbstractStringBuilder() { }
2、創(chuàng)建abstractstringbuilder實(shí)現(xiàn)類的對(duì)象時(shí)指定緩沖區(qū)大小為capacity。
AbstractStringBuilder(int capacity) { value = new char[capacity]; }
當(dāng)子類StringBuilder或StringBuffer實(shí)例化時(shí),會(huì)在構(gòu)造器中調(diào)用此構(gòu)造器。
擴(kuò)充容量
void expandCapacity(int minimumCapacity)
此方法有包訪問(wèn)權(quán)限,類中有多個(gè)方法會(huì)調(diào)用此方法,在容量不足時(shí)擴(kuò)充容量。
源碼:
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); }
將緩沖區(qū)長(zhǎng)度加1乘2的值賦予變量newCapacity, 然后將此值與指定的值比較,將較大值確定為緩沖區(qū)的新容量;然后調(diào)用Arrays類的copyof方法,此方法會(huì)創(chuàng)建一個(gè)新數(shù)組,然后將原數(shù)組中的字符全部復(fù)制進(jìn)新數(shù)組中。
ensureCapacity(int minimumCapacity)
public void ensureCapacity(int minimumCapacity)
確保容量至少等于指定的最小值。如果當(dāng)前容量小于指定值,則創(chuàng)建新數(shù)組,新數(shù)組的容量為指定值的兩倍加2;如果當(dāng)前容量不小于指定值,則直接不做處理。
源碼:
public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > value.length) { expandCapacity(minimumCapacity); } }
測(cè)試:
StringBuffer s = new StringBuffer(); System.out.println("容量:" + s.capacity());// 容量:16 s.ensureCapacity(10); System.out.println("容量:" + s.capacity());// 容量:16 s.ensureCapacity(30); System.out.println("容量:" + s.capacity());// 容量:34 s.ensureCapacity(80); System.out.println("容量:" + s.capacity());// 容量:80
方法
codePointAt方法中都是用Character.codePointAtImpl(value, index, count)來(lái)實(shí)現(xiàn)的
public int codePointAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return Character.codePointAtImpl(value, index, count); }
getChars方法的實(shí)現(xiàn)用的是System.arraycopy()方法
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); }
append方法都牽扯到了ensureCapacityInternal()方法和getChars()方法來(lái)實(shí)現(xiàn)
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()來(lái)實(shí)現(xiàn)
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');字符串之間的復(fù)制
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() 僅改變字符串的大小并未真正的刪除字符串
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; }
學(xué)會(huì)靈活的運(yùn)用System.arraycopy()方法
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; }
總結(jié)
以上就是本文關(guān)于源碼詳細(xì)解讀AbstractStringBuilder類源碼詳細(xì)解讀的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- 詳解.NET中string與StringBuilder在字符串拼接功能上的比較
- C#使用String和StringBuilder運(yùn)行速度測(cè)試及各自常用方法簡(jiǎn)介
- C#中String和StringBuilder的簡(jiǎn)介與區(qū)別
- JDK源碼分析之String、StringBuilder和StringBuffer
- 從源碼角度簡(jiǎn)單看StringBuilder和StringBuffer的異同(全面解析)
- Android中的SpannableString與SpannableStringBuilder詳解
- java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法
- java StringBuilder類的詳解及簡(jiǎn)單實(shí)例
- C#中String StringBuilder StringBuffer類的用法
- string與stringbuilder兩者的區(qū)別
相關(guān)文章
Java中將字符串String轉(zhuǎn)換為整數(shù)int的多種方法
在Java中將String類型轉(zhuǎn)換為int類型是一個(gè)常見(jiàn)的操作,下面這篇文章主要給大家介紹了關(guān)于Java中將字符串String轉(zhuǎn)換為整數(shù)int的多種方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07完美解決java讀取大文件內(nèi)存溢出的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決java讀取大文件內(nèi)存溢出的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08SpringMVC核心DispatcherServlet處理流程分步詳解
這篇文章主要介紹了SpringMVC核心之中央調(diào)度器DispatcherServlet的相關(guān)知識(shí),包括SpringMVC請(qǐng)求處理過(guò)程及SrpingMVC容器和spring IOC容器關(guān)系,需要的朋友可以參考下2023-04-04原因分析IDEA導(dǎo)入Spring-kafka項(xiàng)目Gradle編譯失敗
這篇文章主要為大家介紹分析了IDEA導(dǎo)入Spring-kafka項(xiàng)目Gradle中編譯失敗原因及解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Javabean轉(zhuǎn)換成json字符并首字母大寫代碼實(shí)例
這篇文章主要介紹了javabean轉(zhuǎn)成json字符并首字母大寫代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Java PriorityQueue數(shù)據(jù)結(jié)構(gòu)接口原理及用法
這篇文章主要介紹了Java PriorityQueue數(shù)據(jù)結(jié)構(gòu)接口原理及用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10