How to Check Two Strings are Equal in Iava

In Java, comparing strings is a common operation used to determine whether two strings have the same contents or are in lexicographic order. However, due to the immutability of strings in Java, comparing them requires a different approach than comparing other data types. In this blog post, we will explore the various ways to compare strings in Java.
  • Using equals() Method
  • Using equalsIgnoreCase() Method
  • Using compareTo() Method
  • Using compareToIgnoreCase() Method
  • Using == Operator
Important: Always use equals() if you are checking for equality because it does a value-based comparison.

1. Using equals() Method 

The equals() method is used to compare the contents of two strings for equality. It returns true if the strings have the same content and false otherwise. 

Example:
package net.javaguides.examples;

public class EqualsExample {
    public static void main(String[] args) {
        String str = "javaguides";
        String str1 = "javaguides";
        String str3 = "javatutorial";
        boolean equal = str.equals(str1);
        System.out.println(" Is both string are equal :: " + equal);
    }
}
Output:
 Is both string are equal :: true
I would suggest this approach to check the equality of two string objects.

2. Using equalsIgnoreCase() Method 

The equalsIgnoreCase() method is similar to equals(), but it performs a case-insensitive comparison. It returns true if the strings have the same content, ignoring their case. 

Example:
package net.javaguides.examples;

public class EqualsIgnoreCaseExample {
    public static void main(String[] args) {
        String str = "javaguides";
        boolean equal = str.equalsIgnoreCase("JAVAguides");
        System.out.println("Strings are equal :: " + equal);
    }
}
Output:
Strings are equal :: true

3. Using compareTo() Method 

The compareTo() method compares two strings lexicographically. It returns an integer value that indicates the difference between the two strings. If the result is 0, it means the strings are equal. If the result is positive, the first string is lexicographically greater. If the result is negative, the second string is lexicographically greater.

Example:
package net.javaguides.examples;

public class CompareToExample {
    public static void main(String[] args) {
        String s1 = "Hello World";
        String s2 = "Hello World";
        String s3 = "Java";
        String s4 = "Guides";
        System.out.println(s1.compareTo(s2));
        // 0 because both are equal
        System.out.println(s1.compareTo(s3));
        // -2 because "H" is 2 times lower than "J"
        System.out.println(s1.compareTo(s4));
        // 1 because "G" is 1 times greater than "H"
    }
}
Output:
0
-2
1

4. Using compareToIgnoreCase() Method 

Similar to compareTo(), the compareToIgnoreCase() method also compares strings lexicographically. However, it performs a case-insensitive comparison. 

Example:
package net.javaguides.examples;

public class CompareToIgnoreCaseExample {
    public static void main(String[] args) {
        String s1 = "Hello World";
        String s2 = "hello world";
        String s3 = "Java";
        String s4 = "java";
        System.out.println(s1.compareToIgnoreCase(s2));
        System.out.println(s3.compareToIgnoreCase(s4));
    }
}
Output:
0
0

5. Using Comparison Operators (==) 

The == and != operators can also be used to compare strings in Java. However, these operators do not compare the content of strings; instead, they compare the memory references of the string objects.

Example:
package net.javaguides.examples;

public class EqualOperatorExample {
    public static void main(String[] args) {
        String s1 = "javaguides";
        String s2 = new String("javaguides");
        String s3 = "javaguides";
        boolean result = s1 == s2;
        boolean result2 = s1 == s3;

        System.out.println(" Result :: " + result);
        System.out.println(" Result of == operator :: " + result2);
    }
}
Output:
 Result :: false
 Result of == operator :: true

Conclusion 

Comparing strings in Java is a fundamental operation, and it's essential to choose the appropriate method based on your comparison requirements. The equals() and equalsIgnoreCase() methods are commonly used for content comparison, while the compareTo() and compareToIgnoreCase() methods are useful for lexicographic comparisons. Remember to avoid using the == and != operators for string comparison, as they may not provide the expected results. Always use the methods provided by the String class for accurate and reliable string comparisons in Java.

Comments