Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

The Adventurous Developer’s Guide to JVM languages – Groovy

Groovy is not as adventurous as the languages already covered in the series — Xtend, Kotlin and Ceylon — but definitely is a JVM language you should be interested in. It’s become a mature choice that developers trust when Java verbosity hurts and dynamic typing isn’t an issue.


In any case, I’m not one to argue when given an opportunity to play around with programming languages. I’ve implemented a simple HTTP server in Groovy, available at GitHub, to serve as a comparison point throughout the series. Please do use this opportunity to educate me on idiomatic Groovy through comments and pull requests.

Java supercharged

Java developers can just dive in and be productive in Groovy. Syntax matches Java where possible and this seems to be the trend in the future, as 2.0 release added Java 7 Project Coin enhancements. In addition Groovy smooths Java annoyances encountered on a daily basis. Safe navigation (?.) and Elvis (?:) operators are great examples.

//streetName will be null if user or user.address is null - no NPE thrown
def streetName = user?.address?.street    

//traditional ternary operator usage
def displayName = ? : "Anonymous"

// more compact Elvis operator - does same as above
def displayName = ?: "Anonymous"  


I expected grooviness to stop with syntactic enhancements, but then I spotted “Closures” in the documentation. Why it is called as such is beyond me as functions are first-class citizens in Groovy — function values, higher-order functions and lambda expressions are all supported.

square = { it * it }  // ‘it’ refers to value passed to the function
[ 1, 2, 3, 4 ].collect(square)  // [1, 4, 9, 16]

Excellent use of closures in the standard library makes it a pleasure to use and demonstrates their power. Here’s a good example with syntactic sugar for closures as last method parameter:

def list = ['a','b','c','d']
def newList = []

list.collect( newList ) {
println newList  // [A, B, C, D]


Almost every application relies on collections. Unfortunately collections largely represent the pain of Java. And if you doubt me, please try to have some fun with JSON manipulation. Groovy packs native syntax for collection definitions and makes heavy use of closures for powerful manipulation.

def names = ["Ted", "Fred", "Jed", "Ned"]
println names  //[Ted, Fred, Jed, Ned]

def shortNames = names.findAll { it.size() <= 3 }
println shortNames.size()  // 3 
shortNames.each { println it }  // Ted
                                // Jed
                                // Ned

Static typing

People often get excited about dynamic languages, since for less code you appear to get more functionality. It is often less understood, that surplus gets taken back in maintenance. So we can see more and more dynamic languages getting static typing, and vice versa.

Groovy is no exception, static checks can be enabled by annotation @TypeChecked in the relevant code.

import groovy.transform.TypeChecked

void someMethod() {}

void test() {
    // compilation error:
    // cannot find matching method sommeeMethod()

    def name = "Marion"

    // compilation error:
    // the variable naaammme is undeclared
    println naaammme


As a Java developer, Groovy is easy to get into, and a great choice for web apps, DSLs, templates and script engines. It approaches annoyances common in Java, packs a modern set of features and has a great standard library. Groovy will definitely continue to be widely adopted and a great choice for a dynamic language on JVM. I only fear the loss of focus, as the featureset continues to grow.

Psst! If you liked this post, we wrote a 50-page RebelLabs report on Java 8, Scala, Groovy, Fantom, Clojure, Ceylon, Kotlin & Xtend.

[publication id=”48″ label=”Get the FULL REPORT on all 8 JVM languages”]