if (boolean == false) vs. if (!boolean)

ateiob picture ateiob · Aug 6, 2012 · Viewed 106.7k times · Source

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?

Answer

Ry- picture Ry- · Aug 6, 2012

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.)