Java study notes (a) String, StringBuilder, StringBuffer

Principle and the JVM string of storage may refer to the article: https: //www.cnblogs.com/goody9807/p/6516374.html (very clear, explained from the perspective of JVM memory)

A, String

     At compile time period, i.e., formation of class file constants double quotes string will be interpreted as defined in the class file constant table. When loaded into the JVM class loader, class object will begin dispensing memory, often scale method will be placed in the area, while the character string corresponding to each scale will often detention (forever) objects created in the heap immortalized generations , the value of the object is often gauge string.

     String value is immutable, it is a constant. When creating a string, first determines whether a string constant pool (detention store heap object), if not, create detention objects (stored constant value), created in the heap while the object is stored detention object values.

    PS: We can say that the detention of the above-mentioned objects considered to be in a string constant pool.

     String constant pool is in the stack (the new generation into the old time, immortalized generation) immortalized generation (long dwell area) on.

Example:

1、String s = new String(“xyz”); //创建了几个对象

    Create one or two, if the constant pool with "xyz", then create an object in the heap that XYZ value is, at this time an object is created; if not XYZ constant pool, you need to create, At the same time also create a heap, and at that point two objects.

    This is only created during operation.

2、String s = "a"+"b"+"c"+"d"; //创建了几个对象

   Or create a 7, and the JVM itself.

   StringBuffer such as JVM will be handled in its eyes is String s = "abcd";

   This is the period during compilation has been created.

3, String s = "abc"; // create several objects

    Creating a 0 or a string constant pool will be created if there is no abc, then abc s directly to the memory address in the constant pool. If any, directly to the constant pool of memory addresses. S will not give the heap memory block allocated space, which is the new String is not the same place.

4、String a = "a";

      String b = "b";

      String c = a + b + "c"; // create several objects

    When the class loader to allocate memory, a string constant pool created "a" "b", and a, b points to the memory address corresponding to the string constant pool. Since c is a reference variable, running period, the string constant pool to create a "c", then use StringBuffer to be spliced.

    It is equivalent to:

       String c = new StringBuffer(a).append(b).append(“c”).toString();

Suggest:

 1) When constructing normal String, third more efficient than a first embodiment;

 2) String frequently changed during operation, it is not recommended to use string object creates a new object useless if a string String save frequently modified, each time the string changes, these useless objects is garbage collected, will affect the performance of the procedure, not recommended.

二、StringBuffer

   It is thread safe variable sequence of characters, a String of similar character buffer. String buffer may be safely used in a plurality of threads. It may be necessary when these methods are synchronized so that all operations on any specific example is if the serial order of occurrence, and the method sequentially calls directed to each thread sequentially consistent.

三、StringBuilder

    StringBuffer usage and about the same, the difference in its non-thread-safe.

Fourth, the difference between the three

1、String 和 StringBuffer

     Fact, the main difference between the performance of type String and StringBuffer String type is that immutable objects, so when each change of type String are in fact equivalent to generate a new String object, then the pointer to the new String object, I often change the contents of the string is best not to use string, because every generation objects will have an impact on system performance, especially when the memory is no more after the reference object, JVM GC will begin the work, that speed is certainly quite slow.

   Exceptions, however, the speed and StringBuffer 3 as in the example a.

    In one example of 4, String c = a + b StringBuffer use and the like, but from the point of view the entire example 4, a plurality of objects generated string constants useless. Overall no StringBuffer easy to use;

2、StringBuffer 和 StringBuilder

   StringBuffer is thread-safe 
   StringBuilder (added after the 5.0 version of the class, it is a simple replacement of StringBuffer) non-thread-safe, but the efficiency would be better, do recommend the use of such a large amount of accumulated string in single-threaded environment

Java provides String, StringBuffer and StringBuilder class to encapsulate the string, and provides a series of a method of operating a string object.

 

 

They are used to package the same point string; CharSequence interfaces are implemented. The difference between them is as follows :( following sources: https: //cloud.tencent.com/developer/article/1414756)

A variable and invariable

After a String class is immutable class, i.e. create a String object that strings are immutable until the object is destroyed. StringBuffer and StringBuilder are inherited from AbstractStringBuilder class, but also the use of an array of characters stored in the string AbstractStringBuilder, the variable type.

Since the variable type is String, adapted to be shared in the case of using, when a modified string is often preferred to use StringBuffer implemented. If you save a string is often modified with String, creates a new object each time the useless string modifications, these useless objects are garbage collected, will affect the performance of the procedure, not recommended.

Second, the initialization method

When you create a String object, you can use the constructor String str = new String ( "Java") approach to initialize, you can also String s = "Java" by way of direct assignment to initialize. Initializing only use StringBuffer constructor StringBuffer sb = new StringBuffer ( "hello") manner.

Third, the string modifications

String String is to first create a modified method StringBuffer, followed by the append call StringBuffer method, the final call StringBuffer toString () method returns the results, the following sample code:

String str = "hello";
str += "java";

The above code is equivalent to the following code:

StringBuffer sb = new StringBuffer(str); sb.append("java"); str = sb.toString();

String string above modification process than StringBuffer some more additional operations, will increase the number of temporary objects, resulting in reduced efficiency of the implementation of the program. StringBuffer and StringBuilder modify the string in terms of higher performance than a String.

Fourth, whether to implement the equals and hashCode methods

String implements equals () method and hashCode () method, new String ( "java") result equals (new String ( "java")) is true.;

The StringBuffer does not implement equals () method and hashCode () method, therefore, new StringBuffer ( "java") . The results equals (new StringBuffer ( "java" )) is false, the StringBuffer object is stored into the Java collection classes will appear problem.

Fifth, whether thread safety

StringBuffer and StringBuilder have provided a series of insert, append, the sequence of characters in a string of changes, their use is basically the same, but StringBuilder is not thread-safe, StringBuffer is thread safe. If you just use a single thread in a string buffer, the StringBuilder efficiency will be higher, but when multi-threaded access, it is best to use StringBuffer.

To sum up, in terms of efficiency, StringBuilder highest, StringBuffer followed, String minimum, in this case, in general, to be operated if the number is relatively small, should give priority to the use of the String class; if the operation is a lot of data in a single thread should be preferentially used StringBuilder class; if it is operating in a multithreaded large amounts of data, using the StringBuilder class priority.

 

Guess you like

Origin www.cnblogs.com/sandyflower/p/12152570.html