Difference between “==” operator and equals method in java

Let’s learn the difference between “==” operator (string equals java) and equals method (.equals java) in java.

difference between "==" operator and equals method in java

Difference between “==” operator and equals method in java

In general we use string equals java for memory reference comparison. Because in java we don’t have concept of operator overloading. Meanwhile java .equals method is used for checking equality of two objects.

Java string equals returns true only, if two object reference is pointing to same object else return false.

Also java string equals is used to compare primitive data types float, boolean, integer.

Also read – java program to check if it is a sparse matrix

While .equals java returns true only, if two objects have same value. Let’s say, if two Students have same studentID then they are considered equal.

Example:

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


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

Let’s compare strings using string equals java and .equals java,

// == vs equals java
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
      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("Compare two reference pointing to same Object using == operator : " + output);
   }
}

Output:

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

NOTE:

  • .equals() method of Object class can also be used for comparing reference based on requirement and is used to check equality of objects only.
  • string equals java and .equals java is used to compare objects to test equality but .equals java is a method while string equals java is an operator.