{"id":682,"date":"2013-01-09T08:53:26","date_gmt":"2013-01-09T08:53:26","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=442"},"modified":"2013-01-09T08:53:26","modified_gmt":"2013-01-09T08:53:26","slug":"when-to-use-string-builder-string-buffer","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/when-to-use-string-builder-string-buffer\/","title":{"rendered":"when to use string builder, string buffer"},"content":{"rendered":"<p>In java String Builder Should be Used in case you need to perform concatenate more string together.<\/p>\n<p>i.e<\/p>\n<pre>\n public String toString()\n {\n    return  a + b + c ;\n }\n<pre>\n\nFor the above code using + will be converted to\n\n<pre>\na = new StringBuilder()\n    .append(a).append(b).append(c)\n    .toString();\n<\/pre>\n<p>For the above case you can use concat as below but since + will be converted as String Builder its better to use + rather than concat.<\/p>\n<pre>\n public String toString()\n {\n    return  a.concat(b).concat(c);\n }\n<\/pre>\n<p>The key is whether you are writing a single concatenation all in one place or accumulating it over time.<\/p>\n<p>There's no point in explicitly using StringBuilder. <\/p>\n<p>But if you are building a string e.g. inside a loop, use StringBuilder.<\/p>\n<p>To clarify, assuming that hugeArray contains thousands of strings, code like this:<\/p>\n<pre>\n...\nString result = \"\";\nfor (String s : hugeArray) {\n    result = result + s;\n}\n<\/pre>\n<p>It should be as below<\/p>\n<pre>\n ...\nStringBuilder sb = new StringBuilder();\n\nfor (String s : hugeArray) {\n    sb.append(s);\n}\nString result = sb.toString();\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In java String Builder Should be Used in case you need to perform concatenate more string together. i.e public String toString() { return a + b + c ; } For the above code using + will be converted to a = new StringBuilder() .append(a).append(b).append(c) .toString(); For the above case you can use concat as&hellip; <a href=\"https:\/\/codethataint.com\/blog\/when-to-use-string-builder-string-buffer\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,172],"tags":[],"class_list":["post-682","post","type-post","status-publish","format-standard","hentry","category-java","category-variables"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":0,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}