Differences between String, StringBuilder, and StringBuffer in Java: javastringbuffer

Source: Internet
Author: User

Differences between String, StringBuilder, and StringBuffer in Java: javastringbuffer

Recently, when I was learning Java, I encountered such a problem: what is the difference between the three classes String, StringBuilder, and StringBuffer? I searched some materials on the Internet, after learning about it, I will sort it out here to help you watch it and deepen your memory of these knowledge points during your learning process. If there are any mistakes, please correct me.

The main difference between these three classes is in two aspects: Running Speed and thread security.

The reason for the slowest String:

String is a String constant, while StringBuilder and StringBuffer are both String variables. That is, once a String object is created, the object cannot be changed, but the objects of the latter two are variables and can be changed.The following code is used 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, you will find that "abc" is output first, and "abcde" is output. It seems that the str object has been changed. In fact, this is just an illusion, JVM processes these lines of code in this way. First, create a String object str, assign "abc" to str, and then in the third line, in fact, the JVM creates a new object named str, and then adds the original str value and "de" to the new str, the original str will be reclaimed by JVM's garbage collection mechanism (GC), so str is not actually changed, that is, once the String object is created, it cannot be changed. Therefore, operations on String objects in Java are actually a process of constantly creating new objects and recycling old objects, so the execution speed is very slow.

The StringBuilder and StringBuffer objects are variables. to operate on variables, you can directly modify the objects without creating or recycling them. Therefore, the speed is much faster than that of String.

In addition, sometimes we assign values to strings in this way.

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());

In this way, the output results are "abcde" and "abcde", but the String speed is much faster than the response speed of StringBuilder. This is because the operations and

String str = "abcde ";

Is exactly the same, so it will be very fast, and if written in the following form

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

As mentioned above, the JVM will continuously create and recycle objects for this operation. The speed will be slow.

2. Thread Security

  In terms of thread security, StringBuilder is thread-safe, while StringBuffer is thread-safe.

  If a StringBuffer object is used by multiple threads in the string buffer, many methods in the StringBuffer can contain the synchronized keyword, so the thread can be ensured to be secure, but the StringBuilder method does not have this keyword, therefore, thread security cannot be guaranteed, and some incorrect operations may occur. Therefore, if you want to perform multi-threaded operations, you need to use StringBuffer. However, in the case of a single thread, we recommend using StringBuilder with a relatively high speed.

3. Summary
String: applicable to a small number of String operations

StringBuilder: Suitable for a large number of operations in the character buffer in a single thread

StringBuffer: Suitable for multi-threaded operations in the character buffer.

 

 

Conclusion: The vast majority of the above summary is found on the Internet and in books. I want to share what I have learned with you so that you can learn it easily and help you remember it, if there is anything wrong, please correct me.

 

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.