Difference between == operator and equals method in java

Let’s learn difference between == operator and equals method in java.

Difference between == operator and equals method in java

In general == is an operator and equals() is a method. We use == operator for reference comparison of objects in heap. Because in java we don’t have concept of operator overloading.

difference between == operator and equals method in java

Meanwhile == operator is used for checking address of two objects is same or not. That is, == operator checks if both objects point to the same memory location or not.

Also == operator is used to compare object types and primitive types like boolean.

Also read – strings in java

While equals() method of String class compares the content of two objects. For example two Student objects are equal if there ID is same.

Now let’s learn the difference between equals and == operator with the help of an example.

In the below example we have created two strings str1 and str2. Now let’s compare these two strings using == operator and equals() method to check whether they are equal or not.

First using == operator. For == comparison output will be false. Because “str1” and “str2” have different addresses. == operator is used for reference comparison.

for better understanding learn string constant pool.

Now using equals() method; Here the output will be true. Because equals() method of String class overrides equals() method of Object class.

NOTE:

  • equals() method of Object class is used for address or reference comparison.
  • While equals() method of String class (which overrides equals() method of Object class) is used to compare contents.

Hence output is true. Here’s difference between == and equals method in java with example.

Example:

// == operator
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1 == str2); // output : false


// equals method
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1.equals(str2)); // output : true



== and equals() example

Let’s compare strings using String equals() method and == operator or equals and == in java (detailed or complete) example.

public class OperatorMethodExample
{
   public static void main(String[] args) 
   {
      String str1 = new String("helloworld");
      String str2 = new String("helloworld");
      // two strings have different object so output is false
      boolean output = str1 == str2;
      System.out.println("Comparing with == operator : " + output);
      // strings contain same content hence true
      // java.lang.String class override equals method of Object class
      output = str1.equals(str2);
      System.out.println("Comparing with same content using equals() java : " + output);
      str2 = str1;
      // both str1 and str2 reference are pointing to same object hence
      // "==" operator return true
      output = (str1 == str2);
      System.out.println("Comparing with == operator : " + output);
   }
}


Output:

Comparing with == operator : false
Comparing with same content using equals() java : true
Comparing with == operator : true


Now let’s compare two objects using == operator and equals() method.

public class ComparisonDemo
{
   public static void main(String[] args)
   {
      Object ob1 = new Object();
      Object ob2 = new Object();
      boolean output;
      output = (ob1 == ob2);
      System.out.println("== operator : " + output);
      output = ob1.equals(ob2);
      System.out.println("equals() method : " + output);
      ob1 = ob2;
      output = (ob1 == ob2);
      System.out.println("Comparing two reference pointing to same Object using == operator : " + output);
   }
}


Output:

== operator : false
equals() method : false
Comparing two reference pointing to same Object using == operator : true


NOTE:

  • == operator is used for reference comparison or address comparison. While equals() method is used for content comparison.
  • equals() method of Object class is used to compare reference of two objects. == and equals() method are same with respect to Object class.

how equals method works in javajava equals override

equals() method of Object class is used to compare address of two objects.

class Object
{
   public boolean equals(Object obj)
   {
      return (this == obj); // this refers to current class instance variable
   }
}


equals() method of String class is used to compare content of two objects. Here equals() method of Object is overridden by equals() method of String class.

class String extends Object
{
   @Override
   public boolean equals(Object str)
   {
      // content comparison
   }
}