Comparing Strings

Use equals to compare string content. Use equalsIgnoreCase only when letter case truly should not matter.

The == operator compares whether two reference variables point to the same object. Two different String objects can contain identical text.

Compare the characters

public class Main {
    public static void main(String[] args) {
        String expected = "dark";
        String actual = "DARK";
        System.out.println(expected.equalsIgnoreCase(actual));
    }
}

Output:

true

The two strings have different capitalization, but equalsIgnoreCase treats their content as equal.

If a variable may be null, calling a method on it throws NullPointerException. A known non-null value can call equals, or Objects.equals can handle nulls.

Compare the two entered words without caring about letter case.