There are three approaches to how to compare strings in Java. Java String CompareTo(), Equals() methods and comparing by reference by == operator. The first two methods compare the string on the basis of content but the last one is by reference.
Compare String in Java by Equals() Method
The String class provides two methods for comparing. See please below.
public boolean equals(Object object) – compares by value with case sensitivity.
public boolean equalsIgnoreCase(String object) – compares without case sensitivity by value.
The strings compare are different depending on what method we use, see the example below.
When we use the method CompareTo() Java analyzes the strings lexicographically and as result, we have three values. Where 0 means that the strings are equal. Values -1 and 1 mean that the strings are different. See the examples below to understand better the returned value of the method.