Hit enter after type your search item

How to compare Java strings?

For string comparison in Java, you may use the equals() and compareTo() methods. There are some differences between the two methods – return values and others as you compare objects/strings.

Both of these methods are explained below with examples.

Java equals() method

In the context of string comparison, the equals method indicates if this string is equal to the other string. The use of equals() method is broad and basically, it checks if this object is equal to the specified object. The object can be a string or other type.

This is how equals() method can be used:

public boolean equals(Object obj)

  • The equals method returns true if given string is equal to this string.
  • If the argument is null, it returns false e.g. str.equals(null).
  • If other string object does not match the same sequence of characters of this object, it returns false.

See the following section for examples with different possibilities as using the equal Java method.

An example of equals() method

For this example, three string objects are created and assigned values. All strings apparently are the “same”, however, in the string1 and string2, the only difference is the case letters for two words. String1 and string3 are exactly the same.

The equals method is used for string comparison and its returned results are displayed as follows:


java equals method

The first comparison returned false and the second as true, because, “String” and “string” are two different words.

The compareTo method

Generally speaking, the Java compareTo method compares this object (string in our case) with the specified object for order. The syntax of using the compareTo() method is:

int compareTo(object_to_compare)

  • The method returns an integer, unlike a Boolean in the case of equals() method.
  • If this object is less than the specified object then it returns a negative number.
  • If it is equal to the specified object then it returns zero.
  • The compareTop() returns positive integer if this object is greater than the specified object.
  • The equals() tells the equality of two strings whereas the compareTo() method tell how strings are compared lexicographically.

So in our context of the strings, we may conclude as follows:

For string comparison of str1 and str2:

  • str1 == str2 returns 0
  • str1 < str 2 returns negative integer
  • str1 > str 2 returns positive integer

An example of string comparison by compareTo() method

For this example, three string objects are created and compared as follows:


compareTo method

What if the specified object is null?

If you try to compare a string that is null the compareTo method throws a NullPointerException error. See the following example:

The output:

compareTo NullPointer Exception

In case of the equals() method, if the compared string is null, it simply returns false:

The output:

java string compare


This div height required for enabling the sticky sidebar