The Groovy programming language is a jvm compatible language that fixes many of Java's shortcomings. Running groovy side by side with your existing Java code is a simple matter of including the groovy jar dependency at runtime even for "enterprise" environments. This is a quick introduction to get your feet wet with this new language. Just enough to feel comfortable with other tools that are based on Groovy, such as the Gradle build system or Grails.

The official documentation on the Groovy website goes into much greater depth. Understanding Java, while not strictly necessary to be productive with Groovy, will help immensely.


I recommend installing Groovy via the Groovy Version Manager. While you're at it, gvm also provides other tools as well such as the next gen build tool, Gradle.

$ curl -s | bash
$ gvm install groovy
$ gvm install gradle

Running Scripts

After installing groovy, you will have access to the following programs:

groovyConsole - brings up a basic editor that can execute groovy scripts
groovysh - in terminal groovy Read Eval Print Loop (repl)
groovy - groovy interpreter, pass it a script to execute
groovyc - groovy compiler, pass it a script to compile into bytecode, the resulting classes depend on groovy-<version>[-indy].jar (e.g. groovy-2.1.2-indy.jar)
    the indy version of the jar is compiled for versions 1.7+ of the jvm, making use of the new invoke dynamic bytecode instructions for greater performance

Quick Rundown

Many Java programs can be run as Groovy scripts without modification. The Groovy language starts mostly compatible with the Java syntax and removes some of the more cumbersome aspects to make a concise language. Like any scripting language, use these syntactic elements to the level of comfort of those you work with and yourself.

println "Hello, World"*

4.times {val -> println val}
4.times {println it} //equivalent, implicit closure variable 'it' is always defined for us inside of a closure

List mylist = [1,1,2,3] // results in an ArrayList containing all elements listed
Set myset = [1,1,2,3] // results in a HashSet containing only [1,2,3]
Map mypet = ["type":"parrot", "name": "polly", "color": "green"]


"The current time is: ${new Date()}"

import groovy.transform.*;

class Foo {
  def value

def ary = [new Foo(value: 8), new Foo(value: 1), new Foo (value: 4), new Foo(value: 2)]
Collections.sort(ary, {lhs, rhs -> lhs.value - rhs.value} as Comparator)
println ary

Breaking Changes

Most of the breaking changes to the Java language come from the dynamic nature of how brackets and braces are now interpreted.
Namely inline array initialization:

int[] ary = new int[] {1,2,3}
is now broken, but replaced by the simpler to understand:

def ary = [1,2,3]