Difference between equals() and == in java

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’s try to understand each quickly.


Equality operator “==”

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.


Equals methods

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’s class equals method, then it behaves same as "==" and compares references only.

Let’s understand difference between equals() and == in java.


Difference between equals() and == in java

  • == is operator whereas equals is method in java.
  • == is recommended to compare primitives whereas equals method is recommended to compare the actual content of objects.
  • Equals method can be overridden but you can’t override behavior of “==” operator
  • == can be used with primitives and objects but you can’t use equals method with primitives.

Let’s take quick example in case of String and custom object.


Example

Equals and == in case of String

String is most common scenario where equals and == methods are used.

package org.arpit.java2blog;

public class StringEqualsAndOpetatorMain {

    public static void main(String[] args) {
        String name1=new String("John");
        String name2=new String("John");

        /*Even though content of objects is same, it will
         * return false, as name1 and name2 points to different objects
         */
        System.out.println(name1==name2);
        /*Equals method compares content, so this will return true
         */
        System.out.println(name1.equals(name2));
        System.out.println("===================");

        name2=name1;

        /*Now name1 and name2 both point to same reference
         * so this will return true
         */
        System.out.println(name1==name2);
        /*Equals method compares content, so this will return true
         */
        System.out.println(name1.equals(name2));
    }

}

When you run above program, you will get below output

false
true
===================
true
true

Equals and == in case of Custom objects

It is very much similar to String. Let’s create Employee class and override equals method. If two employees have same name and age then they will be considered as same.

package org.arpit.java2blog;

public class Employee {
    String name;
    int age;

    public Employee(String name, int age) {
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}

Create another class for Employee comparison

package org.arpit.java2blog;

public class EmployeeEqualsAndOpetatorMain {

    public static void main(String[] args) {
        Employee emp1=new Employee("John",18);
        Employee emp2=new Employee("John",18);

        /*Even though content of objects is same, it will
         * return false, as name1 and name2 points to different objects
         */
        System.out.println(emp1==emp2);
        /*Equals method compares content, so this will return true
         */
        System.out.println(emp1.equals(emp2));
        System.out.println("===================");

        emp2=emp1;

        /*Now name1 and name2 both point to same reference
         * so this will return true
         */
        System.out.println(emp1==emp2);
        /*Equals method compares content, so this will return true
         */
        System.out.println(emp1.equals(emp2));
    }

}

When you run above program, you will get below output

false
true
===================
true
true

That’s all about difference between equals() and == in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.