{"id":3681,"date":"2025-04-26T20:10:29","date_gmt":"2025-04-26T12:10:29","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3681"},"modified":"2025-04-26T20:10:29","modified_gmt":"2025-04-26T12:10:29","slug":"stringequals-%e5%92%8c-objectequals-%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/26\/stringequals-%e5%92%8c-objectequals-%e7%9a%84%e5%8c%ba%e5%88%ab\/","title":{"rendered":"String#equals()\u00a0\u548c\u00a0Object#equals()\u00a0\u7684\u533a\u522b"},"content":{"rendered":"\n<p>\u5728\u9762\u8bd5\u4e2d\uff0c\u5982\u679c\u88ab\u95ee\u5230 <strong><code>String#equals()<\/code><\/strong> \u548c <strong><code>Object#equals()<\/code><\/strong> \u7684\u533a\u522b\uff0c\u4e3b\u8981\u662f\u8981\u7406\u89e3\u5b83\u4eec\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u884c\u4e3a\u548c\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u5b83\u4eec\u7684\u533a\u522b\u4ee5\u53ca\u5b9e\u73b0\u7684\u7ec6\u8282\uff1a<\/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><code>Object#equals()<\/code> \u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Object#equals()<\/code> \u662f Java \u4e2d\u6240\u6709\u7c7b\u7684\u57fa\u7c7b <code>Object<\/code> \u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002\u6240\u6709\u7684\u7c7b\u90fd\u7ee7\u627f\u4e86\u8fd9\u4e2a\u65b9\u6cd5\u3002\u5982\u679c\u6ca1\u6709\u91cd\u5199\u5b83\uff0c\u9ed8\u8ba4\u7684\u884c\u4e3a\u662f\u6bd4\u8f83\u5f15\u7528\u662f\u5426\u76f8\u7b49\uff0c\u5373\u6bd4\u8f83\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u662f\u5426\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u9ed8\u8ba4\u5b9e\u73b0<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Object#equals()<\/code> \u7684\u9ed8\u8ba4\u5b9e\u73b0\u901a\u8fc7 <code>==<\/code> \u6765\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u662f\u5426\u76f8\u540c\uff0c\u53ea\u6709\u5f53\u4e24\u4e2a\u5f15\u7528\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u65f6\uff0c\u8fd4\u56de <code>true<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public boolean equals(Object obj) {\n    return (this == obj);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u7279\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9ed8\u8ba4\u5b9e\u73b0\u4ec5\u4ec5\u6bd4\u8f83\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\uff08\u5f15\u7528\u76f8\u7b49\uff09\uff0c\u4e0d\u8003\u8651\u5bf9\u8c61\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6ca1\u6709\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\uff0c\u90a3\u4e48 <code>Object#equals()<\/code> \u5c31\u662f\u7528\u6765\u5224\u65ad\u5f15\u7528\u76f8\u7b49\u7684\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>Object obj1 = new Object();\nObject obj2 = new Object();\nSystem.out.println(obj1.equals(obj2)); \/\/ false\uff0c\u56e0\u4e3a obj1 \u548c obj2 \u662f\u4e0d\u540c\u7684\u5bf9\u8c61<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>String#equals()<\/code> \u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>String#equals()<\/code> \u662f <code>String<\/code> \u7c7b\u4e2d\u91cd\u5199\u8fc7\u7684 <code>equals()<\/code> \u65b9\u6cd5\u3002\u5b83\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u662f\u5426\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5b9e\u73b0<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>String#equals()<\/code> \u65b9\u6cd5\u91cd\u5199\u4e86 <code>Object#equals()<\/code>\uff0c\u5b83\u4f1a\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u5e8f\u5217\u662f\u5426\u5b8c\u5168\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public boolean equals(Object obj) {\n    if (this == obj) return true; \/\/ \u5f15\u7528\u76f8\u7b49\n    if (obj == null || getClass() != obj.getClass()) return false; \/\/ \u7c7b\u578b\u68c0\u67e5\n    String other = (String) obj;\n    return value.equals(other.value); \/\/ \u6bd4\u8f83\u5b57\u7b26\u5185\u5bb9\n}<\/code><\/pre>\n\n\n\n<p><strong>\u7279\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>String#equals()<\/code> \u6bd4\u8f83\u7684\u662f\u5b57\u7b26\u4e32\u5185\u5bb9\u662f\u5426\u5b8c\u5168\u76f8\u540c\uff0c\u800c\u4e0d\u662f\u6bd4\u8f83\u5185\u5b58\u5730\u5740\u3002<\/li>\n\n\n\n<li>\u5373\u4f7f\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u5f0f\u521b\u5efa\u7684\uff0c\u53ea\u8981\u5b83\u4eec\u7684\u5185\u5bb9\u76f8\u540c\uff0c<code>equals()<\/code> \u8fd4\u56de <code>true<\/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.equals(str2)); \/\/ true\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u5185\u5bb9\u76f8\u540c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4e3b\u8981\u533a\u522b\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th><code>Object#equals()<\/code><\/th><th><code>String#equals()<\/code><\/th><\/tr><\/thead><tbody><tr><td><strong>\u9ed8\u8ba4\u884c\u4e3a<\/strong><\/td><td>\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\uff08\u5f15\u7528\u76f8\u7b49\uff09<\/td><td>\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff08\u5b57\u7b26\u5e8f\u5217\u76f8\u7b49\uff09<\/td><\/tr><tr><td><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/td><td>\u901a\u5e38\u7528\u4e8e\u6bd4\u8f83\u5bf9\u8c61\u7684\u5f15\u7528\u662f\u5426\u76f8\u7b49<\/td><td>\u7528\u4e8e\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c<\/td><\/tr><tr><td><strong>\u91cd\u5199\u60c5\u51b5<\/strong><\/td><td>\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u82e5\u4e0d\u91cd\u5199\u5219\u6309\u5f15\u7528\u6bd4\u8f83<\/td><td>\u5df2\u91cd\u5199\uff0c\u6bd4\u8f83\u5b57\u7b26\u4e32\u5185\u5bb9<\/td><\/tr><tr><td><strong>\u8fd4\u56de\u503c<\/strong><\/td><td>\u4ec5\u5728\u4e24\u4e2a\u5f15\u7528\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u65f6\u8fd4\u56de <code>true<\/code><\/td><td>\u5728\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u5e8f\u5217\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\u8fd4\u56de <code>true<\/code><\/td><\/tr><tr><td><strong>\u6bd4\u8f83\u5185\u5bb9<\/strong><\/td><td>\u5185\u5b58\u5730\u5740<\/td><td>\u5b57\u7b26\u5185\u5bb9<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u9762\u8bd5\u56de\u7b54\u8981\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Object#equals()<\/code> \u662f Java \u4e2d\u6240\u6709\u7c7b\u7684\u57fa\u7c7b <code>Object<\/code> \u4e2d\u7684\u9ed8\u8ba4\u65b9\u6cd5\uff0c\u6bd4\u8f83\u7684\u662f\u5f15\u7528\u662f\u5426\u76f8\u540c\uff0c\u9ed8\u8ba4\u5b9e\u73b0\u662f\u901a\u8fc7 <code>==<\/code> \u6bd4\u8f83\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002<\/li>\n\n\n\n<li><code>String#equals()<\/code> \u662f <code>String<\/code> \u7c7b\u91cd\u5199\u8fc7\u7684 <code>equals()<\/code> \u65b9\u6cd5\uff0c\u6bd4\u8f83\u7684\u662f\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff08\u5b57\u7b26\u5e8f\u5217\uff09\u662f\u5426\u76f8\u540c\u3002<\/li>\n\n\n\n<li>\u9700\u8981\u6ce8\u610f\uff0c<code>String<\/code> \u7c7b\u4e2d\u7684 <code>equals()<\/code> \u65b9\u6cd5\u66f4\u5177\u4f53\u5730\u5224\u65ad\u5b57\u7b26\u4e32\u5185\u5bb9\u662f\u5426\u76f8\u7b49\uff0c\u4e14\u5b83\u4e0d\u4f1a\u8003\u8651\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u662f\u76f8\u540c\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff08\u5373\u5b83\u6bd4\u8f83\u7684\u662f\u5185\u5bb9\u800c\u975e\u5185\u5b58\u5730\u5740\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5e38\u89c1\u7684\u9762\u8bd5\u9898\u53d8\u4f53<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5982\u679c\u6ca1\u6709\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\uff0c<code>String<\/code> \u548c <code>Object<\/code> \u7684 <code>equals()<\/code> \u4f1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/li>\n\n\n\n<li>\u5982\u679c <code>String<\/code> \u6ca1\u6709\u91cd\u5199 <code>equals()<\/code> \u65b9\u6cd5\uff0c\u5b83\u4f1a\u4f7f\u7528 <code>Object<\/code> \u7c7b\u7684\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u6bd4\u8f83\u7684\u662f\u5f15\u7528\u662f\u5426\u76f8\u7b49\uff0c\u800c\u4e0d\u662f\u5185\u5bb9\u662f\u5426\u76f8\u540c\u3002<\/li>\n\n\n\n<li><strong><code>String<\/code> \u4e2d <code>equals()<\/code> \u65b9\u6cd5\u7684\u5b9e\u73b0\u4e3a\u4ec0\u4e48\u6bd4 <code>Object<\/code> \u4e2d\u7684 <code>equals()<\/code> \u66f4\u590d\u6742\uff1f<\/strong><\/li>\n\n\n\n<li>\u56e0\u4e3a <code>String#equals()<\/code> \u9700\u8981\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5185\u5b58\u5730\u5740\uff0c\u56e0\u6b64\u5b9e\u73b0\u4e2d\u9700\u8981\u66f4\u591a\u7684\u903b\u8f91\u6765\u786e\u4fdd\u5185\u5bb9\u76f8\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u8be6\u7ec6\u89e3\u91ca\uff0c\u4f60\u4e0d\u4ec5\u80fd\u591f\u7406\u89e3 <code>String#equals()<\/code> \u548c <code>Object#equals()<\/code> \u4e4b\u95f4\u7684\u533a\u522b\uff0c\u8fd8\u80fd\u5728\u9762\u8bd5\u4e2d\u6e05\u6670\u5730\u8868\u8fbe\u5b83\u4eec\u7684\u5b9e\u73b0\u548c\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9762\u8bd5\u4e2d\uff0c\u5982\u679c\u88ab\u95ee\u5230 String#equals() \u548c Object#equals() \u7684\u533a\u522b\uff0c\u4e3b\u8981\u662f\u8981\u7406\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3682,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-3681","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\/3681","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=3681"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3681\/revisions"}],"predecessor-version":[{"id":3683,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3681\/revisions\/3683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3682"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}