Skip to lesson

Compare Strings: equals() and compareTo()

To check whether two Strings hold the same text, use equals(). To put them in dictionary order, use compareTo(). Never use == for text.

equals(): the same text?

A quiz app must check whether the typed answer is right. a.equals(b) is true when both Strings have the same characters in the same order; capitals count, unless you use equalsIgnoreCase().

Example 1 · A yes/no answer in any capitals

YesNo.javaJava
import java.util.Scanner;
public class YesNo {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("Continue? (yes/no): ");        String ans = sc.nextLine().trim();        if (ans.equalsIgnoreCase("yes")) {            System.out.println("Continuing...");        } else {            System.out.println("Stopped.");        }        sc.close();    }}
Sample output
Continue? (yes/no): YES
Continuing...

What this code does

Experiment

Compare lab

Type two words. Try Yes and yes, then yes and yes with a space.

String a = "YES";
String b = "yes";
System.out.println(a.equals(b));
System.out.println(a.equalsIgnoreCase(b));
a.equals(b)
false
a.equalsIgnoreCase(b)
true

Why == is wrong for text

Two Strings both print Ravi, yet == says false. A String variable holds the address of its object, like an array variable (opens in a new tab), so == asks "the same object?", not "the same text?".

Two variables, a and b, each point to their own String object with the text "Ravi". a == b is false because they are two different objects; a.equals(b) is true because the text is the same.

Example 2 · Same text, but == says false

CompareNames.javaJava
public class CompareNames {    public static void main(String[] args) {        String a = "Ravi";        String part = "Ra";        String b = part + "vi";        System.out.println(a + " and " + b);        System.out.println("a == b: " + (a == b));        System.out.println("a.equals(b): " + a.equals(b));    }}
Output
Ravi and Ravi
a == b: false
a.equals(b): true

What this code does

  • b is built while the program runs, so it is a new object that happens to hold the same text.
  • a == b compares the two objects and gives false; a.equals(b) compares the characters and gives true.
  • The brackets in (a == b) matter: without them, + would join the text first.

Java keeps one shared copy of each literal such as "Ravi" in the String pool, so two literals can give == as true by luck. Text from Scanner, from + with a variable or from a method is a new object, and then == fails.

The rule is simple: for text, always use equals(). Use == only for numbers, char and boolean.

compareTo(): dictionary order

A class list must print names in alphabetical order. a.compareTo(b) gives a negative number when a comes first, 0 when the texts are equal, and a positive number when a comes after b.

Example 3 · Which name comes first?

NameOrder.javaJava
public class NameOrder {    public static void main(String[] args) {        System.out.println("Amit".compareTo("Bala"));        System.out.println("Bala".compareTo("Amit"));        System.out.println("Amit".compareTo("Amit"));        String x = "Neha";        String y = "Karan";        if (x.compareTo(y) < 0) {            System.out.println(x + " comes first");        } else {            System.out.println(y + " comes first");        }    }}
Output
-1
1
0
Karan comes first

What this code does

  • A comes before B, so "Amit".compareTo("Bala") is negative; swapping them makes it positive, and equal texts give 0.
  • "Neha".compareTo("Karan") is positive, because N comes after K, so the else part prints Karan comes first.

Only the sign matters, so test < 0 or > 0, never == -1. Java compares character codes, so every capital letter comes before every small letter: "Zara" comes before "anu". compareToIgnoreCase() ignores capitals.

Next step

Check your understanding

9 quick questions