Getting Started
4 snippetsGroovy 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 neededComments
// Single line comment
/*
* Multi-line
* comment
*/Run Groovy
groovy script.groovy
groovy -e 'println "Hello"' // One-linerVariables & Data Types
5 snippetsDynamic and static typing
Dynamic Typing (def)
def name = "Groovy" // Type inferred
def count = 42
def price = 19.99
def flag = trueStatic Typing
String name = "Groovy"
int count = 42
double price = 19.99
boolean flag = trueGStrings (Interpolation)
def name = "World"
def msg = "Hello, $name!" // Hello, World!
def calc = "2 + 2 = 4" // 2 + 2 = 4Triple Quotes
def multiline = """This is a
multi-line
string"""Type Checking
println name.getClass() // class java.lang.String
println count instanceof Integer // trueOperators
6 snippetsArithmetic 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 // ExponentiationComparison
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 // NOTSafe Navigation
def name = person?.name // Null-safe access
def upper = str?.toUpperCase()Elvis Operator
def name = inputName ?: "Default"
def value = nullableValue ?: 0Spread 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.
Control Flow
4 snippetsConditional 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 falseTernary
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 snippetsIteration 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 snippetsAnonymous functions and blocks
Basic Closure
def greet = { name ->
println "Hello, $name"
}
greet("Alice")Implicit Parameter
def square = { it * it }
println square(5) // 25Multiple Parameters
def add = { a, b ->
a + b
}
println add(5, 3) // 8Closure 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 snippetsGroovy 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 keyMap Iteration
person.each { key, value ->
println "$key: $value"
}Range
def range = 1..10 // Inclusive
def exclusive = 1..<10 // Exclusive
println 5 in range // trueClasses & OOP
4 snippetsObject-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
}