Dashboard > Groovy > ... > User Guide > Operators
Operators Log In | Sign Up   View a printable version of the current page.

Added by Tom Nichols , last edited by Matthew Lachman on Jan 03, 2008  (view change) show comment
Labels: 
(None)

Operators

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.

Arithmetic and Conditional Operators

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

In addition, Groovy supports the ! (not) operator as follows:

def expression = false
assert !expression

For more details about how expressions are corced to a boolean value, see: Groovy Truth.

Collection-based Operators

Spread Operator (*.)

The Spread Operator is used to invoke an action on all items of an aggregate object. It is equivalent to calling the collect method like so:

parent*.action                             //equivalent to:
parent.collect{ child -> child?.action }

The spread operator may be used a method call or property access, and returns a list of the items returned from each child call. So you may effectively override the spread operator by implementing a custom collect method.

Object-Related Operators

  • invokeMethod and get/setProperty (.)
  • Method Reference (.&)
  • 'as' - "manual coercion" - asType(t) method
  • Groovy == ( equals() ) behavior.
    • "is" for identity
  • The instanceof operator (as in Java)

Other Operators

Elvis Operator (?:)

The "Elvis operator" is a shortening of Java's ternary operator. One instance of where this is handy is for returning a 'sensible default' value if an expression resolves to false or null. A simple example might look like this:

def gender = user.male ? "male" : "female"  //traditional ternary operator usage

def displayName = user.name ?: "Anonymous"  //more compact Elvis operator

Safe Navigation Operator (?.)

The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return null instead of throwing an exception, like so:

def user = User.find( "admin" )           //this might be null if 'admin' does not exist
def streetName = user?.address?.street    //streetName will be null if user or user.address is null - no NPE thrown

Regular Expression Operators

  • find (=~)
  • match (==~)

For more details, see: Regular Expressions

There's regex find, but also regex match with ==~
And regarding the range, yes, you can even define your own ranges of non-numeric types with RangeObject, as long as your object implements Comparable or something like that (this is explained pretty well in GinA)
When speaking about the spread operator, we should mention it can be similar in most situations to collect{}, and that we can use it to spread arguments like that foo."$methName"(*listOfArgs). It's also possible to spread the content of a list inside another list [1,2,3,*listOfInts], and even in a map with [a:1, b:2, *:someMap]

Site running on a free Atlassian Confluence Open Source Project License granted to The Codehaus. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators