Open In App
Related Articles

Difference between comparing String using == and .equals() method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Both the equals() method and the == operator are used to compare two objects in Java.

The Java string equals() method, compares two strings and returns true if all characters match in both strings, else returns false.

The == operator compares the reference or memory location of objects in a heap, whether they point to the same location or not.
Whenever we create an object using the operator new, it will create a new memory location for that object. So we use the == operator to check memory location or address of two objects are the same or not.

In general, both equals() and “==” operators in Java are used to compare objects to check equality, but here are some of the differences between the two: 

  1. The main difference between the .equals() method and the == operator is that one is a method, and the other is the operator.
  2. We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
  3. If a class does not override the equals method, then by default, it uses the equals(Object o) method of the closest parent class that has overridden this method. **See Why to Override equals(Object) and hashCode() method? in detail.

Example:

String equals() method and == operator in Java.

Java

// Java program to understand
// the concept of == operator
 
public class Test {
    public static void main(String[] args)
    {
        String s1 = "HELLO";
        String s2 = "HELLO";
        String s3 =  new String("HELLO");
 
        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s1.equals(s2)); // true
        System.out.println(s1.equals(s3)); // true
    }
}

                    

Output
true
false
true
true


Explanation: Here, we create two objects, namely s1 and s2. 

  • Both s1 and s2 refer to the same objects.
  • When we use the == operator for s1 and s2 comparison, the result is true as both have the same addresses in the string constant pool.
  • Using equals, the result is true because it’s only comparing the values given in s1 and s2.

 Java String Pool

s1 = “HELLO”

s2 = “HELLO”

Java Heap

s3 = “HELLO”

Let us understand both the operators in detail:

Equality operator(==)

We can apply equality operators for every primitive type, including the boolean type. We can also apply equality operators for object types. 

Example:

== operator in Java.

Java

// Java program to illustrate
// == operator for compatible data
// types
 
class Test {
    public static void main(String[] args)
    {
        // integer-type
        System.out.println(10 == 20);
 
        // char-type
        System.out.println('a' == 'b');
 
        // char and double type
        System.out.println('a' == 97.0);
 
        // boolean type
        System.out.println(true == true);
    }
}

                    

Output
false
false
true
true


If we apply == for object types then, there should be compatibility between argument types (either child to parent or parent to child or same type). Otherwise, we will get a compile-time error. 

Example:

Java

// Java program to illustrate
// == operator for incompatible data types
 
class Test {
    public static void main(String[] args)
    {
        Thread t = new Thread();
        Object o = new Object();
        String s = new String("GEEKS");
 
        System.out.println(t == o);
        System.out.println(o == s);
 
       // Uncomment to see error
       System.out.println(t==s);
    }
}

                    

Output: 

false
false
// error: incomparable types: Thread and String

Java String equals() Method

In Java, the String equals() method compares the two given strings based on the data/content of the string. If all the contents of both strings are the same, it returns true. If all characters are not matched, then it returns false.

Syntax:

Syntax: public boolean equals(Object anotherObject)

Parameter:

  • anotherObject- String to be compared

Returns:

  • Boolean value:
    • true- If strings are equal
    • false- if strings are not equal

Example:

String equals() method in Java

Java

public class Test {
    public static void main(String[] args)
    {
        Thread t1 = new Thread();
        Thread t2 = new Thread();
        Thread t3 = t1;
 
        String s1 = new String("GEEKS");
        String s2 = new String("GEEKS");
 
        System.out.println(t1 == t3);
        System.out.println(t1 == t2);
        System.out.println(s1 == s2);
 
        System.out.println(t1.equals(t2));
        System.out.println(s1.equals(s2));
    }
}

                    

Output:

true
false
false
false
true

Explanation: Here, we are using the .equals method to check whether two objects contain the same data or not. 

  • In the above example, we create 3 Thread objects and 2 String objects.
  • In the first comparison, we check whether t1 == t3 or not. As we know both t1 and t3 point to the same object. That’s why it returns true.
  • In the second comparison, we are using the operator “==” for comparing the String Objects and not the contents of the objects. Here, both the objects are different, and hence the outcome of this comparison is “False.”
  • When we are comparing 2 String objects by the equals() operator, then we are checking that is both objects contain the same data or not.
  • Both the objects contain the same String, i.e., GEEKS. That’s why it returns true.

Also Read:

Conclusion

Java string equals() method and == operator are both used to compare strings in Java. In this tutorial we have covered the ==operator and String equals() method in Java with examples.

Read More String Methods in Java

Difference between == and .equals() method- FAQs

1. What is the difference between str1 == str2 and str1 equals str2?

In Java, str1==str2 will return true if str1 and str2 are strings with the same content. This is because strings with the same content are stored at the same location.

str1.equals(str2) will return true if str1 and str 2 are different string objects, but have same content.

2. What is the difference between == and equals in string comparison?

The string equals() method compares two strings and returns true if all characters match in both strings, else returns false.

The == operator compares the reference or memory location of objects in a heap, whether they point to the same location or not.

3. What are the rules of equals() method?

  1. Reflexivity: For any object x, x.equals(x) should always return true. 
  2. Symmetry: If x.equals(y) returns true, then y.equals(x) must also return true.
  3. Transitivity: If x.equals(y) and y.equals(z) are both true, then x.equals(z) must also be true. 
  4. Consistency: Multiple invocations of x.equals(y) should return the same result.
  5. Non-nullity: The equals() method should never return true when compared to null.


Last Updated : 16 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads