What is Groovy all about?

Groovy is a dynamic programming language that runs on the JVM. This means it is compiled to Java bytecode and then executed on the Java Virtual Machine. You can read more about its features and download Groovy from the official website.

After unpacking or installing Groovy, you can use the Groovy console (located at $GROOVY_HOME/bin/groovyConsole) for writing and experimenting with Groovy scripts.

Groovy aims to provide greater productivity compared to Java (and other mainstream object-oriented languages), and it excels at this.

A simple “hello world” in Groovy (no main method or anything required):

println "Hello programmers!"

Basic Differences

Almost all Java code is valid Groovy code, allowing for seamless mixing of Java and Groovy within your projects.

Silent Helpers

Groovy offers several features that enhance productivity by removing unnecessary syntax from Java equivalents and replacing it with smart defaults.

Properties (fields)

Groovy provides field access notation for Java bean properties (or fields). It automatically generates getters and setters for private fields behind the scenes. To leverage this, simply leave the default visibility on your fields. For example:

Date date = new Date()
// You can access all Java bean properties
// using their field name.
println date.time
// prints some long number...

class MyClass {

    // Default visibility for fields is private,
    // but Groovy generates setters and getters
    // behind the scenes.
    String firstName
    String lastName

    // Compiles without problem.
    public static void access() {
        MyClass myClass = new MyClass()
        myClass.firstName = "John"
    }
}

Constructors

In Groovy, you can instantiate beans using a list of named arguments. This powerful feature allows you to effectively treat maps as objects that can be passed around. For example:

// Here we create a new SimpleDateFormat and set default
// values to some of its properties.
SimpleDateFormat format =
    new SimpleDateFormat(lenient: false, numberFormat: "ddMMyyyy")

Added Operations

Groovy introduces several new operators not found in Java:

Basic Types in Groovy

In Groovy, you have access to all primitive Java types, along with some Groovy-specific types. Additionally, you can benefit from Groovy’s syntactic sugar when working with basic types.

Maps, Lists, and Ranges

Groovy integrates list and map concepts more naturally into its type system.

Regular Expressions

Groovy provides first-class support for regular expressions at the language level, introducing new literals, operators for matching, and a concise syntax for extracting groups. While regular expressions are a complex topic on their own and beyond the full scope of this introductory article, it’s important to note that Groovy uses forward slashes (not backslashes, like Java) for defining regular expressions. It also introduces two new operators for matching:

Example:

println "999999" ==~ /\d+/
// true

println "mercury9" ==~ /\d+/
// false

println "mercury" ==~ /\w+/
// true

What is Groovy missing

Groovy is not missing much, but some features not present (or different) from Java include: