The difference between string, StringBuffer, and StringBuilder in Java (brief introduction)

Source: Internet
Author: User
Tags stringbuffer

A brief introduction to the three classes commonly used in Java for handling strings: 1, Java.lang.String2, Java.lang.StringBuffer3, Java.lang.StrungBuilder

All three things in common: both final and not inherited, primarily in terms of performance and security, since these classes are often used, taking into account that the parameters are prevented from being affected by parameter modification to other applications.

StringBuffer is thread safe and can be used in multiple threads without the need for additional synchronization;

StringBuilder is non-synchronous, running in multi-threading requires the use of separate synchronization, but faster than stringbuffer faster;

StringBuffer and StringBuilder in common: strings can be manipulated by append, Indert.

A String implements three interfaces: Serializable, comparable<string>, carsequence

StringBuilder only implements two interfaces serializable, charsequence, compared to the instance of string can be compared by CompareTo method, the other two is not.

The difference between the three classes is mainly in two aspects, that is, running speed and thread safety. 1, first say the speed of operation, or the speed of execution, The speed of operation in this area is: StringBuilder > StringBuffer > String

String is the slowest reason: string constants, and StringBuilder and StringBuffer are string variables, that is, once a string object is created, the object is immutable, but the latter object is a variable and can be changed. Take the following section of code as an example:

1 String str= "abc"; 2 System.out.println (str); 3 str=str+ "de"; 4 System.out.println (str);

Running this code will find the first output "ABC", and then output "ABCDE", as if the object is str is changed, in fact, this is just an illusion, the JVM for these lines of code is handled, first create a String object str, and the "ABC" Assigned to STR, Then in the third line, in fact, the JVM has created a new object called STR, and then the original STR value and "de" added to the new STR, and the original STR will be garbage collection mechanism (GC) of the JVM is recycled, so, STR is not actually changed, That is, the string object mentioned earlier cannot be changed once it is created. So, the operation of a string object in Java is actually a process that constantly creates new objects and reclaims the old objects, so the execution speed is slow.

The object of StringBuilder and StringBuffer is a variable, and the action on the variable is to make changes directly to the object without creating and reclaiming the operation, so the speed is much faster than the string.

In addition, sometimes we assign values to strings like this

1 String str= "abc" + "de"; 2 StringBuilder stringbuilder=new StringBuilder (). Append ("abc"). Append ("de"); 3 System.out.println (str); 4 System.out.println (stringbuilder.tostring ());

This output is also "ABCDE" and "ABCDE", but the speed of the string is much faster than StringBuilder, because the action in line 1th and the string str= "ABCDE"; it's exactly the same, so it's going to be fast, And if it's written in the following form

1 String str1= "abc"; 2 String str2= "de"; 3 String str=str1+str2;

Then the JVM will constantly create and recycle objects to do this, as stated above. The speed will be very slow.

2. Thread Safety again

  On-thread security, StringBuilder threads are unsafe, and stringbuffer is thread-safe

  If a StringBuffer object is used by multiple threads in a string buffer, many methods in StringBuffer can have the Synchronized keyword, so that the thread is secure, but the StringBuilder method does not have the keyword. Therefore, there is no guarantee of thread safety, there may be some wrong operation. So if the operation is multi-threaded, then use StringBuffer, but in the case of single-threaded, it is recommended to use a faster StringBuilder.

(When a thread accesses a synchronized (this) in an object to synchronize a block of code, other threads attempting to access the object will be blocked)

3. Summarize  

String: Case for a small number of string operations

StringBuilder: For a single-threaded operation in a character buffer

StringBuffer: For large numbers of operations in a character buffer for multithreading

The difference between string, StringBuffer, and StringBuilder in Java (brief introduction)

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.