Simple Groovy-isms

You learn pretty quick when switching over from Java to Groovy. Problem is that it is fairly backwards compatible, so if you aren’t doing something in the easiest way possible, it may be hard to know. For the basics, I just want to state them so that everybody is at least covering the basics.

Scoping

By default, classes are public in groovy. Don’t add the public keyword. Similarly, member variables are private. The accessors and mutators are generated (as you’ll see in the next section).

Accessors and Mutators

Accessors should be adjusted from

To the following

Similarly, the mutator

should be used as follows

It looks like you are accessing the private method, but it’s not. It’s just shorthand for calling the “get” function. This is important because even if there is no member variable – such as x.getCount(), this should still be called using x.count. That’s one of the fun shortcuts in Groovy.

GStrings vs Strings

You should know the difference. It’s pretty important. For info about these, click here. It covers it in depth pretty well. Basically, just know that single quotes and double quotes are very different. And if you use 3 double quotes or 3 single quotes to start a string, you are doing a multi-line string.

Using this, your strings should be adjusted from the following Java-like syntax

To the following

Print statements

Although I would suggest that you use something like log4j/slf4j/etc rather than printing directly to the console, print statements get easier:

Should be changed to

Collections

There are so many goodies added to the collections GDK that it’s very hard to cover it all. But syntax-wise, there are only a minor couple of changes to make:

For maps, accessing and

should be

Also, the following also works to the same effect

For lists, this

For statements

Basically, don’t use them anymore (there are a couple of valid cases, but they are mostly not necessary within groovy.  The following for statement

can be rewritten as

This uses the range object within groovy.  For more details on the Range Objects in groovy, click here. Also, it then uses the each function on the range object, passing it one parameter, a Groovy Closure. Closure’s are one of the most powerful things in Groovy. It allows you to design very functionally (rather than in a totally object-oriented way). For more information on these, please visit Groovy’s Closure page.

Further Reading

Don’t stop now! There’s so much more to learn! Head over to http://groovykoans.org/ and practice using some simple Groovy testing.