Why StringBuilder when there is String?

2019-01-01 05:15发布

I just encountered StringBuilder for the first time and was surprised since Java already has a very powerful String class that allows appending.

Why a second String class?

Where can I learn more about StringBuilder?

9条回答
与君花间醉酒
2楼-- · 2019-01-01 05:43

To be precise, StringBuilder adding all strings is O(N) while adding String's is O(N^2). Checking the source code, this is internally achieved by keeping a mutable array of chars. StringBuilder uses the array length duplication technique to achieve ammortized O(N^2) performance, at the cost of potentially doubling the required memory. You can call trimToSize at the end to solve this, but usually StringBuilder objects are only used temporarily. You can further improve performance by providing a good starting guess at the final string size.

查看更多
后来的你喜欢了谁
3楼-- · 2019-01-01 05:46

StringBuilder is for, well, building strings. Specifically, building them in a very performant way. The String class is good for a lot of things, but it actually has really terrible performance when assembling a new string out of smaller string parts because each new string is a totally new, reallocated string. (It's immutable) StringBuilder keeps the same sequence in-place and modifies it (mutable).

查看更多
临风纵饮
4楼-- · 2019-01-01 05:48

Efficiency.

Each time you concatenate strings, a new string will be created. For example:

String out = "a" + "b" + "c";

This creates a new, temporary string, copies "a" and "b" into it to result in "ab". Then it creates another new, temporary string, copies "ab" and "c" into it, to result in "abc". This result is then assigned to out.

The result is a Schlemiel the Painter's algorithm of O(n²) (quadratic) time complexity.

StringBuilder, on the other hand, lets you append strings in-place, resizing the output string as necessary.

查看更多
裙下三千臣
5楼-- · 2019-01-01 05:50

String class is immutable whereas StringBuilder is mutable.

String s = "Hello";
s = s + "World";

Above code will create two object because String is immutable

StringBuilder sb = new StringBuilder("Hello");
sb.append("World");

Above code will create only one object because StringBuilder is not immutable.

Lesson: Whenever there is a need to manipulate/update/append String many times go for StringBuilder as its efficient as compared to String.

查看更多
流年柔荑漫光年
6楼-- · 2019-01-01 05:53

StringBuilder is good when you are dealing with larger strings. It helps you to improve performance.

Here is a article that I found that was helpful .

A quick google search could have helped you. Now you hired 7 different people to do a google search for you . :)

查看更多
梦醉为红颜
7楼-- · 2019-01-01 05:53

Java has String, StringBuffer and StringBuilder:

  • String : Its immutable

  • StringBuffer : Its Mutable and ThreadSafe

  • StringBuilder : Its Mutable but Not ThreadSafe, introduced in Java 1.5

String eg:

public class T1 {

    public static void main(String[] args){

        String s = "Hello";

        for (int i=0;i<10;i++) {

            s = s+"a";
            System.out.println(s);
        }
    }
}

}

output: 10 Different Strings will be created instead of just 1 String.

Helloa
Helloaa
Helloaaa
Helloaaaa
Helloaaaaa
Helloaaaaaa
Helloaaaaaaa
Helloaaaaaaaa 
Helloaaaaaaaaa 
Helloaaaaaaaaaa

StringBuilder eg : Only 1 StringBuilder object will be created.

public class T1 {

    public static void main(String[] args){

        StringBuilder s = new StringBuilder("Hello");

        for (int i=0;i<10;i++) {    
            s.append("a");
            System.out.println(s);
        }
    }
}
查看更多
登录 后发表回答