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

StringVsStringBuilder

1 comment:

  1. Thanks for the update. I think the writing was on the wall when your posts
    started dropping off noticeably in the past several months, but I'm glad that
    you will each continue blogging in your own forums. Best of luck to both of you
    in the future, and thank you for many informative, lovely posts

    ReplyDelete