Difference Between == and equals() in Java

Difference Between == and equals() in Java | In Java, we can compare objects in two ways either by using its reference or by using its 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 compares primitives based on their values, and objects based on their reference.

2) equals() method:- It compares objects either by using their reference or by using their data, It 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 compares objects based on their reference. To compare objects based on their data we must override the equals() method.

== vs equals() in Java

The below table shows the major differences between the == operator and equals() method in Java.

In a summary, we can say:- In general we use the == operator for reference comparison and the equals() method for state/data comparison in Java programming language.

Equal (==) operatorequals() method
It can be used to compare both primitive values and objects.It can be used for comparing objects only. It can’t be used for primitive values.
It compares primitives based on their values, and objects based on their reference.It compares objects either with their reference or with their state, it depends upon the equals() method implementation.
The equal (==) operator can’t compare incompatible objects, the compiler throws the compile-time error.The equals() method can compare incompatible objects & in this case, it always returns “false”.
It is an operator and can’t be overridden.It is a method and can be overridden.

Now let us see the different test cases for the == operator 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

Now let us see the different test cases for the equals method in Java. In the equals() method we demonstrated these comparisons with example code, you can check there.

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*
Two Null referencedNoNPE*
Object with null referencedYesfalse
Incompatible objectsYesfalse

NPE* => NullPointerException, error => Compile-time error

Relation Between == Operator and equals() method in Java

Assume t1 and t2 are objects of the 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 detail.

Equal (==) Operator in Java

The equal (==) operator compares 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 the equal (==) operator to compare incompatible types values otherwise the compiler will throw an error. For example boolean is an incompatible primitive data type with the remaining other data types, therefore we can’t compare boolean with other primitive data types.

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 the equal (==) operator

For two objects if both referenced variables are pointing to the same object then the 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 an equal (==) operator to compare incompatible types of objects, otherwise, the compiler will throw an 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 the 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 in Java

By default equals() method of java.lang.Object class compares two objects with their reference. In the Object class, the equals() method is implemented to compare two objects with their references. Since the equals() method is an instance/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 the equal(==) operator, the equals() method will not throw an error for comparing incompatible objects, rather it returns false because the compiler checks only whether it is possible to invoke the equals() method using the 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, and if 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 the equals() method

We can’t invoke the method using null, it leads to a compile-time error. If we invoke the equals() method using the null referenced variable, then it raises 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 the == operator is already there to compare objects then why equals() method is given in Java?

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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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