Why StringBuilder when there is String?

String does not allow appending. Each method you invoke on a String creates a new object and returns it. This is because String is immutable - it cannot change its internal state.

String does not allow appending. Each method you invoke on a String creates a new object and returns it. This is because String is immutable - it cannot change its internal state.

On the other hand StringBuilder is mutable. When you call append(..) it alters the internal char array, rather than creating a new string object. Thus it is more efficient to have: StringBuilder sb = new StringBuilder(); for (int I = 0; I Append(i); } rather than str += i, which would create 500 new string objects.

Note that in the example I use a loop. As helios notes in the comments, the compiler automatically translates expressions like String d = a + be + c to something like String d = new StringBuilder(a). Append(b).

Append(c).toString(); Note also that there is StringBuffer in addition to StringBuilder. The difference is that the former has synchronized methods. If you use it as a local variable, use StringBuilder.

If it happens that it's possible for it to be accessed by multiple threads, use StringBuffer (that's rarer).

1 well done describing mutable v. Immutable – brendan Mar 8 '11 at 15:01 5 +1. You could add: "hence StrungBuilder looks for performance" and "Java compilers substitutes expressions like A + B + C with new StringBuilder(A).

Append(B). Append(C).toString() in order to avoid object creation performance penalties" :) – helios Mar 8 '11 at 15:04 and all Thank you very much. You all deserve +1's (which will be delivered promptly :) – an00b Mar 8 '11 at 15:04 super like the recall of 'immutable' objects.

– Gary Tsui Mar 8 '11 at 8:46.

String class is immutable whereas StringBuilder is mutable. String s = " Above code will create two object because String is immutable StringBuilder sb = new StringBuilder("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.

StringBuilder is not thread-safe; StringBuffer is. – Adamski Mar 8 '11 at 15:04 @Adamski Yes Thank you I got confused with StringBuffer. – Umesh Kacha Mar 8 '11 at 15:05.

Here is a concrete example on why - int total = 50000; String s = ""; for (int I = 0; I ValueOf(i)); } // 4ms As you can see the difference in performance is significant.

Ps. I ran this on my Macbook Pro Dual core. – Amir Raminfar Mar 8 '11 at 15:04 This doesn't explain why – Steve Kuo Mar 8 '11 at 19:02 This does explain Why StringBuilder when there is String?

This doesn't explain Why StringBuilder is so fast. But that is not The Question. So this is a valid answer.

– Kerem Baydo? An Mar 27 '11 at 0:26 @krmby - Agreed. To answer why is really meant for another question.

– Amir Raminfar Mar 28 '11 at 18:42.

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).

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.

The StringBuilder class is mutable and unlike String, it allows you to modify the contents of the string without needing to create more String objects, which can be a performance gain when you are heavily modifying a string. There is also a counterpart for StringBuilder called StringBuffer which is also synchronized so it is ideal for multithreaded environments. The biggest problem with String is that any operation you do with it, will always return a new object, say: String s1 = "something"; String s2 = "else"; String s3 = s1 + s2; // this is creating a new object.

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 . :).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions