NumericToBooleanCoercion
DRAFT

Misconception:

An expression of a numeric type can be coerced to type boolean, where 0 means false and any other value means true. This allows conditions like int i; ... if (i) ....

Incorrect

Numeric types can be coerced to boolean

Correct

Numeric types cannot be coerced to boolean

Correction
Here is what's right.

Java strictly separates numeric and boolean types. There is no implicit coercion, and there are not even explicit casts.

int i = 0;
boolean b;

b = i; // illegal (no coercion)
b = (boolean)i; // illegal cast

As Section 4.2.5 of the Java Language Specification describes, to convert a number to a boolean, one can use comparison operators:

if (i != 0) {
  ...
}

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.