Operators
Most are listed here: Operator Overloading
TODO make this page a child of the User Guide
TODO the ! (not) operator; link to Groovy Truth documentation
Conditional Operators
Elvis Operator (?:)
The "Elvis operator" is a shortening of the ) 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 conditional operators 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 (..)
Object- Related Operators
- invokeMethod and get/setProperty (.)
- Method Reference (.&)
- 'as' - "manual coercion" -
asType(t)method - Groovy == (
equals()) behavior.- "is" for identity
Regular Expression Operators
- find (=~)
- match (==~)