java - CharSequence equals() 不能正常工作

标签 java string character

有这个代码:

if (cs.equals(keywordUnderProcess)) {

} else {

}

cskeywordUnderProcess 都是CharSequence,值为star,但else 情况下会执行。为什么? equals 检查值,不是吗?

enter image description here

enter image description here

最佳答案

如果您阅读 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/

相关文章:

java - SonarQube 支持 Java9 吗?

C++ 带字符串参数的 Switch 大小写

java - 我如何执行以下转换?

SQL:尝试避免使用某些元素的名称

java - Android解压功能不起作用

java - 如何用spring MVC处理页面维护

java - jdbc 如何确保 2 行在一起是唯一的

c# - 修剪字符串数组时减少代码膨胀

swift - 如何编写函数来评估 String 是否只是字符?

Java计算字符串字符个数的方法