Mik*_* S. 2 java conditional netbeans
Netbeans给我The conditional statement is redundant警告:
return (this.getId(person) == null) ? false : true;
Run Code Online (Sandbox Code Playgroud)
如果我遵守这一点,它就改为:
return (this.getId(person) != null);
Run Code Online (Sandbox Code Playgroud)
这两个是平等的吗?第二个如何有条件?
是的,它们在概念上都是一样的.
第二个不是有条件的.它的表达式解析为boolean,所以只需要你可以得到那个表达式.
为什么在第一种情况下它是多余的,就像写作一样
if(true){
return true;
}else{
return false;
}
Run Code Online (Sandbox Code Playgroud)
看看你的情况
this.getId(person) != null;
Run Code Online (Sandbox Code Playgroud)
你this.getId(person) 可能是null 也可能不是.所以表达式解析为true或false.而已.对 ?所以你的IDE告诉你使用那个表达式结果.