有这个代码:
if (cs.equals(keywordUnderProcess)) {
} else {
}
cs
和keywordUnderProcess
都是CharSequence
,值为star
,但else 情况下会执行。为什么? equals
检查值,不是吗?
最佳答案
如果您阅读 CharSequence 的 javadoc 页面接口(interface),它表示 equals
的行为未定义:
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.
关于java - CharSequence equals() 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39541233/