{"id":1060,"date":"2017-02-14T14:37:30","date_gmt":"2017-02-14T06:37:30","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1060"},"modified":"2017-02-14T14:37:30","modified_gmt":"2017-02-14T06:37:30","slug":"java%e4%b8%adstring%e3%80%81stringbuffer%e3%80%81stringbuilder%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1060","title":{"rendered":"Java\u4e2dString\u3001StringBuffer\u3001StringBuilder\u7684\u533a\u522b"},"content":{"rendered":"<p>\n\t\u7b80\u8981\u7684\u8bf4\uff0cString\u7c7b\u578b\u548cStringBuffer\u7c7b\u578b\u7684\u4e3b\u8981\u6027\u80fd\u533a\u522b\u5728\u4e8eString\u662f\u4e0d\u53ef\u53d8\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u6bcf\u6b21\u5bf9String\u7c7b\u578b\u8fdb\u884c\u6539\u53d8\u7684\u65f6\u5019\u5176\u5b9e\u90fd\u7b49\u540c\u4e8e\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684String\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u5c06\u6307\u9488\u6307\u5411\u65b0\u7684String\u5bf9\u8c61\uff0c\u6240\u4ee5\u7ecf\u5e38\u6539\u53d8\u5185\u5bb9\u7684\u5b57\u7b26\u4e32\u6700\u597d\u4e0d\u8981\u4f7f\u7528String\uff0c\u56e0\u4e3a\u6bcf\u6b21\u751f\u6210\u5bf9\u8c61\u90fd\u4f1a\u5bf9\u7cfb\u7edf\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\uff0c\u7279\u522b\u5f53\u5185\u5b58\u4e2d\u65e0\u5f15\u7528\u5bf9\u8c61\u591a\u4e86\u4ee5\u540e\uff0cJVM\u7684GC\u5c31\u4f1a\u5f00\u59cb\u5de5\u4f5c\uff0c\u90a3\u901f\u5ea6\u662f\u4e00\u5b9a\u4f1a\u6162\u7684\u3002\n<\/p>\n<p>\n\t\u800c\u5982\u679c\u4f7f\u7528StringBuffer\u7c7b\u5219\u7ed3\u679c\u5c31\u4e0d\u4e00\u6837\u4e86\uff0c\u6bcf\u6b21\u7ed3\u679c\u90fd\u4f1a\u5bf9StringBuffer\u5bf9\u8c61\u672c\u8eab\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c\u4e0d\u662f\u751f\u6210\u65b0\u7684\u5bf9\u8c61\u3002\u6240\u4ee5\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\u63a8\u8350\u4f7f\u7528StringBuffer\uff0c\u7279\u522b\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\u7ecf\u5e38\u6539\u53d8\u7684\u60c5\u51b5\u4e0b\u3002\u800c\u5728\u67d0\u4e9b\u7279\u522b\u60c5\u51b5\u4e0b\uff0cString\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u5176\u5b9e\u662f\u88abJVM\u89e3\u91ca\u6210\u4e86StringBuffer\u5bf9\u8c61\u7684\u62fc\u63a5\uff0c\u6240\u4ee5\u8fd9\u4e9b\u65f6\u5019String\u5bf9\u8c61\u7684\u901f\u5ea6\u5e76\u4e0d\u4f1a\u6bd4StringBuffer\u5bf9\u8c61\u6162\uff0c\u800c\u7279\u522b\u662f\u4ee5\u4e0b\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u751f\u6210\u4e2d\uff0cString\u6548\u7387\u8981\u8fdc\u6bd4StringBuffer\u5feb\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nString s = &quot;This is only a &quot; + &quot;simple &quot; + &quot;test&quot;;\r\nStringBuffer sb = new StringBuffer(&quot;This is only a&quot;).append.(&quot; simple &quot;).append.(&quot; test&quot;);<\/pre>\n<p>\n\t\u5176\u5b9e\u8fd9\u662fJVM\u7684\u4e00\u4e2a\u5c0f\u628a\u620f\uff0c\u5728JVM\u773c\u91cc\uff0c\u8fd9\u4e2aString s = &quot;This is only a &quot; + &quot;simple &quot; + &quot;test&quot;;\u5176\u5b9e\u5c31\u662f\uff1aString s = &quot;This is only a simple test&quot;;\u6240\u4ee5\u5f53\u7136\u4e0d\u9700\u8981\u592a\u591a\u7684\u65f6\u95f4\u4e86\uff0c\u4f46\u662f\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4f60\u7684\u5b57\u7b26\u4e32\u662f\u6765\u81ea\u53e6\u5916\u7684String\u5bf9\u8c61\u7684\u8bdd\uff0c\u901f\u5ea6\u5c31\u6ca1\u90a3\u4e48\u5feb\u4e86\u3002\u6bd4\u5982\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nString s2 = &quot;This is only a&quot;;\r\nString s3 = &quot;simple &quot;;\r\nString s4 = &quot;test&quot;;\r\nString s1 = s2 + s3 + s4;<\/pre>\n<p>\n\t\u8fd9\u65f6\u5019JVM\u5c31\u4f1a\u89c4\u89c4\u77e9\u77e9\u7684\u6309\u7167\u539f\u6765\u7684\u65b9\u5f0f\u53bb\u505a\u3002<br \/>\n\t\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684String\u7684\u793a\u4f8b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class StringDemo {\r\n    public static void main(String[] args) {\r\n        String s1 = &quot;Hello&quot;;\r\n        String s2 = &quot; World&quot;;\r\n        String s3 = s1 + s2;\r\n        String s4 = &quot;Hello World&quot;;\r\n        String s5 = &quot;Hello &quot; + &quot;World&quot;;\r\n        String s6 = new String(&quot;Hello World&quot;);\r\n    }\r\n}<\/pre>\n<p>\n\t<img decoding=\"async\" alt=\"String\u7684\u5185\u5b58\u56fe\" src=\"http:\/\/yedward.net\/zb_users\/upload\/2015\/02\/201502161424073575623745.png\" style=\"width: 468px;height: 411px\" \/>\n<\/p>\n<p>\n\t\u5982\u4e0a\u56fe1\u5c31\u662f\u4e0a\u8ff0\u4ee3\u7801\u7684\u8fd0\u884c\u56fe\u89e3\uff0c\u6ce8\u610fString s6 = new String(&quot;Hello World&quot;);\u8fd9\u4e00\u6bb5\u4ee3\u7801\u6267\u884c\u7684\u65f6\u5019\uff0c\u662f\u5148\u5f00\u8f9f\u4e00\u4e2a\u7a7a\u95f4\uff0c\u5b58\u4e86Hello World\uff0c\u7136\u540enew String\u540e\uff0c\u53c8\u5f00\u8f9f\u4e86\u4e00\u4e2a\u7a7a\u95f4\uff0c\u6240\u4ee5\u8fd9\u4e00\u884c\u4ee3\u7801\u4f1a\u521b\u5efa\u4e24\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u8be5\u4ee3\u7801\u4e4b\u524ds3\u3001s4\u3001s5\u90fd\u5df2\u7ecf\u6307\u5411\u4e86\u4e00\u4e2aHello World\u4e86\uff0c\u7531\u4e8eString\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u53ef\u590d\u7528\u7684\uff0c\u6240\u4ee5s6\u8fd9\u91cc\u62ec\u53f7\u5185\u7684Hello World\u5c31\u4e0d\u7528\u518d\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u4e86\u3002\n<\/p>\n<p>\n\tStringBuffer\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u53ef\u53d8\u5b57\u7b26\u5e8f\u5217\uff0c\u4e00\u4e2a\u7c7b\u4f3c\u4e8eString\u7684\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\uff0c\u4f46\u662f\u4e0d\u80fd\u4fee\u6539\u3002\u867d\u7136\u5728\u4efb\u610f\u65f6\u95f4\u70b9\u4e0a\u5b83\u90fd\u5305\u542b\u67d0\u79cd\u7279\u5b9a\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u4f46\u901a\u8fc7\u67d0\u4e9b\u65b9\u6cd5\u8c03\u7528\u53ef\u4ee5\u6539\u53d8\u8be5\u5e8f\u5217\u7684\u957f\u5ea6\u548c\u5185\u5bb9\u3002\u53ef\u5c06\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u5b89\u5168\u5730\u7528\u4e8e\u591a\u4e2a\u7ebf\u7a0b\u3002\u53ef\u4ee5\u5728\u5fc5\u8981\u65f6\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u540c\u6b65\uff0c\u56e0\u6b64\u4efb\u610f\u7279\u5b9a\u5b9e\u4f8b\u4e0a\u7684\u6240\u6709\u64cd\u4f5c\u5c31\u597d\u50cf\u662f\u4ee5\u4e32\u884c\u987a\u5e8f\u53d1\u751f\u7684\uff0c\u8be5\u987a\u5e8f\u4e0e\u6240\u6d89\u53ca\u7684\u6bcf\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u7684\u65b9\u6cd5\u8c03\u7528\u987a\u5e8f\u4e00\u81f4\u3002StringBuffer \u4e0a\u7684\u4e3b\u8981\u64cd\u4f5c\u662fappend\u548cinsert \u65b9\u6cd5\uff0c\u53ef\u91cd\u8f7d\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u3002\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u5c06\u7ed9\u5b9a\u7684\u6570\u636e\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u8be5\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u8ffd\u52a0\u6216\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u4e2d\u3002append \u65b9\u6cd5\u59cb\u7ec8\u5c06\u8fd9\u4e9b\u5b57\u7b26\u6dfb\u52a0\u5230\u7f13\u51b2\u533a\u7684\u672b\u7aef\uff1b\u800c insert \u65b9\u6cd5\u5219\u5728\u6307\u5b9a\u7684\u70b9\u6dfb\u52a0\u5b57\u7b26\u3002\n<\/p>\n<p>\n\tStringBuilder\u4e5f\u662f\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u4e0eStringBuffer\u7684\u533a\u522b\u662f\u4e0d\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u7528\u5728\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u5355\u4e2a\u7ebf\u7a0b\u4f7f\u7528\u7684\u65f6\u5019\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5efa\u8bae\u4f18\u5148\u91c7\u7528\u8be5\u7c7b\uff0c\u56e0\u4e3a\u5728\u5927\u591a\u6570\u5b9e\u73b0\u4e2d\uff0c\u5b83\u6bd4StringBuffer\u5feb\uff0c\u4e8c\u8005\u7684\u65b9\u6cd5\u57fa\u672c\u76f8\u540c\u3002\u6709\u65f6\u5019\u4e0d\u8bb0\u5f97StringBuffer\u548cStringBuilder\u54ea\u4e2a\u662f\u7ebf\u7a0b\u5b89\u5168\uff0c\u54ea\u4e2a\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff0c\u53ef\u4ee5\u6253\u5f00\u67e5\u770b\u4e0b\u4e24\u4e2a\u7684\u7c7b\uff0c\u641c\u7d22\u4e0bsynchronized\u8fd9\u4e2a\u5173\u952e\u5b57\uff0c\u6709\u8fd9\u4e2a\u5173\u952e\u5b57\u7684\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\n<\/p>\n<p>\n\t\u7efc\u4e0a\u53ef\u4ee5\u603b\u7ed3\u5f97\u51fa\uff0cString\u662f\u4e0d\u53ef\u53d8\u5b57\u7b26\u4e32\uff0cStringBuffer\u548cStringBuilder\u662f\u53ef\u53d8\u5b57\u7b26\u4e32\uff0c\u4e00\u822c\u6765\u8bf4\uff0cStringBuilder\u5feb\u4e8eStringBuffer\u5feb\u4e8eString\uff0cStringBuffer\u4e0eStringBuilder\u57fa\u672c\u7c7b\u4f3c\uff0c\u6700\u5927\u7684\u533a\u522b\u5728\u4e8eStringBuffer\u662f\u7ebf\u7a0b\u5b89\u5168\uff0cStringBuilder\u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1060\">Java\u4e2dString\u3001StringBuffer\u3001StringBuilder\u7684\u533a\u522b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7b80\u8981\u7684\u8bf4\uff0cString\u7c7b\u578b\u548cStringBuffer\u7c7b\u578b\u7684\u4e3b\u8981\u6027\u80fd\u533a\u522b\u5728\u4e8eString\u662f\u4e0d\u53ef\u53d8\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u6bcf [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,797,796,798,531],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-string","tag-stringbuffer","tag-stringbuilder","tag-531"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1060"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":1061,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions\/1061"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}