Java中String、StringBuilder、StringBuffer常用源碼分析及比較(三):String、StringBuilder、StringBuffer比較

來源:互聯網
上載者:User

標籤:seq   ini   方法   span   exp   細節   empty   getc   from   

看這篇隨筆之前請務必先看前面兩章:

Java中String、StringBuilder、StringBuffer常用源碼分析及比較(一):String源碼分析

Java中String、StringBuilder、StringBuffer常用源碼分析及比較(二):StringBuilder、StringBuffer源碼分析

這裡講解了這三個類型的源碼,也或多或少的講解了他們的不同。

下面進入正題:

相同點:

一、成員變數:

  1.通過之前對源碼的分析,可以知道他們都是通過char數組value來存字串的

  2.StringBuilder與StringBuffer都繼承自AbstractStringBuilder,且他們的構造方法基本都來自AbstractStringBuilder

  3.String、StringBuilder、StringBuffer都實現了CharSequence介面,,這個介面定義如下方法

/**     * Returns the length of this character sequence.  The length is the number     * of 16-bit <code>char</code>s in the sequence.</p>     *     * @return  the number of <code>char</code>s in this sequence     */    int length();    /**     * Returns the <code>char</code> value at the specified index.  An index ranges from zero     * to <tt>length() - 1</tt>.  The first <code>char</code> value of the sequence is at     * index zero, the next at index one, and so on, as for array     * indexing. </p>     *     * <p>If the <code>char</code> value specified by the index is a     * <a href="{@docRoot}/java/lang/Character.html#unicode">surrogate</a>, the surrogate     * value is returned.     *     * @param   index   the index of the <code>char</code> value to be returned     *     * @return  the specified <code>char</code> value     *     * @throws  IndexOutOfBoundsException     *          if the <tt>index</tt> argument is negative or not less than     *          <tt>length()</tt>     */    char charAt(int index);    /**     * Returns a new <code>CharSequence</code> that is a subsequence of this sequence.     * The subsequence starts with the <code>char</code> value at the specified index and     * ends with the <code>char</code> value at index <tt>end - 1</tt>.  The length     * (in <code>char</code>s) of the     * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt>     * then an empty sequence is returned. </p>     *     * @param   start   the start index, inclusive     * @param   end     the end index, exclusive     *     * @return  the specified subsequence     *     * @throws  IndexOutOfBoundsException     *          if <tt>start</tt> or <tt>end</tt> are negative,     *          if <tt>end</tt> is greater than <tt>length()</tt>,     *          or if <tt>start</tt> is greater than <tt>end</tt>     */    CharSequence subSequence(int start, int end);    /**     * Returns a string containing the characters in this sequence in the same     * order as this sequence.  The length of the string will be the length of     * this sequence. </p>     *     * @return  a string consisting of exactly this sequence of characters     */    public String toString();

二、成員方法:

  1.StringBuilder與StringBuffer中大多方法都來自於他們共同的父類AbstractStringBuilder,所實現的功能基本一模一樣。

 

不同點:

一、成員變數

  1.雖然String、StringBuilder、StringBuffer都是以char數組value來存字串,但是String中value是final的(即構造方法初始化後無法被修改),而其餘兩個卻沒有,所以再做拼接時,String需要不停的建立String對象,並用copy方法來實現拼接,而StringBuilder、StringBuffer則可在value容量足夠情況下在value後拼接字串;

二、成員方法

  1.在String拼接中,有兩個方法,‘+’與concat方法、實現的效果一樣,不同的是concat沒有進行參數的判空,若參數為null,就會報null 指標異常,其餘兩個都只有append方法拼接且該拼接不會建立對象(StringBuilder,StringBuffer);

  2.StringBuilder、StringBuffer一個安全執行緒、一個線程不安全,其實原因也就來自於StringBuffer中append方法使用了synchronized來實現不允許多線程同時訪問;

 

疑惑點:

在StringBuilder、StringBuffer中其實還有一個不同點,先分別給出AbstractStringBuilder、StringBuffer、StringBuilder有關這部分的源碼

AbstractStringBuilder:

// Documentation in subclasses because of synchro difference    public AbstractStringBuilder append(StringBuffer sb) {        if (sb == null)            return append("null");        int len = sb.length();        ensureCapacityInternal(count + len);        sb.getChars(0, len, value, count);        count += len;        return this;    }

StringBuffer:

/**     * Appends the specified <tt>StringBuffer</tt> to this sequence.     * <p>     * The characters of the <tt>StringBuffer</tt> argument are appended,     * in order, to the contents of this <tt>StringBuffer</tt>, increasing the     * length of this <tt>StringBuffer</tt> by the length of the argument.     * If <tt>sb</tt> is <tt>null</tt>, then the four characters     * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>.     * <p>     * Let <i>n</i> be the length of the old character sequence, the one     * contained in the <tt>StringBuffer</tt> just prior to execution of the     * <tt>append</tt> method. Then the character at index <i>k</i> in     * the new character sequence is equal to the character at index <i>k</i>     * in the old character sequence, if <i>k</i> is less than <i>n</i>;     * otherwise, it is equal to the character at index <i>k-n</i> in the     * argument <code>sb</code>.     * <p>     * This method synchronizes on <code>this</code> (the destination)     * object but does not synchronize on the source (<code>sb</code>).     *     * @param   sb   the <tt>StringBuffer</tt> to append.     * @return  a reference to this object.     * @since 1.4     */    public synchronized StringBuffer append(StringBuffer sb) {        super.append(sb);        return this;    }

StringBuilder:

// Appends the specified string builder to this sequence.    private StringBuilder append(StringBuilder sb) {        if (sb == null)            return append("null");        int len = sb.length();        int newcount = count + len;        if (newcount > value.length)            expandCapacity(newcount);        sb.getChars(0, len, value, count);        count = newcount;        return this;    }    /**     * Appends the specified <tt>StringBuffer</tt> to this sequence.     * <p>     * The characters of the <tt>StringBuffer</tt> argument are appended,     * in order, to this sequence, increasing the     * length of this sequence by the length of the argument.     * If <tt>sb</tt> is <tt>null</tt>, then the four characters     * <tt>"null"</tt> are appended to this sequence.     * <p>     * Let <i>n</i> be the length of this character sequence just prior to     * execution of the <tt>append</tt> method. Then the character at index     * <i>k</i> in the new character sequence is equal to the character at     * index <i>k</i> in the old character sequence, if <i>k</i> is less than     * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>     * in the argument <code>sb</code>.     *     * @param   sb   the <tt>StringBuffer</tt> to append.     * @return  a reference to this object.     */    public StringBuilder append(StringBuffer sb) {        super.append(sb);        return this;    }

可以看到在父類、StringBuffer都有append(StringBuffer sb)這個方法,但是卻沒有append(StringBuilder sb),只有StringBuilder有,而且還定義為private,我覺得可能這也跟StringBuilder、StringBuffer的執行緒安全性有關,但是想不到具體細節,留下一個疑慮點,希望知道的小夥伴能給一下答案,謝謝!

Java中String、StringBuilder、StringBuffer常用源碼分析及比較(三):String、StringBuilder、StringBuffer比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.