Skip to end of metadata
Go to start of metadata

Boolean expressions

Groovy supports the standard conditional operators on boolean expressions, e.g.:

In addition, Groovy has special rules for coercing non-boolean objects to a boolean value.

Collections

Empty collections are coerced to false.

Iterators and Enumerations

Iterators and Enumerations with no further elements are coerced to false.

Maps

Non-empty maps are coerced to true.

Matchers

Matching regex patterns are coerced to true.

Strings

Non-empty Strings, GStrings and CharSequences are coerced to true.

Numbers

Non-zero numbers are coerced to true.

Object references

Non-null object references are coerced to true.

  • No labels

2 Comments

  1. Null values

    null is not coerced to false

    i.e.:

  2. That is not null coercion, that is null equality testing. These work fine:

    You need to remember that coercion here is implicit. The implicit-ness comes from not testing or converting to a boolean manually (i.e. no null.toBoolean, null.asBoolean, (boolean) null, new Boolean(null), etc).