{"id":202618,"date":"2025-05-22T09:33:02","date_gmt":"2025-05-22T01:33:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202618\/"},"modified":"2025-05-22T09:33:02","modified_gmt":"2025-05-22T01:33:02","slug":"%e4%bc%98%e5%8c%96-java-%e5%87%bd%e6%95%b0%e6%89%a7%e8%a1%8c%e6%95%88%e7%8e%87%e7%9a%84%e5%b8%b8%e8%a7%81%e6%8a%80%e6%9c%af%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202618\/","title":{"rendered":"\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u5e38\u89c1\u6280\u672f\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u5e38\u89c1\u6280\u672f\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u5e38\u89c1\u6280\u672f\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729924819671c8ed3716ea.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684 7 \u79cd\u5e38\u89c1\u6280\u672f<\/strong><\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>\u5728 Java \u5f00\u53d1\u4e2d\uff0c\u63d0\u5347\u51fd\u6570\u6267\u884c\u6548\u7387\u5bf9\u4e8e\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e03\u79cd\u5e38\u89c1\u7684\u4f18\u5316\u6280\u672f\uff0c\u52a9\u529b\u60a8\u63d0\u9ad8 Java \u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>1. \u53ca\u65e9\u8fd4\u56de<\/strong><\/p>\n<p>\u53ca\u65e9\u8fd4\u56de\u662f\u6307\u5728\u51fd\u6570\u8fbe\u5230\u5176\u9884\u671f\u7684\u76ee\u6807\u65f6\u7acb\u5373\u8fd4\u56de\u3002\u8fd9\u907f\u514d\u4e86\u5bf9\u4e0d\u5fc5\u8981\u7684\u4ee3\u7801\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public boolean isValid(int value) {\n    if (value &lt; 0) {\n        return false;\n    }\n    \/\/ ... \n}<\/pre>\n<p><strong>2. \u4f7f\u7528\u672c\u5730\u53d8\u91cf<\/strong><\/p>\n<p>\u5728\u5faa\u73af\u6216\u5d4c\u5957\u5757\u4e2d\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u51cf\u5c11\u5bf9\u57df\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u4ece\u800c\u51cf\u5c11\u67e5\u627e\u57df\u5bf9\u8c61\u6240\u9700\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public int sum(int[] numbers) {\n    int total = 0;\n    for (int i = 0; i &lt; numbers.length; i++) {\n        total += numbers[i];\n    }\n    return total;\n}<\/pre>\n<p><strong>3. \u7f13\u5b58\u7ed3\u679c<\/strong><\/p>\n<p>\u5bf9\u4e8e\u591a\u6b21\u8c03\u7528\u7684\u6602\u8d35\u64cd\u4f5c\uff0c\u7f13\u5b58\u7ed3\u679c\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>private Map&lt;Integer, String&gt; cache = new HashMap&lt;&gt;();\n\npublic String getName(int id) {\n    String name = cache.get(id);\n    if (name == null) {\n        name = Database.getNameById(id);\n        cache.put(id, name);\n    }\n    return name;\n}<\/pre>\n<p><strong>4. \u4f7f\u7528\u53cd\u5c04<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u53cd\u5c04\u53ef\u4ee5\u63d0\u9ad8\u6267\u884c\u6548\u7387\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u865a\u62df\u65b9\u6cd5\u8c03\u7528\u6240\u5e26\u6765\u7684\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>Method method = cls.getMethod(\"getName\");\nString name = (String) method.invoke(obj);<\/pre>\n<p><strong>5. \u5e76\u884c\u5316\u5904\u7406<\/strong><\/p>\n<p>\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5e76\u884c\u5316\u5904\u7406\u53ef\u4ee5\u5c06\u4efb\u52a1\u5206\u89e3\u4e3a\u8f83\u5c0f\u7684\u5757\uff0c\u5e76\u5728\u591a\u4e2a\u7ebf\u7a0b\u6216\u6838\u5fc3\u4e0a\u540c\u65f6\u6267\u884c\u5b83\u4eec\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ExecutorService executor = Executors.newFixedThreadPool(4);\nexecutor.submit(() -&gt; {\n    \/\/ \u4efb\u52a1 1\n});\nexecutor.submit(() -&gt; {\n    \/\/ \u4efb\u52a1 2\n});<\/pre>\n<p><strong>6. \u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u9009\u62e9\u9002\u5408\u7279\u5b9a\u4efb\u52a1\u7684\u9ad8\u6548\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 ArrayList \u5b58\u50a8\u65e0\u5e8f\u5217\u8868\nList&lt;Integer&gt; numbers = new ArrayList&lt;&gt;();\n\n\/\/ \u4f7f\u7528 HashMap \u5b58\u50a8\u952e\u503c\u5bf9\nMap&lt;Integer, String&gt; names = new HashMap&lt;&gt;();<\/pre>\n<p><strong>7. \u914d\u7f6e Java \u865a\u62df\u673a (JVM)<\/strong><\/p>\n<p>JVM \u53c2\u6570\u548c\u4f18\u5316\u53ef\u4ee5\u5f71\u54cd\u51fd\u6570\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>java -XX:+UseParallelGC -XX:+AggressiveOpts MyApplication<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4e0d\u9ad8\u6548\u7684\u51fd\u6570\uff1a<\/p>\n<pre>public boolean contains(int[] array, int value) {\n    for (int i = 0; i &lt; array.length; i++) {\n        if (array[i] == value) {\n            return true;\n        }\n    }\n    return false;\n}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53ca\u65e9\u8fd4\u56de\u548c\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u5bf9\u8be5\u51fd\u6570\u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<pre>public boolean contains(int[] array, int value) {\n    for (int i = 0; i &lt; array.length; i++) {\n        if (array[i] == value) {\n            return true;\n        } else if (array[i] &gt; value) {\n            return false; \/\/ \u53ca\u65e9\u8fd4\u56de\n        }\n    }\n    return false;\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u5e38\u89c1\u6280\u672f\u662f\u4ec0\u4e48\uff1f\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,\u6267\u884c\u6548\u7387\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f18\u5316 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u5e38&#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-202618","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202618","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=202618"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202618\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}