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

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.

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

Also == operator is used to compare objects and primitive data types like float, boolean, integer.

Also read – variables in java

While equals() method is defined in java.lang.Object class. equals() method 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 two objects have different location in heap.

Now using equals method; Here the output will be true. Because equals() method of Object class is overridden by java.lang.String class that checks contents of objects.

Using class String equals() method contents are same. Hence output is true. Here’s difference between == and equals 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



Equals and == in java example

Let’s compare strings using string equals and == operator or equals and == in java example,

public class OperatorVsMethod 
{
   public static void main(String[] args) 
   {
      String str1 = new String("hello world java");
      String str2 = new String("hello world java");
      // 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.

java 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
   }
}