From source to understand String, StringBuffer and StringBuuilder

1.String

(. 1) the class definition


public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence


Final String class is a class that implements the serialization interface, a read only compare the size of characters and serial port interfaces. String and eight other basic data types wrapper classes together for the immutable class.

(2) the main variables


private final char value [];

the bottom is substantially String class char type array, some of the basic method is the basic method of the String char array call.

(3) primary constructor

public String () {
  this.value = "" .Value;
}

default value String of "";

(. 4) String constant pool

jvm at startup will load a space string constant pool feature is the same and only a literal, if there are other same literal, jvm this literal reference is returned, if not the same literal, is created in the literal string constant pool and returns it references. Obtained by using a new keyword objects on the heap, but not loaded into the string constant pool, Intern () method enables a positioned stack string dynamically added to the string constant pool during operation.

(5) splicing string

String string concatenation by + sign is generally implemented, there are normally two forms:

String A = "ab &" + "CD";


At this point the JVM at compile time it considers the plus sign is useless, when he compiled directly into abcd, even if efficiency is to add more strings behind will not be slower than StringBuiler or StringBuffer.

A = String "ab &";
String B = "CD";
String C = A + B;

In this case the strings together into a system by optimizing new StringBuiler, twice append operation, which is performed in the heap, if multiple splicing results in several StringBuiler objects, efficiency will naturally be lower, but if it is done the splicing operation in the same line of code, just a StringBuiler additional new target, efficiency does not slow.

2.StringBuilder

(. 1) the class definition

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


AbstractStringBuilder the implements the Appendable class abstract, CharSequence

the StringBuilder is the final category, implements the serialization interface, a character read-only serial port, a read only parent class implements interfaces character sequences and splice interface which is the variable type.

(2) the main variables

private final char value [];

underlying StringBuilder basic implementation and String are consistent

(3) primary constructor

public StringBuilder () {
  Super (16);
}

AbstractStringBuilder (int Capacity) {
  value = new new char [ capacity];
}

base class constructor StringBuiler string array is a capacity of 16

(4) string concatenation

public StringBuilder append(String str) {
    super.append(str);
    return this;
}


AbstractStringBuilder the append public (String STR) {
    IF (STR == null)
        return appendNull ();
    int len = str.length ();
    ensureCapacityInternal (COUNT + len);
    str.getChars (0, len, value, COUNT);
    COUNT = len +;
    return the this;
}

Private void ensureCapacityInternal (int the minimumCapacity) {
    // overflow-Conscious code
    IF (the minimumCapacity - value.length> 0) {
          value = Arrays.copyOf (value,
                newCapacity (the minimumCapacity));
    }
}

internal char array is achieved by the operation, if the capacity is exceeded, the expansion array by copying operation mode occurs.

(5) thread safety

  Parses the above code, count + = len; not atomic operation, if two threads access to this method, the AbstractStringBuilder the count is not is the same, so the two threads are at the location of the underlying char array count start append add, that thread the end result is certainly executed after append will go in front of a data overwrite, so StringBuilder in string concatenation operations are thread safe. Because String stitching is achieved by StringBuiler, so String character stitching is not thread-safe

3.StringBuffer

(1) the class definition

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

StringBuffer parent class implements the interface read-only character sequences and splice interfaces with a common parent StringBuilder AbstractStringBuilder, class is mutable class.

(2) primary variable
underlying StringBuilder StringBuffer basic implementation and consistent.

(3) Constructor

StringBuffer the constructor and StringBuilder are consistent.

(4) The string concatenation


public synchronized StringBuffer append(String str) {
    toStringCache = null;
    super.append(str);
    return this;
}

 

StringBuffer string concatenation is basically consistent with the internal StringBuilder, the main difference is in the method body by synchronized lock key.

(5) the security thread

  from the above code parsing, since the method of adding synchronize keywords, so the string concatenation operation is thread safe.

 

Note: When a formal interview, the basic answer is the difference between the three: String is immutable class, StringBuffer and StringBuilder is mutable class; String string concatenation in a number of low efficiency and thread-safe, highest StringBuilder efficiency, but thread-safe, StringBuffer efficiency both among the former, but the thread safe.

Guess you like

Origin www.cnblogs.com/hxlr/p/11417615.html