Java Program to Compare Strings

Updated on November 12, 2024
Compare strings header image

Introduction

When it comes to programming in Java, comparing strings is a fundamental operation that you may encounter in various scenarios such as sorting, searching, and user authentication. String comparison is unique because it's not just about checking for equality in character sequence, but also involves memory address locations, reference checks, and case sensitivity issues.

In this article, you will learn how to compare strings in Java using different methods. You'll see how each approach works through relevant examples, ensuring you understand when to use each method based on the scenario.

Using the equals() Method

Check Exact Match

  1. Create two string variables for comparison.

  2. Use the equals() method to compare the two strings.

    java
    String str1 = "Hello";
    String str2 = "Hello";
    boolean isEqual = str1.equals(str2);
    System.out.println("String equality: " + isEqual);
    

    This code snippet compares str1 and str2 for equality in their characters and case. The output will be true because both strings are identical in content and case.

Using the equalsIgnoreCase() Method

Compare Ignoring Case

  1. Initialize strings in different cases.

  2. Apply equalsIgnoreCase() to compare irrespective of case.

    java
    String str1 = "hello";
    String str2 = "HELLO";
    boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
    System.out.println("Case insensitive equality: " + isEqualIgnoreCase);
    

    Here, even though str1 and str2 have different cases, equalsIgnoreCase() considers them equal. The method will return true.

Using the == Operator

Check String Interning

  1. Understand that == checks if two references point to the same object.

  2. Compare two string references using ==.

    java
    String str1 = "Hello";
    String str2 = "Hello";
    boolean isSameReference = (str1 == str2);
    System.out.println("Reference equality: " + isSameReference);
    

    In most cases, literal strings like in this example are interned by the JVM, meaning str1 and str2 point to the same memory location. Thus, the output here is true.

  3. Show scenario where == gives false.

    java
    String str1 = new String("Hello");
    String str2 = new String("Hello");
    boolean isSameReference = (str1 == str2);
    System.out.println("Reference equality with new: " + isSameReference);
    

    Here, str1 and str2 are created with new, so they refer to different objects. Despite having the same content, the output will be false.

Using the compareTo() Method

Sorting Strings Lexicographically

  1. Use the compareTo() method to determine the lexicographical order.

  2. Compare two strings.

    java
    String str1 = "Apple";
    String str2 = "Banana";
    int result = str1.compareTo(str2);
    System.out.println("Lexicographical comparison: " + result);
    

    This method returns a negative number because "Apple" comes before "Banana" alphabetically. The value indicates the difference between the first non-matching characters.

Conclusion

Java provides several methods for comparing strings, each suitable for different circumstances. Use equals() for a direct character-by-character comparison, equalsIgnoreCase() for a case-insensitive check, == for reference comparison, and compareTo() for determining lexicographical ordering. Understanding the nuances of each method allows you to choose the most appropriate string comparison technique for your Java applications, enhancing both functionality and performance.