String, StringBuffer, and StringBuilder in Java

Source: Internet
Author: User

First, String, StringBuffer, and StringBuilder are all strings in java. Their differences are as follows:
 
The main performance difference between the String type and the StringBuffer type is that the String type is an immutable object, because every change to the String type is actually equivalent to generating a new String object, then point the pointer to a New String object, so it is best not to use a String that often changes the content, because every time an object is generated, it will affect the system performance, especially when there are no referenced objects in the memory, the jvm gc will start to work, and the speed will be quite slow.
 
Here is an example: String S1 = "abc"; For (int I = 0; I <10000; I ++) // multiple calls of the program {S1 + = "accp"; S1 = "";} If so, after the for loop is complete, if the objects in the memory are not cleared by GC, there are more than 20 thousand objects in the memory, an astonishing number. If this is a system that many people use, this number can be imagined, so you must be careful when using it.
 
If the StringBuffer class is used, the results will be different. Each result will operate on the StringBuffer object itself, instead of generating a new object, and then change the object reference. Therefore, we generally recommend using StringBuffer, especially when string objects change frequently.
 
In some special cases, the String concatenation of the String object is actually interpreted by JVM as the concatenation of the StringBuffer object. Therefore, the speed of the String object in these cases is not slower than that of the StringBuffer object, in particular, in the generation of the following String objects, the String efficiency is far faster than the StringBuffer: String S1 = "This is only a" + "simple" + "test "; stringBuffer Sb = new StringBuilder ("This is only "). append ("simple "). append ("test"); you will be surprised to find that the speed of generating String S1 objects is too fast, and the StringBuffer speed is not dominant at all. Because the value set by S1 is considered by Jvm as a string, it is faster than the StringBuffer method.
 
If your String is from another String object, the speed will not be so fast. For example: String S2 = "this is a"; String S3 = "shool"; String S4 = "of chinese"; String S1 = S2 + S3 + S4;
 
Let's look at the differences between StringBuilder and StringBuffer: StringBuilder is a newly added class in JDK5.0. It differs from StringBuffer in Java. lang. StringBuffer thread-safe variable character sequences. It is similar to the String buffer, but cannot be modified. The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary. Therefore, all operations on any specific instance are generated in serial order, this sequence is consistent with the method call sequence of each involved thread. Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. From JDK 5.0, an equivalent class used by a single thread is added to this class, that is, StringBuilder. Compared with this class, the StringBuilder class should be used first, because it supports all the same operations, but because it does not execute synchronization, It is faster. However, it is not safe to apply StringBuilder instances to multiple threads. If such synchronization is required, StringBuffer is recommended.
 
Summary:
All operations of String must generate a new object.
All StringBuffer operations, object unchanged.
StringBuilder is the StringBuffer equivalence class, but the thread is not secure.
The one-time concatenation operation of String is equivalent to StringBuffer.

Author: ERDP Technical Architecture"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.