{"id":37241,"date":"2023-12-11T14:40:45","date_gmt":"2023-12-11T06:40:45","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37241"},"modified":"2023-12-11T14:40:45","modified_gmt":"2023-12-11T06:40:45","slug":"%e4%b8%8e-equals-%e7%9a%84%e5%8c%ba%e5%88%ab%ef%bc%9f%e9%87%8d%e8%a6%81","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e4%b8%8e-equals-%e7%9a%84%e5%8c%ba%e5%88%ab%ef%bc%9f%e9%87%8d%e8%a6%81\/","title":{"rendered":"== \u4e0e equals \u7684\u533a\u522b\uff1f\u91cd\u8981"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<p>&lt;h3&gt;== \u89e3\u8bfb:&lt;\/h3&gt;<\/p>\n<p>\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u548c\u5f15\u7528\u7c7b\u578b == \u7684\u4f5c\u7528\u6548\u679c\u662f\u4e0d\u540c\u7684\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>&lt;strong&gt;\u57fa\u672c\u7c7b\u578b\uff1a\u6bd4\u8f83\u7684\u662f\u503c\u662f\u5426\u76f8\u540c\uff1b&lt;\/strong&gt;&lt;strong&gt;\u5f15\u7528\u7c7b\u578b\uff1a\u6bd4\u8f83\u7684\u662f\u5f15\u7528\u662f\u5426\u76f8\u540c\uff1b&lt;\/strong&gt;\u4ee3\u7801\u793a\u4f8b\uff1a&lt;\/p&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;String x = &quot;&quot;string&quot;&quot;;String y = &quot;&quot;string&quot;&quot;;String z = new String(&quot;&quot;string&quot;&quot;);System.out.println(x==y); \/\/ trueSystem.out.println(x==z); \/\/ falseSystem.out.println(x.equals(y)); \/\/ trueSystem.out.println(x.equals(z)); \/\/ true&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>\u4ee3\u7801\u89e3\u8bfb\uff1a\u56e0\u4e3a x \u548c y \u6307\u5411\u7684\u662f\u540c\u4e00\u4e2a\u5f15\u7528\uff0c\u6240\u4ee5 == \u4e5f\u662f true\uff0c\u800c new String()\u65b9\u6cd5\u5219\u91cd\u5199\u5f00\u8f9f\u4e86\u5185\u5b58\u7a7a\u95f4\uff0c\u6240\u4ee5 == \u7ed3\u679c\u4e3a false\uff0c\u800c equals \u6bd4\u8f83\u7684\u4e00\u76f4\u662f\u503c\uff0c\u6240\u4ee5\u7ed3\u679c\u90fd\u4e3a true\u3002<\/p>\n<p>&lt;h3&gt;equals \u89e3\u8bfb:&lt;\/h3&gt;<\/p>\n<p>equals \u672c\u8d28\u4e0a\u5c31\u662f ==\uff0c\u53ea\u4e0d\u8fc7 String \u548c Integer \u7b49\u91cd\u5199\u4e86 equals \u65b9\u6cd5\uff0c\u628a\u5b83\u53d8\u6210\u4e86\u503c\u6bd4\u8f83\u3002\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u660e\u767d\u4e86\u3002<\/p>\n<p>\u9996\u5148\u6765\u770b\u9ed8\u8ba4\u60c5\u51b5\u4e0b equals \u6bd4\u8f83\u4e00\u4e2a\u6709\u76f8\u540c\u503c\u7684\u5bf9\u8c61\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;class Cat {public Cat(String name) {this.name = name;}private String name; public String getName() {return name;} public void setName(String name) {this.name = name;}}Cat c1 = new Cat(&quot;&quot;\u7cbe\u5f69\u733f\u7b14\u8bb0&quot;&quot;);Cat c2 = new Cat(&quot;&quot;\u7cbe\u5f69\u733f\u7b14\u8bb0&quot;&quot;);System.out.println(c1.equals(c2)); \/\/ false&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u8f93\u51fa\u7ed3\u679c\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\uff0c\u7adf\u7136\u662f false\uff1f\u8fd9\u662f\u600e\u4e48\u56de\u4e8b\uff0c\u770b\u4e86 equals \u6e90\u7801\u5c31\u77e5\u9053\u4e86\uff0c\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;public boolean equals(Object obj) {return (this == obj);}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u539f\u6765 equals \u672c\u8d28\u4e0a\u5c31\u662f ==\u3002\u90a3\u95ee\u9898\u6765\u4e86\uff0c\u4e24\u4e2a\u76f8\u540c\u503c\u7684 String \u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48\u8fd4\u56de\u7684\u662f true\uff1f\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;String s1 = new String(&quot;&quot;\u7cbe\u5f69\u733f\u7b14\u8bb0&quot;&quot;);String s2 = new String(&quot;&quot;\u7cbe\u5f69\u733f\u7b14\u8bb0&quot;&quot;);System.out.println(s1.equals(s2)); \/\/ true&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u540c\u6837\u7684\uff0c\u5f53\u6211\u4eec\u8fdb\u5165 String \u7684 equals \u65b9\u6cd5\uff0c\u627e\u5230\u4e86\u7b54\u6848\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;public boolean equals(Object anObject) {if (this == anObject) {return true;}if (anObject instanceof String) {String anotherString = (String)anObject;int n = value.length;if (n == anotherString.value.length) {char v1[] = value;char v2[] = anotherString.value;int i = 0;while (n&#8211; != 0) {if (v1[i] != v2[i])return false;i++;}return true;}}return false;}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u539f\u6765\u662f String \u91cd\u5199\u4e86 Object \u7684 equals \u65b9\u6cd5\uff0c\u628a\u5f15\u7528\u6bd4\u8f83\u6539\u6210\u4e86\u503c\u6bd4\u8f83\u3002\u603b\u7ed3 \uff1a== \u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6765\u8bf4\u662f\u503c\u6bd4\u8f83\uff0c\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u6765\u8bf4\u662f\u6bd4\u8f83\u7684\u662f\u5f15\u7528\uff1b\u800c equals \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u5f15\u7528\u6bd4\u8f83\uff0c\u53ea\u662f\u5f88\u591a\u7c7b\u91cd\u65b0\u4e86 equals \u65b9\u6cd5\uff0c\u6bd4\u5982 String\u3001Integer \u7b49\u628a\u5b83\u53d8\u6210\u4e86\u503c\u6bd4\u8f83\uff0c\u6240\u4ee5\u4e00\u822c\u60c5\u51b5\u4e0b equals \u6bd4\u8f83\u7684\u662f\u503c\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; &lt;h3&gt;== \u89e3\u8bfb:&lt;\/h3&gt; \u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u548c\u5f15\u7528\u7c7b\u578b [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37241","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37241"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37241\/revisions"}],"predecessor-version":[{"id":37242,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37241\/revisions\/37242"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37241"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}