The difference between the three string,stringbuilder,stringbuffer in Java

Source: Internet
Author: User

Recently in the study of Java, encountered such a problem, that is, String,stringbuilder and stringbuffer the three class between what is the difference between them, their own from the Internet to search for some information, some understanding of the post here to tidy up, for everyone to watch, Also easy to deepen their learning process of these knowledge points of memory, if wrong, please correct me.

The difference between the three classes is mainly in two aspects, that is, running speed and thread safety.

    1. First of all, the speed of operation, or the speed of execution, in this respect the speed of operation is: StringBuilder > StringBuffer > String

The slowest reason for string:

strings are 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);

If you run this code will find that the 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 ());

The output is also "ABCDE" and "ABCDE", but the speed of the string is much faster than StringBuilder, because the action in line 1th and

String str= "ABCDE";

is exactly the same, so it will be soon, and if written in the form below

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.

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

Reprint Source: http://www.cnblogs.com/su-feng/p/6659064.html

The difference between the three string,stringbuilder,stringbuffer in Java

Related Article

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.