Answer: To compare two strings in Java, the most common method is equals().
In Java, a String is a sequence of characters. It is one of the most commonly used data types in Java and is represented by the String class in the java.lang package. Comparing two strings is the most common task while working with Strings. It is used in validating user input, searching and sorting data, implementing conditional logic, and ensuring data consistency in applications.
In this blog, we will learn, how to compare strings in Java using different methods.
Table of Contents:
String Comparison Methods in Java
Here are the following methods by which we can compare two strings in Java:
Method 1: Using String.equals() Method to Compare Strings in Java
The equals() method in Java is used to compare the values of two strings. It checks whether two strings have the same sequence of characters.
Syntax:
boolean result = string1.equals(string2);
Where,
- string1 is the first string to compare.
- string2 is the second string to compare with string1.
- result is a boolean value that will be true if both strings have the same sequence of characters, otherwise false.
Example:
Output:
Method 2: Using String.equalsIgnoreCase() Method to Compare Strings in Java
The equalsIgnoreCase() method in Java compares two strings irrespective of their case(either uppercase or lowercase). It returns true if the value of both strings is the same.
Syntax:
boolean result = string1.equalsIgnoreCase(string2);
Example:
Output:
Method 3: Using == Operator to Compare Strings in Java
The == operator in Java is used to compare the references (memory locations) of two strings, not their actual value. It checks whether both references point to the same string object in memory.
Syntax:
boolean result = string1 == string2;
Where:
- string1 – First string to compare.
- string2 – Second string to compare.
- result – Boolean value (true if both references point to the same object, otherwise false).
Example:
Output:
Method 4: Using Objects.equals() Method to Compare Strings in Java
The Objects.equals() method in Java is a utility method from the java.util.Objects class. It is used to compare two strings (or any objects), handling null values safely. It returns true if both objects are equal or both are null.
Syntax:
boolean result = Objects.equals(string1, string2);
Example:
Output:
Method 5: Using String.compareTo() Method to Compare Strings in Java
The compareTo() method in Java is used to compare two strings lexicographically (dictionary order). It compares the ASCII values of characters in both strings.
Syntax:
int result = string1.compareTo(string2);
Return Type: It returns an integer value
- Returns 0: If both strings are equal.
- Negative value: If string1 is lexicographically less than string2.
- Positive value: If string1 is lexicographically greater than string2.
Example:
Output:
Method 6: Using compareToIgnoreCase() Method to Compare Strings in Java
The compareToIgnoreCase() method in Java is used to compare two strings lexicographically, ignoring case differences. It works similarly to compareTo(), but it does not consider cases while comparing characters.
Syntax:
int result = string1.compareToIgnoreCase(string2);
Example:
Output:
Method 7: Using startsWith() and endsWith() Methods to Compare Strings in Java
The startsWith() and endsWith() methods in Java are used to check whether a string starts or ends with a specific sequence of characters. These methods are helpful when you need to match a prefix or suffix in a string.
Syntax of startsWith()
boolean result = string.startsWith(prefix);
Syntax of endsWith()
boolean result = string.endsWith(suffix);
Where:
- string – The string to check.
- prefix – The substring that you want to check at the beginning of the string.
- suffix – The substring that you want to check at the end of the string.
- result – Boolean value (true if the condition is met, otherwise false)
Example:
Output:
Method 8: Using User-Defined Function to Compare Strings in Java
You can create a user-defined function to compare two strings based on your own logic.
Algorithm:
- Step 1: Check if either string is null:
- If string1 == null or string2 == null, return false.
- Step 2: Compare the lengths of both strings:
- If string1.length() != string2.length(), return false.
- Step 3: Iterate through each character of both strings:
- Loop from i = 0 to string1.length() – 1.
- Compare characters at each index i:
- If string1.charAt(i) != string2.charAt(i), return false.
- Step 4: If all characters match, return true.
Example:
Output:
Why Not Use == for String Comparison?
The == operator compares memory locations (references), not the values of strings.
Even if two strings have the same characters, == can return false if they are different objects.
Use .equals() to compare string values instead of ==.
== only works when both strings refer to the same object in the String Pool.
String Comparison With Apache Commons
The Apache Commons Lang library provides the StringUtils class, which provides us with methods for string comparison.
Note: Apache Commons Lang is an external library. You need to add commons-lang3 dependency to your project
Method 1: Using equals() and equalsIgnoreCase() Methods to Compare Strings in Java
These methods compare two strings:
- equals(): Case-sensitive comparison.
- equalsIgnoreCase(): Case-insensitive comparison.
Example:
Output:
false
true
Method 2: Using equalsAny() and equalsAnyIgnoreCase() Methods to Compare Strings in Java
These methods check if a string is equal to any of the given strings:
- equalsAny(): Case-sensitive comparison.
- equalsAnyIgnoreCase(): Case-insensitive comparison.
Example:
Output:
true
true
Method 3: Using compare() and compareIgnoreCase() Methods to Compare Strings in Java
These methods compare two strings lexicographically:
- compare(): Case-sensitive comparison.
- compareIgnoreCase(): Case-insensitive comparison.
Return value:
- 0: if both strings are equal.
- Less than 0: if the first string is lexicographically smaller.
- Greater than 0: if the first string is lexicographically larger.
Example:
Output:
32
0
Choosing the Right Method for String Comparison
Choosing the appropriate method for comparing strings in Java depends on the specific requirements of your comparison.
Method | When to Use |
equals() | When you need an exact match (case-sensitive) between two strings. |
equalsIgnoreCase() | When you need to ignore case differences while comparing strings. |
== Operator | Only when checking if two string references point to the same object (not for content comparison). |
compareTo() | When you need lexicographical comparison (useful for sorting strings). |
compareToIgnoreCase() | When you need a lexicographical comparison, ignoring case. |
Objects.equals() | When you need to handle null values safely while comparing strings. |
startsWith() / endsWith() | When you need to check if a string starts or ends with a specific substring. |
Conclusion
So far in this blog, we have learned how we can compare two strings with the help of different methods. We have also learned which method is used in which scenario.Choosing the right comparison method depends on your use case. Use equals() for exact matches, compareTo() for ordering, and Objects.equals() or StringUtils for null-safe checks. If you want to be an expert in Java Programming language, you may refer to our Java Course.