java - jdk 1.8 String.equalsIgnoreCase 中的重复空检查
问题描述
String.equalsIgnoreCase 实现如下(Oracle JavaSE 1.8)
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
我想知道是否(anotherString != null)
有必要进行检查,因为this != anotherString
已经表明它anotherString
不为空。
解决方案
让我们假设你是对的,看看我们打电话时会发生什么equalsIgnoreCase(null)
:
this == anotherString
是假的;anotherString.value.length == value.length
- 我们正在获得一个 NPEanotherString.value
。
因此,anotherString != null
这里是必要和关键的。
this != anotherString
已经表明anotherString
不是null
。
不,它没有。它只能说明是否this
和anotherString
不相等。
例如,两者this != null
和this != "test"
return true
。
推荐阅读
- c - 写入文件。创建文件但不写入文件
- python - 使用资源模块导入数据文件
- android - 将颤振项目发布到 Play 商店后,Firebase Auth 登录不起作用
- sql - 在分层查询中确定 (root), (root+1), (root+2)
- flowtype - 为反应组件包装子组件,该组件的子组件仅限于某些类型
- gnuplot - gnuplot pm3d 将第二列映射为颜色渐变
- html - 如何在html中访问上一个目录
- scala - scala.util.Success 和 akka.actor.Status.Success 之间的主要区别?
- reactjs - ID 不能表示非字符串和非整数值
- python - 我应该如何让我的不和谐机器人欢迎某人?