General Java

Compare Two Strings in Java5 min read

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:



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:


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.



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.



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



Using compareToIgnoreCase() in Java

This is the same method that the previous one. The only difference is that this method ignores the case.




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:


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.







Leave a Comment