Checking the "boolean" result of an "int" type
Why not use the boolean
type ? That will work as you expect without the potentially problematic integer/boolean conflation.
private boolean isValid;
...
if (!isValid) {
...
}
Note that this is the idiomatic Java approach. 3rd party libs use this, and consumers of your API will use and expect it too. I would expect libs that you use to give you booleans
, and as such it's just you treating ints
as booleans
.
Try BooleanUtils from Apache common-lang.
BooleanUtils.toBoolean(0) = Boolean.FALSE
BooleanUtils.toBoolean(1) = Boolean.TRUE
BooleanUtils.toBoolean(2) = Boolean.TRUE
In Java,
if ( i != 0 )
is the idiomatic way to check whether the integer i
differs from zero
.
If i
is used as a flag, it should be of type boolean
and not of type int
.