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 26 Next »

Groovy uses a similar syntax to Java although in Groovy semicolons are optional.
This saves a little typing but also makes code look much cleaner (surprisingly so for such a minor change). So normally if one statement is on each line you can ommit semicolons altogether - though its no problem to use them if you want to. If you want to put multiple statements on a line use a semicolon to separate the statements.

If the end of the line is reached and the current statement is not yet complete it can be spanned across multiple lines. So for things like method parameters or creating lists or for complex if expressions you can span multiple lines.

Multiple assignments

The previous code snippets showed some examples of assignments, but Groovy also supports multiple assignments. 

In Groovy 1.6, there is only one syntax addition for being able to define and assign several variables at once:

A more meaninful example may be methods returning longitute and latitude coordinates. If these coordinates are represented as a list of two elements, you can easily get back to each element as follows:

And you can also define the types of the variables in one shot as follows:

For the assignment (with prior definition of the variables), just omit the def keyword:

If the list on the right-hand side contains more elements than the number of variables on the left-hand side, only the first elements will be assigned in order into the variables. Also, when there are less elements than variables, the extra variables will be assigned null.

So for the case with more variables than list elements, here, c will be null:

Whereas in the case where there are more list elements than variables, we'll get the following expectations:

For the curious minds, supporting multiple assignments also means we can do the standard school swap case in one line:


The characters "//" begin a comment that last for the rest of the line.

The characters "/*" begin a comment that lasts until the first "*/".

The character "#" is not a comment character.

Method calls

Method calling syntax is similar to Java where methods can be called on an object (using dot) or a method on the current class can be called. Static and instance methods are supported.

Notice that the return statement is optional at the end of methods. Also you don't need to specify a return type (it will default to Object in the bytecode if none is specified).

Optional parenthesis

Method calls in Groovy can omit the parenthesis if there is at least one parameter and there is no ambiguity.

It is also possible to omit parenthesis when using named arguments. This makes for nicer DSLs:

Named parameter passing

When calling a method you can pass in named parameters. Parameter names and values are separated by a colon (like the Map syntax) though the parameter names are identifiers rather than Strings.

Currently this kind of method passing is only implemented for calling methods which take a Map or for constructing JavaBeans.

Passing closures into methods

Closures are described in more detail

Error formatting macro: link: java.lang.IllegalArgumentException: Link needs a name and a URL as arguments.

Closures can be passed into methods like any other object

Though there is some syntax sugar to make calling methods which take a closure easier. Instead of specifying parenthesis, you can just specify a closure. e.g.

The above code is equivalent to the previous code, just a little more groovy. If a method takes parameters you can leave the closure outside of the parenthesis (provided that the closure parameter is the last parameter on the underlying method).

The above code is equivalent to the following (but just neater)

Important Note

Note that when using the neater syntax for specifying closures either without parenthesis or by specifying the closure after the parenthesis, the closure must start on the same line. i.e. the { symbol must be on the same line as the method call statement. Otherwise the parser interprets the { as a start of a block.

For a in depth description of what kind of method signatures Groovy supports see the Extended Guide to Method Signatures

Dynamic method dispatch

Groovy always uses dynamic dispatch, even if a variable is constrained by a type. The type of the variable only ensures that the variable is at least of that type and avoids you assigning a String to for example int. Dynamic method dispatch is often referred to as dynamic typing whereas Java uses static typing by default.


These are described in more detail in the Groovy Beans section.
To access properties you use dot with the property name. e.g.

The above uses a special bean called Expando which allows properties to be added dynamically at runtime.

An Expando is a Map which behaves as a dynamic bean: adding new key/value pairs add the equivalent getter and setter methods, as if they were defined in a real bean.

Safe navigation

If you are walking a complex object graph and don't want to have NullPointerExceptions thrown you can use the ?. operator rather than . to perform your navigation.

  • No labels