{"id":3678,"date":"2025-04-26T20:07:44","date_gmt":"2025-04-26T12:07:44","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3678"},"modified":"2025-04-26T20:07:44","modified_gmt":"2025-04-26T12:07:44","slug":"%e5%af%b9%e8%b1%a1%e7%9a%84%e7%9b%b8%e7%ad%89%e5%92%8c%e5%bc%95%e7%94%a8%e7%9b%b8%e7%ad%89%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/26\/%e5%af%b9%e8%b1%a1%e7%9a%84%e7%9b%b8%e7%ad%89%e5%92%8c%e5%bc%95%e7%94%a8%e7%9b%b8%e7%ad%89%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f\/","title":{"rendered":"\u5bf9\u8c61\u7684\u76f8\u7b49\u548c\u5f15\u7528\u76f8\u7b49\u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"\n<p>\u9762\u8bd5\u4e2d\u5e38\u5e38\u4f1a\u95ee\u5230<strong>\u5bf9\u8c61\u7684\u76f8\u7b49<\/strong>\u548c<strong>\u5f15\u7528\u76f8\u7b49<\/strong>\u7684\u533a\u522b\uff0c\u867d\u7136\u8fd9\u4e24\u4e2a\u6982\u5ff5\u770b\u8d77\u6765\u5f88\u76f8\u4f3c\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u5f88\u5927\u7684\u4e0d\u540c\u3002\u4e0b\u9762\u4ece\u9762\u8bd5\u7684\u89d2\u5ea6\uff0c\u7ed3\u5408\u5177\u4f53\u7684\u5b9a\u4e49\u548c\u4ee3\u7801\u4f8b\u5b50\uff0c\u6765\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e24\u8005\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u5f15\u7528\u76f8\u7b49\uff08Reference Equality\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f15\u7528\u76f8\u7b49<\/strong>\uff08Reference Equality\uff09\u662f\u6307\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u662f\u5426\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u64cd\u4f5c\u7b26<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u5f15\u7528\u76f8\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7279\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>==<\/code> \u6bd4\u8f83\u7684\u662f<strong>\u5185\u5b58\u5730\u5740<\/strong>\uff0c\u5373\u4e24\u4e2a\u5f15\u7528\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4e24\u4e2a\u5f15\u7528\u6307\u5411\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u5219 <code>==<\/code> \u8fd4\u56de <code>true<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>false<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f8b\u5b50<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String str1 = new String(\"hello\");\nString str2 = new String(\"hello\");\n\nSystem.out.println(str1 == str2); \/\/ false\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61<\/code><\/pre>\n\n\n\n<p>\u867d\u7136 <code>str1<\/code> \u548c <code>str2<\/code> \u7684\u5185\u5bb9\u76f8\u540c\uff08\u90fd\u662f <code>\"hello\"<\/code>\uff09\uff0c\u4f46\u5b83\u4eec\u662f\u901a\u8fc7 <code>new<\/code> \u521b\u5efa\u7684\u4e24\u4e2a\u4e0d\u540c\u5bf9\u8c61\uff0c\u56e0\u6b64\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u4e0d\u540c\uff0c<code>str1 == str2<\/code> \u8fd4\u56de <code>false<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5bf9\u8c61\u76f8\u7b49\uff08Object Equality\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5bf9\u8c61\u76f8\u7b49<\/strong>\uff08Object Equality\uff09\u662f\u6307<strong>\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c<\/strong>\u3002Java \u4e2d\u901a\u5e38\u901a\u8fc7 <code>equals()<\/code> \u65b9\u6cd5\u6765\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002\u4e0d\u540c\u7684\u7c7b\u53ef\u4ee5\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\uff0c\u5b9a\u4e49\u5185\u5bb9\u6bd4\u8f83\u7684\u89c4\u5219\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>equals()<\/code> \u65b9\u6cd5\u6765\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\u662f\u5426\u76f8\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7279\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>equals()<\/code> \u6bd4\u8f83\u7684\u662f\u5bf9\u8c61\u7684\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u5730\u5740\u3002<\/li>\n\n\n\n<li><code>equals()<\/code> \u662f\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u4e14\u901a\u5e38\u53ef\u4ee5\u88ab\u7c7b\u91cd\u5199\uff08\u8986\u76d6\uff09\u4ee5\u5b9a\u4e49\u5177\u4f53\u7684\u6bd4\u8f83\u89c4\u5219\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u7c7b\u6ca1\u6709\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\uff0c\u9ed8\u8ba4\u6bd4\u8f83\u7684\u662f\u5f15\u7528\u76f8\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f8b\u5b50<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String str1 = new String(\"hello\");\nString str2 = new String(\"hello\");\n\nSystem.out.println(str1.equals(str2)); \/\/ true\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u5185\u5bb9\u76f8\u540c<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u867d\u7136 <code>str1<\/code> \u548c <code>str2<\/code> \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61\uff08\u5185\u5b58\u5730\u5740\u4e0d\u540c\uff09\uff0c\u4f46\u662f\u5b83\u4eec\u7684\u5185\u5bb9\u662f\u76f8\u540c\u7684\uff0c\u56e0\u4e3a <code>String<\/code> \u7c7b\u91cd\u5199\u4e86 <code>equals()<\/code> \u65b9\u6cd5\u6765\u6bd4\u8f83\u5185\u5bb9\uff0c\u56e0\u6b64 <code>str1.equals(str2)<\/code> \u8fd4\u56de <code>true<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5173\u952e\u533a\u522b\u603b\u7ed3\uff1a<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7ef4\u5ea6<\/th><th>\u5f15\u7528\u76f8\u7b49\uff08<code>==<\/code>\uff09<\/th><th>\u5bf9\u8c61\u76f8\u7b49\uff08<code>equals()<\/code>\uff09<\/th><\/tr><\/thead><tbody><tr><td><strong>\u6bd4\u8f83\u5185\u5bb9<\/strong><\/td><td>\u6bd4\u8f83\u7684\u662f\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\uff08\u5f15\u7528\uff09\u662f\u5426\u76f8\u540c<\/td><td>\u6bd4\u8f83\u7684\u662f\u5bf9\u8c61\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c<\/td><\/tr><tr><td><strong>\u9ed8\u8ba4\u884c\u4e3a<\/strong><\/td><td>\u9ed8\u8ba4\u6bd4\u8f83\u5185\u5b58\u5730\u5740<\/td><td>\u9ed8\u8ba4\u6bd4\u8f83\u5185\u5b58\u5730\u5740\uff08\u5982\u679c\u6ca1\u6709\u91cd\u5199 <code>equals()<\/code>\uff09<\/td><\/tr><tr><td><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/td><td>\u5224\u65ad\u4e24\u4e2a\u5f15\u7528\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b<\/td><td>\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c<\/td><\/tr><tr><td><strong>\u91cd\u5199<\/strong><\/td><td>\u4e0d\u9700\u8981\u91cd\u5199<\/td><td>\u901a\u5e38\u9700\u8981\u91cd\u5199 <code>equals()<\/code> \u6765\u5b9a\u4e49\u5177\u4f53\u7684\u5185\u5bb9\u6bd4\u8f83\u65b9\u5f0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5e38\u89c1\u9762\u8bd5\u4e2d\u7684\u5751<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7c7b\u6ca1\u6709\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\u65f6<\/strong>\uff1a\u5982\u679c\u7c7b\u6ca1\u6709\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\uff0c<code>equals()<\/code> \u4f1a\u8c03\u7528 <code>Object<\/code> \u7c7b\u7684\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u800c\u9ed8\u8ba4\u5b9e\u73b0\u662f\u6bd4\u8f83\u5f15\u7528\u76f8\u7b49\uff08\u4e0e <code>==<\/code> \u76f8\u540c\uff09\u3002 \u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  class Person {\n      String name;\n      Person(String name) {\n          this.name = name;\n      }\n  }\n\n  Person p1 = new Person(\"John\");\n  Person p2 = new Person(\"John\");\n\n  System.out.println(p1.equals(p2)); \/\/ false, \u56e0\u4e3a\u6ca1\u6709\u91cd\u5199 equals()\uff0c\u9ed8\u8ba4\u6bd4\u8f83\u5f15\u7528<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u91cd\u5199 <code>equals()<\/code> \u65f6\u5e38\u89c1\u9519\u8bef<\/strong>\uff1a\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u9075\u5faa<strong>\u5bf9\u79f0\u6027\u3001\u53cd\u5c04\u6027\u3001\u4f20\u9012\u6027\u548c\u4e00\u81f4\u6027<\/strong>\u7b49\u89c4\u5219\u3002\u5426\u5219\u4f1a\u5bfc\u81f4\u4e0d\u53ef\u9884\u6599\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63a8\u8350<\/strong>\uff1a\u5bf9\u4e8e\u7c7b\u7684\u8bbe\u8ba1\uff0c\u901a\u5e38\u4f1a\u91cd\u5199 <code>equals()<\/code> \u548c <code>hashCode()<\/code> \u65b9\u6cd5\uff0c\u4fdd\u8bc1\u5b83\u4eec\u7684\u884c\u4e3a\u4e00\u81f4\u6027\u3002<code>equals()<\/code> \u65b9\u6cd5\u7528\u4e8e\u6bd4\u8f83\u5185\u5bb9\u76f8\u7b49\uff0c<code>hashCode()<\/code> \u7528\u4e8e\u54c8\u5e0c\u7ed3\u6784\uff08\u5982 <code>HashMap<\/code>\uff09\u4e2d\u7684\u5feb\u901f\u67e5\u627e\u3002<\/li>\n\n\n\n<li><strong>\u91cd\u5199 <code>equals()<\/code> \u548c <code>hashCode()<\/code> \u65f6<\/strong>\uff0c\u5fc5\u987b\u4fdd\u8bc1\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49\u65f6\uff0c\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\u4e5f\u5fc5\u987b\u76f8\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic boolean equals(Object obj) {\n    if (this == obj) return true; \/\/ \u5f15\u7528\u76f8\u7b49\n    if (obj == null || getClass() != obj.getClass()) return false;\n    Person person = (Person) obj;\n    return name != null ? name.equals(person.name) : person.name == null;\n}\n\n@Override\npublic int hashCode() {\n    return name != null ? name.hashCode() : 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u9762\u8bd5\u56de\u7b54\u8981\u70b9\u603b\u7ed3\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9762\u8bd5\u4e2d\uff0c\u5982\u679c\u4f60\u88ab\u95ee\u5230\u5bf9\u8c61\u76f8\u7b49\u548c\u5f15\u7528\u76f8\u7b49\u7684\u533a\u522b\uff0c\u9996\u5148\u8981\u660e\u786e\u5b83\u4eec\u7684\u5b9a\u4e49\u3002<\/li>\n\n\n\n<li><strong>\u5f15\u7528\u76f8\u7b49<\/strong>\u662f\u901a\u8fc7 <code>==<\/code> \u5224\u65ad\u5185\u5b58\u5730\u5740\u662f\u5426\u76f8\u540c\uff0c\u800c<strong>\u5bf9\u8c61\u76f8\u7b49<\/strong>\u662f\u901a\u8fc7 <code>equals()<\/code> \u6bd4\u8f83\u5bf9\u8c61\u5185\u5bb9\u662f\u5426\u76f8\u540c\u3002<\/li>\n\n\n\n<li>\u5f3a\u8c03\u7c7b\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\u5185\u5bb9\u6bd4\u8f83\u7684\u903b\u8f91\uff0c\u4ee5\u53ca\u5982\u4f55\u4fdd\u8bc1\u4e00\u81f4\u6027\uff08\u901a\u5e38\u540c\u65f6\u91cd\u5199 <code>hashCode()<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u6837\u56de\u7b54\uff0c\u4e0d\u4ec5\u80fd\u5c55\u793a\u51fa\u4f60\u5bf9 Java \u57fa\u672c\u6982\u5ff5\u7684\u7406\u89e3\uff0c\u8fd8\u80fd\u4f53\u73b0\u51fa\u4f60\u5bf9\u5b9e\u9645\u5f00\u53d1\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u5751\u7684\u8b66\u89c9\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u8bd5\u4e2d\u5e38\u5e38\u4f1a\u95ee\u5230\u5bf9\u8c61\u7684\u76f8\u7b49\u548c\u5f15\u7528\u76f8\u7b49\u7684\u533a\u522b\uff0c\u867d\u7136\u8fd9\u4e24\u4e2a\u6982\u5ff5\u770b\u8d77\u6765\u5f88\u76f8\u4f3c\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u5f88\u5927\u7684\u4e0d\u540c\u3002\u4e0b\u9762\u4ece\u9762\u8bd5\u7684\u89d2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-3678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3678"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3678\/revisions"}],"predecessor-version":[{"id":3680,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3678\/revisions\/3680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3679"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}