{"id":36076,"date":"2024-11-26T13:17:15","date_gmt":"2024-11-26T05:17:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36076\/"},"modified":"2024-11-26T13:17:15","modified_gmt":"2024-11-26T05:17:15","slug":"%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%9ajava-%e4%b8%ad%e7%9a%84%e5%9e%83%e5%9c%be%e6%94%b6%e9%9b%86%e5%92%8c%e4%b8%8d%e5%8f%98%e6%80%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36076\/","title":{"rendered":"\u5b57\u7b26\u4e32\uff1aJava \u4e2d\u7684\u5783\u573e\u6536\u96c6\u548c\u4e0d\u53d8\u6027"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173093997974816.jpg\" class=\"aligncenter\" title=\"\u5b57\u7b26\u4e32\uff1aJava \u4e2d\u7684\u5783\u573e\u6536\u96c6\u548c\u4e0d\u53d8\u6027\u63d2\u56fe\" alt=\"\u5b57\u7b26\u4e32\uff1aJava \u4e2d\u7684\u5783\u573e\u6536\u96c6\u548c\u4e0d\u53d8\u6027\u63d2\u56fe\" \/><\/p>\n<p>\u5728java\u4e2d\uff0c\u5b57\u7b26\u4e32\u7531\u4e8e\u5176<strong>\u4e0d\u53d8\u6027<\/strong>\u548c<strong>\u5b9e\u4e60<\/strong>\u7279\u6027\u5728\u5185\u5b58\u7ba1\u7406\u4e2d\u53d1\u6325\u7740\u72ec\u7279\u7684\u4f5c\u7528\u3002\u8fd9\u4e9b\u6982\u5ff5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5f15\u5165\u5185\u5b58\u5904\u7406\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u8fd9\u5728\u9762\u8bd5\u4e2d\u901a\u5e38\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u6df1\u5165\u63a2\u8ba8<strong>\u5783\u573e\u6536\u96c6<\/strong>\u548c<strong>\u4e0d\u53d8\u6027<\/strong>\uff0c\u5e76\u6ce8\u610f\u5b57\u7b26\u4e32\u6c60\u548cjvm\u5185\u5b58\u7ba1\u7406\u5982\u4f55\u4e0e\u8fd9\u4e9b\u6982\u5ff5\u4ea4\u4e92\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u5efa\u7acb\u5728\u4e0a\u4e00\u7bc7\u5173\u4e8e\u5b57\u7b26\u4e32\u6c60\u548c\u5185\u5b58\u7ba1\u7406\u7684\u6587\u7ae0\u4e2d\u8ba8\u8bba\u7684\u6982\u5ff5\u7684\u57fa\u7840\u4e0a\u3002\u9996\u5148\u67e5\u770b\u8be5\u6587\u7ae0\u5c06\u4e3a\u7406\u89e3\u6b64\u5904\u6db5\u76d6\u7684\u4e3b\u9898\u63d0\u4f9b\u6709\u7528\u7684\u57fa\u7840\u3002<\/p>\n<hr>\n<h2> 1. \u5b57\u7b26\u4e32\u5783\u573e\u6536\u96c6 <\/h2>\n<p>\u5728 java \u4e2d\uff0c\u5b57\u7b26\u4e32\u6587\u5b57\u5728\u5783\u573e\u56de\u6536 (gc) \u65b9\u9762\u7684\u884c\u4e3a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1\u3002\u5b57\u7b26\u4e32\u6c60\u4e2d\u672a\u5f15\u7528\u7684\u6587\u5b57<\/strong><\/p>\n<pre>string string3 = \"world\"; \/\/ stored in string pool\n\n\/\/ a new string is created in the pool due to case-sensitivity\nstring3 = \"world\";\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u539f\u59cb\u201cworld\u201d\u4ecd\u5728\u5b57\u7b26\u4e32\u6c60\u4e2d\uff0c\u5373\u4f7f string3 <strong>\u91cd\u65b0\u5206\u914d<\/strong>\u3002<\/p>\n<\/li>\n<li>\n<p>jvm <strong>\u5728\u6c60\u4e2d\u4fdd\u7559<\/strong>\u672a\u5f15\u7528\u7684\u6587\u5b57\uff0c\u5141\u8bb8\u5c06\u6765\u91cd\u7528\uff0c\u4f46\u8fd9\u4e9b\u6587\u5b57\u4e0d\u4f1a\u50cf\u5e38\u89c4\u5806\u5bf9\u8c61\u90a3\u6837\u53d7\u5230\u5783\u573e\u56de\u6536\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>2\u3002\u5806\u5bf9\u8c61<\/strong><\/p>\n<pre>string str1 = new string(\"world\"); \/\/ stored in heap\n\n\/\/ string pool reference is used now\n\/\/ leaving the previous \"world\" eligible for gc in heap\nstr1 = \"world\";\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 new \u521b\u5efa\u65f6\uff0c\u5b57\u7b26\u4e32\u4f1a\u8fdb\u5165 <strong>\u5806\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u6c60<\/strong>\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5f15\u7528\u53d1\u751f\u53d8\u5316\uff0c\u5c31\u50cf str1 \u4e00\u6837\uff0c\u5806\u4e2d\u672a\u4f7f\u7528\u7684\u201cworld\u201d\u5b57\u7b26\u4e32<strong>\u53ef\u4ee5\u88ab<\/strong>\u5783\u573e\u56de\u6536\uff0c\u56e0\u4e3a\u5b83\u4e0d\u518d\u88ab\u5f15\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2> 2. \u5b57\u7b26\u4e32\u4e0d\u53d8\u6027 <\/h2>\n<p>java \u4e2d\u7684\u5b57\u7b26\u4e32<strong>\u4e0d\u53ef\u53d8<\/strong>\u2014\u2014\u4e00\u65e6\u521b\u5efa\uff0c\u5c31\u65e0\u6cd5\u4fee\u6539\u3002\u4efb\u4f55\u201c\u4fee\u6539\u201d\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u66f4\u6539\u73b0\u6709\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002<\/p>\n<h3> \u4e0d\u53d8\u6027\u7684\u5b9e\u9645\u6548\u679c <\/h3>\n<p><strong>1\u3002\u7f16\u8bd1\u65f6\u4e32\u8054\uff08\u4f7f\u7528\u6587\u5b57\u4f18\u5316\uff09<\/strong><\/p>\n<pre>   string string5 = \"this\" + \"string\";\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\n<p>\u5f53<strong>\u4e32\u8054\u6587\u5b57<\/strong>\u65f6\uff0c\u901a\u8fc7\u5728<strong>\u7f16\u8bd1\u65f6<\/strong>\u6267\u884c\u4e32\u8054\u6765\u4f18\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>\u751f\u6210\u7684\u5b57\u7b26\u4e32\uff08\u201cthisstring\u201d\uff09\u76f4\u63a5\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u6c60\u4e2d\uff0c\u5b8c\u5168\u907f\u514d\u4e86\u5806\u3002<\/p>\n<\/li>\n<li>\n<p>\u6b64\u8fc7\u7a0b\u4e5f\u79f0\u4e3a<strong>\u6052\u5b9a\u6c60\u6298\u53e0<\/strong>\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>2\u3002\u8fd0\u884c\u65f6\u4e32\u8054\uff08\u65e0\u4f18\u5316\uff09<\/strong><\/p>\n<pre>   string string1 = \"hello\";\n   string1 = string1 + \"hello\"; \/\/ stored in heap\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\n<p>\u5f53<strong>\u4e00\u4e2a\u6216\u591a\u4e2a\u64cd\u4f5c\u6570<\/strong>\u662f\u53d8\u91cf\uff08\u975e\u6587\u5b57\uff09\u65f6\uff0c\u8fde\u63a5\u53d1\u751f\u5728<strong>\u8fd0\u884c\u65f6<\/strong>\uff0c\u4ea7\u751f\u4e00\u4e2a\u4e0d\u9a7b\u7559\u5728\u5b57\u7b26\u4e32\u6c60\u4e2d\u7684\u5806\u5bf9\u8c61.<\/p>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a\u539f\u59cb\u201chello\u201d\u6587\u5b57\u4fdd\u7559\u5728\u6c60\u4e2d\uff0c\u800c\u8fde\u63a5\u7684\u201chellohello\u201d\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u5806\u4e2d\uff0c\u786e\u8ba4\u539f\u59cb\u201chello\u201d\u7684\u4e0d\u53d8\u6027\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>3\u3002\u4f7f\u7528 concat() \u65b9\u6cd5<\/strong><\/p>\n<pre>   string string2 = \"hello\";\n   string2 = string2.concat(\"hello\"); \/\/ stored in heap\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>concat() \u65b9\u6cd5\u59cb\u7ec8\u6267\u884c <strong>\u8fd0\u884c\u65f6<\/strong> \u8fde\u63a5\uff0c\u5c06\u65b0\u5b57\u7b26\u4e32\u653e\u7f6e\u5728 <strong>\u5806<\/strong> \u4e2d\uff0c\u65e0\u8bba\u64cd\u4f5c\u6570\u662f<strong>\u6587\u5b57\u8fd8\u662f\u53d8\u91cf<\/strong>\u3002<\/li>\n<\/ul>\n<p><strong>4\u3002\u5e94\u7528 intern() \u65b9\u6cd5<\/strong><\/p>\n<pre>   String string3 = \"World\".concat(\" says Hello\").intern();\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fc7\u7a0b<\/strong>\uff1a<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\uff0cconcat() \u64cd\u4f5c\u5728 <strong>\u5806<\/strong> \u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\uff0c\u5176\u503c\u4e3a\u201cworld say hello\u201d\uff0cstring3 \u6700\u521d\u5f15\u7528\u8be5\u5b57\u7b26\u4e32\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u6211\u4eec\u8c03\u7528 intern() \u65f6\uff0c\u5b83\u4f1a\u68c0\u67e5\u8be5\u503c\u662f\u5426\u5df2\u7ecf\u5728 <strong>\u5b57\u7b26\u4e32\u6c60<\/strong> \u4e2d\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5b83\u5c06\u5c06\u8be5\u503c\u6dfb\u52a0\u5230\u6c60\u4e2d\u5e76\u8fd4\u56de\u5bf9\u8be5\u6c60\u5b9e\u4f8b\u7684\u5f15\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528 intern() \u540e\uff0cstring3 \u6307\u5411\u5b57\u7b26\u4e32\u7684\u6c60\u5316\u526f\u672c\u3002\u539f\u59cb\u5806\u5b9e\u4f8b\u73b0\u5728\u6ca1\u6709\u4efb\u4f55\u6d3b\u52a8\u5f15\u7528\uff0c\u53ef\u4ee5\u8fdb\u884c\u5783\u573e\u56de\u6536\uff0c\u4ece\u800c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h3> \u8981\u70b9\u603b\u7ed3 <\/h3>\n<ul>\n<li> <strong>\u5b57\u7b26\u4e32\u6c60\u4f18\u5316<\/strong>\uff1a\u5b57\u7b26\u4e32\u6c60\u5bf9\u4e8e\u901a\u8fc7\u91cd\u7528\u6587\u5b57\u6765\u8282\u7701\u5185\u5b58\u81f3\u5173\u91cd\u8981\u3002<\/li>\n<li> <strong>\u4e0d\u53d8\u6027<\/strong>\uff1a\u4e0d\u53d8\u6027\u4f7f\u5b57\u7b26\u4e32\u7ebf\u7a0b\u5b89\u5168\u4e14\u53ef\u9760\uff0c\u4f46\u5728\u4fee\u6539\u5b57\u7b26\u4e32\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u989d\u5916\u7684\u5806\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u5783\u573e\u6536\u96c6<\/strong>\uff1a\u6c60\u4e2d\u672a\u5f15\u7528\u7684\u6587\u5b57<strong>\u4e0d\u4f1a<\/strong>\u88ab\u5783\u573e\u6536\u96c6\uff0c<strong>\u4f46\u662f<\/strong>\u672a\u4f7f\u7528\u7684\u57fa\u4e8e\u5806\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7406\u89e3\u548c\u5229\u7528\u8fd9\u4e9b\u539f\u5219\uff0cjava \u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7f16\u5199\u5185\u5b58\u6548\u7387\u66f4\u9ad8\u3001\u6027\u80fd\u66f4\u9ad8\u7684\u4ee3\u7801\u3002<\/p>\n<hr>\n<h2> \u76f8\u5173\u5e16\u5b50 <\/h2>\n<ul>\n<li>java \u57fa\u7840\u77e5\u8bc6<\/li>\n<li>\u6570\u7ec4\u9762\u8bd5\u8981\u70b9<\/li>\n<li>java \u5185\u5b58\u8981\u70b9<\/li>\n<li>java \u5173\u952e\u5b57\u8981\u70b9<\/li>\n<li>java oop \u57fa\u7840\u77e5\u8bc6<\/li>\n<li>\u96c6\u5408\u6846\u67b6\u8981\u70b9<\/li>\n<\/ul>\n<p>\u7f16\u7801\u5feb\u4e50\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b57\u7b26\u4e32\uff1aJava \u4e2d\u7684\u5783\u573e\u6536\u96c6\u548c\u4e0d\u53d8\u6027\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728java\u4e2d\uff0c\u5b57\u7b26\u4e32\u7531\u4e8e\u5176\u4e0d\u53d8\u6027\u548c\u5b9e\u4e60\u7279\u6027\u5728\u5185\u5b58\u7ba1\u7406\u4e2d\u53d1\u6325\u7740\u72ec\u7279\u7684\u4f5c\u7528\u3002\u8fd9\u4e9b\u6982\u5ff5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5f15\u5165\u5185\u5b58\u5904\u7406\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u8fd9\u5728\u9762\u8bd5\u4e2d\u901a\u5e38\u81f3\u5173\u91cd\u8981\u3002 \u8ba9\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u5783\u573e\u6536\u96c6\u548c\u4e0d\u53d8\u6027\uff0c\u5e76\u6ce8\u610f\u5b57\u7b26\u4e32\u6c60\u548cjvm\u5185\u5b58\u7ba1\u7406\u5982\u4f55\u4e0e\u8fd9\u4e9b\u6982\u5ff5\u4ea4\u4e92\u3002 \u8fd9\u7bc7\u6587\u7ae0\u5efa\u7acb\u5728\u4e0a\u4e00\u7bc7\u5173\u4e8e\u5b57\u7b26\u4e32\u6c60\u548c\u5185\u5b58\u7ba1\u7406\u7684\u6587\u7ae0\u4e2d\u8ba8\u8bba\u7684\u6982\u5ff5\u7684\u57fa\u7840\u4e0a\u3002\u9996\u5148\u67e5\u770b\u8be5\u6587\u7ae0\u5c06\u4e3a\u7406\u89e3\u6b64\u5904\u6db5\u76d6\u7684\u4e3b\u9898\u63d0\u4f9b\u6709\u7528\u7684\u57fa\u7840\u3002 1. \u5b57\u7b26\u4e32\u5783\u573e\u6536\u96c6 \u5728 java \u4e2d\uff0c\u5b57\u7b26\u4e32\u6587\u5b57\u5728\u5783\u573e\u56de\u6536 (gc) \u65b9\u9762\u7684\u884c\u4e3a\u6709\u6240\u4e0d\u540c\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1\u3002\u5b57\u7b26\u4e32\u6c60\u4e2d\u672a\u5f15\u7528\u7684\u6587\u5b57 string string3 = &#8220;world&#8221;; \/\/ stored in string pool \/\/ a new string is created in the pool due to case-sensitivity string3 = &#8220;world&#8221;; \u767b\u5f55\u540e\u590d\u5236 \u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u539f\u59cb\u201cworld\u201d\u4ecd\u5728\u5b57\u7b26\u4e32\u6c60\u4e2d\uff0c\u5373\u4f7f string3 \u91cd\u65b0\u5206\u914d\u3002 jvm \u5728\u6c60\u4e2d\u4fdd\u7559\u672a\u5f15\u7528\u7684\u6587\u5b57\uff0c\u5141\u8bb8\u5c06\u6765\u91cd\u7528\uff0c\u4f46\u8fd9\u4e9b\u6587\u5b57\u4e0d\u4f1a\u50cf\u5e38\u89c4\u5806\u5bf9\u8c61\u90a3\u6837\u53d7\u5230\u5783\u573e\u56de\u6536\u3002 2\u3002\u5806\u5bf9\u8c61 string str1 = new string(&#8220;world&#8221;); \/\/ stored in heap \/\/ string pool [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36076","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36076"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36076\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}