Thursday, May 3, 2012

The 3+3+3 will not creates lot of strings

A couple of days ago during the code review i have observed lots of interesting things. One of the important thing is when to use String builder and when not to use String Builder.

                 There are so many difference between String and String Builder.  Any way i don't want to cover all the things here because it going to rewriting the same code again. But here i am going clear the confusion about  String or String Builder during the concatenation.

String Builder comes with its own overhead. If you have fewer than about seven concatenations, Use simple string concatenation even if you get intermediate strings on the heap.Secondly, when concatenating strings in one shot, only the final string is created. In that case, do not use String Builder

Ex: String s = “s1” +” s2” + “s3” + “s4”;

String Builder capacity

The String Builder  default capacity is 16. Each time the String Builder runs out of capacity, it allocates a new buffer of twice the size of the old buffer