[java] if (boolean == false) vs. if (!boolean)

Possible Duplicate:
Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?

In this NotePadProvider sample code, I noticed that the author chose the form:

    if (values.containsKey(NoteColumns.CREATED_DATE) == false) {
        values.put(NoteColumns.CREATED_DATE, now);
    }

Over:

    if (!values.containsKey(NoteColumns.CREATED_DATE)) {
        values.put(NoteColumns.CREATED_DATE, now);
    }

Is there any advantage in the first form over the more logical one?

This question is related to java boolean boolean-logic boolean-expression

The answer is


The first form, when used with an API that returns Boolean and compared against Boolean.FALSE, will never throw a NullPointerException.

The second form, when used with the java.util.Map interface, also, will never throw a NullPointerException because it returns a boolean and not a Boolean.

If you aren't concerned about consistent coding idioms, then you can pick the one you like, and in this concrete case it really doesn't matter. If you do care about consistent coding, then consider what you want to do when you check a Boolean that may be NULL.


No. I don't see any advantage. Second one is more straitforward.

btw: Second style is found in every corners of JDK source.


Mostly READABILITY. When reading others code, it is much more intuitive to read as NOT CONTAINS KEY !values.containsKey(NoteColumns.CREATED_DATE) instead of reading CONTAINS KEY IS FALSE (values.containsKey(NoteColumns.CREATED_DATE) == false).


- Here its more about the coding style than being the functionality....

- The 1st option is very clear, but then the 2nd one is quite elegant... no offense, its just my view..


Note: With ConcurrentMap you can use the more efficient

values.putIfAbsent(NoteColumns.CREATED_DATE, now);

I prefer the less verbose solution and avoid methods like IsTrue or IsFalse or their like.


This is a style choice. It does not impact the performance of the code in the least, it just makes it more verbose for the reader.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to boolean

Convert string to boolean in C# In c, in bool, true == 1 and false == 0? Syntax for an If statement using a boolean Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() Ruby: How to convert a string to boolean Casting int to bool in C/C++ Radio Buttons ng-checked with ng-model How to compare Boolean? Convert True/False value read from file to boolean Logical operators for boolean indexing in Pandas

Examples related to boolean-logic

pandas: multiple conditions while indexing data frame - unexpected behavior How can I obtain the element-wise logical NOT of a pandas Series? How to test multiple variables against a value? Any good boolean expression simplifiers out there? if (boolean == false) vs. if (!boolean) How do I test if a variable does not equal either of two values? Differences in boolean operators: & vs && and | vs || Check if at least two out of three booleans are true How to convert "0" and "1" to false and true Does Python support short-circuiting?

Examples related to boolean-expression

'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays? Any good boolean expression simplifiers out there? if (boolean == false) vs. if (!boolean) How to make "if not true condition"? How can I express that two values are not equal to eachother? Python `if x is not None` or `if not x is None`? How to use OR condition in a JavaScript IF statement? Is this the proper way to do boolean test in SQL?