The difference between String, StringBuffer, and StringBuilder

Source: Internet
Author: User

The difference between String, StringBuffer, and StringBuilder

I recently learned about StringBuffer, but I have some questions in my mind. I searched for something about String, StringBuffer, and StringBuilder. Now I want to sort it out.

The position of these three classes in string processing is self-evident. What are their advantages and disadvantages and who should they use? Below we will explain from the following points

  1.Comparison of the three methods in execution speed:StringBuilder> StringBuffer> String

  2.String <(StringBuffer, StringBuilder)Cause

String: String constant

StringBuffer: character-based Variable Creation

StringBuilder: character variable Creation

From the above name, we can see that String is a "character to create a constant", that is, an unchangeable object. Your understanding of this sentence may lead to such a question, such as this Code:

String s = "abcd";s = s+1;System.out.print(s);// result : abcd1

 

We obviously changed the String-type variable s. Why is it not changed? In fact, this is a kind of spoofing. JVM parses this code like this: first create the object s, assign an abcd, and then create a new object s to execute the second line of code, that is to say, our previous object s has not changed, so we say that the String type is an unchangeable object. Because of this mechanism, every time a String is used to manipulate a String, in fact, it is constantly creating new objects, and the original objects will become garbage collected by GC. it is conceivable that the execution efficiency will be much lower.

StringBuffer and StringBuilder are different. They are string variables and changeable objects. Every time we use them to operate on strings, they are actually operated on an object, in this way, we will not create some external objects like strings for operations, but the speed will be faster.

  3.A special example:

 String str = “This is only a” + “ simple” + “ test”; StringBuffer builder = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”);

You will be surprised to find that the str object generation speed is too fast, and the StringBuffer speed is not dominant at all. In fact, this is a JVM trick. In fact:

String str = "This is only a" + "simple" + "test ";

Actually:

String str = "This is only a simple test ";

So it does not take much time. However, you should note that if your String is from another String object, the speed will not be so fast. For example:

String str2 = "This is only ";

String str3 = "simple ";

String str4 = "test ";

String str1 = str2 + str3 + str4;

At this time, the JVM will follow the original method in a regular manner.

  4.StringBuilder and StringBuffer

StringBuilder: non-secure thread

StringBuffer: thread-safe

When we use it in string buffering by multiple threads, the JVM cannot ensure that the StringBuilder operation is safe. Although its speed is the fastest, it can ensure that StringBuffer can be operated correctly. Of course, in most cases, we perform operations in a single thread. In most cases, we recommend using StringBuilder instead of StringBuffer, which is the reason for speed.

 

Summary of the three usage: 1. If you want to operate a small amount of data, use = String

2. A single thread operates on a large amount of data in the string buffer = StringBuilder

3. multi-threaded operations on a large amount of data in the string buffer = StringBuffer

 

I am a beginner, just summing up what I learned. It is inevitable that there are others in the writing. learning is a process of memory. These things are just for learning. Some things may be wrong, I hope you will give me some corrections.

 

Turn: http://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html

Thank you!

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.