{"id":3863,"date":"2019-02-14T10:38:05","date_gmt":"2019-02-14T02:38:05","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3863"},"modified":"2019-02-14T10:38:05","modified_gmt":"2019-02-14T02:38:05","slug":"java-%e4%b8%ba%e4%bb%80%e4%b9%88%e8%a6%81%e8%ae%a9-string-%e8%ae%be%e8%ae%a1%e6%88%90%e4%b8%8d%e5%8f%af%e5%8f%98%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3863","title":{"rendered":"Java \u4e3a\u4ec0\u4e48\u8981\u8ba9 String \u8bbe\u8ba1\u6210\u4e0d\u53ef\u53d8\u7684\uff1f"},"content":{"rendered":"<p>\n\t\u63a5\u4e0a\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3861\">Java \u4e2d String \u4e3a\u4ec0\u4e48\u8bbe\u8ba1\u6210 final \u7c7b\uff1f\u4e3a\u4ec0\u4e48\u5b83\u6709&ldquo;\u4e0d\u53ef\u53d8\u6027&rdquo;\uff1f<\/a>\u300b\u6211\u4eec\u518d\u6765\u8bf4\u8bf4 Java \u4e3a\u4ec0\u4e48\u8981\u8ba9 String \u8bbe\u8ba1\u6210\u4e0d\u53ef\u53d8\u7684\uff1f\n<\/p>\n<p>\n\t\u4efb\u4f55\u4e8b\u60c5\u90fd\u5f97\u6709\u539f\u56e0\u5427\uff0cJava \u8bbe\u8ba1 String \u4e5f\u4e0d\u662f\u62cd\u8111\u888b\u5e72\u7684\u3002\u800c\u662f\u7ecf\u8fc7\u7cbe\u5bc6\u8bbe\u8ba1\u548c\u601d\u8003\u5f97\u6765\u7684\u3002\n<\/p>\n<p>\n\t\u4e3a\u4e86\u641e\u6e05\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Java \u4e3a\u4ec0\u4e48\u8981\u8ba9 String \u8bbe\u8ba1\u6210\u4e0d\u53ef\u53d8\u7684\uff1f\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g05q7zp5jej20e107oq3x.jpg\" style=\"width: 505px;height: 276px\" \/>\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.string.test;\r\npublic class StringTest {\r\n  public static void main(String[] args) {\r\n    StringBuffer a, b, c;\r\n    a = new StringBuffer(&quot;test&quot;);\r\n    b = a;\r\n    c = b;\r\n    String processA = processA(a);\r\n    String processB = processB(b);\r\n    String processC = processC(c);\r\n    System.out.println(processA);\r\n    System.out.println(processB);\r\n    System.out.println(processC);\r\n  }\r\n  static String processA(StringBuffer str){\r\n    return str.append(&quot;A&quot;).toString();\r\n  }\r\n  static String processB(StringBuffer str){\r\n    return str.append(&quot;B&quot;).toString();\r\n  }\r\n  static String processC(StringBuffer str){\r\n    return str.append(&quot;C&quot;).toString();\r\n  }\r\n}\r\n\/\/OUTPUT\r\n\/\/ testA\r\n\/\/testAB\r\n\/\/testABC<\/pre>\n<p>\n\t\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u7684\u672c\u610f\u662f\uff0ca &nbsp;= b = c = &ldquo;test&rdquo;\uff0c\u6700\u7ec8\u8f93\u51fa\u7684\u7ed3\u679c\u671f\u671b\u662f\uff1atestA\u3001testB\u3001testC\u3002\u6211\u4eec\u7684\u672c\u610f\u662f\u5e0c\u671b\u53d8\u91cf\u662f\u4e0d\u53d8\u7684\u3002\n<\/p>\n<p>\n\t\u597d\u4e86\uff0c\u73b0\u5728\u7684\u8f93\u51fa\u7ed3\u679c\u8d85\u51fa\u9884\u671f\u3002\u5168\u4e71\u4e86\uff0c\u4e0d\u53ef\u63a7\u4e86\u3002\u6240\u4ee5 String \u4e0d\u53ef\u53d8\u7684\u5b89\u5168\u6027\u5c31\u4f53\u73b0\u5728\u8fd9\u91cc\u3002\u5b9e\u9645\u4e0a StringBuffer \u7684\u4f5c\u7528\u5c31\u662f\u8d77\u5230\u4e86 String \u7684\u53ef\u53d8\u914d\u5957\u7c7b\u89d2\u8272\u3002\n<\/p>\n<p>\n\t\u8fd9\u53ea\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u573a\u666f\uff0c\u5176\u5b83\u7684\u8fd8\u6709 HashSet\u3001HashMap\u3001\u591a\u7ebf\u7a0b\u573a\u666f\uff0c\u90a3\u5c31\u66f4\u4e71\u4e86\uff0c\u66f4\u4e0d\u53ef\u63a7\u4e86\uff0c\u66f4\u4e0d\u5b89\u5168\u4e86\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nHashSet&lt;StringBuilder&gt; hs = new HashSet&lt;StringBuilder&gt;();<\/pre>\n<p>\n\t\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u7684 HashSet \u641e\u6210\u4e0a\u9762\u8fd9\u6837\u7684\uff0c\u4f1a\u51fa\u73b0\u4ec0\u4e48\u540e\u679c\uff1f\n<\/p>\n<pre class=\"brush:java;\">\r\nclass Test{\r\n    public static void main(String[] args){\r\n        HashSet&lt;StringBuilder&gt; hs=new HashSet&lt;StringBuilder&gt;();\r\n        StringBuilder sb1=new StringBuilder(&quot;\u4e1a\u4f59\u8349&quot;);\r\n        StringBuilder sb2=new StringBuilder(&quot;\u4e1a\u4f59\u8349www.xttblog.com&quot;);\r\n        hs.add(sb1);\r\n        hs.add(sb2);    \/\/\u8fd9\u65f6\u5019HashSet\u91cc\u662f{&quot;\u4e1a\u4f59\u8349&quot;,&quot;www.xttblog.com&quot;}\r\n \r\n        StringBuilder sb3=sb1;\r\n        sb3.append(&quot;www.xttblog.com&quot;);  \r\n        \/\/\u8fd9\u65f6\u5019HashSet\u91cc\u662f{&quot;\u4e1a\u4f59\u8349www.xttblog.com&quot;,&quot;\u4e1a\u4f59\u8349www.xttblog.com&quot;}\r\n        System.out.println(hs);\r\n    }\r\n}\r\n\/\/Output:\r\n\/\/[\u4e1a\u4f59\u8349www.xttblog.com, \u4e1a\u4f59\u8349www.xttblog.com]<\/pre>\n<p>\n\t\u771f\u8981\u8bbe\u8ba1\u6210\u8fd9\u6837\uff0cJava \u90fd\u8981\u5b8c\u5168\u88ab\u4f60\u98a0\u8986\u3002\n<\/p>\n<p>\n\t\u4e0d\u53ef\u53d8\u6027\u5728\u7ebf\u7a0b\u5b89\u5168\u65b9\u9762\u7684\u4f5c\u7528\uff0c\u6211\u5c31\u4e0d\u8bf4\u4e86\u3002\u6211\u4eec\u518d\u6765\u770b\u53e6\u5916\u4e00\u4e2a\u70b9\uff1a\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u3002\n<\/p>\n<p>\n\t\u5047\u8bbe\u73b0\u5728\u6709\u4e0b\u9762\u4e24\u4e2a\u53d8\u91cf\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nString one = &quot;someString&quot;;\r\nString two = &quot;someString&quot;;<\/pre>\n<p>\n\t\u90a3\u4e48\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\uff0c\u5c31\u662f\u8fd9\u6837\u7684\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Java \u5b57\u7b26\u4e32\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u7ed3\u6784\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g05r1uz5nrj20h90afjs1.jpg\" style=\"width: 621px;height: 375px\" \/>\n<\/p>\n<p>\n\t\u8fd9\u6837\u5728\u5927\u91cf\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u7a7a\u95f4\uff0c\u63d0\u9ad8\u6548\u7387\u3002\u5982\u679c\u8981\u662f\u53ef\u53d8\u7684\uff0c\u5b8c\u4e86\uff0c\u53d8\u91cf one \u548c two \u5f97\u4e00\u4e2a\u4eba\u5360\u4e00\u70b9\u5185\u5b58\u3002\u90a3\u8c01\u7528 Java \u8c01\u5012\u9709\uff0c\u8fd9\u4e48\u6d6a\u8d39\u5185\u5b58\u7684\u8c01\u8fd8\u4f1a\u7528\uff1fJava \u5c06\u4e0d\u518d\u662f java\u3002\n<\/p>\n<p>\n\t\u6240\u4ee5\uff0c\u8981\u60f3\u8282\u7ea6\u4e00\u4e9b\u5185\u5b58\u3002String \u7684\u4e0d\u53ef\u53d8\u6027\u5c31\u662f\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u5fc5\u8981\u6761\u4ef6\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=3863\">Java \u4e3a\u4ec0\u4e48\u8981\u8ba9 String \u8bbe\u8ba1\u6210\u4e0d\u53ef\u53d8\u7684\uff1f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u63a5\u4e0a\u7bc7\u6587\u7ae0\u300aJava \u4e2d String \u4e3a\u4ec0\u4e48\u8bbe\u8ba1\u6210 final \u7c7b\uff1f\u4e3a\u4ec0\u4e48\u5b83\u6709&ldquo;\u4e0d\u53ef\u53d8\u6027&#038;rdq [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,1700,616,565,241],"class_list":["post-3863","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-java-11","tag-java-7","tag-java-","tag-java-util-logging"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3863","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=3863"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3863\/revisions"}],"predecessor-version":[{"id":3864,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3863\/revisions\/3864"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}