{"id":545,"date":"2022-05-02T09:23:43","date_gmt":"2022-05-02T01:23:43","guid":{"rendered":"https:\/\/blog.lijiajia.top\/?p=545"},"modified":"2026-06-19T20:52:46","modified_gmt":"2026-06-19T12:52:46","slug":"%e3%80%90java%e3%80%91equals-%e5%92%8c-%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=545","title":{"rendered":"\u3010Java\u3011equals \u548c == \u7684\u533a\u522b"},"content":{"rendered":"<p>\u8f6c\u8f7d\uff1a<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/RioTian\/p\/14924959.html\"><a href=\"https:\/\/www.cnblogs.com\/RioTian\/p\/14924959.html\">https:\/\/www.cnblogs.com\/RioTian\/p\/14924959.html<\/a><\/a><\/p>\n<\/p>\n<p>\u5728\u7f16\u5199\u4ee3\u7801\u7684\u65f6\u5019\u6211\u4eec\u7ecf\u5e38\u4f1a\u4f7f\u7528 equals \u548c == \u6765\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\uff0c\u90a3\u4e48\u4e24\u8005\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u70b9\u533a\u522b\uff1a<\/p>\n<p>1.\u9996\u5148\u7684\u533a\u522b\u662f\uff0cequals \u662f\u65b9\u6cd5\uff0c\u800c == \u662f\u64cd\u4f5c\u7b26\uff1b<\/p>\n<p>2.\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u7684\u53d8\u91cf\u6765\u8bf4\uff08\u5982 short\u3001 int\u3001 long\u3001 float\u3001 double\uff09\uff0c\u53ea\u80fd\u4f7f\u7528 == \uff0c\u56e0\u4e3a\u8fd9\u4e9b\u57fa\u672c\u7c7b\u578b\u7684\u53d8\u91cf\u6ca1\u6709 equals \u65b9\u6cd5\u3002\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u53d8\u91cf\u7684\u6bd4\u8f83\uff0c\u4f7f\u7528 == \u6bd4\u8f83\uff0c \u4e00\u822c\u6bd4\u8f83\u7684\u662f\u5b83\u4eec\u7684\u503c\u3002<\/p>\n<p>3.\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\u6765\u8bf4\uff08\u4f8b\u5982 String \u7c7b\uff09\u624d\u6709 equals \u65b9\u6cd5\uff0c\u56e0\u4e3a String \u7ee7\u627f\u4e86 Object \u7c7b\uff0c equals \u662f Object \u7c7b\u7684\u901a\u7528\u65b9\u6cd5\u3002\u5bf9\u4e8e\u8be5\u7c7b\u578b\u5bf9\u8c61\u7684\u6bd4\u8f83\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e5f\u5c31\u662f\u6ca1\u6709\u590d\u5199 Object \u7c7b\u7684 equals \u65b9\u6cd5\uff0c\u4f7f\u7528 == \u548c equals \u6bd4\u8f83\u662f\u4e00\u6837\u6548\u679c\u7684\uff0c\u90fd\u662f\u6bd4\u8f83\u7684\u662f\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u653e\u5730\u5740\u3002\u4f46\u662f\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u6765\u8bf4\uff0c\u4e3a\u4e86\u6ee1\u8db3\u81ea\u8eab\u4e1a\u52a1\u9700\u6c42\uff0c\u53ef\u80fd\u5b58\u5728 equals \u65b9\u6cd5\u88ab\u590d\u5199\u7684\u60c5\u51b5\uff0c\u8fd9\u65f6\u4f7f\u7528 equals \u65b9\u6cd5\u6bd4\u8f83\u9700\u8981\u770b\u5177\u4f53\u7684\u60c5\u51b5\uff0c\u4f8b\u5982 String \u7c7b\uff0c\u4f7f\u7528 equals \u65b9\u6cd5\u4f1a\u6bd4\u8f83\u5b83\u4eec\u7684\u503c\uff1b<\/p>\n<p>\u5bf9\u4e8e\u4e0a\u8ff0\u7b2c\u4e09\u70b9\u7406\u89e3\u8d77\u6765\u53ef\u80fd\u6709\u70b9\u590d\u6742\uff0c\u56e0\u4e3a\u8fd9\u91cc equals \u65b9\u6cd5\u6bd4\u8f83\u9700\u8981\u5206\u4e24\u79cd\u60c5\u51b5\u6765\u8ba8\u8bba\uff0c\u4e00\u79cd\u60c5\u51b5\u662f\u8be5\u65b9\u6cd5\u6ca1\u6709\u88ab\u91cd\u5199\uff0c\u53e6\u5916\u4e00\u79cd\u662f\u8be5\u65b9\u6cd5\u88ab\u91cd\u5199\u3002<\/p>\n<p>\u5bf9\u4e8e equals \u65b9\u6cd5\u6ca1\u6709\u88ab\u91cd\u5199\u7684\u60c5\u51b5\u3002\u5982\u679c\u7c7b\u6ca1\u6709\u91cd\u5199\u8be5\u65b9\u6cd5\uff0c\u90a3\u4e48\u9ed8\u8ba4\u4f7f\u7528\u7684\u5c31\u662f Object \u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u662f Object \u7c7b\u7684 equals \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">  public boolean equals(Object obj) {\n      return (this == obj);\n  }<\/code><\/pre>\n<p>\u4ece\u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u91cc\u9762\u4f7f\u7528\u7684\u5c31\u662f == \u6bd4\u8f83\uff0c\u6240\u4ee5\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6bd4\u8f83\u7684\u5c31\u662f\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u653e\u5730\u5740\u3002<\/p>\n<p>\u5bf9\u4e8e equals \u65b9\u6cd5\u88ab\u91cd\u5199\u7684\u60c5\u51b5\u3002\u4ee5 String \u7c7b\u4e3a\u4f8b\uff0c\u4ee5\u4e0b\u662f String \u7c7b\u4e2d\u7684 equals \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">  @Override public boolean equals(Object other) {\n  if (other == this) {\n    return true;\n  }\n  if (other instanceof String) {\n      String s = (String)other;\n      int count = this.count;\n      if (s.count != count) {\n          return false;\n      }\n      if (hashCode() != s.hashCode()) {\n          return false;\n      }\n      char[] value1 = value;\n      int offset1 = offset;\n      char[] value2 = s.value;\n      int offset2 = s.offset;\n      for (int end = offset1 + count; offset1 &lt; end; ) {\n          if (value1[offset1] != value2[offset2]) {\n              return false;\n          }\n          offset1++;\n          offset2++;\n      }\n      return true;\n  } else {\n      return false;\n  }\n}<\/code><\/pre>\n<p>\u4ece\u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0c String \u7c7b\u590d\u5199\u4e86 equals \u65b9\u6cd5\uff0c\u5f53\u4f7f\u7528 == \u6bd4\u8f83\u5185\u5b58\u7684\u5b58\u653e\u5730\u5740\u4e0d\u76f8\u7b49\u65f6\uff0c\u63a5\u4e0b\u6765\u4f1a\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u662f\u5426 \u76f8\u7b49\uff0c\u6240\u4ee5 String \u7c7b\u4e2d\u7684 equals \u65b9\u6cd5\u4f1a\u6bd4\u8f83\u4e24\u8005\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u662f\u5426\u4e00\u6837\u3002\u6211\u4eec\u6765\u770b\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-java\">    String a = &quot;Hello World&quot;;\n    String b = new String(&quot;Hello World&quot;);\n    String c = b; \/\/\u5f15\u7528\u4f20\u9012\n    System.out.println(&quot;a == b:&quot; + a == b);             \/\/false\n    System.out.println(&quot;b == c:&quot; + b == c);             \/\/true\n    System.out.println(&quot;a == c:&quot; + a == c);             \/\/false\n    System.out.println(&quot;a.equals(b):&quot; + a.equals(b));   \/\/true\n    System.out.println(&quot;b.equals(c):&quot; + b.equals(c));   \/\/true\n    System.out.println(&quot;a.equals(c):&quot; + a.equals(c));   \/\/true\n\n    \u6700\u7ec8\u7684\u6253\u5370\u4f1a\u662f\uff1a\n    a == b:false\n    b == c:true\n    a == c:false\n    a.equals(b):true\n    b.equals(c):true\n    a.equals(c):true<\/code><\/pre>\n<p>\u56e0\u4e3a String b \u901a\u8fc7 new \u7684\u65b9\u5f0f\u5df2\u7ecf\u5f00\u8f9f\u4e86\u65b0\u7684\u5806\u5185\u5b58\uff0c\u800c String a = &quot;Hello World&quot; \u662f\u5b58\u653e\u5728\u5e38\u91cf\u6c60\u91cc\u7684\uff0c\u4e24\u8005\u5728 Java \u5185\u5b58\u91cc\u5b58\u5728\u653e\u7684\u4f4d\u7f6e\u662f\u4e0d\u540c\u7684\uff0c\u6240\u4ee5 a == b \u4e3a false\uff1b\u800c equals \u65b9\u6cd5\u5f53\u4e24\u8005\u5b58\u653e\u7684\u5185\u5b58\u5730\u5740\u4e0d\u540c\u65f6\uff0c\u4f1a\u6bd4\u8f83\u4e24\u8005\u7684\u503c\uff0c\u4e24\u8005\u7684\u503c\u90fd\u662f &quot;Hello World&quot; \uff0c\u6240\u4ee5 a.equals(b) \u4e3a true\u3002<\/p>\n<p>\u53e6\u5916\u8bf7\u601d\u8003\u4e00\u4e0b\u4e0b\u65b9\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\u4e3a\u4ec0\u4e48\u662f True\u3001false<\/p>\n<pre><code class=\"language-java\">public class Java_epuals {\n    public static void main(String[] args) {\n        Integer i2 = 10;\n        Integer i3 = 10;\n        System.out.println(i2 == i3); \/\/ true\n\n        Integer i4 = 128;\n        Integer i5 = 128;\n        System.out.println(i4 == i5); \/\/ false\n    }\n}<\/code><\/pre>\n<p>\u5bf9\u4e8e i2 \u548c i3 \u7684\u6bd4\u8f83\uff0c\u56e0\u4e3a\u53d8\u91cf\u7684\u5b9a\u4e49\u65b9\u6cd5\u662f i2(i3) = 10 \u5b83\u4eec\u7684\u53d8\u91cf\u4f1a\u7f6e\u4e8e\u5e38\u91cf\u533a\uff0c\u4e24\u4e2a\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u76f8\u540c\u3002<\/p>\n<p>\u6b64\u65f6 == \u8fd4\u56de true\u3002<\/p>\n<p>\u90a3\u4e48\u4e3a\u4ec0\u4e48 i4 \u548c i5 \u662f\u8fd4\u56de false \u5462\uff1f<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a Java \u4e2d integer \u8303\u56f4\u53d6\u503c\u8981\u5728-128\u5230+127 \uff08\u4e3a\u4ec0\u4e48Integer\u5bf9\u8c61\u8303\u56f4\uff08-128-127\uff09\u4e4b\u95f4\uff09\uff0c\u800c\u6211\u4eec\u8d4b\u7684\u503c\u662f 128 \uff0c\u6b64\u65f6\u53d8\u91cf\u5e76\u4e0d\u5728\u5e38\u91cf\u533a\u5b9a\u4e49\u3002\u6240\u4ee5\u4e24\u4e2a\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u4e0d\u540c\uff0c== \u8fd4\u56de false<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a https:\/\/www.cnblogs.com\/RioTian\/p\/14924959.html \u5728\u7f16\u5199 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[136],"tags":[42],"class_list":["post-545","post","type-post","status-publish","format-standard","hentry","category-notes-misc","tag-java"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=545"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions"}],"predecessor-version":[{"id":2043,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions\/2043"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}