{"id":202591,"date":"2025-05-22T08:03:10","date_gmt":"2025-05-22T00:03:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202591\/"},"modified":"2025-05-22T08:03:10","modified_gmt":"2025-05-22T00:03:10","slug":"%e9%81%bf%e5%85%8d%e5%b8%b8%e8%a7%81%e7%9a%84-java-%e5%87%bd%e6%95%b0%e5%86%85%e5%ad%98%e4%bc%98%e5%8c%96%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202591\/","title":{"rendered":"\u907f\u514d\u5e38\u89c1\u7684 Java \u51fd\u6570\u5185\u5b58\u4f18\u5316\u9519\u8bef"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u907f\u514d\u5e38\u89c1\u7684 Java \u51fd\u6570\u5185\u5b58\u4f18\u5316\u9519\u8bef<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u907f\u514d\u5e38\u89c1\u7684 Java \u51fd\u6570\u5185\u5b58\u4f18\u5316\u9519\u8bef\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>\u5728 Java \u51fd\u6570\u4e2d\uff0c\u5e38\u89c1\u7684\u5185\u5b58\u4f18\u5316\u9519\u8bef\u5305\u62ec\uff1a\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\uff1a\u907f\u514d\u521b\u5efa\u77ed\u547d\u7684\u5bf9\u8c61\u3002\u8fc7\u65e9\u89e3\u5f15\u7528\uff1a\u5728\u51fd\u6570\u7ed3\u675f\u524d\u907f\u514d\u89e3\u5f15\u7528\u5c40\u90e8\u53d8\u91cf\u6216\u53c2\u6570\u3002\u672a\u5728 try-catch \u5757\u4e2d\u91ca\u653e\u5bf9\u8c61\uff1a\u5728\u6240\u6709\u9000\u51fa\u8def\u5f84\u4e2d\u91ca\u653e\u5bf9\u8c61\u3002\u672a\u8003\u8651 escape \u5206\u6790\uff1a\u4f7f\u7528\u5de5\u5177\u8bc6\u522b\u53ef\u80fd\u9003\u9038\u51fd\u6570\u8fb9\u754c\u7684\u5bf9\u8c61\u3002\u4f7f\u7528\u4e0d\u6070\u5f53\u7684\u5bb9\u5668\uff1a\u6839\u636e\u6570\u636e\u7279\u6027\u9009\u62e9\u5408\u9002\u7684\u5bb9\u5668\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729920690671c7eb29e027.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u907f\u514d\u5e38\u89c1\u7684 Java \u51fd\u6570\u5185\u5b58\u4f18\u5316\u9519\u8bef<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u8c28\u614e\u7ba1\u7406\u51fd\u6570\u5185\u521b\u5efa\u7684\u5bf9\u8c61\u81f3\u5173\u91cd\u8981\u3002\u4e0d\u5f53\u7684\u5bf9\u8c61\u521b\u5efa\u4f1a\u5bf9\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u548c\u5185\u5b58\u5360\u7528\u4ea7\u751f\u91cd\u5927\u5f71\u54cd\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5e38\u89c1\u7684 Java \u51fd\u6570\u5185\u5b58\u4f18\u5316\u9519\u8bef\uff0c\u5e76\u63d0\u4f9b\u5b9e\u6218\u6848\u4f8b\u548c\u89e3\u51b3\u65b9\u6848\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u63d0\u9ad8\u51fd\u6570\u7684\u6548\u7387\u3002<\/p>\n<p><strong>1. \u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u51fd\u6570\u5185\u521b\u5efa\u4e86\u77ed\u547d\u6216\u4e00\u6b21\u6027\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e9b\u5bf9\u8c61\u4f7f\u7528\u540e\u5c31\u7acb\u5373\u4e22\u5f03\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u5c3d\u91cf\u907f\u514d\u5728\u51fd\u6570\u5185\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u3002\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u6216\u5f15\u7528\u73b0\u6709\u5bf9\u8c61\u3002<\/p>\n<p><strong>2. \u8fc7\u65e9\u89e3\u5f15\u7528<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5728\u51fd\u6570\u7ed3\u675f\u4e4b\u524d\u5c31\u89e3\u5f15\u7528\u5c40\u90e8\u53d8\u91cf\u6216\u53c2\u6570\u3002\u5f53\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u8fd9\u4e9b\u5f15\u7528\u5c06\u5931\u6548\uff0c\u5bfc\u81f4 NullPointerException\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u5728\u9700\u8981\u65f6\u624d\u5bf9\u5c40\u90e8\u53d8\u91cf\u6216\u53c2\u6570\u8fdb\u884c\u89e3\u5f15\u7528\u3002\u5728\u51fd\u6570\u7ed3\u675f\u4e4b\u524d\uff0c\u4e0d\u8981\u91ca\u653e\u5bf9\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u8fc7\u65e9\u89e3\u5f15\u7528\u5bfc\u81f4 NullPointerException\npublic void example1() {\n    String str = \"Hello World\";\n    return str.toUpperCase(); \/\/ \u6b64\u5904 str \u5df2\u5931\u6548\n}\n\n\/\/ \u6b63\u786e\u7684\u505a\u6cd5\npublic void example2() {\n    String str = \"Hello World\";\n    String result = str.toUpperCase(); \/\/ \u5728\u8fd4\u56de\u524d\u521b\u5efa\u7ed3\u679c String \u5bf9\u8c61\n    return result;\n}<\/pre>\n<p><strong>3. \u672a\u5728 try-catch \u5757\u4e2d\u91ca\u653e\u5bf9\u8c61<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5728\u51fd\u6570\u7684 try-catch \u5757\u4e2d\u6355\u83b7\u5f02\u5e38\u540e\uff0c\u672a\u91ca\u653e\u5df2\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u8fd9\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u786e\u4fdd\u5728\u6240\u6709\u9000\u51fa\u8def\u5f84\u4e2d\uff08\u5305\u62ec try-catch \u5757\u548c finally \u5757\uff09\u91ca\u653e\u5c40\u90e8\u5bf9\u8c61\u3002<\/p>\n<p><strong>4. \u672a\u8003\u8651 escape \u5206\u6790<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u51fd\u6570\u5185\u7684\u5bf9\u8c61\u521b\u5efa\u53ef\u80fd\u4f1a\u9003\u9038\u51fd\u6570\u8fb9\u754c\uff0c\u5bfc\u81f4\u5728\u51fd\u6570\u8fd4\u56de\u540e\u4ecd\u7136\u53ef\u89c1\u3002\u8fd9\u4f1a\u59a8\u788d\u5bf9\u8c61\u56de\u6536\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528\u9003\u9038\u5206\u6790\u5de5\u5177\uff08\u5982 JaCkHammer\uff09\u6765\u6807\u8bc6\u53ef\u80fd\u9003\u9038\u51fd\u6570\u8fb9\u754c\u7684\u5bf9\u8c61\u3002\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u6216\u5f15\u7528\u73b0\u6709\u7684\u5bf9\u8c61\u6765\u907f\u514d\u5bf9\u8c61\u9003\u9038\u3002<\/p>\n<p><strong>5. \u4f7f\u7528\u4e0d\u6070\u5f53\u7684\u5bb9\u5668<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u9009\u62e9\u4f7f\u7528\u4e0d\u9002\u5408\u7279\u5b9a\u573a\u666f\u7684\u5bb9\u5668\u3002\u4f8b\u5982\uff0c\u8fc7\u65e9\u4f7f\u7528\u5927\u578b ArrayList\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u6839\u636e\u6570\u636e\u7279\u6027\u548c\u9884\u671f\u7528\u6cd5\u9009\u62e9\u5408\u9002\u7684\u5bb9\u5668\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5c11\u91cf\u5143\u7d20\uff0c\u4f7f\u7528\u6570\u7ec4\u6bd4 ArrayList \u66f4\u9ad8\u6548\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u907f\u514d\u5e38\u89c1\u7684 Java \u51fd\u6570\u5185\u5b58\u4f18\u5316\u9519\u8bef\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u7f16\u7a0b\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u907f\u514d\u5e38\u89c1\u7684 Java \u51fd\u6570\u5185\u5b58\u4f18&#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-202591","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202591","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=202591"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202591\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}