All posts by Karol Depka Prądziński

About Karol Depka Prądziński

Checking a CharSequence for equality – use contentEquals instead of equals, or you will have a bug

In Android, You sometimes get CharSequence as a parameter of a callback.

It might be tempting to check for equality using  “equals” method of a String.

But this would be a bug:


because equals requires the other object to be an instance of String as well, as explained in JavaDoc of String.equals :

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

The correct way would be to use contentEquals: