Compare two strings in Java
This post will explore several ways to compare two strings in Java using equals() and compareTo() methods of String class, equals() method of the Objects class, and StringUtils class of Apache Commons library.
1. Avoid equal-to operator (==) method
Every Java programmer should know that equal-to operator == should not be used for string equality as it will check for reference equality, i.e., whether two strings point to the same object or not. This behavior is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Main { public static void main(String[] args) { String s1 = new String("Equal-to operator in Java"); String s2 = new String("Equal-to operator in Java"); System.out.println(s1 == s2); // Evaluates to false System.out.println(null == s2); // Evaluates to false System.out.println(s1 == null); // Evaluates to false System.out.println(null == null); // Evaluates to true } } |
Since the == operator checks whether the references to the objects are equal or not, it might return true for two string literals with equal value as they will share the same object in the JVM string pool, as shown below. It is recommended not to rely on such behavior and use standard methods for string equality.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
class Main { public static void main(String[] args) { String s1 = new String("Equal-to operator in Java"); String s2 = new String("Equal-to operator in Java"); // Evaluates to true as both objects point to the same reference // in String pool System.out.println(s1 == s2); } } |
2. Using String.equals() method
Java String class has the equals() method that compares the actual contents of a string with another string and returns a boolean value determining if they are the same or not. The String class also offers:
equalsIgnoreCase(String)method, which is similar to theequals()method except it ignores the case of characters in the String.contentEquals(CharSequence)method, which compares the string to the specified CharSequence.contentEquals(StringBuffer)method, which compares the string to the specified stringBuffer.
Here’s a simple Java program that demonstrates the working of the equals() method. Note that we should not call equals() on an empty string as it will lead to a NullPointerException.
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { String s1 = new String("Compare two strings in Java"); String s2 = new String("Compare two strings in Java"); System.out.println(s1 != null && s1.equals(s2)); // Evaluates to true } } |
3. Using Objects.equals() method
Java 7 introduced the Object class, which consists of several static utility methods for operating on an object. It has the equals() method, which returns a boolean value determining if the arguments are equal to each other or not.
Unlike the equals() method of the String class, it gracefully handles nulls and doesn’t throw a NullPointerException, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Objects; class Main { public static void main(String[] args) { String s1 = new String("Compare two strings in Java"); String s2 = new String("Compare two strings in Java"); System.out.println(Objects.equals(s1, s2)); // Evaluates to true System.out.println(Objects.equals(null, null)); // Evaluates to true System.out.println(Objects.equals(null, s2)); // Evaluates to false System.out.println(Objects.equals(s1, null)); // Evaluates to false } } |
4. Using String.compareTo() method
String class also provides the compareTo() method, which compares two strings lexicographically, i.e., character by character based on the dictionary ordering. It returns 0 when the string is equal to the argument. Otherwise, it returns the difference between the two character values present at the first non-matching index.
The String class also offers the compareToIgnoreCase() method, which is similar to compareTo() method but ignores the case of both strings. Like the equals() method of the String class, one should not call the compareTo() method on an empty string as it will lead to a NullPointerException.
Here’s a short Java program to demonstrate the working of the compareTo() method:
|
1 2 3 4 5 6 7 8 9 10 11 |
class Main { public static void main(String[] args) { String s1 = new String("Compare two strings in Java"); String s2 = new String("Compare two strings in Java"); // Evaluates to true System.out.println(s1 != null && s1.compareTo(s2) == 0); } } |
5. Using Apache Commons Lang
The Apache Commons StringUtils class provides several static utility methods for string comparison. The equals() and compare() method of StringUtils class is an enhanced version of the equals() and compareTo() method of String class, respectively, which also handles null values.
StringUtils class also has equalsIgnoreCase() and compareIgnoreCase() method, which are similar to the equals() and compare() method except they ignore the case of both strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String s1 = new String("Compare two strings in Java"); String s2 = new String("Compare two strings in Java"); // Evaluates to true System.out.println(StringUtils.equals(s1, s2)); // Evaluates to true System.out.println(StringUtils.compare(s1, s2) == 0); } } |
That’s all about comparing two strings in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)