Sub topic 4
Comparing String Contents
A String is a Java type that stores text. When you compare two strings, you usually want to know whether they contain the same characters. Java provides methods for comparing contents and checking their order.
A method is an action you call on a value. For strings, the equals method checks whether two strings have the same contents.
Compare contents with `equals`
The expression "cat".equals("cat") gives true. The expression "cat".equals("Cat") gives false because the first letters have different cases.
Spaces matter too: "cat" and "cat " are different strings.
Use equalsIgnoreCase when you want to ignore the difference between uppercase and lowercase letters. For example, "cat".equalsIgnoreCase("CAT") gives true. This method is useful for simple case-insensitive checks, but it does not follow the language rules of every region.
Why not use `==`?
The == operator checks whether two string variables refer to the same string object. An object is a value Java stores. Two separate string objects can contain the same text, so == is not a reliable way to compare their contents.
Run this example:
import java.util.Objects;
class CompareText {
public static void main(String[] args) {
String first = new String("cat");
String second = new String("cat");
String missing = null;
System.out.println(first == second);
System.out.println(first.equals(second));
System.out.println(first.equalsIgnoreCase("CAT"));
System.out.println("cat".equals(missing));
System.out.println(Objects.equals(first, missing));
System.out.println(first.compareTo("dog") < 0);
}
}Save it as CompareText.java. Then compile and run it:
javac CompareText.java
java CompareTextThe lines printed are:
false:firstandsecondrefer to separate objects.true: their contents are the same.true: the words match when case is ignored.false:nullis not the text"cat".false:firsthas text, butmissingisnull.true:"cat"comes before"dog"in this comparison.
The example uses new String to make two separate objects. You do not usually need it when writing string values.
Handle a missing string
null means a variable does not refer to a string. Calling a method on null, as in missing.equals("cat"), causes a NullPointerException. That is an error that stops the program unless it is handled.
If one value is a known string, call equals on that value: "cat".equals(missing) safely returns false.
If either value might be null, use Objects.equals(a, b). It returns true if both are null, false if only one is null, and otherwise compares their contents. The example imports Objects so it can use this method.
Compare order with `compareTo`
The compareTo method compares strings from the beginning:
- A result less than zero means the first string comes before the second.
- Zero means their contents are equal.
- A result greater than zero means the first string comes after the second.
Check whether the result is less than or greater than zero rather than expecting a particular number. The comparison uses Java’s UTF-16 values, not the sorting rules of a particular language. Uppercase and lowercase letters can sort in an unexpected order.
Exercise
What do "Hi".equals("hi") and "Hi".equalsIgnoreCase("hi") return?
Show answer
The first returns false because case differs. The second returns true because it ignores case.