The difference between String,stringbuffer and StringBuilder in Java

Source: Internet
Author: User
Tags stringbuffer

Immediately to autumn recruit, I really catch the urgent, to check the key moment of gaps.
There's no more nonsense to say. The following are from other people's blogs kingzone columns and tutorials on the web, only for their own review and use. The code is written by itself. String

A string is an immutable object, so each time a change is made to the string, a new string object is generated, and then the pointer is directed to the new string object, so the string that changes the content often is best not used with string, Because each generation of objects has an impact on system performance, especially when there are more objects in memory that are not referenced, the JVM's GC (garbage collection mechanism) starts to work, and degrades. StringBuffer

Very important and often used.
Objects of the StringBuffer class can be modified multiple times without creating new unused objects.
StringBuffer supports a number of important methods that are used when interviewing for writing code. Must be familiar with and attention.
Such as:

Topic description
Implement a function that replaces a space in a string with "%20." For example, when the string is Are Happy, the replaced string is We%20are%20happy.

Public String replacespace (StringBuffer str) {
        if (str.length () = = 0) return
            str.tostring ();
        int length = Str.length ();
        int count = 0;
        int newlength = 0;
        String replacestr = "%20";
        for (int i = 0; i < length; i++) {
            if (Str.charat (i) = = ')
                count++
        }
        Newlength = length + count*replacestr.length ()-count;
        Str.setlength (newlength);
        for (int i = length-1 i >= 0; i--) {
            if (Str.charat (i)!= ')
                Str.setcharat (--newlength,str.charat (i));
            else{
                Str.setcharat (--newlength, ' 0 ');
                Str.setcharat (--newlength, ' 2 ');
                Str.setcharat (--newlength, '% ');
            }
        return str.tostring ();
    }
StringBuilder

Objects of the StringBuilder class can be modified multiple times without creating new unused objects.
The biggest difference between it and StringBuffer is that the StringBuilder method is not thread-safe (cannot be synchronized).

Class object is not variable is thread safe
String object is not variable
StringBuffer Object variable Thread Safety
StringBuilder Object variable Thread not secure
Use Policy

Quote from Kingzone 's Column

(1) Basic principles: If you want to manipulate a small amount of data, using string, single-threaded operation of large amounts of data, using StringBuilder, multithreading operation of large amounts of data, with StringBuffer.

(2) Do not use the String class "+" for frequent stitching, because such performance is very poor, should use StringBuffer or StringBuilder class, this is a more important in Java optimization principle.

(3) In order to obtain better performance, the capacity of StringBuffer or StringBuilder should be specified whenever possible. Of course, if you're working with a string length of no more than 16 characters, you don't need to do that, and when you do not specify a capacity (capacity), the default is to construct an object with a capacity of 16. Not specifying capacity can significantly degrade performance.

(4) StringBuilder generally use within the method to complete similar "+" function, because the thread is not safe, so you can discard after use. StringBuffer is mainly used in global variables.

(5) The use of StringBuilder in the same situation compared to the use of StringBuffer can only achieve 10%~15% performance improvement, but to take the risk of multiple thread insecurity. In the real-world modular programming, the programmer in charge of a module is not necessarily able to determine clearly whether the module will be put into a multithreaded environment, so: unless you determine that the system bottleneck is on the stringbuffer, and that your module will not run in multithreaded mode, Can use the StringBuilder, otherwise still use StringBuffer.

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.