なぜJavaのStringBuilderのはCharSequence引数のコンストラクタと文字列のために別のものを持っているのでしょうか?

AbdelRahmane:

文字列を実装CharSequenceインタフェースことを知って、なぜStringBuilderのはCharSequence引数のコンストラクタと文字列のために別のものを持っているのでしょうか?javadocツールで表示なし!

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {...}
public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence
{

...

    /**
     * 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);
    }
...
}
ジョージ・ザビエル:

最適化。私は間違っていないよ場合は、追記の2つの実装があります。append(String)より効率的であるappend(CharSequence)ところCharSequenceの文字列です。私はいくつかの余分なルーチンをしなければならなかった場合は確認してくださいするにはCharSequence、文字列と互換性のある文字列に変換し、直接のappend(String)をより長くなりアペンド(String)を、実行します。同じ結果。異なるスピード。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=122331&siteId=1
おすすめ