How to Compare Strings in Java

In order to compare Strings for equality, you should use the String object’s equals or equalsIgnoreCase methods. We will also see why we should not use the == operator to compare strings.

Comparing Strings with equals() Method

If we need to compare two strings in java and also care about the casing of the strings we can use the equals() method.

For example, the following snippet will determine if the two instances of String are equal on all characters including casing:

public class CompareTwoStrings {

    public static void main(String[] args) {
        String firstString = "Test123";
        String secondString = "Test" + 123;
        String thirdString = "TEST123";

        if (firstString.equals(secondString)) {
            System.out.println("first and second strings are equal");
        }

        if (firstString.equals(thirdString)) {
            System.out.println("first and third string are equal");
        }
    }
}

Output:

first and second strings are equal

Comparing Strings with equalsIgnoreCase() Method

If we need to compare two strings in java but don’t care about the casing of the strings we can use the equalsIgnoreCase() method.

For example, in the above code snippet, if we replaced .equals() with .equalsIgnoreCase() method, then both print statements get executed:

public class CompareTwoStrings {

    public static void main(String[] args) {
        String firstString = "Test123";
        String secondString = "Test" + 123;
        String thirdString = "TEST123";

        if (firstString.equalsIgnoreCase(secondString)) {
            System.out.println("first and second strings are equal");
        }

        if (firstString.equalsIgnoreCase(thirdString)) {
            System.out.println("first and third string are equal");
        }
    }
}

Output:

    first and second strings are equal
    first and third string are equal

Do not use the == operator to compare Strings

These operators actually test references, and since multiple String objects can represent the same String, this is liable to give the wrong answer.

Instead, use the String.equals(Object) method, which will compare the String objects based on their values.

public class CompareTwoStrings {

    public static void main(String[] args) {
        String firstString = "Test123";
        String secondString = "Test123";
        String thirdString = new String("Test123");

        if (firstString == secondString) {
            System.out.println("first and second strings are equal");
        }

        if (firstString == thirdString) {
            System.out.println("first and third strings are equal");
        }
    }
}

Output:

    first and second strings are equal

Comparing Strings With Constant Values

When comparing a String to a constant value, you can put the constant value on the left side of equals to ensure that you won’t get a NullPointerException if the other String is null.

For example:

"baz".equals(foo)

While foo.equals("baz") will throw a NullPointerException if foo is null, "baz".equals(foo) will evaluate to false.

A more readable alternative is to use Objects.equals(), which does a null check on both parameters:

e.g. Objects.equals(foo, "baz").

Comparing Strings in a Switch Statement

As of Java 1.7, it is possible to compare a String variable to literals in a switch statement. Make sure that the String is not null, otherwise it will always throw a NullPointerException. Values are compared using String.equals, i.e. case sensitive.

public class CompareTwoStrings {

    public static void main(String[] args) {
        String stringToSwitch = "A";
        
        switch (stringToSwitch) { 
            case "a":
                System.out.println("a");
                break; 
            
            case "A":
                System.out.println("A"); //the code goes here
                break; 
            
            case "B":
                System.out.println("B");
                break;
            
            default:
                break;
        }
    }
}

Conclusion

In this post we explained how to compare strings in java with code examples. When casing of the strings matters, we should use .equals() and when casing is not important, then we should use .equalsIgnoreCase().

Moreover, we should not use the == operator to compare strings, as the == operator checks the reference and not the value.