国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

??
1. StringBuilder
1. StringBuilder? ?? ?? ? ??
2.StringBuilder的subString()方法toString()方法
3.StringBuilder的其它方法
二、StringBuffer
三、 總結
? Java Java??? ??? ???? ??? ?? ???. (2) Java? ???? ?? ???? ??

??? ???? ??? ?? ???. (2) Java? ???? ?? ???? ??

Sep 19, 2020 am 09:36 AM
java ?

??? ???? ??? ?? ???. (2) Java? ???? ?? ???? ??

?? ?? ????: Java ?? ????

?? ???? String? ???? ?? ??? ?? ?? ??? ??????. ? ????? String? ??? ?? ? ???? StringBuilder ? StringBuffer? ????? ?????. ? ? ???? String? ??? ?????? ?? ?? ??? ?????? ???????.

??? ???? ??? ?? ???. (2) Java? ???? ?? ???? ??

???? StringBuilder? StringBuffer? ?? AbstractStringBuilder? ???? AbstractStringBuilder? String? ?? ????? CharSequence? ???? ?? ? ? ????.

??? ???? ??? ??? ????? ?? ?? ????. ???? ?? ??? char ??(jdk9 ?? ??? ?? ??)? ???? ?? ??? ????? ???? ??? ????? ??? ?? ???? ???. ??? ??? ??????. ?? ???? ??? String? ?? ??? final? ????? ??? ??? ? ??? ?? ?? ?? ?????. ??? String ?? ??, ??, ?? ? ?? ??? ?? ? ??? ??????? ?????. ?? ??? StringBuilder? StringBuffer? String?? ? ?????. ????, ? ? ?? ??? ?? ?????.

1. StringBuilder

StringBuilder? ?? ???? AbstractStringBuilder?? ?? ??? ? ? ????.

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;
}復制代碼

StringBuilder? String? ????? char ??? ???? ?????. ???? StringBuilder?? ?? ??? ??? ????. ???? ???????. ???? StringBuilder? ???? ?? ?? ??? ???????. ??? ??? ????.

 /**
     * Constructs a string builder with no characters in it and an
     * initial capacity of 16 characters.
     */
    public StringBuilder() {        super(16);
    }復制代碼

? ????? AbstractStringBuilder? ???? ?? ??? ??? ??? ?????.

    /**
     * Creates an AbstractStringBuilder of the specified capacity.
     */
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }復制代碼

AbstractStringBuilder? ?? ??? ????? ??? ??????. ?, StringBuilder? ????? ??? 16? char[] ??? ??????. ???? ?? ?? ??? StringBuilder? ?? ?? ??? ?????. ?? ??? ??? ????.

 /**
     * Constructs a string builder with no characters in it and an
     * initial capacity specified by the {@code capacity} argument.
     *
     * @param      capacity  the initial capacity.
     * @throws     NegativeArraySizeException  if the {@code capacity}
     *               argument is less than {@code 0}.
     */
    public StringBuilder(int capacity) {        super(capacity);
    }    /**
     * Constructs a string builder initialized to the contents of the
     * specified string. The initial capacity of the string builder is
     * {@code 16} plus the length of the string argument.
     *
     * @param   str   the initial contents of the buffer.
     */
    public StringBuilder(String str) {        super(str.length() + 16);
        append(str);
    }    /**
     * Constructs a string builder that contains the same characters
     * as the specified {@code CharSequence}. The initial capacity of
     * the string builder is {@code 16} plus the length of the
     * {@code CharSequence} argument.
     *
     * @param      seq   the sequence to copy.
     */
    public StringBuilder(CharSequence seq) {        this(seq.length() + 16);
        append(seq);
    }復制代碼

? ??? ? ?? ??? ??? ???? StringBuilder? ??????. ?? ? ?? ???? ?? String ? CharSequence? ???? StringBuilder? ???? ? ????. ? ? ?? ???? ??? 16?? ??? ??? ??? ?????.

1. StringBuilder? ?? ?? ? ??

?? ??? StringBuilder? ?? ???? ?? ???? ??? ??? ??? ? ??? ?? ?? ?? ?????. Append(String)? ?? ?? StringBuilder? Append? ?? ???? Append ???? ???? ?? ? ? ??. ??? Append?? ??? StringBuilder ???? ???? ???? ?? ?? ???? ?? ???? ?? ????. Append ???? ????? ??? ????.

    // StringBuilder
    @Override
    public StringBuilder append(String str) {        super.append(str);        return this;
    }    
  // AbstractStringBuilder
  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;
    }復制代碼

Append ???? ? ?? ???? ?? Null ??? ????, Null? ?? ?? AppendNull ???? ?????. ?? ??? ??? ????.

private AbstractStringBuilder appendNull() {        int c = count;
        ensureCapacityInternal(c + 4);        final char[] value = this.value;
        value[c++] = 'n';
        value[c++] = 'u';
        value[c++] = 'l';
        value[c++] = 'l';
        count = c;        return this;
    }復制代碼

appendNull ?????? ??? ?? ??? ??????? ???? ?? verifyCapacityInternal? ?? ?????. ???? verifyCapacityInternal ???? ??? ?????. ???? char[] ?? ?? "null" ??? ??? ?? ??? ? ????.

??? StringBuilder ?? ??? ?? ??? 16??? ??????. ??? ???? ???? ? ?? char[] ??? ??? ???? ???? ???. ??? char[] ??? ??? ???? ???? ?? AppendNull ???? Append ??? ???? verifyCapacityInternal ???? ?????. ??? ???? ??? ??? ?????.

private void ensureCapacityInternal(int minimumCapacity) {        // overflow-conscious code
        if (minimumCapacity - value.length > 0)
            expandCapacity(minimumCapacity);
    }復制代碼

??? ??? ???? ??? ??? ????. ??? ??? ??? ??? ???? ?? ExpandCapacity? ???? ??? ?????.

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);
    }復制代碼

expandCapacity? ??? ?? ?????. ?? ?? ??? ??? 2? ??? 2? ??? ??? ?? ??? ?????. ???? newCapacity? maximumCapacity?? ?? ?? newCapacity? maximumCapacity ?? ????? ????. ExpandCapacity ???? ???? ?? 2? ????? ???? ???? ?? ? ??? ?????.

?? ?? ??? ?? ??????. newCapacity? maximumCapacity? 0?? ?? ? ???? maximumCapacity? 0?? ??? OutOfMemoryError ??? ?????. ???? ??? ???? ??? 0?? ????. ??? ???? ??? ?? ?? ????? ?? ?? ??? 2? ??? ?? 1??? ???? ???? ?? ????. ???? ?? ??, ???? 8??? ?????. ?? ?? ??? ?? ?? ??? ?? ?????. ??? 0?? ??? 1???. ??? ???? ??? ? ?? ?? ??? [-128~127]?? ??? 127?? ?? ??? ?????. ?, ?? ?? ?? ??? ?? ???? 1? ?????. ???? ??? ?????. ?? byte? ??? int??? ??? ?????.

另外在這個方法的最后一句通過Arrays.copyOf進行了一個數(shù)組拷貝,其實Arrays.copyOf在上篇文章中就有見到過,在這里不妨來分析一下這個方法,看源碼:

 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;
    }復制代碼

咦?copyOf方法中竟然也去實例化了一個對象?。∧遣粫绊懶阅軉??莫慌,看一下這里僅僅是實例化了一個newLength長度的空數(shù)組,對于數(shù)組的初始化其實僅僅是指針的移動而已,浪費的性能可謂微乎其微。接著這里通過System.arraycopy的native方法將原數(shù)組復制到了新的數(shù)組中。

2.StringBuilder的subString()方法toString()方法

StringBuilder中其實沒有subString方法,subString的實現(xiàn)是在StringBuilder的父類AbstractStringBuilder中的。它的代碼非常簡單,源碼如下:

public String substring(int start, int end) {        if (start < 0)            throw new StringIndexOutOfBoundsException(start);        if (end > count)            throw new StringIndexOutOfBoundsException(end);        if (start > end)            throw new StringIndexOutOfBoundsException(end - start);        return new String(value, start, end - start);
    }復制代碼

在進行了合法判斷之后,substring直接實例化了一個String對象并返回。這里和String的subString實現(xiàn)其實并沒有多大差別。 而StringBuilder的toString方法的實現(xiàn)其實更簡單,源碼如下:

 @Override
    public String toString() {        // Create a copy, don&#39;t share the array
        return new String(value, 0, count);
    }復制代碼

這里直接實例化了一個String對象并將StringBuilder中的value傳入,我們來看下String(value, 0, count)這個構造方法:

    public String(char value[], int offset, int count) {        if (offset < 0) {            throw new StringIndexOutOfBoundsException(offset);
        }        if (count < 0) {            throw new StringIndexOutOfBoundsException(count);
        }        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {            throw new StringIndexOutOfBoundsException(offset + count);
        }        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }復制代碼

可以看到,在String的這個構造方法中又通過Arrays.copyOfRange方法進行了數(shù)組拷貝,Arrays.copyOfRange的源碼如下:

   public static char[] copyOfRange(char[] original, int from, int to) {        int newLength = to - from;        if (newLength < 0)            throw new IllegalArgumentException(from + " > " + to);        char[] copy = new char[newLength];
        System.arraycopy(original, from, copy, 0,
                         Math.min(original.length - from, newLength));        return copy;
    }復制代碼

Arrays.copyOfRange與Arrays.copyOf類似,內部都是重新實例化了一個char[]數(shù)組,所以String構造方法中的this.value與傳入進來的value不是同一個對象。意味著StringBuilder在每次調用toString的時候生成的String對象內部的char[]數(shù)組并不是同一個!這里立一個Falg!

3.StringBuilder的其它方法

StringBuilder除了提供了append方法、subString方法以及toString方法外還提供了還提供了插入(insert)、刪除(delete、deleteCharAt)、替換(replace)、查找(indexOf)以及反轉(reverse)等一些列的字符串操作的方法。但由于實現(xiàn)都非常簡單,這里就不再贅述了。

二、StringBuffer

在第一節(jié)已經(jīng)知道,StringBuilder的方法幾乎都是在它的父類AbstractStringBuilder中實現(xiàn)的。而StringBuffer同樣繼承了AbstractStringBuilder,這就意味著StringBuffer的功能其實跟StringBuilder并無太大差別。我們通過StringBuffer幾個方法來看

     /**
     * A cache of the last value returned by toString. Cleared
     * whenever the StringBuffer is modified.
     */
    private transient char[] toStringCache;    @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;        super.append(str);        return this;
    }    /**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     * @since      1.2
     */
    @Override
    public synchronized StringBuffer delete(int start, int end) {
        toStringCache = null;        super.delete(start, end);        return this;
    }  /**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     * @since      1.2
     */
    @Override
    public synchronized StringBuffer insert(int index, char[] str, int offset,                                            int len)
    {
        toStringCache = null;        super.insert(index, str, offset, len);        return this;
    }@Override
    public synchronized String substring(int start) {        return substring(start, count);
    }    
// ...復制代碼

可以看到在StringBuffer的方法上都加上了synchronized關鍵字,也就是說StringBuffer的所有操作都是線程安全的。所以,在多線程操作字符串的情況下應該首選StringBuffer。 另外,我們注意到在StringBuffer的方法中比StringBuilder多了一個toStringCache的成員變量 ,從源碼中看到toStringCache是一個char[]數(shù)組。它的注釋是這樣描述的:

toString返回的最后一個值的緩存,當StringBuffer被修改的時候該值都會被清除。

我們再觀察一下StringBuffer中的方法,發(fā)現(xiàn)只要是操作過操作過StringBuffer中char[]數(shù)組的方法,toStringCache都被置空了!而沒有操作過字符數(shù)組的方法則沒有對其做置空操作。另外,注釋中還提到了 toString方法,那我們不妨來看一看StringBuffer中的 toString,源碼如下:

   @Override
    public synchronized String toString() {        if (toStringCache == null) {
            toStringCache = Arrays.copyOfRange(value, 0, count);
        }        return new String(toStringCache, true);
    }復制代碼

這個方法中首先判斷當toStringCache 為null時會通過 Arrays.copyOfRange方法對其進行賦值,Arrays.copyOfRange方法上邊已經(jīng)分析過了,他會重新實例化一個char[]數(shù)組,并將原數(shù)組賦值到新數(shù)組中。這樣做有什么影響呢?細細思考一下不難發(fā)現(xiàn)在不修改StringBuffer的前提下,多次調用StringBuffer的toString方法,生成的String對象都共用了同一個字符數(shù)組--toStringCache。這里是StringBuffer和StringBuilder的一點區(qū)別。至于StringBuffer中為什么這么做其實并沒有很明確的原因,可以參考StackOverRun 《Why StringBuffer has a toStringCache while StringBuilder not?》中的一個回答:

1.因為StringBuffer已經(jīng)保證了線程安全,所以更容易實現(xiàn)緩存(StringBuilder線程不安全的情況下需要不斷同步toStringCache) 2.可能是歷史原因

三、 總結

本篇文章到此就結束了?!渡钊肜斫釰ava中的字符串》通過兩篇文章深入的分析了String、StringBuilder與StringBuffer三個字符串相關類。這塊內容其實非常簡單,只要花一點時間去讀一下源碼就很容易理解。當然,如果你沒看過此部分源碼相信這篇文章能夠幫助到你。不管怎樣,相信大家通過閱讀本文還是能有一些收獲。解了這些知識后可以幫助我們在開發(fā)中對字符串的選用做出更好的選擇。同時,這塊內容也是面試常客,相信大家讀完本文去應對面試官的問題也會綽綽有余。

?????? ?? ? ??? ?? ??? php training ??? ??????!

? ??? ??? ???? ??? ?? ???. (2) Java? ???? ?? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

??? ????
1601
29
PHP ????
1502
276
???
JDBC? Java? ??? ???? ??? ?????? JDBC? Java? ??? ???? ??? ?????? Aug 02, 2025 pm 12:29 PM

JDBC ????? ???? ????? ?? ?? ?? ??? ?? ?? ??? ?? ? ?? ??? ?? ?? ?? ??? ???????. 1. ????? ????? Conn.SetAutoCommit (False)?? ??????. 2. ??? ? ????? ?? ?? SQL ??? ?????. 3. ?? ??? ??? ?? Conn.commit ()?? ???? ??? ???? ???? ?? ??? ???? Conn.Rollback ()?? ??????. ???, ? ??? ???? ????, ??? ???? ????, ?? ??? ??? ?? ??? ??? ???? ? ???????. ?? ?? ?? ???? ????? ??? ???? ?? ?? ???? ???? ??? ????? ?? ??? ??? ? ?? ???? ?? ????.

Java? ??? ?? ??? ?????? Java? ??? ?? ??? ?????? Aug 02, 2025 am 02:38 AM

?? ?? ? ?? ???? ???? ?? Java.Time ???? ???? ??????. 2. LocalDate, LocalDateTime ? LocalTime? ?? ?? ??? ??? ?????. 3. () ???? ???? ?? ??? ??? ????. 4. ???/???? ??? ???? ??? ????? ??? ??????. 5. ZonedDateTime ? Zoneid? ???? ???? ??????. 6. DateTimeFormatter? ?? ?? ? ?? ?? ?? ???; 7. ??? ?? ?? ?? ??? ????? ?? ??????. ?? Java? ?? ??? ???? ??? ??? ???? Java.Timeapi ??? ?? ??? ???????.

Java ??? ?? ?? : Spring Boot vs Quarkus vs Micronaut Java ??? ?? ?? : Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-FormancetArtUptimeMoryUsage, Quarkusandmicronautleadduetocompile-timeprocessingandgraalvsupport, withquarkusoftenperforminglightbetterine serverless sinarios.2.thyvelopecosyste,

??? ??? Java?? ??? ?????? ??? ??? Java?? ??? ?????? Aug 02, 2025 pm 01:55 PM

Java 's Garbage Collection (GC)? ???? ???? ???? ??????, ?? ? ??? ??? ? ??? ??? ??? ??? ????. 1.GC? ?? ?? (? : ?? ??, ?? ???, ?? ?? ?)?? ??? ???? ????, ?? ? ??? ??? ???? ?????. 2. ?? ???? ????? ????, ?? ?? ??? ??? ???? ?? ??? ??????. 3. ?? ?? ?? ?? : ??? ?? (Eden, S0, S1)? ?? ????? ?????. ??? ??? ?? ? MajorGC? ???? ? ??? ? ????. Metaspace? ??? ?? ???? ?????. 4. JVM? ??? GC ??? ?????. SerialGC? ??? ?? ????? ?????. ParallelGC? ???? ??????. CMS? ?? ???

?? HTTP ???? ?? ??? ?????? ?? HTTP ???? ?? ??? ?????? Aug 03, 2025 am 11:35 AM

GO? HTTP ?? ????? ?? ??, ??, ????? IP ? ?? ??? ?? ? ? ????. 1. http.handlerfunc? ???? ????? ????, 2. ??? ???? ?? ?? ??? ?? ??? ??????. ?? ?? ??? ???? ??? ?????? ??? ????? ???? ? ?????. ?? ???? ?? ?? ??, JSON ?? ?? ? ?? ID ??? ?????.

??? ???? html` ?? '??? ????? ??? ???? html` ?? '??? ????? Aug 03, 2025 am 11:07 AM

??? htmlinput ??? ???? ??? ???? ????? ??? ??? ?? ??? ???? ???? ? ????. 1. ???, ???, ??, ?? ? ??? ?? ??? ??? ?? ?? ?? ??? ???? ???? ??? ? ???? ??? ? ????. 2. HTML5? ?????? ??? ? ?? ?? ??? ?? ? ??? URL, ??, ?? ? ??? ?? ??? ??? ??????. 3. ?? ?? ? ? ??? ??? ???? ?? ??? ???? ???? ?? ???? ?? ???? ???? ?? ? ? ??? ?? ???????.

Java ?? ?? ?? : Maven vs. Gradle Java ?? ?? ?? : Maven vs. Gradle Aug 03, 2025 pm 01:36 PM

GradleisBetTerChoiceFormostNewProjectSduetoitssuperiorflexible, Performance, and ModernToolingsupport.1.Gradle'Sgroovy/kotlindslismoreConcisENDEXPRESSIVETHANMAVEN'SVOSEXML.2.GradleOutsMaveninbuildweedweedweedweedweedweedweedweedweedweedweedweedweedweede

??? ????. ?? ??? ?? ??? ????. ?? ??? ?? Aug 02, 2025 am 06:26 AM

DEFER? ??? ???? ?? ??? ??? ???? ? ?????. ?? ??? ?? ? ? ?? ????, ??? ??? ? ?? ?? (LIFO)? ??? ?????. 1. ?? ??? ??? ? ??? ?????. 2. ?? ??? ?? ??? ??? ????? ?????. 3. ?? ? ?? ?? ??? ? ????. 4. ??? ?????? ??? ??? ???? ?????. 5. ?? ??? ???? ?? ??? ?? ??? ?????. ??? ??? ?? ?? ? ???? ???? ? ????.

See all articles