Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CharSequence equals() does not work well

Have this code:

if (cs.equals(keywordUnderProcess)) {

} else {

}

Both cs and keywordUnderProcess is CharSequence, has the value star, but the else case will execute. Why? equals check the value, isn't it?

enter image description here

enter image description here

like image 782
János Avatar asked Sep 16 '16 23:09

János


1 Answers

If you read the javadoc page for the CharSequence interface, it says that the behavior for equals is undefined:

This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other. It is therefore inappropriate to use arbitrary CharSequence instances as elements in a set or as keys in a map.

like image 72
Matthew Diana Avatar answered Sep 30 '22 07:09

Matthew Diana