{"id":25322,"date":"2024-03-16T04:44:36","date_gmt":"2024-03-16T04:44:36","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/"},"modified":"2024-03-22T05:53:36","modified_gmt":"2024-03-22T05:53:36","slug":"in-java-when-should-you-use-and-equals","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/","title":{"rendered":"In Java, when should you use == and equals?"},"content":{"rendered":"<p>In Java, &#8220;==&#8221; and equals() have different applications.<\/p>\n<ol>\n<li>This is used to compare the references of two objects to check if they are equal. When using &#8220;==&#8221; to compare two objects, it will return true if they reference the same memory address, and false if they do not. This is typically used to determine if two objects are the same instance.<\/li>\n<\/ol>\n<p>For example:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">str1<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"hello\"<\/span>;\r\n<span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">str2<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"hello\"<\/span>;\r\n<span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">str3<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">String<\/span>(<span class=\"hljs-string\">\"hello\"<\/span>);\r\n\r\nSystem.out.println(str1 == str2); <span class=\"hljs-comment\">\/\/ true\uff0c\u56e0\u4e3astr1\u548cstr2\u5f15\u7528\u4e86\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf<\/span>\r\nSystem.out.println(str1 == str3); <span class=\"hljs-comment\">\/\/ false\uff0c\u56e0\u4e3astr1\u548cstr3\u5f15\u7528\u7684\u662f\u4e0d\u540c\u7684\u5bf9\u8c61<\/span>\r\n<\/code><\/pre>\n<ol>\n<li>The equals() method is used to compare if the contents of two objects are equal. Typically, we need to override the equals() method to compare the contents of custom objects. By default, the equals() method behaves the same as &#8220;==&#8221; by comparing if the references of two objects are equal.<\/li>\n<\/ol>\n<p>For example:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Person<\/span> {\r\n    <span class=\"hljs-keyword\">private<\/span> String name;\r\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-type\">int<\/span> age;\r\n\r\n    <span class=\"hljs-comment\">\/\/ \u7701\u7565\u6784\u9020\u65b9\u6cd5\u548c\u5176\u4ed6\u4ee3\u7801<\/span>\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">boolean<\/span> <span class=\"hljs-title function_\">equals<\/span><span class=\"hljs-params\">(Object obj)<\/span> {\r\n        <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-built_in\">this<\/span> == obj) {\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">true<\/span>;\r\n        }\r\n        <span class=\"hljs-keyword\">if<\/span> (obj == <span class=\"hljs-literal\">null<\/span> || getClass() != obj.getClass()) {\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">false<\/span>;\r\n        }\r\n        <span class=\"hljs-type\">Person<\/span> <span class=\"hljs-variable\">person<\/span> <span class=\"hljs-operator\">=<\/span> (Person) obj;\r\n        <span class=\"hljs-keyword\">return<\/span> age == person.age &amp;&amp; Objects.equals(name, person.name);\r\n    }\r\n}\r\n\r\n<span class=\"hljs-type\">Person<\/span> <span class=\"hljs-variable\">person1<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">Person<\/span>(<span class=\"hljs-string\">\"Alice\"<\/span>, <span class=\"hljs-number\">25<\/span>);\r\n<span class=\"hljs-type\">Person<\/span> <span class=\"hljs-variable\">person2<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">Person<\/span>(<span class=\"hljs-string\">\"Bob\"<\/span>, <span class=\"hljs-number\">30<\/span>);\r\n<span class=\"hljs-type\">Person<\/span> <span class=\"hljs-variable\">person3<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">Person<\/span>(<span class=\"hljs-string\">\"Alice\"<\/span>, <span class=\"hljs-number\">25<\/span>);\r\n\r\nSystem.out.println(person1.equals(person2)); <span class=\"hljs-comment\">\/\/ false\uff0c\u56e0\u4e3aname\u548cage\u4e0d\u540c<\/span>\r\nSystem.out.println(person1.equals(person3)); <span class=\"hljs-comment\">\/\/ true\uff0c\u56e0\u4e3aname\u548cage\u76f8\u540c<\/span>\r\n<\/code><\/pre>\n<p>In conclusion, use &#8220;==&#8221; to determine if two objects are the same instance and use equals() to compare if their contents are equal. In most cases, we need to override the equals() method based on our specific requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java, &#8220;==&#8221; and equals() have different applications. This is used to compare the references of two objects to check if they are equal. When using &#8220;==&#8221; to compare two objects, it will return true if they reference the same memory address, and false if they do not. This is typically used to determine if [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-25322","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>In Java, when should you use == and equals? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"In Java, when should you use == and equals?\" \/>\n<meta property=\"og:description\" content=\"In Java, &#8220;==&#8221; and equals() have different applications. This is used to compare the references of two objects to check if they are equal. When using &#8220;==&#8221; to compare two objects, it will return true if they reference the same memory address, and false if they do not. This is typically used to determine if [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T04:44:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T05:53:36+00:00\" \/>\n<meta name=\"author\" content=\"Emily Johnson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Emily Johnson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/\"},\"author\":{\"name\":\"Emily Johnson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\"},\"headline\":\"In Java, when should you use == and equals?\",\"datePublished\":\"2024-03-16T04:44:36+00:00\",\"dateModified\":\"2024-03-22T05:53:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/\"},\"wordCount\":157,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/\",\"name\":\"In Java, when should you use == and equals? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T04:44:36+00:00\",\"dateModified\":\"2024-03-22T05:53:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"In Java, when should you use == and equals?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\",\"name\":\"Emily Johnson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"caption\":\"Emily Johnson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"In Java, when should you use == and equals? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/","og_locale":"en_US","og_type":"article","og_title":"In Java, when should you use == and equals?","og_description":"In Java, &#8220;==&#8221; and equals() have different applications. This is used to compare the references of two objects to check if they are equal. When using &#8220;==&#8221; to compare two objects, it will return true if they reference the same memory address, and false if they do not. This is typically used to determine if [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T04:44:36+00:00","article_modified_time":"2024-03-22T05:53:36+00:00","author":"Emily Johnson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Emily Johnson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/"},"author":{"name":"Emily Johnson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378"},"headline":"In Java, when should you use == and equals?","datePublished":"2024-03-16T04:44:36+00:00","dateModified":"2024-03-22T05:53:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/"},"wordCount":157,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/","url":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/","name":"In Java, when should you use == and equals? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T04:44:36+00:00","dateModified":"2024-03-22T05:53:36+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/in-java-when-should-you-use-and-equals\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"In Java, when should you use == and equals?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378","name":"Emily Johnson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","caption":"Emily Johnson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=25322"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25322\/revisions"}],"predecessor-version":[{"id":59419,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25322\/revisions\/59419"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=25322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=25322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=25322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}