Grails Guide

Tutorials And Examples on Grails

Groovy Tutorial - Creating Classes and Scripts

Most developers are attracted to Groovy for it's powerful support of object oriented programming and pure scripting. A Groovy source file can either have class definitions or just plain script. Both will be explained in this tutorial

Notes

Groovy is a superset of Java. It means it supports all you can do in Java, and it adds other cool features on top of that. This also implies that any valid Java code is also a valid Groovy code.
Java source code are stored with the .java file extension. Groovy on the other hand uses .groovy file extension. But once compiled, both are translated into bytecode that can be run on a Java Virtual Machine.

Groovy Class

Just like in Java, Groovy supports classes. It is the essential feature of object-oriented programming because they define the behavior of objects. Here is an example Groovy class:
class Person {
    private String name;

public String getName() { return name; }

public void setName(String name) { this.name = name; } }

Note that the above code is also a valid Java code.
Access Modifier
By default, access to class methods and properties are public unless specified otherwise. Hence this code:
class Person {
    public String name;
    public String getGreetings() {
        return "Hello " + name;
    }
}
is the same as:
class Person {
    String name;
    String getGreetings() {
        return "Hello " + name;
    }
}
Default Imports
Some commonly used Java and Groovy packages are automatically imported to source files for convenience. There is no need to declare explicitly. These are the packages:
  • java.lang
  • java.io
  • java.math
  • java.net
  • java.util
  • groovy.lang
  • groovy.util

You can use any class that belongs to these packages without importing them. For example:

class MyUtil {
    public static int getNumberOfTokens(String str) {
        StringTokenizer st = new StringTokenizer(str);
        return st.countTokens();
    }
}

Note that StringTokenizer is under java.util package and was not explicitly imported.
Weak Typing
Groovy is not a strong typed language. For example, this is valid in Groovy.
def x = new Integer(10);
x = "John";
Note that you can use the identifier "def" to denote dynamic typing and make Groovy less strict with values assigned to the variable.
String
Groovy has a concept called GString. It is a more powerful version of Java Strings. For example:
def name = "John Doe"
def age = 25
def conclusion = "${name} is {age} years old"
The "conclusion" string will have the value of "John Doe is 25 years old"
Concise Data Structure
Data structures are usually very tedious in Java. Groovy offers cool syntax to manage lists and maps.

For lists, instead of having to code this:

List<Integer> mylist = new ArrayList<Integer>()	
mylist.add( new Integer(100) );
mylist.add( new Integer(200) );
mylist.add( new Integer(300) );
mylist.add( new Integer(400) );
mylist.add( new Integer(500) );

You can just have this code in Groovy

def mylist = [100, 200, 300, 400, 500]

For maps, instead of having to code this:
List<String, String> mymap = new ArrayList<String, String>()	
mymap.put("color", "Red");
mymap.put("shape", "Circle");
mymap.put("location", "Middle");
You can just have this code in Groovy
def mymap = [color:"Red", shape:"Circle", location:"Middle", ]

Groovy Scripts

For convenience, groovy source can be written without any interface or class declarations. The code can contain instructions to be executed right away. For example, this code:
class MyTest {
    public static void main(String[] args) {
        int a = 5
        int b = 10
        println a*b
    }
}
is the same as having this code (class and main method declaration are removed);
int a = 5
int b = 10
println a*b
This is very useful for writing code intended for plain scripting purposes only.