如果(布尔==假)与IF(!布尔) [英] if (boolean == false) vs. if (!boolean)
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/2661110/is-it-bad-to-explicitly-compare-against-boolean-constants-e-g-if-b-false-i\">Is它坏明确比较的布尔常量例如如果(二==假)在Java中?
在<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/2.3.4_r1/com/example/android/notepad/NotePadProvider.java#NotePadProvider.insert%28android.net.Uri,android.content.ContentValues%29\">this NotePadProvider样本code ,我注意到,笔者选择的形式:
In this NotePadProvider sample code, I noticed that the the author chose the form:
if (values.containsKey(NoteColumns.CREATED_DATE) == false) {
values.put(NoteColumns.CREATED_DATE, now);
}
在
if (!values.containsKey(NoteColumns.CREATED_DATE)) {
values.put(NoteColumns.CREATED_DATE, now);
}
有第一形式的一个优点在多个逻辑一个?
Is there an advantage of the first form over the more logical one?
推荐答案
除了可读性,没有。他们是功能上等同。
Apart from "readability", no. They're functionally equivalent.
<子>(可读性是加引号,因为我讨厌 ==虚假
并找到!
更可读,但别人不一样。)
("Readability" is in quotes because I hate == false
and find !
much more readable. But others don't.)
这篇关于如果(布尔==假)与IF(!布尔)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!