{"id":77,"date":"2006-04-10T23:05:04","date_gmt":"2006-04-11T04:05:04","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/04\/10\/java-five-oh-5-stringbuilder\/"},"modified":"2006-04-10T23:05:04","modified_gmt":"2006-04-11T04:05:04","slug":"java-five-oh-5-stringbuilder","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/04\/10\/java-five-oh-5-stringbuilder\/","title":{"rendered":"Java Five-Oh #5: StringBuilder"},"content":{"rendered":"<p>If you are using Java 5 you should immediately replace StringBuffer with StringBuilder.  By now everyone knows that you should never use the plus sign (+) to concatenate strings.  As a rule of green thumb, if I concatenate more than five strings I do so using an instance of StringBuffer.  When building a dynamic string StringBuffer is faster than plain old string concatenation.  But with Java 5, StringBuffer got a performance boost in the the form of StringBuilder.<\/p>\n<p>StringBuilder is a drop down plug and play replacement for StringBuffer except when working in an multiple treaded environment.  StringBuilder is not synchronized while StringBuffer is.  It takes longer to invoke a synchronized method than a non-synchronized one so I imagine that most of the StringBuilder optimization comes from the fact that it&#8217;s methods are not synchronized.  This is one of the reasons why you should use an ArrayList instead of a Vector.<\/p>\n<p>Just to put up some code with this post, here is how I would write a Hello World example using StringBuilder.<\/p>\n<pre>\npublic String getGreetings(String name) {\n   return new StringBuilder(\"Hello, \")\n      .append(name)\n      .toString();\n}\n<\/pre>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/java\" rel=\"tag\">java<\/a>, <a href=\"http:\/\/technorati.com\/tag\/java+1.5\" rel=\"tag\"> java 1.5<\/a>, <a href=\"http:\/\/technorati.com\/tag\/java+5\" rel=\"tag\"> java 5<\/a>, <a href=\"http:\/\/technorati.com\/tag\/hello+world\" rel=\"tag\"> hello world<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are using Java 5 you should immediately replace StringBuffer with StringBuilder. By now everyone knows that you should never use the plus sign (+) to concatenate strings. As a rule of green thumb, if I concatenate more than five strings I do so using an instance of StringBuffer. When building a dynamic string [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,19,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-1f","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}