How to Check Two Strings are Equal in Java

Introduction

Checking if two strings are equal is a common task in Java programming. Whether you are comparing user input, validating data, or implementing business logic, string comparison is essential. In Java, there are multiple ways to compare strings for equality. This blog post will explore different methods to check if two strings are equal in Java.

Table of Contents

  1. Using equals() Method
  2. Using equalsIgnoreCase() Method
  3. Using compareTo() Method
  4. Using Objects.equals() Method
  5. Complete Example Program
  6. Conclusion

1. Using equals()() Method

The equals() method is the most commonly used method to check if two strings are equal. It compares the content of the strings and returns true if they are equal, and false otherwise.

Example:

public class StringEqualityUsingEquals {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "World";

        // Check if str1 and str2 are equal
        boolean areEqual1 = str1.equals(str2);
        System.out.println("Are str1 and str2 equal? " + areEqual1);

        // Check if str1 and str3 are equal
        boolean areEqual2 = str1.equals(str3);
        System.out.println("Are str1 and str3 equal? " + areEqual2);
    }
}

Output:

Are str1 and str2 equal? true
Are str1 and str3 equal? false

Explanation:

  • str1.equals(str2) returns true because the contents of str1 and str2 are the same.
  • str1.equals(str3) returns false because the contents of str1 and str3 are different.

2. Using equalsIgnoreCase()() Method

The equalsIgnoreCase() method compares two strings, ignoring case considerations. It returns true if the strings are equal, ignoring case, and false otherwise.

Example:

public class StringEqualityUsingEqualsIgnoreCase {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "hello";
        String str3 = "World";

        // Check if str1 and str2 are equal ignoring case
        boolean areEqual1 = str1.equalsIgnoreCase(str2);
        System.out.println("Are str1 and str2 equal ignoring case? " + areEqual1);

        // Check if str1 and str3 are equal ignoring case
        boolean areEqual2 = str1.equalsIgnoreCase(str3);
        System.out.println("Are str1 and str3 equal ignoring case? " + areEqual2);
    }
}

Output:

Are str1 and str2 equal ignoring case? true
Are str1 and str3 equal ignoring case? false

Explanation:

  • str1.equalsIgnoreCase(str2) returns true because the contents of str1 and str2 are the same, ignoring case.
  • str1.equalsIgnoreCase(str3) returns false because the contents of str1 and str3 are different.

3. Using compareTo()() Method

The compareTo() method compares two strings lexicographically. It returns a negative integer, zero, or a positive integer if the first string is less than, equal to, or greater than the second string, respectively.

Example:

public class StringEqualityUsingCompareTo {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "World";

        // Check if str1 and str2 are equal using compareTo
        boolean areEqual1 = str1.compareTo(str2) == 0;
        System.out.println("Are str1 and str2 equal using compareTo? " + areEqual1);

        // Check if str1 and str3 are equal using compareTo
        boolean areEqual2 = str1.compareTo(str3) == 0;
        System.out.println("Are str1 and str3 equal using compareTo? " + areEqual2);
    }
}

Output:

Are str1 and str2 equal using compareTo? true
Are str1 and str3 equal using compareTo? false

Explanation:

  • str1.compareTo(str2) == 0 returns true because str1 and str2 are equal lexicographically.
  • str1.compareTo(str3) == 0 returns false because str1 and str3 are not equal lexicographically.

4. Using Objects.equals()() Method

The Objects.equals() method is a utility method that checks if two objects are equal. This method can be used to compare strings as well.

Example:

import java.util.Objects;

public class StringEqualityUsingObjectsEquals {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "World";

        // Check if str1 and str2 are equal using Objects.equals()
        boolean areEqual1 = Objects.equals(str1, str2);
        System.out.println("Are str1 and str2 equal using Objects.equals()? " + areEqual1);

        // Check if str1 and str3 are equal using Objects.equals()
        boolean areEqual2 = Objects.equals(str1, str3);
        System.out.println("Are str1 and str3 equal using Objects.equals()? " + areEqual2);
    }
}

Output:

Are str1 and str2 equal using Objects.equals()? true
Are str1 and str3 equal using Objects.equals()? false

Explanation:

  • Objects.equals(str1, str2) returns true because str1 and str2 are equal.
  • Objects.equals(str1, str3) returns false because str1 and str3 are not equal.

5. Complete Example Program

Here is a complete program that demonstrates all the methods discussed above to check if two strings are equal in Java.

Example Code:

import java.util.Objects;

public class StringEqualityExample {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "hello";
        String str4 = "World";

        // Using equals() Method
        boolean equalsResult = str1.equals(str2);
        System.out.println("Using equals(): Are str1 and str2 equal? " + equalsResult);

        // Using equalsIgnoreCase() Method
        boolean equalsIgnoreCaseResult = str1.equalsIgnoreCase(str3);
        System.out.println("Using equalsIgnoreCase(): Are str1 and str3 equal ignoring case? " + equalsIgnoreCaseResult);

        // Using compareTo() Method
        boolean compareToResult = str1.compareTo(str4) == 0;
        System.out.println("Using compareTo(): Are str1 and str4 equal? " + compareToResult);

        // Using Objects.equals() Method
        boolean objectsEqualsResult = Objects.equals(str1, str2);
        System.out.println("Using Objects.equals(): Are str1 and str2 equal? " + objectsEqualsResult);
    }
}

Output:

Using equals(): Are str1 and str2 equal? true
Using equalsIgnoreCase(): Are str1 and str3 equal ignoring case? true
Using compareTo(): Are str1 and str4 equal? false
Using Objects.equals(): Are str1 and str2 equal? true

6. Conclusion

Comparing strings for equality in Java can be accomplished in several ways. The equals() and equalsIgnoreCase() methods are commonly used for content comparison, while the compareTo() method provides a lexicographical comparison. The Objects.equals() method is a utility method that can be used for a null-safe comparison. By understanding these different methods, you can choose the one that best fits your needs and coding style.

Happy coding!

Comments