AnatomyOf

Anatomy of a Groovy file

example.groovy
1def GREETING = 'Hello' // dynamic typing: the variable that keeps its options open
2
3class Robot {
4 String name
5 def battery = 100
6
7 String status() {
8 "${name} is at ${battery}% -- still more reliable than my WiFi"
9 }
10}
11
12def beep = { String label -> println "${label}: beep boop" } // a closure, Groovy's anonymous function
13
14def units = [new Robot(name: 'R2'), new Robot(name: 'C3')]
15for (unit in units) {
16 println unit.status()
17 beep(unit.name)
18}
Groovy is an agile, dynamic language for the Java Platform with features like closures, optional typing, and scripting capabilities.