Difference between == operator & equals method in java (example)

1. Difference between equality operator and equals method

  1. It’s one of frequently asked interview questions in java.
  2. We will look into the important differences, with the help of examples.
    1. We will go through the basics of equality operator and equals method.
    2. We will summarize the difference between equality operator and equals method.

2. Equality or == operator in java

  • Equality operator is mostly used to compare primitives like int, boolean, char etc.
    • e.g. 5 == 6 ? true or false ?
    • e.g. ‘a’ == ‘A’ ? true or false?
  • Equality operator can be used to compare two objects.
    • The two objects will be equals if they same.
      • e.g.
      • Object obj1 = new Object()
      • Object obj3 = obj1
      • obj1 & obj3 are equals because both are pointing to same “new Object()”.
    • The objects will be equals if they have same references like obj3 & obj1.

3. Code: equality (or ==) operator to compare primitives

package org.learn;

public class ComparePrimitivesEqualityOperator {
    public static void main(String[] args) {
        boolean result;
        //Example 1:
        int int10 = 10;
        int int20 = 20;
        int anotherInt10 = 10;

        //Output: result = false
        result = (int10 == int20);
        System.out.printf("1. Result of %d == %d is %b",int10,int20,result);
        //Output: result = true
        result = (int10 == anotherInt10);
        System.out.printf("\n1.1. Result of %d == %d is %b",int10,anotherInt10,result);

        //Example 2:
        int char_a = 'a';
        int char_b = 'b';
        int char_A = 'A';
        //Output: result = false;
        result = (char_a == char_b);
        System.out.printf("\n2. Result of %c == %c is %b",char_a,char_b,result);
        //Output: result = false;
        result = (char_a == char_A);
        System.out.printf("\n2.1. Result of %c == %c is %b",char_a,char_A,result);
    }
}

4. Output – equality (or ==) operator to compare primitives in java.

1. Result of 10 == 20 is false
1.1. Result of 10 == 10 is true
2. Result of a == b is false
2.1. Result of a == A is false

5. Program – equality (or ==) operator to compare objects in java.

package org.learn;

public class CompareObjectsEqualityOperator {
    public static void main(String[] args) {

        //Example 1:
        //obj1 points to unique memory address in heap store
        Object obj1 = new Object();
        //obj2 points to unique memory address in heap store
        Object obj2 = new Object();

        //obj3 and obj1 points to same memory address
        Object obj3 = obj1;

        //Output: Obj1 and Obj2 are not equals
        if(obj1 == obj2) {
            System.out.println("1. Obj1 and Obj2 are equals");
        } else {
            System.out.println("1. Obj1 and Obj2 are not equals");
        }

        //Output: Obj1 and Obj3 are equals
        if(obj1 == obj3) {
            System.out.println("2. Obj1 and Obj3 are equals");
        } else {
            System.out.println("2. Obj1 and Obj3 are not equals");
        }

        //Example 2:
        String str1 = new String("USA");
        String str2 = new String("USA");

        //Output: str1 and str2 are not equals
        if(str1 == str2) {
            System.out.println("3. str1 and str2 are equals");
        } else {
            System.out.println("3. str1 and str2 are not equals");
        }
    }
}

6. Output – equality (or ==) operator to compare objects in java.

1. Obj1 and Obj2 are not equals
2. Obj1 and Obj3 are equals
3. str1 and str2 are not equals

7. Equals method in java

  • equals method is defined in the Object class.
    • The equals method of Object class, check whether two objects are equals.
    • The Object class implementation of equals method is same as equality operator (to compare objects).
      • Object obj1 = new Object();
      • Object obj2 = obj1;
      • obj1.equals(obj2) will returns true
      • public boolean equals(Object obj) {
         return (this == obj);
        }
  • The concrete classes overrides, the equals method of Object class.
    • e.g. String class override equals method to compare contents of String.
      • String str1 = new String(“USA”);
      • String str2 = new String(“USA”);
      • str1.equals(str2) will returns true
  • Generally concrete classes like String etc., override equals method, to compare the contents.
    • So, the behavior of equals method, depends upon the implementation, in concrete class.

8. Program – equals method to compare two objects in java

package org.learn;

public class ComparePrimitivesEqualityOperator {
    public static void main(String[] args) {
        boolean result;
        //Example 1:
        int int10 = 10;
        int int20 = 20;
        int anotherInt10 = 10;

        //Output: result = false
        result = (int10 == int20);
        System.out.printf("1. Result of %d == %d is %b",int10,int20,result);
        //Output: result = true
        result = (int10 == anotherInt10);
        System.out.printf("\n1.1. Result of %d == %d is %b",int10,anotherInt10,result);

        //Example 2:
        int char_a = 'a';
        int char_b = 'b';
        int char_A = 'A';
        //Output: result = false;
        result = (char_a == char_b);
        System.out.printf("\n2. Result of %c == %c is %b",char_a,char_b,result);
        //Output: result = false;
        result = (char_a == char_A);
        System.out.printf("\n2.1. Result of %c == %c is %b",char_a,char_A,result);
    }
}

9. Output – equals method to compare two objects in java.

1. Obj1 and obj2 are equals
2. Contents of str1 and str2 are equals

10. Difference between equality operator and equals method in java

  • Equality operator is mostly used to compare primitives (Refer Section 1).
    • What will happens when equality operator compare objects?
      • The comparison operator will return true, when same objects are compared.
  • Equals method is generally used to compare the contents.
    • e.g. equals method of String class, compares the content of two String.
  • The behavior of equals method is dependent, upon the implementation in concrete class.
Scroll to Top