0
0
JavaHow-ToBeginner · 3 min read

How to Compare Strings in Java: Syntax and Examples

In Java, use string1.equals(string2) to check if two strings have the same characters. For case-insensitive comparison, use string1.equalsIgnoreCase(string2). To compare lexicographically, use string1.compareTo(string2) which returns a number indicating order.
📐

Syntax

Here are the main ways to compare strings in Java:

  • string1.equals(string2): Returns true if both strings have exactly the same characters.
  • string1.equalsIgnoreCase(string2): Returns true if strings are equal ignoring uppercase/lowercase differences.
  • string1.compareTo(string2): Returns 0 if strings are equal, a negative number if string1 is lexicographically less, or a positive number if greater.
java
boolean isEqual = string1.equals(string2);
boolean isEqualIgnoreCase = string1.equalsIgnoreCase(string2);
int comparison = string1.compareTo(string2);
💻

Example

This example shows how to compare two strings using equals, equalsIgnoreCase, and compareTo.

java
public class StringCompareExample {
    public static void main(String[] args) {
        String a = "Hello";
        String b = "hello";
        String c = "Hello";

        System.out.println("a.equals(b): " + a.equals(b));
        System.out.println("a.equalsIgnoreCase(b): " + a.equalsIgnoreCase(b));
        System.out.println("a.equals(c): " + a.equals(c));
        System.out.println("a.compareTo(b): " + a.compareTo(b));
        System.out.println("a.compareTo(c): " + a.compareTo(c));
    }
}
Output
a.equals(b): false a.equalsIgnoreCase(b): true a.equals(c): true a.compareTo(b): -32 a.compareTo(c): 0
⚠️

Common Pitfalls

Many beginners mistakenly use == to compare strings, which checks if both variables point to the same object, not if their text is the same. Always use equals() for content comparison.

Also, remember that compareTo() is case-sensitive and returns an integer, not a boolean.

java
public class WrongStringCompare {
    public static void main(String[] args) {
        String s1 = new String("test");
        String s2 = new String("test");

        // Wrong: compares references
        System.out.println(s1 == s2); // false

        // Right: compares content
        System.out.println(s1.equals(s2)); // true
    }
}
Output
false true
📊

Quick Reference

MethodDescriptionReturns
equals()Checks if two strings have the same charactersboolean (true/false)
equalsIgnoreCase()Checks equality ignoring case differencesboolean (true/false)
compareTo()Compares strings lexicographicallyint (0 if equal, <0 if less, >0 if greater)
== operatorChecks if two string references point to the same objectboolean (true/false)

Key Takeaways

Use equals() to compare string contents, not ==.
equalsIgnoreCase() compares strings ignoring case differences.
compareTo() returns an integer showing lexicographical order.
Avoid using == for string content comparison as it checks references.
Remember compareTo() is case-sensitive and returns an int, not boolean.