Chapter 8. Groovy: Java’s dynamic friend

 

This chapter covers

  • Why you should learn Groovy
  • Basic Groovy syntax
  • Differences between Groovy and Java
  • Powerful Groovy features not found in Java
  • How Groovy is also a scripting language
  • Groovy interoperability with Java

Groovy is an OO, dynamically typed language that, like Java, runs on the JVM. In fact, it can be seen as a language that provides dynamic capabilities to complement the static world of Java. The Groovy project was initially founded by James Strachan and Bob McWhirter in late 2003, with its leadership changed to Guillaume Laforge in 2004. The community based at http://groovy.codehaus.org/ continues to thrive and grow today. Groovy is seen as the most popular language on the JVM after Java itself.

Inspired by Smalltalk, Ruby, and Python, Groovy has implemented several language features that Java doesn’t have, such as:

  • Function literals
  • First-class[1] support for collections

    1 By first-class, we mean that support is built into the language syntax as opposed to requiring library calls.

  • First-class support for regular expressions
  • First-class support for XML processing
Note

In Groovy, function literals are called closures. As explained in chapter 7, they’re functions that can be put into variables, passed to methods, and manipulated in the same ways as any other value.

8.1. Getting started with Groovy

8.2. Groovy 101—syntax and semantics

8.3. Differences from Java—traps for new players

8.4. Groovy features not (yet) in Java

8.5. Interoperating between Groovy and Java

8.6. Summary