{"id":10021019,"date":"2025-09-26T18:16:45","date_gmt":"2025-09-26T10:16:45","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021019"},"modified":"2025-09-26T18:16:45","modified_gmt":"2025-09-26T10:16:45","slug":"java%e4%b8%ad%e6%af%94%e8%be%83%e5%a4%a7%e5%b0%8f%e7%9a%84%e8%a7%84%e5%88%99%e5%92%8c%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021019","title":{"rendered":"java\u4e2d\u6bd4\u8f83\u5927\u5c0f\u7684\u89c4\u5219\u548c\u5e94\u7528\u573a\u666f"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u6700\u8fd1\u5728\u5199\u4ee3\u7801\u65f6\uff0c\u603b\u662f\u72af\u4e00\u4e2a\u4f4e\u7ea7\u7684\u9519\u8bef\uff0c\u5728\u5bf9\u8c61\u6bd4\u8f83\u65f6\u76f4\u63a5\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u6765\u8fdb\u884c\u6bd4\u8f83\uff0c\u9020\u6210\u4e86\u770b\u8d77\u6765\u6ca1\u6709\u95ee\u9898\uff0c\u5b9e\u5219\u95ee\u9898\u5f88\u5927\uff0c\u7ecf\u8fc7\u5b66\u4e60\uff0c\u628a\u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u76f4\u63a5\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u4ec0\u4e48\u65f6\u5019\u4e0d\u53ef\u4ee5\u8fd0\u7528\u8fdb\u884c\u603b\u7ed3\u3002<\/p>\n<h2>1.\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u5f15\u7528\u6570\u636e\u7c7b\u578b\u7684\u533a\u522b\uff1a<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7eac\u5ea6<\/th>\n<th>\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/th>\n<th>\u5bf9\u8c61\u7c7b\u578b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u5b58\u50a8\u4f4d\u7f6e<\/td>\n<td>\u6808\uff08Stack\uff09<\/td>\n<td>\u6808\u4e2d\u5b58\u5730\u5740\uff0c\u5806\uff08Heap\uff09\u4e2d\u5b58\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>\u503c\u7684\u5185\u5bb9<\/td>\n<td>\u771f\u5b9e\u7684\u6570\u503c<\/td>\n<td>\u5bf9\u8c61\u7684\u5730\u5740\uff08\u6307\u9488\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u8d4b\u503c\u884c\u4e3a<\/td>\n<td>\u503c\u4f20\u9012\uff08\u62f7\u8d1d\u503c\uff09<\/td>\n<td>\u5730\u5740\u62f7\u8d1d\uff08\u5171\u4eab\u5bf9\u8c61\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<ol>\n<li>\n<p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<pre><code>\u6808\u5185\u5b58 (Stack)\na -&gt; 2\nb -&gt; 2<\/code><\/pre>\n<p>\u5c31\u53ef\u4ee5\u7c7b\u6bd4\u6210\u4f60\u624b\u91cc\uff08\u6808\uff09\u62ff\u4e86\u4e24\u4e2a\ud83c\udf4e\uff0c\u5c0f\u5f20\u624b\u91cc\uff08\u6808\uff09\u4e5f\u62ff\u4e86\u4e24\u4e2a\ud83c\udf4e\uff0c\u6bd4\u8f83\u7684\u65f6\u5019\u6211\u4eec\u77e5\u9053\u53ea\u9700\u8981\u6bd4\u8f83\u8fd9\u4e24\u4e2a\ud83c\udf4e\u7684\u6570\u91cf\u662f\u5426\u76f8\u540c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f15\u7528\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<pre><code>\u6808\u5185\u5b58 (Stack)        \u5806\u5185\u5b58 (Heap)\ns1 \u2500\u2500&gt; [0x1001] \u2500\u2500\u2500\u25ba 2 (\u5bf9\u8c611)\ns2 \u2500\u2500&gt; [0x1002] \u2500\u2500\u2500\u25ba 2 (\u5bf9\u8c612\uff09\n<\/code><\/pre>\n<p>\u540c\u6837\u4e5f\u53ef\u4ee5\u7c7b\u6bd4\u6210\u4f60\u624b\u91cc\uff08\u6808\uff09\u6709\u4e2a\u5feb\u9012\u53d6\u4ef6\u7801\uff0c\u5c0f\u5f20\u624b\u91cc\uff08\u6808\uff09\u4e5f\u6709\u4e2a\u5feb\u9012\u53d6\u4ef6\u7801\uff0c\u6bcf\u4e2a\u5feb\u9012\u53d6\u4ef6\u7801\u53d6\u7684\ud83d\udce6\u4e2d\uff08\u5806\uff09\u90fd\u6709\u4e24\u4e2a\ud83c\udf4e\uff0c\u6240\u4ee5\u6bd4\u8f83\u7684\u65f6\u5019\u6211\u4eec\u8981\u533a\u5206\u6e05\u695a\u6bd4\u8f83\u7684\u662f\u8fd9\u4e2a\u53d6\u4ef6\u7801\u8fd8\u662f\u82f9\u679c\u7684\u6570\u91cf\u3002<\/p>\n<\/li>\n<\/ol>\n<h2>2.\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/h2>\n<p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5305\u62ec\u5c0f\u4e8e(&lt;)\uff0c\u5927\u4e8e(&gt;)\uff0c\u7b49\u4e8e(==)\uff0c\u4e0d\u7b49\u4e8e(!=)\uff0c\u5927\u4e8e\u7b49\u4e8e(&gt;=)\uff0c\u5c0f\u4e8e\u7b49\u4e8e(&lt;=)\u3002<\/p>\n<h4>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u516b\u5927\u7c7b\uff1a<\/h4>\n<ul>\n<li>int\uff0clong\uff0cshort\uff0cbyte<\/li>\n<li>float\uff0cdouble<\/li>\n<li>char<\/li>\n<li>boolean\uff08\u53ea\u80fd\u7528==\u548c!=\uff09<\/li>\n<\/ul>\n<p>\u4ee5\u4e09\u4e2a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e3a\u4f8b\uff1a<\/p>\n<pre><code>public class Main {\n    public static void main(String[] args) {\n        \/\/ 1. int\n        int i1 = 1000;\n        int i2 = 2000;\n        System.out.println(&quot;int \u6bd4\u8f83: &quot; + (i1 == i2));\n\n        \/\/ 2. char\n        char c1 = 'A';  \/\/ Unicode: 65\n        char c2 = 'B';  \/\/ Unicode: 66\n        System.out.println(&quot;char \u6bd4\u8f83: &quot; + (c1 &lt; c2));\n\n        \/\/ 3. boolean\n        boolean bool1 = true;\n        boolean bool2 = false;\n        System.out.println(&quot;boolean \u6bd4\u8f83: &quot; + (bool1 == bool2));\n    }\n}\n<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code>int \u6bd4\u8f83: false\nchar \u6bd4\u8f83: true\nboolean \u6bd4\u8f83: false<\/code><\/pre>\n<p>\u4ee5int\u4e3a\u4f8b\u5b50\uff0c\u6211\u4eec\u8fd9\u91cc\u6bd4\u8f83\u7684\u662f1000\u4e2a\ud83c\udf4e\u548c2000\u4e2a\ud83c\udf4e\u7684\u6570\u91cf\u662f\u5426\u4e00\u6837\u3002<\/p>\n<h2>3.\u5bf9\u8c61\u7c7b\u578b<\/h2>\n<p>\u5bf9\u8c61\u7c7b\u578b\u53ef\u5206\u4e3a\u56db\u7c7b\uff1a<\/p>\n<ul>\n<li>String(\u5b57\u7b26\u4e32\u7c7b\u578b\uff09<\/li>\n<li>\u5305\u88c5\u7c7b\u578b<\/li>\n<li>\u6570\u7ec4<\/li>\n<li>\u81ea\u5b9a\u4e49\u7c7b\u578b<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u4ee5\u524d\u4e24\u4e2a\u4e3a\u4f8b\u5b50\uff1a<\/p>\n<h4>1.String(\u5b57\u7b26\u4e32\u7c7b\u578b\uff09\uff1a<\/h4>\n<p>\u4ee5\u4ee3\u7801\u4e3a\u4f8b\uff1a<\/p>\n<pre><code>public class Main {\n    public static void main(String[] args) {\n        String s1 = new String(&quot;hello&quot;);\n        String s2 = new String(&quot;hello&quot;);\n\n        System.out.println(&quot;\u6253\u5370\u7ed3\u679c\uff1a&quot; + (s1 == s2));\n    }\n}<\/code><\/pre>\n<p>\u6211\u4eec\u5fc3\u91cc\u53ef\u80fd\u6709\u8d85\u8fc750%\u7684\u53ef\u80fd\u89c9\u5f97\u6253\u5370\u51fa\u6765\u7684\u662ftrue\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\uff1a<\/p>\n<pre><code>\u6253\u5370\u7ed3\u679c\uff1afalse\n<\/code><\/pre>\n<p>\u4e0d\u8003\u8651\u53d8\u91cf\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e2a==\u6bd4\u8f83\u7684\u53ea\u662f\u6808\u7684\u5927\u5c0f\uff0c\u4e5f\u5c31\u662f\ud83d\udce6\u5bf9\u5e94\u7684\u53d6\u4ef6\u7801\u7684\u6bd4\u8f83\uff0c\u81ea\u7136\u4e0d\u540c\u3002<br \/>\u90a3\u4e48\u5982\u4f55\u6bd4\u8f83\u5462\uff1f<br \/>1\ufe0f\u20e3 \u4f7f\u7528 equals \u65b9\u6cd5\u6bd4\u8f83\u5185\u5bb9(\u8bf4\u660e\uff1aequals \u6bd4\u8f83\u7684\u662f\u5806\u7684\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\ud83c\udf4e\u7684\u6570\u91cf\uff0c\u800c\u4e0d\u662f\u6808\u7684\u5927\u5c0f\uff0c\u4e5f\u5c31\u662f\ud83d\udce6\u7684\u53d6\u4ef6\u7801\uff09<\/p>\n<p>2\ufe0f\u20e3 \u4f7f\u7528 compareTo \u65b9\u6cd5\u6bd4\u8f83\uff0c\u4e0eequals\u65b9\u6cd5\u540c\u7406\uff0c\u4f46\u662f\u5b83\u53ef\u4ee5\u6839\u636e\u8fd4\u56de\u503c\u7684\u5927\u5c0f\u6765\u8fdb\u884c\u5224\u65ad\u5927\u5c0f<\/p>\n<ul>\n<li>\u7b49\u4e8e0 \u2192 \u4e24\u4e2a\u6570\u503c\u76f8\u7b49<\/li>\n<li>\u5c0f\u4e8e0 \u2192 \u5f53\u524d\u5bf9\u8c61\u5c0f\u4e8e\u53c2\u6570\u5bf9\u8c61<\/li>\n<li>\u5927\u4e8e0 \u2192 \u5f53\u524d\u5bf9\u8c61\u5927\u4e8e\u53c2\u6570\u5bf9\u8c61<\/li>\n<\/ul>\n<p>\u6b63\u786e\u7684\u6bd4\u8f83\uff1a<\/p>\n<pre><code>public class Main {\n    public static void main(String[] args) {\n        String s1 = new String(&quot;hello&quot;);\n        String s2 = new String(&quot;hello&quot;);\n\n        System.out.println(&quot;\u4f7f\u7528equals\u65b9\u6cd5\u6bd4\u8f83\uff1a&quot; + s1.equals(s2));\n        boolean isEqual = s1.compareTo(s2) == 0;\n        System.out.println(&quot;\u4f7f\u7528compareTo\u65b9\u6cd5\u6bd4\u8f83\uff1a&quot; + isEqual);\n    }\n}\n\n<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u4f7f\u7528equals\u65b9\u6cd5\u6bd4\u8f83\uff1atrue\n\u4f7f\u7528compareTo\u65b9\u6cd5\u6bd4\u8f83\uff1atrue<\/code><\/pre>\n<p>\u8fd9\u6b21\u7ec8\u4e8e\u662f\u6bd4\u8f83\ud83d\udce6\u7bb1\u91cc\u9762\u7684\u5185\u5bb9\u4e86\uff08\u4e0d\u8003\u8651\u7c7b\u578b\uff09\u3002<\/p>\n<h4>2.\u5305\u88c5\u7c7b\u578b\uff1a<\/h4>\n<p>\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230\u5305\u88c5\u7c7b\u578b\uff0c\u5305\u88c5\u7c7b\u578b\u548c\u516b\u5927\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e00\u4e00\u5bf9\u5e94\uff0c\u662f\u628a\u57fa\u672c\u7c7b\u578b\u5c01\u88c5\u6210\u5bf9\u8c61\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u57fa\u672c\u7c7b\u578b<\/th>\n<th>\u5305\u88c5\u7c7b<\/th>\n<th>\u57fa\u672c\u7c7b\u578b<\/th>\n<th>\u5305\u88c5\u7c7b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>byte<\/td>\n<td>Byte<\/td>\n<td>float<\/td>\n<td>Float<\/td>\n<\/tr>\n<tr>\n<td>short<\/td>\n<td>Short<\/td>\n<td>double<\/td>\n<td>Double<\/td>\n<\/tr>\n<tr>\n<td>int<\/td>\n<td>Integer<\/td>\n<td>char<\/td>\n<td>Character<\/td>\n<\/tr>\n<tr>\n<td>long<\/td>\n<td>Long<\/td>\n<td>boolean<\/td>\n<td>Boolean<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u76f4\u63a5\u7ed9\u51fa\u4f8b\u5b50\uff08\u4ee5\u4e24\u4e2a\u4e3a\u4f8b\u5b50\uff09\uff1a<\/p>\n<pre><code>public class Main {\n    public static void main(String[] args) {\n        Double x = 1.0;\n        Double y = 1.0;\n\n        System.out.println(&quot;\u76f4\u63a5\u6bd4\u8f83: &quot; + (x == y));\n        System.out.println(&quot;\u7528equals\u65b9\u6cd5\u6bd4\u8f83: &quot; + x.equals(y));\n        System.out.println(&quot;\u7528compareTo\u65b9\u6cd5\u6bd4\u8f83: &quot; + (x.compareTo(y) == 0));\n\n        Long m = 200L;\n        Long n = 200L;\n\n        System.out.println(&quot;\u76f4\u63a5\u6bd4\u8f83 &quot; + (m == n));\n        System.out.println(&quot;\u7528equals\u65b9\u6cd5\u6bd4\u8f83: &quot; + m.equals(n));\n        System.out.println(&quot;\u7528compareTo\u65b9\u6cd5\u6bd4\u8f83: &quot; + (m.compareTo(n) == 0));\n    }\n}\n<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u76f4\u63a5\u6bd4\u8f83: false\n\u7528equals\u65b9\u6cd5\u6bd4\u8f83: true\n\u7528compareTo\u65b9\u6cd5\u6bd4\u8f83: true\n\u76f4\u63a5\u6bd4\u8f83 false\n\u7528equals\u65b9\u6cd5\u6bd4\u8f83: true\n\u7528compareTo\u65b9\u6cd5\u6bd4\u8f83: true<\/code><\/pre>\n<h2>\u603b\u7ed3\uff1a<\/h2>\n<p>\u5728\u8fdb\u884c\u6bd4\u8f83\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9996\u5148\u8981\u5224\u65ad\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8fd8\u662f\u5bf9\u8c61\u7c7b\u578b\u7684\u6765\u51b3\u5b9a\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u8fd0\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u6765\u76f4\u63a5\u6bd4\u8f83\uff0c\u4e0d\u8981\u72af\u4e0b\u4f4e\u7ea7\u9519\u8bef\uff0c\u4e3a\u4ee5\u540e\u7559\u4e0b\u9690\u60a3\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047290263\">https:\/\/segmentfault.com\/a\/1190000047290263<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6700\u8fd1\u5728\u5199\u4ee3\u7801\u65f6\uff0c\u603b\u662f\u72af\u4e00\u4e2a\u4f4e\u7ea7\u7684\u9519\u8bef\uff0c\u5728\u5bf9\u8c61\u6bd4\u8f83\u65f6\u76f4\u63a5\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u6765\u8fdb\u884c\u6bd4\u8f83\uff0c\u9020\u6210\u4e86\u770b\u8d77\u6765\u6ca1\u6709\u95ee\u9898\uff0c\u5b9e\u5219 [&hellip;]<\/p>\n","protected":false},"author":2,"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-10021019","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\/10021019","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021019"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021019\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}