Difference between String, StringBuffer and StringBuilder

Source: Internet
Author: User

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) reason String: String constant StringBuffer: character creation variable StringBuilder: character creation variable can be seen from the above name, String is "character creation constant ", that is, unchangeable objects. Your understanding of this sentence may lead to such a question, such as this code: [java] 1 String s = "abcd"; 2 s = s + 1; 3 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: [java] String str = "This is only a" + "simple" + "test"; StringBuffer builder = new StringBuilder ("This is only "). 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, it should be noted 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. 4. stringBuilder and StringBuffer StringBuilder: thread-safe StringBuffer: thread-safe. When we are used by multiple threads in the string buffer, the JVM cannot guarantee the security of StringBuilder operations, 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: 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 operation of a large amount of data in the string buffer = StringBuffer

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.