Skip to content

Groovy

Basics

Item Description
println('Hello, World!') print to console
/* comment */ single-line or multi-line comments
def variableName = value define a variable
return value return a value from a method
"String interpolation ${variableName}" interpolate variables in a string

Data Types

Item Description
String a sequence of characters
int an integer
float a floating-point number
boolean true or false
List an ordered collection of objects
Map a collection of key-value pairs

Control Flow

Item Description
if (condition) { ... } conditional statement
if (condition) { ... } else { ... } conditional statement with an else block
for (item in list) { ... } iterate over a list
while (condition) { ... } loop while a condition is true
switch (value) { case caseValue: ... } conditional branching based on the value of a variable

Functions

Item Description
def methodName() { ... } define a method
def methodName(parameter) { ... } define a method with a parameter
def methodName(parameter1, parameter2) { ... } define a method with multiple parameters
methodName() or methodName(parameter) call a method

Classes and Objects

Item Description
class ClassName { ... } define a class
new ClassName() create a new instance of a class
def variable = new ClassName() create a new instance of a class and assign it to a variable
variable.propertyName access a property of an object
variable.methodName() call a method on an object

Advanced

Item Description
Closures a code block that can be assigned to a variable or passed as an argument to a method
Metaprogramming changing the behavior of a program at runtime by modifying classes and objects
Builders a domain-specific language for creating complex structures like XML or HTML