JDK source series (6) -StringBuilder

I. Overview

StringBuilder is a mutable string sequence, this class is designed to be compatible StringBuffer class API, but does not guarantee the thread safety, is an alternative under the circumstances StringBuffer single-threaded implementation. Where possible, it is recommended, preferably used in the code, because it's faster.

Second, the common method

The main method is to append and insert methods, there are many overloaded, so as to receive any type of data. Is added from a tail, a is inserted from any position.

Like, at the StringBuilder insufficient capacity, the method calls the parent class expansion for expansion the formula is: 2 and 2 minCapacity doubly larger of the reference capacity to take new original capacity.

Class is defined as follows:

public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence

Constructor as default capacity of 16, if you pass a string constructed when that capacity is +16 for the length of the string.

append method and insert methods do not add synchronized keyword synchronized.

Also delete, replace, reverse, toString methods.

Third, the serialization method

    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();
    }

Fourth, the relatively Stringbuffer and StringBuilder toString method

StringBuffer的:

    public synchronized String toString() {
        if (toStringCache == null) {
            toStringCache = Arrays.copyOfRange(value, 0, count);
        }
        return new String(toStringCache, true);
    }

To a method for using protected String, in the same package, can be used:

     /*
    * Package private constructor which shares value array for speed.
    * this constructor is always expected to be called with share==true.
    * a separate constructor is needed because we already have a public
    * String(char[]) constructor that makes a copy of the given char[].
    */
    String(char[] value, boolean share) {
        // assert share : "unshared not supported";
        this.value = value;
    }

We can see from the code, the method and String (char [] value) there are two differences, the first one, which one more argument: boolean share, in fact, the fundamental parameter method is not used in the body, but also to the comments, it does not currently support the use of false, only the true. So it can be concluded, adding this share is only in order to distinguish String (char [] value) method, without this there is no way to define the parameters of this function, only the parameters can not be reloaded. Then, the second difference is that the specific methods to achieve different. As we mentioned earlier, String (char [] value) method will be used when creating the String method will be used copyOf Arrays will copy the contents of value in them one by one to a String and this String (char [] value, boolean share) method is a direct reference to the value assigned to a String value.

The general practice is as follows:

this.value = Arrays.copyOf(value, value.length);

The advantage is that a good performance, shared memory array of internal savings. Moreover, this method also calls on the outside can not, will not damage the immutable characteristics of a String.

StringBuilder的:

    public String toString() {
        //Create a copy, don't share the array
        return new String(value, 0, count);
    }

 

Guess you like

Origin blog.csdn.net/m0_37609579/article/details/103500772