{"id":10020855,"date":"2025-06-24T14:28:58","date_gmt":"2025-06-24T06:28:58","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020855"},"modified":"2025-06-24T14:28:58","modified_gmt":"2025-06-24T06:28:58","slug":"java%e7%bc%96%e7%a8%8b%e6%9c%89%e5%93%aa%e4%ba%9b%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020855","title":{"rendered":"Java\u7f16\u7a0b\u6709\u54ea\u4e9b\u5e38\u89c1\u9519\u8bef\uff1f"},"content":{"rendered":"<p>\u4e0b\u9762\u662fJava\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u7684\u603b\u7ed3\uff0c\u7ed3\u5408\u6700\u65b0\u6280\u672f\u5b9e\u8df5\u6574\u7406\uff1a<\/p>\n<p>\u2699\ufe0f \u4e00\u3001\u7f16\u8bd1\u671f\u9519\u8bef\uff08\u7f16\u8bd1\u5668\u62a5\u9519\uff09<br \/>\u200c\u9519\u8bef\u7c7b\u578b\u200c    \u200c\u89e6\u53d1\u573a\u666f\u200c    \u200c\u89e3\u51b3\u65b9\u6848\u200c    \u200c\u793a\u4f8b\u4ee3\u7801\u200c<br \/>\u200c\u7b26\u53f7\u672a\u627e\u5230\u200c     \u672a\u58f0\u660e\u53d8\u91cf\/\u65b9\u6cd5\u3001\u5927\u5c0f\u5199\u9519\u8bef\u3001\u672a\u5bfc\u5165\u5305    \u68c0\u67e5\u58f0\u660e\u3001\u786e\u8ba4\u5927\u5c0f\u5199\u3001\u8865\u5145import\u8bed\u53e5    System.out.printl(&#8220;Hi&#8221;); \u2192 \u7f3a\u5c11n<br \/>\u200c\u6587\u4ef6\u4e0e\u7c7b\u540d\u4e0d\u5339\u914d\u200c     \u7c7b\u540d\u4e0e.java\u6587\u4ef6\u540d\u4e0d\u4e00\u81f4    \u4fee\u6539\u7c7b\u540d\u6216\u6587\u4ef6\u540d\u4fdd\u6301\u4e00\u81f4    class Foo{} \u4fdd\u5b58\u5728Bar.java<br \/>\u200c\u8bed\u6cd5\u7ed3\u6784\u7f3a\u5931\u200c     \u7f3a\u5c11\u5206\u53f7\u3001\u62ec\u53f7\u672a\u95ed\u5408\u3001\u5b57\u7b26\u4e32\u5f15\u53f7\u7f3a\u5931    IDE\u81ea\u52a8\u68c0\u67e5\u7f29\u8fdb\uff0c\u8865\u5145\u7f3a\u5931\u7b26\u53f7    String s = &#8220;Hello; \u2192 \u7f3a\u5c11\u95ed\u5408\u5f15\u53f7<br \/>\u200c\u975e\u6cd5\u8868\u8fbe\u5f0f\u5f00\u5934\u200c     \u8bed\u6cd5\u4f4d\u7f6e\u9519\u8bef\uff08\u5982\u65b9\u6cd5\u5185\u5d4c\u5957\u65b9\u6cd5\uff09    \u8c03\u6574\u4ee3\u7801\u7ed3\u6784\u7b26\u5408\u8bed\u6cd5\u89c4\u8303    void A() { void B() {&#8230;} } \u2192 \u975e\u6cd5\u5d4c\u5957<br \/>\ud83d\udca5 \u4e8c\u3001\u8fd0\u884c\u65f6\u5f02\u5e38\uff08RuntimeException\uff09<br \/>\u200c\u7a7a\u6307\u9488\u5f02\u5e38\uff08NullPointerException\uff09\u200c <\/p>\n<p>\u200c\u539f\u56e0\u200c\uff1a\u8c03\u7528null\u5bf9\u8c61\u7684\u65b9\u6cd5\u6216\u5c5e\u6027<br \/>\u200c\u89e3\u51b3\u200c\uff1a\u4f7f\u7528Optional\u5305\u88c5\u3001\u5224\u7a7a\u68c0\u67e5<br \/>java<br \/>Copy Code<br \/>String text = null;<br \/>System.out.println(Optional.ofNullable(text).orElse(&#8220;&#8221;)); \/\/ \u907f\u514dNPE<br \/>\u200c\u6570\u7ec4\u8d8a\u754c\uff08ArrayIndexOutOfBoundsException\uff09\u200c <\/p>\n<p>\u200c\u539f\u56e0\u200c\uff1a\u8bbf\u95ee\u975e\u6cd5\u7d22\u5f15\uff08\u5982\u7d22\u5f15\u2265\u6570\u7ec4\u957f\u5ea6\uff09<br \/>\u200c\u89e3\u51b3\u200c\uff1a\u5faa\u73af\u524d\u6821\u9a8c\u7d22\u5f15\u8303\u56f4<br \/>java<br \/>Copy Code<br \/>int[] arr = {1, 2, 3};<br \/>if (index &gt;= 0 &#038;&#038; index &lt; arr.length) {&#8230;}<br \/>\u200c\u7c7b\u578b\u8f6c\u6362\u5f02\u5e38\uff08ClassCastException\uff09\u200c <\/p>\n<p>\u200c\u539f\u56e0\u200c\uff1a\u5f3a\u5236\u8f6c\u6362\u4e0d\u517c\u5bb9\u7c7b\u578b<br \/>\u200c\u89e3\u51b3\u200c\uff1a\u4f7f\u7528instanceof\u9884\u5148\u68c0\u67e5<br \/>java<br \/>Copy Code<br \/>if (obj instanceof String) str = (String)obj;<br \/>\ud83d\udd04 \u4e09\u3001\u96c6\u5408\u64cd\u4f5c\u9677\u9631<br \/>\u200c\u904d\u5386\u65f6\u4fee\u6539\u96c6\u5408\u200c <\/p>\n<p>\u200c\u73b0\u8c61\u200c\uff1aConcurrentModificationException<br \/>\u200c\u89e3\u51b3\u200c\uff1a\u7528Iterator.remove()\u6216\u5e76\u53d1\u96c6\u5408\uff08\u5982CopyOnWriteArrayList\uff09<br \/>java<br \/>Copy Code<br \/>Iterator&lt;String&gt; it = list.iterator();<br \/>while (it.hasNext()) {<\/p>\n<pre><code>it.next(); it.remove(); \/\/ \u5b89\u5168\u79fb\u9664<\/code><\/pre>\n<p>}<br \/>\u200csubList\u5f15\u53d1\u7684\u5171\u4eab\u4fee\u6539\u200c <\/p>\n<p>\u200c\u95ee\u9898\u200c\uff1a\u4fee\u6539\u5b50\u5217\u8868\u4f1a\u540c\u6b65\u5f71\u54cd\u539f\u5217\u8868<br \/>\u200c\u89e3\u51b3\u200c\uff1a\u521b\u5efa\u65b0\u526f\u672cnew ArrayList&lt;&gt;(list.subList())<br \/>\u200c\u4e0d\u53ef\u53d8\u96c6\u5408\u8bef\u4fee\u6539\u200c <\/p>\n<p>\u200c\u6848\u4f8b\u200c\uff1aArrays.asList()\/Collections.singletonList()\u4e0d\u53ef\u53d8<br \/>\u200c\u65b9\u6848\u200c\uff1a\u9700\u53ef\u53d8\u65f6\u4f7f\u7528new ArrayList&lt;&gt;()\u5305\u88c5<br \/>\u26a1 \u56db\u3001\u5e76\u53d1\u4e0e\u8d44\u6e90\u95ee\u9898<br \/>\u200c\u7ebf\u7a0b\u5b89\u5168\u5de5\u5177\u8bef\u7528\u200c <\/p>\n<p>\u200c\u5178\u578b\u200c\uff1a\u591a\u7ebf\u7a0b\u4e0bSimpleDateFormat\u975e\u7ebf\u7a0b\u5b89\u5168<br \/>\u200c\u66ff\u4ee3\u200c\uff1aThreadLocal&lt;DateFormat&gt;\u6216DateTimeFormatter\uff08Java8+\uff09<br \/>\u200c\u8d44\u6e90\u6cc4\u6f0f\u200c <\/p>\n<p>\u200c\u573a\u666f\u200c\uff1a\u672a\u5173\u95edInputStream\/\u6570\u636e\u5e93\u8fde\u63a5<br \/>\u200c\u89c4\u8303\u200c\uff1a\u4f7f\u7528Try-with-Resources\u81ea\u52a8\u5173\u95ed<br \/>java<br \/>Copy Code<br \/>try (FileReader fr = new FileReader(&#8220;file.txt&#8221;)) {&#8230;}<br \/>\ud83e\udde0 \u4e94\u3001\u9690\u853d\u903b\u8f91\u9519\u8bef<br \/>\u200c\u81ea\u52a8\u62c6\u7bb1NPE\u200c <\/p>\n<p>java<br \/>Copy Code<br \/>Integer count = null;<br \/>int result = count; \/\/ \u89e6\u53d1NullPointerException\uff08\u81ea\u52a8\u62c6\u7bb1\uff09<br \/>\u200c\u65b9\u6848\u200c\uff1a\u5305\u88c5\u7c7b\u578b\u5224\u7a7a\u540e\u518d\u62c6\u7bb1<br \/>\u200c\u65e5\u671f\u6708\u4efd\u4ece0\u5f00\u59cb\u200c <\/p>\n<p>\u200c\u9677\u9631\u200c\uff1aCalendar.set(2025, 1, 1) \u2192 \u5b9e\u9645\u4e3a2\u67081\u65e5<br \/>\u200c\u6ce8\u610f\u200c\uff1a\u6708\u4efd\u53c2\u65700\u4ee3\u88681\u6708\uff0c1\u4ee3\u88682\u6708<br \/>\u4ee5\u4e0a\u9519\u8bef\u8986\u76d6\u4e86\u5f00\u53d1\u4e2d80%\u7684\u9ad8\u9891\u95ee\u9898\uff0c\u7ed3\u5408\u5355\u5143\u6d4b\u8bd5\uff08\u5982JUnit\uff09\u548c\u9759\u6001\u5206\u6790\u5de5\u5177\uff08\u5982SonarLint\uff09\u53ef\u663e\u8457\u964d\u4f4e\u51fa\u9519\u6982\u7387 \u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046802062\">https:\/\/segmentfault.com\/a\/1190000046802062<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662fJava\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u7684\u603b\u7ed3\uff0c\u7ed3\u5408\u6700\u65b0\u6280\u672f\u5b9e\u8df5\u6574\u7406\uff1a \u2699\ufe0f \u4e00\u3001\u7f16\u8bd1\u671f\u9519\u8bef\uff08\u7f16\u8bd1\u5668\u62a5\u9519 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020855","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020855"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020855\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}