What is the role of StringBuffer and StringBuilder?

Source: Internet
Author: User
A Java string object that has an attribute that is invariant and can only be created without changing its value. Therefore, some programs that use large numbers of strings may have performance bottlenecks and even memory overflows.
For such a problem, Java provides developers with the appropriate solution.
        public class stringbbtest{public static void main (string args []) {string a= "a";
        String b= "B";
        String c= "C";
        String d= "D";
    String abcd=a+b+c+d; In the above code, 7 string objects were created altogether. For a, B, C, and D variables, they are string objects created by means of double quotes.
The ABCD variables are connected by 4 of them, and in the connected process, the first execution of the a+b operation, the AB string, and then the C, the ABC string, and finally the D to the ABCD string object. As you can see from the example above, the efficiency of stitching strings directly by adding string is very low, which may produce superfluous string objects, such as AB,ABC. If there are tens of thousands of strings that need to be spliced in a program, the load on the JVM is quite large, which seriously affects the performance of the program. In fact, if you have a large number of strings that need stitching, you should use the StringBuffer and StringBuilder classes, which are a complement to string. For example, the same functionality can be written to code to improve program performance, as the following example: public class stringbbtest{public static void main (string args []) {string A= "a
        ";
        String b= "B";
        String c= "C";
        String d= "D";
        StringBuffer sb=new StringBuffer ();
        Sb.append (a);
        Sb.append (b);
        Sb.append (c);
        Sb.append (d);
        String abcd=sb.tostring (); System.Out.println (ABCD); The example above is a concatenation of strings through StringBuffer, which does not guarantee thread security.
If thread-safe issues may be involved in stitching strings, you should use StringBuilder, which are similar in their functionality and APIs. In Java programs, if you have a large number of concatenation strings, you should use the StringBuffer and StringBuilder classes to avoid unnecessary string objects from being created to improve program performance.
 They are similar in function, except that StringBuilder threads are safe.

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.