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

TARA RAM GOYAL
3 min readMar 29, 2023

--

We go deeply into the == Operator and the equals() function, attempting to figure out distinguish them.

To make the usage of the == operator mandatory, there must be some relationship between the argument types (either child to parent, parent to child, or same type); otherwise, we will obtain a compile-time error stating IncomparableTypeException.

If there is no relationship between the argument types, the equals() method will not throw any compile- or run-time error and will simply return false.

The == Operator

  1. The == operator is a Java operator that may be used on both primitive and object types.
  2. In the case of object reference ==, the operator meant reference comparison (address comparison).
  3. For content comparison, we cannot override the == operator.
  4. There must be a relationship between the argument types to make the use of the == operator mandatory (either parent to child, child to parent, or same type). Otherwise, we’ll get a compile-time error that says IncomparableTypeException.

The equals() Method

  1. The equals() is a method present in Object class that only applies to object types and not primitive types..
  2. The equals() method is inherited by default from the object class and is clearly meant for reference comparison.
  3. For content comparison, we can override the equals() method. For example, the equals() method is overridden in the String class for content comparison.
  4. If there is no relationship between the argument types, the equals() method will not throw any compile- or run-time errors; it will simply return false.

For a better understanding, I’ve explained this using code snippets.

String s1 = new String("java");
String s2 = new String("java");

StringBuffer sb1 = new StringBuffer("java");
StringBuffer sb2 = new StringBuffer("java");

System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //true

System.out.println(sb1 == sb2); //false
System.out.println(sb1.equals(sb2)); //false

System.out.println(s1 == sb2); //Compile-Time Error: IncomparableTypeExeception
System.out.println(s1.equals(sb2)); //false

In the above code snippet, when we are comparing two String type objects by using the == operator, the result is false since the two objects are pointing to separate object references.

When we use the equals() method to compare two String-type objects, the result is true because the String class overrides the equals() method for content comparison.

When we use the == operator to compare two StringBuffer types of objects, it returns false because they are only comparing the reference, and thus they are pointing to two different reference variables.

When we are comparing two StringBuffer types objects by using the equals() method, it will return false as a result because the equals() method is inherited from the object class, and in the object class, the equals() method is meant for reference comparison.

In general, we compare references using the == operator and content using the equals() method.

Any reference r == null or r.equal(null) then always return false as result. here below I’m adding code snippet for your better understanding

Thread thread = new Thread();
System.out.println(thread == null); //false
System.out.println(thread.equals(null)); //false

In addition you can refer my previous post Relation between The == Operator and equals() Method.

--

--