{"id":690,"date":"2013-02-27T16:20:28","date_gmt":"2013-02-27T16:20:28","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=464"},"modified":"2013-02-27T16:20:28","modified_gmt":"2013-02-27T16:20:28","slug":"how-equals-work-in-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-equals-work-in-java\/","title":{"rendered":"How equals work in java"},"content":{"rendered":"<p>The equals method that comes along java.lang.Object provides an equal method which does the following<\/p>\n<p>&lt;pre&gt;<br \/>\npublic boolean equals(Object o)<br \/>\n{<br \/>\nif(this == 0) return true;<br \/>\n}<br \/>\n&lt;\/pre&gt;<\/p>\n<p>But the above implementation only for Integer, Boolean, Character and Other Wrapper classes because it is overridden.<\/p>\n<p>When we define our own class and define equals the below is how its going to be.<\/p>\n<p>&lt;pre&gt;<\/p>\n<p>class mac<br \/>\n{<br \/>\nint Age;<br \/>\n}<br \/>\n&lt;\/pre&gt;<\/p>\n<p>Now the below code does not give any output until equals method is overridden if you comment out \u00a0 objmac2 = objmac1;\u00a0\u00a0 \u00a0\u00a0\u00a0.<\/p>\n<p>&lt;pre&gt;<br \/>\nInteger num1 = new Integer(5);<br \/>\nInteger num2 = new Integer(5);<\/p>\n<p>mac objmac1 = new mac();<br \/>\nmac objmac2 = new mac();<\/p>\n<p>objmac2 = objmac1;<\/p>\n<p>objmac1.Age = 25;<br \/>\nobjmac2.Age = 25;<\/p>\n<p>if(objmac1 == objmac2)<br \/>\nSystem.out.println(&#8220;They are same&#8221;);<\/p>\n<p>if(objmac1.equals(objmac2))<br \/>\nSystem.out.println(&#8220;Objects are same&#8221;);<\/p>\n<p>&lt;\/pre&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The equals method that comes along java.lang.Object provides an equal method which does the following &lt;pre&gt; public boolean equals(Object o) { if(this == 0) return true; } &lt;\/pre&gt; But the above implementation only for Integer, Boolean, Character and Other Wrapper classes because it is overridden. When we define our own class and define equals the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-equals-work-in-java\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-690","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=690"}],"version-history":[{"count":0,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}