Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »


In general all operators supported in Java are identical in Groovy. Groovy goes a step further by allowing you to customize behavior of operators on Groovy types.

TODO make this page a child of the User Guide, and make Operator Overloading a child of this page.

Arithmetic Operators

See Operator Overloading for a list of the common arithmetic operators that Groovy supports.

Conditional Operators

TODO the ! (not) operator; link to Groovy Truth documentation

Elvis Operator (?:)

The "Elvis operator" is a shortening of the ternary operator which is useful for returning a 'sensible default' value if an expression resolves to false or null. A conversion of the example of the ternary operator example below would look like this:

Which translates to "if name is not null (or false) use that value, otherwise use 'Anonymous'."

Safe Navigation Operator (?.)

The Safe Navigation operator is used to avoid NullPointerException}}s. In the traditional case where you have a reference to an object you must verify that it is not null before accessing methods or properties of the object to avoid the {{NPE. To avoid this, the safe navigation operator will simply return null instead of throwing an exception, like so:

In the above example, if either user or user.address are null, streetName will be assigned null.

Ternary Operator (? :)

See the section on

Error rendering macro 'link' : Link needs a name and a URL as arguments.
in the Java Tutorial. A common example:

Which means "if user.male is true, gender='male', otherwise gender='female'."

Collection-based Operators

  • Spread Operator (*.)
  • Range Operator (..)
  • Membership Operator (in)

Object- Related Operators

Regular Expression Operators

  • find (=~)
  • match (==~)
  • No labels