AI Generate Groovy docs instantly

Groovy Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Groovy basics and syntax

Hello World

println "Hello, World!"
// or
System.out.println("Hello, World!")

Semicolons Optional

def x = 10
def y = 20
println x + y  // No semicolons needed

Comments

// Single line comment

/*
 * Multi-line
 * comment
 */

Run Groovy

groovy script.groovy
groovy -e 'println "Hello"'  // One-liner

Variables & Data Types

5 snippets

Dynamic and static typing

Dynamic Typing (def)

def name = "Groovy"  // Type inferred
def count = 42
def price = 19.99
def flag = true

Static Typing

String name = "Groovy"
int count = 42
double price = 19.99
boolean flag = true

GStrings (Interpolation)

def name = "World"
def msg = "Hello, $name!"  // Hello, World!
def calc = "2 + 2 = 4"  // 2 + 2 = 4

Triple Quotes

def multiline = """This is a
multi-line
string"""

Type Checking

println name.getClass()  // class java.lang.String
println count instanceof Integer  // true

Operators

6 snippets

Arithmetic and special operators

Arithmetic

def sum = a + b
def diff = a - b
def prod = a * b
def quot = a / b
def mod = a % b
def power = a ** b  // Exponentiation

Comparison

a == b   // Equal
a != b   // Not equal
a > b
a < b
a >= b
a <= b
a <=> b  // Spaceship (compareTo)

Logical

a && b   // AND
a || b   // OR
!a       // NOT

Safe Navigation

def name = person?.name  // Null-safe access
def upper = str?.toUpperCase()

Elvis Operator

def name = inputName ?: "Default"
def value = nullableValue ?: 0

Spread Operator

def list = [1, 2, 3]
def expanded = [0, *list, 4]  // [0, 1, 2, 3, 4]

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Control Flow

4 snippets

Conditional statements

If Statement

if (age >= 18) {
    println "Adult"
} else {
    println "Minor"
}

Groovy Truth

if (str) { }        // Non-empty string is true
if (list) { }       // Non-empty collection is true
if (0) { }          // 0 is false!
if (null) { }       // null is false

Ternary

def result = (age >= 18) ? "Adult" : "Minor"

Switch

switch (value) {
    case 1:
        println "One"
        break
    case 2..5:
        println "Between 2 and 5"
        break
    case [6, 7]:
        println "Six or Seven"
        break
    default:
        println "Other"
}

Loops

6 snippets

Iteration constructs

For Loop (Range)

for (i in 1..10) {
    println i
}

For Loop (List)

def fruits = ["Apple", "Banana", "Orange"]
for (fruit in fruits) {
    println fruit
}

Each (Closure)

(1..10).each { i ->
    println i
}

While Loop

def i = 0
while (i < 10) {
    println i
    i++
}

Times

5.times {
    println "Hello"
}

5.times { i ->
    println "Iteration $i"
}

Break and Continue

for (i in 1..100) {
    if (i == 50) break
    if (i % 2 == 0) continue
    println i
}

Closures

5 snippets

Anonymous functions and blocks

Basic Closure

def greet = { name ->
    println "Hello, $name"
}
greet("Alice")

Implicit Parameter

def square = { it * it }
println square(5)  // 25

Multiple Parameters

def add = { a, b ->
    a + b
}
println add(5, 3)  // 8

Closure as Argument

def repeat(n, closure) {
    n.times { closure() }
}

repeat(3) {
    println "Hello"
}

Collecting Results

def doubled = [1, 2, 3].collect { it * 2 }
println doubled  // [2, 4, 6]

Lists, Maps & Ranges

5 snippets

Groovy collection types

List

def list = [1, 2, 3, 4, 5]
list << 6  // Append
list[0] = 10  // Update
println list.size()

List Operations

list.each { println it }
def evens = list.findAll { it % 2 == 0 }
def doubled = list.collect { it * 2 }
def sum = list.sum()

Map

def person = [name: "John", age: 30]
println person.name  // "John"
println person['age']  // 30
person.city = "NYC"  // Add new key

Map Iteration

person.each { key, value ->
    println "$key: $value"
}

Range

def range = 1..10  // Inclusive
def exclusive = 1..<10  // Exclusive
println 5 in range  // true

Classes & OOP

4 snippets

Object-oriented features

Class Definition

class Person {
    String name
    int age

    def greet() {
        println "Hello, I'm $name"
    }
}

Create Instance

def person = new Person(name: "John", age: 30)
person.greet()

Constructor

class Person {
    String name
    int age

    Person(String name, int age) {
        this.name = name
        this.age = age
    }
}

Traits

trait Flyable {
    void fly() {
        println "Flying"
    }
}

class Bird implements Flyable {
    String name
}

More Cheat Sheets

FAQ

Frequently asked questions

What is a Groovy cheat sheet?

A Groovy cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Groovy. It helps developers quickly look up syntax without searching through documentation.

How do I learn Groovy quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Groovy concepts?

Key Groovy concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Groovy code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Groovy code using AI.

Code Conversion Tools

Convert Groovy to Other Languages

Easily translate your Groovy code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Groovy Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week