DEV Community

Cover image for String Comparison in Java: equals(), ==, compareTo() and More
Rachit Joshi
Rachit Joshi

Posted on

String Comparison in Java: equals(), ==, compareTo() and More

Introduction

String comparison is a common task in Java programming. Whether you are checking user input, comparing values, sorting text, or checking prefixes and suffixes, Java provides several ways to work with strings.

The important thing to understand is that comparing string content and comparing object references are different operations. Methods such as equals() compare content, while == checks whether two references point to the same object.

1. Comparing Strings Using equals()

The equals() method compares the actual content of two strings.

public class Main {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "Java";
String str3 = "Python";

    System.out.println(str1.equals(str2));
    System.out.println(str1.equals(str3));
}
Enter fullscreen mode Exit fullscreen mode

}

Output:

true
false

equals() is case-sensitive, so "Java" and "java" are considered different strings.

2. Using equalsIgnoreCase()

When you want to compare strings without considering uppercase and lowercase differences, use equalsIgnoreCase().

public class Main {
public static void main(String[] args) {
String a = "Java";
String b = "JAVA";

    System.out.println(a.equalsIgnoreCase(b));
}
Enter fullscreen mode Exit fullscreen mode

}

Output:

true

This method is useful when the capitalization of the input should not affect the comparison.

3. Comparing Strings with ==

The == operator compares object references, not the string contents.

public class Main {
public static void main(String[] args) {
String a = "Java";
String b = "Java";
String c = new String("Java");

    System.out.println(a == b);
    System.out.println(a == c);
}
Enter fullscreen mode Exit fullscreen mode

}

Possible output:

true
false

The first comparison can be true because identical string literals may refer to the same pooled String object. The object created with new String() is separate, even though its content is the same.

For comparing string values, equals() is generally the appropriate choice.

4. Using compareTo()

The compareTo() method performs a lexicographical comparison between two strings.

public class Main {
public static void main(String[] args) {
String first = "Apple";
String second = "Banana";

    System.out.println(first.compareTo(second));
}
Enter fullscreen mode Exit fullscreen mode

}

The method returns:

0 when the strings are equal
A negative value when the first string comes before the second
A positive value when the first string comes after the second

This makes compareTo() particularly useful when strings need to be ordered or sorted.

5. Using startsWith()

The startsWith() method checks whether a string begins with a specified sequence of characters.

String text = "Java Programming";

System.out.println(text.startsWith("Java"));

Output:

true

It returns true when the specified prefix matches the beginning of the string.

6. Using endsWith()

Similarly, endsWith() checks whether a string finishes with a particular sequence.

String text = "Java Programming";

System.out.println(text.endsWith("Programming"));

Output:

true

This can be useful for checking file extensions, suffixes, or specific endings in text.

Conclusion

Java provides several methods for comparing strings, and each has a specific purpose. Use equals() when you need to compare string content, equalsIgnoreCase() when case should be ignored, and compareTo() when you need lexicographical ordering.

The startsWith() and endsWith() methods are useful for checking prefixes and suffixes.

Understanding the difference between value comparison and reference comparison is especially important when working with Java Strings.

Top comments (0)