Java String equals() method – Java compare strings

Java String equals() method is used to compare a string with the method argument object.

1. Java String.equals() method

/**
* @param  anObject - The object to compare
* @return true -  if the non-null argument object represents the same sequence of characters to this string
*         false - in all other cases       
*/
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}
  1. String class overrides equals() method from Object class. The equality is done in case-sensitive manner.
  2. Use equals() method to check the equality of string contents.
  3. Do not use '==' operator. It checks the object references, which is not not desirable in most cases.
  4. Passing ‘null’ to method is allowed. It will return false.
  5. Use equalsIgnoreCase() method to check equal strings in case-insensitive manner.

2. Java String equals() method example

Java program to check if two strings are equal (case-sensitive).

public class Main 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        String authorName = "Lokesh gupta";
        
        //1 - check two strings for same character sequence
        boolean isEqualString = blogName.equals(authorName);    //false
        
        //2
        isEqualString = blogName.equals("howtodoinjava.com");   //true
        
        //3
        isEqualString = blogName.equals(null);                  //false
        
        //4 - case-sensitive
        isEqualString = blogName.equals("HOWTODOINJAVA.COM");   //false
    }
}

3. Java String equalsIgnoreCase() example

Java program to check if two strings are equal (case-insensitive). Notice that equals() and equalsIgnoreCase() methods behave in same way, except later is case-insensitive.

public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        String authorName = "Lokesh gupta";
        
        //1 - check two strings for same character sequence
        boolean isEqualString = blogName.equalsIgnoreCase(authorName);    //false
        
        //2
        isEqualString = blogName.equalsIgnoreCase("howtodoinjava.com");   //true
        
        //3
        isEqualString = blogName.equalsIgnoreCase(null);                  //false
        
        //4 - case-insensitive
        isEqualString = blogName.equalsIgnoreCase("HOWTODOINJAVA.COM");   //TRUE
    }
}

4. Difference between == and equals in Java

As mentioned earlier, '==' operator checks for same object references. It does not check for string content.

Whereas equals() method checks for string content.

public class Main 
{
    public static void main(String[] args) 
    {
        String blogName1 = new String("howtodoinjava.com");
        String blogName2 = new String("howtodoinjava.com");
       
        boolean isEqual1 = blogName1.equals(blogName2);         //true
        boolean isEqual2 = blogName1 == blogName2;              //false
    }
}

Happy Learning !!

Reference:

Java String Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.