{"id":202616,"date":"2025-05-22T08:57:39","date_gmt":"2025-05-22T00:57:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202616\/"},"modified":"2025-05-22T08:57:39","modified_gmt":"2025-05-22T00:57:39","slug":"%e6%8f%90%e9%ab%98-java-%e5%87%bd%e6%95%b0%e5%86%85%e5%ad%98%e6%95%88%e7%8e%87%e7%9a%84%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202616\/","title":{"rendered":"\u63d0\u9ad8 Java \u51fd\u6570\u5185\u5b58\u6548\u7387\u7684\u6280\u5de7"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u63d0\u9ad8 Java \u51fd\u6570\u5185\u5b58\u6548\u7387\u7684\u6280\u5de7<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u63d0\u9ad8 Java \u51fd\u6570\u5185\u5b58\u6548\u7387\u7684\u6280\u5de7\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>\u63d0\u9ad8 Java \u51fd\u6570\u5185\u5b58\u6548\u7387\u7684\u6280\u5de7\u5305\u62ec\uff1a\u907f\u514d\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u3001\u4f7f\u7528\u57fa\u672c\u7c7b\u578b\u3001\u8c28\u614e\u4f7f\u7528\u96c6\u5408\u6846\u67b6\u548c\u4f7f\u7528\u503c\u4f20\u9012\u3002\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u4f18\u5316\u5185\u5b58\u4f7f\u7528\uff0c\u4ece\u800c\u6539\u5584\u51fd\u6570\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729924712671c8e687fbd5.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u63d0\u9ad8 Java \u51fd\u6570\u5185\u5b58\u6548\u7387\u7684\u6280\u5de7<\/strong><\/p>\n<p>Java \u662f\u4e00\u79cd\u5806\u5206\u914d\u8bed\u8a00\uff0c\u610f\u5473\u7740\u5b83\u5728\u5806\u5185\u5b58\u4e2d\u5206\u914d\u5bf9\u8c61\u3002\u5806\u662f\u4e00\u79cd\u7f13\u6162\u4e14\u6602\u8d35\u7684\u64cd\u4f5c\uff0c\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u6765\u63d0\u9ad8 Java \u51fd\u6570\u7684\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>1. \u907f\u514d\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61<\/strong><\/p>\n<p>\u5f53\u4e0d\u518d\u9700\u8981\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528 <code>null<\/code> \u6765\u8986\u76d6\u5f15\u7528\uff1a<\/p>\n<pre>String str = \"Hello\";\n\/\/ ... \u4f7f\u7528 str ...\nstr = null; \/\/ \u91ca\u653e\u5bf9 str \u5bf9\u8c61\u7684\u5f15\u7528<\/pre>\n<p><strong>2. \u8003\u8651\u4f7f\u7528\u57fa\u672c\u7c7b\u578b<\/strong><\/p>\n<p>\u57fa\u672c\u7c7b\u578b\uff08\u4f8b\u5982 int\u3001long\u3001double\uff09\u6bd4\u5bf9\u8c61\u5360\u7528\u66f4\u5c11\u7684\u5185\u5b58\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u6808\u4e0a\u5206\u914d\u3002\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u4f18\u5148\u4f7f\u7528\u57fa\u672c\u7c7b\u578b\uff1a<\/p>\n<pre>int count = 0; \/\/ \u800c\u4e0d\u662f Integer count = new Integer(0);<\/pre>\n<p><strong>3. \u4f7f\u7528\u96c6\u5408\u6846\u67b6\u65f6\u8981\u8c28\u614e<\/strong><\/p>\n<p>\u96c6\u5408\u6846\u67b6\uff08\u4f8b\u5982 ArrayList \u548c HashMap\uff09\u57fa\u4e8e\u5bf9\u8c61\uff0c\u56e0\u6b64\u5b83\u4eec\u4f1a\u6d88\u8017\u5927\u91cf\u5185\u5b58\u3002\u8003\u8651\u4ee5\u4e0b\u66ff\u4ee3\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\u4ee3\u66ff ArrayList<\/li>\n<li>\u4f7f\u7528\u539f\u59cb\u7c7b\u578b\u6620\u5c04\u4ee3\u66ff HashMap<\/li>\n<\/ul>\n<p><strong>4. \u4f7f\u7528\u503c\u4f20\u9012\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u4f20\u9012<\/strong><\/p>\n<p>Java \u4f7f\u7528\u5f15\u7528\u4f20\u9012\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u63a5\u6536\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u53d8\u91cf\u672c\u8eab\u3002\u4e3a\u4e86\u907f\u514d\u610f\u5916\u5730\u4fee\u6539\u539f\u59cb\u5bf9\u8c61\uff0c\u8bf7\u4f7f\u7528\u503c\u4f20\u9012\uff1a<\/p>\n<pre>\/\/ \u503c\u4f20\u9012\uff08\u521b\u5efa\u65b0\u526f\u672c\uff09\nvoid myFunction(int value) {\n  \/\/ ...\n}\n\n\/\/ \u5f15\u7528\u4f20\u9012\uff08\u4fee\u6539\u539f\u59cb\u5bf9\u8c61\uff09\nvoid myFunction(String text) {\n  \/\/ ...\n  text = \"Updated\"; \/\/ \u539f\u59cb text \u5bf9\u8c61\u88ab\u4fee\u6539\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b Java \u51fd\u6570\uff1a<\/p>\n<pre>public static List&lt;String&gt; getNames() {\n  List&lt;String&gt; names = new ArrayList&lt;&gt;();\n  names.add(\"John\");\n  names.add(\"Jane\");\n  names.add(\"Bob\");\n  return names;\n}<\/pre>\n<p><strong>\u4f18\u5316\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\u4ee3\u66ff ArrayList\uff1a<code>String[] names = { \"John\", \"Jane\", \"Bob\" };<\/code><\/li>\n<li>\u4f7f\u7528\u503c\u4f20\u9012\uff1a<code>public static String[] getNames()<\/code><\/li>\n<\/ul>\n<p><strong>\u4f18\u5316\u540e\u7684\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>public static String[] getNames() {\n  return new String[] { \"John\", \"Jane\", \"Bob\" };\n}<\/pre>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u63d0\u9ad8 Java \u51fd\u6570\u7684\u5185\u5b58\u6548\u7387\uff0c\u4ece\u800c\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u63d0\u9ad8 Java \u51fd\u6570\u5185\u5b58\u6548\u7387\u7684\u6280\u5de7\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u9ad8 Java \u51fd\u6570\u5185\u5b58\u6548\u7387\u7684\u6280&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202616","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202616"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202616\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}