我从数据库返回的字符串总是等于一。
此字符串可能等于“0”,“1”,“Y”,“N”或null,这实际上是数据库中由于日志目的而不允许替换的约束。
考虑到值“Y”或“N”不区分大小写,那么性能更高?
public boolean isTrue {
return this.str != null && (this.str.equals("1") || this.str.equalsIgnoreCase("S"));
}
或
public boolean isTrue {
if (this.str != null) {
final char character = this.str.charAt(0);
return character == '1' || character == 'S' || character == 's';
}
return false;
}
PS:请不要考虑方法,字段和变量的名称。
答案 0 :(得分:5)
要查看"01YN"
中的信件,我会做
public boolean isTrue {
return this.str != null && ("01YN".indexOf(this.str)>=0);
}
我认为这是一种清晰易读的方式。此外,您可以为Constant
声明"01YN"
并执行,而不是在此处进行硬编码。
答案 1 :(得分:2)
这可能值得一试:
switch(str == null ? 'x' : str.charAt(0)) {
case '1', 'y', 'Y':
return true;
}
return false;