Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is Groovy?

Learn what Groovy is, where it came from, how it relates to Java, what it is used for (Gradle, Jenkins, scripting, testing) and how Groovy code runs on the JVM.

Groovy is a programming language for the Java Virtual Machine (JVM). It looks like a relaxed version of Java: fewer semicolons, fewer type declarations, built-in syntax for lists and maps, and a powerful feature called closures. Yet it compiles to the same bytecode as Java, uses the same libraries, and can call or be called by any Java code.

This lesson explains what Groovy is, why people use it and where you will meet it in real projects. The rest of the course builds on this picture, so take a few minutes to get the big ideas straight.

How this course runs examples. Groovy itself cannot run on this site. Groovy examples are shown as read-only code blocks, and we only print an output when we are certain what it is. Where an idea can be demonstrated in a language the site can run, you will find a runnable Java stand-in, clearly labelled. It shows the same logic with Java syntax so you can experiment.

A short history

Groovy was created by James Strachan in 2003, inspired by Python, Ruby and Smalltalk, but with a deliberate goal: stay close to Java so that Java developers feel at home. It later became an Apache Software Foundation project (Apache Groovy). The current line is Groovy 5, which runs on modern Java versions.

The important design decision is compatibility with Java. Almost any valid Java code is also valid Groovy, with a few exceptions. You can start by writing Java-style Groovy and gradually adopt the shorter Groovy idioms.

What makes Groovy different

Compare a tiny task, printing the squares of the numbers 1 to 5, in Groovy:

GROOVY
(1..5).each { n -> println n * n }

The Java version needs a class, a main method, a loop and type declarations. The Groovy line has a range (1..5), a closure (the code in braces) and the each method that is added by Groovy to every collection. You will learn each of these ideas in the following chapters.

Key characteristics:

  • Optional typing. You can write def x = 10 and let Groovy decide the type at run time, or declare int x = 10 and keep the safety of types. You can even add static type checking with an annotation.
  • Concise syntax. Semicolons, parentheses in many calls, and return are often optional. Getters and setters are generated for properties.
  • First-class collections. Lists, maps and ranges have literal syntax: [1, 2, 3], [name: 'Ada'], 1..10.
  • Closures. Blocks of code that you can store in variables, pass to methods and return from methods.
  • Dynamic behavior. Methods can be added or intercepted at run time (metaprogramming), which powers builders and domain specific languages.
  • A rich library (the GDK). The Groovy Development Kit adds many helper methods to Java classes such as String, List, File and Date.

How Groovy relates to Java

Groovy source files end in .groovy. When you run one, the Groovy compiler turns it into JVM bytecode (a .class file, in memory or on disk). From then on it is ordinary JVM code.

flowchart LR
    A["Script.groovy"] --> B["Groovy compiler"]
    B --> C["JVM bytecode"]
    D["Java library (.jar)"] --> E["Java Virtual Machine"]
    C --> E
    E --> F["Output"]

Groovy and Java meet at the bytecode level, so they can use each other's classes freely.

Because of this, a Groovy script can use java.util.ArrayList, java.time.LocalDate or any library such as Apache Commons or Jackson, with no wrapper code. A Java program can also call Groovy classes, and many projects mix both languages.

Where Groovy is used

You will meet Groovy in more places than you might expect:

Area What Groovy does there
Build automation Gradle build files (build.gradle) are Groovy scripts (Gradle also supports Kotlin)
CI/CD Jenkins pipelines (Jenkinsfile) use a Groovy-based syntax
Scripting Quick automation, file processing, text and data munging
Testing The Spock framework for expressive unit and behavior tests
Web Grails and Micronaut/Ratpack support for web applications
Embedding Applications that let users write scripts or rules in Groovy

Gradle and Jenkins alone make Groovy valuable to learn: millions of developers read Groovy every week without having studied it.

Static versus dynamic: a first look

Java checks types when you compile. Groovy by default checks them when the code runs, which allows flexible code but can move some errors from compile time to run time. A tiny Java stand-in shows the difference in spirit: Java needs a type for every value, while a Groovy def variable can hold anything.

Java Java stand-in: a value that can hold different types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Groovy does the same thing for you automatically: a def variable is an Object reference, and method calls on it are resolved when the code runs. This is called dynamic dispatch, and the Java stand-in below imitates it with a tiny lookup table.

Java Java stand-in: dynamic method dispatch by name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

A taste of real Groovy

Here is a small, complete Groovy script. Read it as a preview; you will understand every line by the end of the next chapters.

GROOVY
def languages = ['Groovy', 'Java', 'Kotlin', 'Scala']

languages
    .findAll { it.length() > 4 }
    .collect { it.toUpperCase() }
    .each { println it }

It filters the names longer than four letters, converts them to upper case and prints each one. There is no class, no main, no imports (the common packages like java.util are imported for you) and no loop variable.

Strengths and trade-offs

Groovy shines when you want to write less code, build a readable DSL, script around Java libraries, or test thoroughly. The trade-offs are worth knowing:

  • Dynamic code is a little slower than Java unless you opt into static compilation (@CompileStatic).
  • Fewer errors are caught at compile time unless you use type checking.
  • Tooling and job market are smaller than Java's, though Gradle and Jenkins keep the language relevant.

None of these are deal breakers. You choose the strictness: Groovy lets you start loose and tighten later.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Groovy compiler produce?

    Groovy compiles to JVM bytecode, the same format Java produces. That is why Groovy and Java can use each other's classes.
  2. 2. Which of these tools uses Groovy for its configuration or scripts?

    Gradle build scripts and Jenkins pipelines are commonly written in Groovy, which makes the language useful even if you never build an application with it.
  3. 3. What does def x = 10 mean in Groovy?

    def declares a variable with no explicit type (internally an Object reference). You can still write int x = 10 when you want a fixed type.
  4. 4. Which statement about Groovy and Java is true?

    Groovy is designed for seamless Java interoperability: libraries work in both directions and most Java syntax is accepted.

Summary

  • Groovy is a concise, flexible JVM language that compiles to Java bytecode.
  • It supports optional typing, closures, literal lists and maps, and a rich library of helper methods.
  • Gradle, Jenkins, Spock and Grails are the most common places you meet it.
  • Dynamic features make code short but move some checks to run time; static options exist when you need them.
  • In this course, Groovy blocks are read-only and Java stand-ins are runnable.

Next: Install and Run Groovy