Site icon

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

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

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

Exit mobile version