Getting your Grails on: Groovy Basics


This is an excerpt from my upcoming book Getting your Grails on, based on my presentation from Kansas City Developers Conference.

In this first chapter, we are going to discuss some of the basics of the Groovy language and what makes it so powerful. I think you will enjoy this book and learn something too at the same time. Don’t just read along make sure you try out some of code in the demo sections as well.

Dynamic Language

Groovy is an optionally typed dynamic open source language for the JVM. Dynamic languages bring capabilities that allow application developers to be more productive. It provides many features of advanced languages such as closures and dynamic methods.

Groovy can be compiled or interpreted. Dynamic languages can extend a program at runtime to change types or behaviors. It brings the power of functional style programming and metaprogramming capabilities to the Java ecosystem.

Super Set

Groovy is a superset of Java, just add the groovy jar to Java and you are ready to go! It compiles to Java bytecode and extends Java API and libraries. It is open-sourced under the Apache License 2.0 version. You can seamlessly integrate Groovy with your other Java applications, it complements those applications well.

Inspired by others

Groovy takes inspiration from many other languages including Python, Ruby, and Smalltalk to name a few. It has a syntax that is very similar to Java so developers should be able to make the switch easily. This makes it a low barrier to entry for experienced Java developers.

Scripting language

Groovy can be used as a scripting language for the JVM. It is great for writing concise tests and works great for automating tasks and building your applications. Groovy’s main strength as a scripting language is accessing the enormous library of Java code that’s out there directly and conveniently. If that’s not your need, Groovy still provides a scripting environment about as rich as other languages such as Perl, Python or Ruby.

Domain Specific Languages

Groovy provides flexible syntax and customization mechanisms to integrate readable business rules in your applications. Domain Specific Languages make modeling your business logic much easier.

Groovy supports Java

Groovy supports many things Java has as well, like packages, imports, exception handling, control structures, classes, and methods. Groovy adds many additional default imports java.lang,, java.util, groovy.lang,, BigInteger, BigDecimal. This reduces the clutter of the numerous imports that you find in most Java programs.

Groovy Syntax

To get a better understanding of the syntax difference between Java and Groovy let’s take a look here and see a simple example.  The first example is of a hello world in Java.

public class HelloWorld { // JAVA
  public static void main( String[] args )
     System.out.println(“Hello World!”);

Here is an example of Groovy

class HelloWorld {  // Groovy
  static main( args ){
     println ‘Hello World!’

Groovy supports dynamic typing, like JavaScript and Perl do. So we can drop type information on the main() declaration–even the return type. The default visibility of methods and fields is public, so we can drop that too. The println which can be seen as a shortcut for System.out.println.

The semicolon at the end-of-line is optional too, it follows the same rules as other languages like JavaScript.

class HelloWorld {
  def greet( name ){
     “Hello $name!”
def hm = new HelloWorld()
println hm.greet(“Groovy”)

The return type of the method greet is not of a specific type, so we use the reserved keyword def which can be seen as the var keyword in JavaScript. It is not required to write the return keyword. The value of the last executed sentence will be used as the return value automatically.

Look at how there is no type on the parameter passed to the method as there would be in Java. The string “Hello !” is not a simple java.lang.String. It is, in fact, one of the sexiest features: a GString. These type of strings allow variable interpolation and arbitrary expression executing much like in Perl.

Comments in Groovy are similar to Java and many other languages. The two types are illustrated here with a single line and multiple lines.

//  Single line comments start with two forward slashes
Multi-line comments look like this.

Creating an empty collection in groovy is easy along with adding elements to it as well.

def technologies = []


And a simple loop in Groovy looks like this.


           println “$it – size: ${it.size()}”


A Groovy Closure is like a “code block” or a method pointer. It is a piece of code that is defined and then executed at a later point. Closures are reusable blocks of code.

def clos = { println “Hello World!” }

Groovy Tools

There a few Groovy tools that you can use to help create, execute and document your Groovy code.
groovyc – Is used to compile Groovy source code to JVM class files

groovysh – can execute Groovy code interactively

groovy – Will execute Groovy scripts in your environment

groovyconsole – GUI for interactive Groovy code execution

groovydoc – Is similar to Javadoc to create documentation for your application

That should just whet your appetite for the next part of Groovy. In Groovy Demo, we will dive deeper into the code and get you setup to get your hands dirty. This language is powerful and you should test it out and kick the tires.

No comments yet.

Leave a Reply

Powered by WordPress. Designed by Woo Themes