In this post we will learn how to compare two strings in java.
String is a sequence of characters. It is a data type which is commonly used in Java, thus the comparison of strings is one of the mostly used Java operations. However, very often the developers cannot cope with this operation. If you’re trying to deal with that problem, you’re at the right place! In this article we are going to present different ways to compare strings in Java.
String Comparison with String Class in Java
This method suggests five different ways to compare strings in Java. We are going to take into consideration each of them.
Using “==” Comparison Operator in Java
Attention: The “==” operator only compares references, not values. So this is an incorrect way to compare text values. Let’s see an example:
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class JavaExample { public static void main(String args[]){ String string1 = "using comparison operator"; String string2 = "using comparison operator"; String string3 = new String("using comparison operator"); if(string1 == string2) System.out.println("string1 equels to string2"); else System.out.println("string1 does not equels to string2"); if(string1 == string3) System.out.println("string1 equels to string3"); else System.out.println("string1 does not equels to string3"); } } |
Output:
1 2 3 4 | string1 equels to string2 string1 does not equels to string3 |
As you can see, the two variables point to the same String literal, that’s why the first assertion is true, but the second assertion is false because string1 is created with a literal and string3 is created with the new operator, which means that they reference different objects.
Using Java String Equals method
This method compares two strings based on their content. It’s a comparison by character, which ignores their address. If the two strings are of the same length and the characters are in the same order, it considers them equal and returns true. It returns false if the characters don’t match.
String class suggests two methods
- public boolean equals(Object another) compares this string to the specified object.
- public boolean equalsIgnoreCase(String another) compares this string to another string, ignoring case.
Let’s now see some examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class JavaExample { public static void main(String args[]){ String string1 = "comparing strings"; String string2 = "comparing strings"; String string3 = "comparing STRINGS"; String string4 = new String("comparing strings"); System.out.println(string1.equals(string2)); System.out.println(string1.equals(string4)); System.out.println(string1.equals(null)); System.out.println(string1.equals(string3)); } } |
Output:
1 2 3 4 5 6 | true true false false |
In this example, string1, string2, and string4 variables are equal because they have the same case and value irrespective of their address.
For string3 the method returns false, as it’s case sensitive.
Also, if any of the two strings is null, then the method returns false.
Now let’s have a look at another example to make sure you got it.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class JavaExample { public static void main(String args[]){ String style = new String("Bold"); String style2 = new String("Bold"); if(style.equals(style2)) System.out.println("Equal"); else System.out.println("Not Equal"); } } |
Output:
1 2 3 | Equal |
Using equalsIgnoreCase() in Java / Java String Compare Ignore Case
As mentioned above, there also exists another method, which returns a boolean value. This method ignores the case of the characters while comparing Strings.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 | public class JavaExample { public static void main(String args[]){ String string1 = "using equals ignore case"; String string2 = "USING EQUALS IGNORE CASE"; System.out.println(string1.equalsIgnoreCase(string2)); } } |
Output:
1 2 3 | true |
Using compareTo() in Java
This method compares the characters of two strings lexicographically according to a dictionary or the natural ordering. It returns an integer value that describes if the first string is less than, equal to or greater than the second string.
Let’s imagine string1 and string2 are two different variables. There are three possible scenarios:
- string1 == string2 :0
- string1 > string2 :positive value
- string1 < string2 :negative value
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class JavaExample { public static void main(String args[]){ String author = "author"; String book = "book"; String duplicateBook = "book"; System.out.println(author.compareTo(book)); System.out.println(book.compareTo(author)); System.out.println(duplicateBook.compareTo(book)); } } |
Output:
1 2 3 4 5 | -1 1 0 |
Using compareToIgnoreCase() in Java
This is the same method that the previous one. The only difference is that this method ignores the case.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class JavaExample { public static void main(String args[]){ String author = "Author"; String book = "book"; String duplicateBook = "BOOK"; System.out.println(author.compareToIgnoreCase(book)); System.out.println(book.compareToIgnoreCase(author)); System.out.println(duplicateBook.compareToIgnoreCase(book)); } } |
Output:
1 2 3 4 5 | -1 1 0 |
String Comparison with Objects Class in Java
An utility class Objects contains an equals() method. It can equally be useful to compare two strings.
This method firstly compares the two strings according to their address, and if they are the same, it returns true. If both arguments are null, it returns true, but if one argument is null, it returns false. It’s a case sensitive method because it internally calls the equals() method of the String class.
Let’s check it out with an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Objects; public class JavaExample { public static void main(String args[]){ String string1 = "using objects equals"; String string2 = "using objects equals"; String string3 = new String("using objects equals"); System.out.println(Objects.equals(string1, string2)); System.out.println(Objects.equals(string1, string3)); System.out.println(Objects.equals(null, null)); System.out.println(Objects.equals(null, string1)); } } |
Output:
1 2 3 4 5 6 | true true true false |
String Comparison with Apache Commons in Java
The Apache Commons library contains a utility class for string-related operations, which is called String utils and provides useful methods for string comparison. Let’s check them out.
Using equals() and equalsIgnoreCase() in Java
We have already presented the equals() method of the String class and found out that it doesn’t handle null values. On the contrary, the equals() method of the StringUtils class also accepts null values. So, we can say that this is the upgraded version of the one belonging to the String class.
Example
1 2 3 4 5 6 7 8 9 10 11 | import com.sun.xml.internal.ws.util.StringUtils; public class JavaExample { public static void main(String args[]){ System.out.println(StringUtils.capitalize("equals method").equals("EQUALS METHOD")); System.out.println(StringUtils.decapitalize("equals method").equals("equals method")); } } |
Output:
1 2 3 4 | false true |
Source: w3docs.com/snippets/java/how-to-compare-strings-in-java.html