Difference Between == and equals() in Java

In Java, we can compare objects in two ways either by using objects reference or by using objects state/data/values. Two objects of a class are said to be equal only if they have the same reference or the same state. In Java, we compare objects by using the “==” operator or by using the equals() method.

1) Equal (==) operator:- It compare primitives based on their values, and objects based on their reference.

2) equals() method:- It compare objects either by using their reference or by using their data, It’s depends on the implementation logic.

It is not true that the equals() method always compare two objects with their data. The default implementation of equals() method given in java.lang.Object class and it compare objects based on their reference. To compare objects based on their data we must override the equals() method.

== vs equals() in Java

Below table list the major differences between == operator and equals() method in Java. In general we use == operator for reference comparison and equals() method for state/data comparison in Java programming language.

Equal (==) operatorEquals() method
It can be used for comparing primitive values, and objects.It can be used for comparing objects only.
It compare primitives based on their values, and objects based on their reference.It compares objects either with a reference or with a state based on its implementation.
The equal (==) operator can’t compare incompatible objects, the compiler throws the compile-time error.The equals() method can compare incompatible objects using equals() method it returns “false”.
It is an operator and can’t be overriden.It is a non-static method and can be overriden.

Now let us the difference test cases for == and equals method in Java.

Comparison using ==
Operator
Valid?Return
Two objectsYesfalse
Two primitive variablesYesfalse/true
Two nullsYestrue
Null with objectYesfalse
Object with nullYesfalse
Null referenced with nullYestrue
Null referenced with objectYesfalse
Object with null referencedYesfalse
Incompatible objectsNoerror

Comparison using
equals()
Valid?Return
Two objectsYesfalse/true
Two primitive variablesNoerror
Two nullsNoerror
Null with objectNoerror
Object with nullYesfalse
Null referenced with nullNoNPE*
Null referenced with objectNoNPE*
Object with null referencedYesfalse
Incompatible objectsYesfalse

NPE* => NullPointerException

Relation between == operator and equals() method in Java

Assume t1 and t2 are objects of Test class.

1) If t1 == t2 is true then t1.equals(t2) is always true. If two referenced variables are pointing to the same object then the state/data of these objects will be always the same.
2) if t1==t2 is false then t1.equals(t2) can be true or false.

t1==t2t1.equals(t2)
truetrue
falsetrue/false

3) If t1.equals(t2) is true then t1 == t2 can be true or false.
4) If t1.equals(t2) is false then t1 == t2 will be always false.

t1.equals(t2)t1==t2
truetrue/false
falsefalse

Now let us see the examples of these differences in details.

Equal (==) operator in Java

The equal (==) operator compare primitives based on their values, and objects based on their reference.

Comparing two primitive values using equal == operator

int x = 9;
int y = 10; 
int z = 9;

System.out.println(x == y); // false
System.out.println(x == z); // true
System.out.println(y == z); // false

We can’t use equal (==) operator to compare incompatible types values otherwise compiler will throw error.

int a = 9;
double b = 10.5;
boolean c = true;

System.out.println(a==b); // false

System.out.println(b==c); // error
// error: incomparable types: double and boolean

System.out.println(a==c); // error
// error: incomparable types: int and boolean

Comparing two objects using equal (==) operator

For two objects if both referenced variables are pointing to same object then equal (==) operator returns true, else it returns false.

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

      Test t1 = new Test();
      Test t2 = new Test();
      Test t3 = t1;

      System.out.println(t1 == t2); // false
      System.out.println(t1 == t3); // true
      System.out.println(t2 == t3); // false
   }
}

We can’t use equal (==) operator to compare incompatible types objects, otherwise compiler will throw error.

class A{}

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

      Test t1 = new Test();
      A a1 = new A();

      System.out.println(t1 == a1); // error
      // error: incomparable types: Test and A
   }
}

Comparing null values using equal (==) operator

There are four cases,

Comparing null withReturn value
Another nullTrue
Null reference variableTrue
Object reference variableFalse
Objectfalse
Test t1 = null;
Test t2 = new Test();

// Comparing null with other null
System.out.println(null==null); // true

// Comparing null with null reference variable
System.out.println(null==t1); // true

// Comparing null with object reference variable
System.out.println(null==t2); // false

// Comparing null with object
System.out.println(null==new Test()); // false

equals() method

By default equals() method compares two objects with their reference. In Object class, the equals() method is implemented to compare two objects with their references. Since the equals() method is a non-static method, therefore we can’t compare two primitive values using the equals() method, an object is required.

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

      Test t1 = new Test();
      Test t2 = new Test();
      Test t3 = t1;

      System.out.println(t1.equals(t2)); // false
      System.out.println(t1.equals(t3)); // true
      System.out.println(t2.equals(t3)); // false
   }
}

Like equal(==) operator, equals() method will not throw an error for comparing incompatible objects, rather it returns false because compiler checks only whether it is possible to invoke equals() method using c1 object by passing t1 object or not. Since this invocation is possible, so the compiler will not throw an error. JVM checks references stored in both variables, since those references are different it returns false.

class A{}

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

      Test t1 = new Test();
      A a1 = new A();

      System.out.println(t1.equals(a1)); // false
   }
}

Comparing two null values using equals() method

We can’t invoke the method using null, it leads to a compile-time error. If we invoke equals() method using the null referenced variable, then it leads to exception NullPointerException.

Test t1 = null;
Test t2 = new Test();

System.out.println(null.equals(null)); // error
// error: <null> cannot be dereferenced

System.out.println(t1.equals(null)); // exception
// Exception java.lang.NullPointerException

System.out.println(t2.equals(null)); // false
// equals method can be invoked by passing null

When == operator is already there to compare objects then why equals() method is given in Java?
Ans:- In Java, the equal (==) operator compares only reference of the objects, so the equals() method is given to compare objects with state. The equals() method is implemented in java.lang.Object class with default implementation comparing object with references. Using equals() method, if we want to compare objects using their state then we must override it in subclass.

Leave a Reply