{"id":6720,"date":"2018-10-21T21:17:28","date_gmt":"2018-10-21T15:47:28","guid":{"rendered":"https:\/\/java2blog.com\/?p=6720"},"modified":"2021-01-11T18:52:24","modified_gmt":"2021-01-11T13:22:24","slug":"difference-between-equals-equality-operator-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/difference-between-equals-equality-operator-java\/","title":{"rendered":"Difference between equals() and == in java"},"content":{"rendered":"<p>In this post, we will see about the difference between equals and == in java.<\/p>\n<p>This is most asked java interview question for <a href=\"https:\/\/java2blog.com\/java-interview-programs-for-freshers\/\" target=\"_blank\" rel=\"noopener\">fresher<\/a> or <a href=\"https:\/\/java2blog.com\/java-interview-questions-for-2-years-experience\/\" target=\"_blank\" rel=\"noopener\">2-year experienced<\/a> java developer and it might be confusing sometimes. Let&#8217;s try to understand each quickly.<\/p>\n<hr \/>\n<h2><span style=\"color: #f89820;\">Equality operator &#8220;==&#8221;<\/span><\/h2>\n<p>Equality operator can be used to compare primitives but when you compare objects using ==, it just compares reference not the actual content of the objects.<\/p>\n<hr \/>\n<h2><span style=\"color: #f89820;\">Equals methods<\/span><\/h2>\n<p>If you want to compare the actual content of the object, then you need to override equals method in your class. If you do not override object&#8217;s class equals method, then it behaves same as &quot;==&quot; and compares references only.<\/p>\n<p>Let&#8217;s understand difference between equals() and == in java.<\/p>\n<hr \/>\n<h2><span style=\"color: #f89820;\">Difference between equals() and == in java<\/span><\/h2>\n<ul>\n<li>== is operator whereas equals is method in java.<\/li>\n<li>== is recommended to compare primitives whereas equals method is recommended to compare the actual content of objects.<\/li>\n<li>Equals method can be overridden but you can&#8217;t override behavior of &#8220;==&#8221; operator<\/li>\n<li>== can be used with primitives and objects but you can&#8217;t use equals method with primitives.<\/li>\n<\/ul>\n<p>Let&#8217;s take quick example in case of String and custom object.<\/p>\n<hr \/>\n<h2><span style=\"color: #f89820;\">Example<\/span><\/h2>\n<h3>Equals and == in case of String<\/h3>\n<p>String is most common scenario where equals and == methods are used.<\/p>\n<pre class=\"java\" name=\"code\" title=\"StringEqualsAndOpetatorMain.java\">package org.arpit.java2blog;\n\npublic class StringEqualsAndOpetatorMain {\n\n    public static void main(String[] args) {\n        String name1=new String(\"John\");\n        String name2=new String(\"John\");\n\n        \/*Even though content of objects is same, it will\n         * return false, as name1 and name2 points to different objects\n         *\/\n        System.out.println(name1==name2);\n        \/*Equals method compares content, so this will return true\n         *\/\n        System.out.println(name1.equals(name2));\n        System.out.println(\"===================\");\n\n        name2=name1;\n\n        \/*Now name1 and name2 both point to same reference\n         * so this will return true\n         *\/\n        System.out.println(name1==name2);\n        \/*Equals method compares content, so this will return true\n         *\/\n        System.out.println(name1.equals(name2));\n    }\n\n}\n<\/pre>\n<p>When you run above program, you will get below output<\/p>\n<div class=\"content-box-green\">false<br \/>\ntrue<br \/>\n===================<br \/>\ntrue<br \/>\ntrue<\/div>\n<h3>Equals and == in case of Custom objects<\/h3>\n<p>It is very much similar to String. Let&#8217;s create Employee class and override equals method. If two employees have same name and age then they will be considered as same.<\/p>\n<pre class=\"java\" name=\"code\" title=\"Employee.java\">package org.arpit.java2blog;\n\npublic class Employee {\n    String name;\n    int age;\n\n    public Employee(String name, int age) {\n        this.name=name;\n        this.age=age;\n    }\n    public String getName() {\n        return name;\n    }\n    public void setName(String name) {\n        this.name = name;\n    }\n    public int getAge() {\n        return age;\n    }\n    public void setAge(int age) {\n        this.age = age;\n    }\n\n    @Override\n    public boolean equals(Object obj) {\n        if (this == obj)\n            return true;\n        if (obj == null)\n            return false;\n        if (getClass() != obj.getClass())\n            return false;\n        Employee other = (Employee) obj;\n        if (age != other.age)\n            return false;\n        if (name == null) {\n            if (other.name != null)\n                return false;\n        } else if (!name.equals(other.name))\n            return false;\n        return true;\n    }\n}\n<\/pre>\n<p>Create another class for Employee comparison<\/p>\n<pre class=\"java\" name=\"code\" title=\"EmployeeEqualsAndOpetatorMain.java\">package org.arpit.java2blog;\n\npublic class EmployeeEqualsAndOpetatorMain {\n\n    public static void main(String[] args) {\n        Employee emp1=new Employee(\"John\",18);\n        Employee emp2=new Employee(\"John\",18);\n\n        \/*Even though content of objects is same, it will\n         * return false, as name1 and name2 points to different objects\n         *\/\n        System.out.println(emp1==emp2);\n        \/*Equals method compares content, so this will return true\n         *\/\n        System.out.println(emp1.equals(emp2));\n        System.out.println(\"===================\");\n\n        emp2=emp1;\n\n        \/*Now name1 and name2 both point to same reference\n         * so this will return true\n         *\/\n        System.out.println(emp1==emp2);\n        \/*Equals method compares content, so this will return true\n         *\/\n        System.out.println(emp1.equals(emp2));\n    }\n\n}\n<\/pre>\n<p>When you run above program, you will get below output<\/p>\n<div class=\"content-box-green\">false<br \/>\ntrue<br \/>\n===================<br \/>\ntrue<br \/>\ntrue<\/div>\n<p>That&#8217;s all about difference between equals() and == in java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will see about the difference between equals and == in java. This is most asked java interview question for fresher or 2-year experienced java developer and it might be confusing sometimes. Let&#8217;s try to understand each quickly. Equality operator &#8220;==&#8221; Equality operator can be used to compare primitives but when you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[144,27],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/6720"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=6720"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/6720\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=6720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=6720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=6720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}