{"id":61,"date":"2021-10-02T08:38:27","date_gmt":"2021-10-02T00:38:27","guid":{"rendered":"https:\/\/www.inorysnow.cn\/?p=61"},"modified":"2021-10-12T12:07:33","modified_gmt":"2021-10-12T04:07:33","slug":"%e5%92%8cequals%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.inorysnow.cn\/?p=61","title":{"rendered":"==\u548cequals\u7684\u533a\u522b"},"content":{"rendered":"\n<p>\u5728\u6211\u7684\u8ba4\u8bc6\u4e2d==\u548cequals\u7684\u533a\u522b\u53ef\u4ee5\u7b80\u5355\u6982\u62ec\u4e3a\uff1a==\u6bd4\u8f83\u7684\u662f\u5185\u5b58\u5730\u5740\u662f\u5426\u4e00\u81f4\uff1bequals\u6bd4\u8f83\u7684\u662f\u5bf9\u8c61\u7684\u503c\u662f\u5426\u4e00\u81f4\u3002\u4f46\u662f\u5728\u6df1\u5165\u4e86\u89e3\u540e\u53d1\u73b0\u4e0d\u80fd\u8fd9\u4e48\u7b80\u5355\u7684\u8fdb\u884c\u533a\u5206\u3002<\/p>\n\n\n\n<p>==\u5728\u6bd4\u8f83\u65f6\u9488\u5bf9\u5bf9\u6570\u636e\u7c7b\u578b\u5206\u4e3a\u4e24\u7c7b\u60c5\u51b5\uff1a<\/p>\n\n\n\n<p>1\u3001\u6bd4\u8f83\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\uff08\u6574\u6570\u3001\u6d6e\u70b9\u3001\u5b57\u7b26\u3001\u5e03\u5c14\uff09\uff1a\u6bd4\u8f83\u7684\u662f\u503c\u662f\u5426\u4e00\u81f4<\/p>\n\n\n\n<p>2\u3001\u6bd4\u8f83\u5f15\u7528\u6570\u636e\u7c7b\u578b\u65f6\uff1a\u6bd4\u8f83\u7684\u662f\u5185\u5b58\u5730\u5740\u662f\u5426\u4e00\u81f4<\/p>\n\n\n\n<p>equals\u5728\u6bd4\u8f83\u65f6\u540c\u6837\u4e5f\u9700\u8981\u5206\u4e3a\u4e24\u7c7b\u60c5\u51b5\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\u9700\u8981\u77e5\u9053\uff0cJava\u4e2d\u6240\u6709\u7684\u7c7b\u90fd\u662f\u7ee7\u627f\u4e8eObject\u8fd9\u4e2a\u8d85\u7c7b\u7684\uff0c\u56e0\u6b64\u5728Object\u7c7b\u4e2d\uff0c\u5df2\u7ecf\u6709\u5b9a\u4e49\u8fc7\u4e00\u4e2aequals\u65b9\u6cd5\uff0c\u53ef\u4ee5\u770b\u5230\u8be5equals\u65b9\u6cd5\u5b9e\u9645\u4e0a\u4e5f\u8fd8\u662f\u901a\u8fc7==\u5b9e\u73b0\u7684\u6bd4\u8f83\uff0c\u6bd4\u8f83\u7684\u662f\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u662f\u5426\u4e00\u81f4\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u610f\u4e49\u4e0d\u5927\uff0c\u56e0\u6b64\u5728\u4e00\u4e9b\u7c7b\u5e93\u5f53\u4e2d\u8fd9\u4e2a\u65b9\u6cd5\u88ab\u91cd\u5199\u8fc7\u4e86\uff0c\u4f8b\u5982String\u3001Integer\u3001Date\u7b49\u3002\u50cf\u6bd4\u8f83\u5e38\u7528\u7684String\u7684equals\u65b9\u6cd5\uff0c\u53ef\u4ee5\u770b\u5230\u5728\u91cd\u5199equals\u65b9\u6cd5\u540e\u6bd4\u8f83\u7684\u5c31\u662f\u5bf9\u8c61\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Object.java\npublic boolean equals(Object obj) {\n    return (this == obj);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ String.java\npublic boolean equals(Object anObject) {\n    if (this == anObject) {\n        return true;\n    }\n    if (anObject instanceof String) {\n        String anotherString = (String)anObject;\n        int n = value.length;\n        if (n == anotherString.value.length) {\n            char v1&#91;] = value;\n            char v2&#91;] = anotherString.value;\n            int i = 0;\n            while (n-- != 0) {\n                if (v1&#91;i] != v2&#91;i])\n                    return false;\n                i++;\n            }\n            return true;\n        }\n    }\n    return false;\n}<\/code><\/pre>\n\n\n\n<p>\u56e0\u6b64<\/p>\n\n\n\n<p>1\u3001\u5982\u679cequals\u65b9\u6cd5\u6ca1\u6709\u88ab\u91cd\u5199\u8fc7\uff1a\u6bd4\u8f83\u7684\u5185\u5b58\u5730\u5740\u662f\u5426\u4e00\u81f4<\/p>\n\n\n\n<p>2\u3001\u5982\u679cequals\u65b9\u6cd5\u88ab\u91cd\u5199\u8fc7\uff1a\u6309\u7167\u88ab\u91cd\u5199\u7684equals\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982String\u7684equals\uff0c\u6bd4\u8f83\u7684\u5c31\u662f\u5bf9\u8c61\u7684\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Test {\n    public static void main(String args&#91;]) {\n        Person person1 = new Person(\"name\", \"age\");\n        Person person2 = new Person(\"name\", \"age\");\n        \/\/ \u5982\u679cPerson\u7c7b\u4e2d\u4e0d\u91cd\u5199equals\u65b9\u6cd5\uff0c\u56e0\u4e3aObject\u7c7b\u4e2d\u7684equals\u65b9\u6cd5\u6bd4\u8f83\u7684\u662f\u5185\u5b58\u5730\u5740\u662f\u5426\u4e00\u81f4\uff0c\u8fd4\u56defalse\n        \/\/ \u5982\u679cPerson\u7c7b\u4e2d\u91cd\u5199\u4e86equals\u65b9\u6cd5\uff0c\u90a3\u4e48\u6309\u7167\u91cd\u5199\u7684\u65b9\u6cd5\uff0c\u6bd4\u8f83\u7684\u662fPerson\u5bf9\u8c61\u4e2d\u7684name\u548cage\u5b57\u6bb5\uff0c\u8fd4\u56detrue\n        System.out.println(person1.equals(person2));\n    }\n}\n\npublic class Person {\n    private String name;\n    private String age;\n\n    public Person(String name, String age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    \/\/ \u91cd\u5199Object\u7c7b\u4e2d\u7684equals\u65b9\u6cd5\n    @Override\n    public boolean equals(Object obj){\n        \/\/ \u5982\u679c\u4e24\u4e2a\u5185\u5b58\u5730\u5740\u76f8\u540c\uff0c\u90a3\u4e48\u4e00\u5b9a\u662f\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\uff0c\u5c31\u65e0\u9700\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\n        if(this == obj){\n            return true;\n        }\n\n        \/\/ \u5224\u65adobj\u662f\u4e0d\u662fPerson\u7684\u4e00\u4e2a\u5bf9\u8c61\n        \/\/ \u5982\u679c\u662f\uff0c\u505a\u5411\u4e0b\u8f6c\u578b\n        \/\/ \u5982\u679c\u4e0d\u662f\uff0c\u76f4\u63a5\u8fd4\u56defalse\u3002\n        if(!(obj instanceof Person)){\n            return false;\n        }\n\n        \/\/ \u6bd4\u8f83Person\u5bf9\u8c61\u4e2d\u7684name\u548cage\u5b57\u6bb5\u503c\u662f\u5426\u4e00\u81f4\n        Person person = (Person)obj;\n        return this.name.equals(person.name) &amp;&amp; this.age.equals(person.age);\n    }\n\n    \/\/ \u91cd\u5199equals\u65b9\u6cd5\u65f6\u5fc5\u987b\u91cd\u5199hashCode\u65b9\u6cd5\n    @Override\n    public int hashCode() {\n        return Objects.hash(name, age);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u5728Person\u7c7b\u4e2d\u91cd\u5199equals\u65b9\u6cd5\uff0c\u90a3\u4e48\u6bd4\u8f83\u65f6\u5c31\u4f1a\u6267\u884cObject\u7c7b\u4e2d\u7684equals\u65b9\u6cd5\uff0c\u76f4\u63a5\u6bd4\u8f83\u5185\u5b58\u5730\u5740\u662f\u5426\u4e00\u81f4\uff1b\u5982\u679c\u6709\u7279\u5b9a\u9700\u6c42\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u91cd\u5199equals\u65b9\u6cd5\uff08\u91cd\u5199equals\u65b9\u6cd5\u65f6\u5fc5\u987b\u91cd\u5199hashCode\u65b9\u6cd5\uff09\uff0c\u6b64\u65f6\u518d\u6267\u884cequals\u65b9\u6cd5\u65f6\u5c31\u6309\u7167\u91cd\u5199\u7684\u903b\u8f91\u8fdb\u884c\u6bd4\u8f83\u5224\u65ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Test {\n    public static void main(String args&#91;]) {\n        String str1 = \"str\";\n        String str2 = \"str\";\n        String str3 = new String(\"str\");\n        String str4 = new String(\"str\");\n        str4 = str4.intern();\n\n        System.out.println(str1 == str2); \/\/ true\n        System.out.println(str1.equals(str2)); \/\/ true\n        System.out.println(str1 == str3); \/\/ false\n        System.out.println(str1.equals(str3)); \/\/ true\n        System.out.println(str1 == str4); \/\/ true\n        System.out.println(str1.equals(str4)); \/\/ true\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"619\" src=\"https:\/\/www.inorysnow.cn\/wp-content\/uploads\/2021\/10\/image.png\" alt=\"\" class=\"wp-image-64\" srcset=\"https:\/\/www.inorysnow.cn\/wp-content\/uploads\/2021\/10\/image.png 686w, https:\/\/www.inorysnow.cn\/wp-content\/uploads\/2021\/10\/image-300x271.png 300w, https:\/\/www.inorysnow.cn\/wp-content\/uploads\/2021\/10\/image-470x424.png 470w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/figure>\n\n\n\n<p>str1\u3001str2\u6307\u5411\u7684\u662f\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u5b58\u653e\u5728\u5e38\u91cf\u6c60\u4e2d\uff0c\u56e0\u6b64str1 == str2\u5224\u65ad\u4e3atrue\u3002<\/p>\n\n\n\n<p>str3\u901a\u8fc7new String(\u201cstr\u201d)\u521b\u5efa\u5bf9\u8c61\uff0c\u4f1a\u5148\u5411\u5e38\u91cf\u6c60\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u201dstr\u201d\u5b57\u7b26\u4e32\u5bf9\u8c61\uff08 \u5982\u679c\u5e38\u91cf\u6c60\u4e2d\u201cstr\u201d\u5b57\u7b26\u4e32\u4e0d\u5b58\u5728 \uff09\uff0c\u7136\u540e\u5728\u5806\u5185\u5b58\u4e2d\u65b0\u521b\u5efa\u4e00\u4e2a\u5b58\u50a8\u4e86\u201cstr\u201d\u7684String\u5bf9\u8c61\u3002str3\u6307\u5411\u8fd9\u4e2a\u5806\u5185\u5b58\u4e2d\u7684String\u5bf9\u8c61\uff0cstr1\u6307\u5411\u5e38\u91cf\u6c60\u4e2d\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u56e0\u6b64str1 == str3\u5224\u65ad\u4e3afalse\u3002<\/p>\n\n\n\n<p>\u5728JDK8\u4e2d\uff0cString\u8c03\u7528\u4e86intern\uff08\uff09\u65b9\u6cd5\uff0c\u5982\u679c\u5e38\u91cf\u6c60\u5148\u524d\u5df2\u521b\u5efa\u51fa\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u5219\u8fd4\u56de\u5e38\u91cf\u6c60\u4e2d\u7684\u8be5\u5b57\u7b26\u4e32\u7684\u5f15\u7528\u3002\u5426\u5219\uff0c\u5982\u679c\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u5df2\u5b58\u5728\u4e0eJava\u5806\u4e2d\uff0c\u5219\u5c06\u5806\u4e2d\u5bf9\u6b64\u5bf9\u8c61\u7684\u5f15\u7528\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5e76\u4e14\u8fd4\u56de\u8be5\u5f15\u7528\uff1b\u5982\u679c\u5806\u4e2d\u4e0d\u5b58\u5728\uff0c\u5219\u5728\u5e38\u91cf\u6c60\u4e2d\u521b\u5efa\u8be5\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u5176\u5f15\u7528\u3002<\/p>\n\n\n\n<p>str4\u8c03\u7528 intern\u65b9\u6cd5\u65f6\uff0c\u56e0\u4e3a\u5e38\u91cf\u6c60\u5df2\u7ecf\u5b58\u5728\u201dstr\u201d\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u76f4\u63a5\u8fd4\u56de\u8be5\u5b57\u7b26\u4e32\u7684\u5f15\u7528\uff0c\u6b64\u65f6str1\u3001str4\u6307\u5411\u5e38\u91cf\u6c60\u4e2d\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u56e0\u6b64str1 == str4\u5224\u65ad\u4e3atrue\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff1a<\/p>\n\n\n\n<p>1\u3001\u901a\u8fc7String str = \u201cstr\u201d\u65b9\u5f0f\u521b\u5efa\u5bf9\u8c61\uff0c\u4f1a\u5c06\u201dstr\u201d\u5b57\u7b26\u4e32\u653e\u5165\u5e38\u91cf\u6c60\uff08\u5982\u679c\u5e38\u91cf\u6c60\u4e2d\u201cstr\u201d\u5b57\u7b26\u4e32\u4e0d\u5b58\u5728\uff09\uff0c\u76f8\u5f53\u4e8e\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>2\u3001\u901a\u8fc7String str = new String(\u201cstr\u201d)\u65b9\u5f0f\u521b\u5efa\u5bf9\u8c61\uff0c \u4f1a\u5c06\u201dstr\u201d\u5b57\u7b26\u4e32\u653e\u5165\u5e38\u91cf\u6c60\uff08\u5982\u679c\u5e38\u91cf\u6c60\u4e2d\u201cstr\u201d\u5b57\u7b26\u4e32\u4e0d\u5b58\u5728\uff09 \uff0c\u7136\u540e\u5728\u5806\u5185\u5b58\u4e2d\u65b0\u521b\u5efa\u4e00\u4e2a\u5b58\u50a8\u4e86\u201cstr\u201d\u7684String\u5bf9\u8c61 \uff0c\u76f8\u5f53\u4e8e\u521b\u5efa\u4e86\u4e24\u4e2a\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6211\u7684\u8ba4\u8bc6\u4e2d==\u548cequals\u7684\u533a\u522b\u53ef\u4ee5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-all","tag-java"],"_links":{"self":[{"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=61"}],"version-history":[{"count":4,"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions\/70"}],"wp:attachment":[{"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inorysnow.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}