Learn Groovy - 10 Code Examples & CST Typing Practice Test
Groovy is a powerful, dynamic JVM language that integrates seamlessly with Java, offering concise syntax, scripting capabilities, functional programming patterns, and strong support for automation, DSL creation, and Gradle build scripts. It blends Java familiarity with Python/Ruby-like expressiveness.
Learn GROOVY with Real Code Examples
Updated Nov 19, 2025
Code Sample Descriptions
Groovy Counter and Theme Toggle
def count = 0
def isDark = false
def updateUI = {
println "Counter: $count"
println "Theme: ${isDark ? 'Dark' : 'Light'}"
}
def increment = { count++; updateUI() }
def decrement = { count--; updateUI() }
def reset = { count = 0; updateUI() }
def toggleTheme = { isDark = !isDark; updateUI() }
// Simulate actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()
Demonstrates a simple counter with theme toggling using Groovy variables and console output.
Groovy Simple Calculator
def add = { a, b -> a + b }
def subtract = { a, b -> a - b }
def multiply = { a, b -> a * b }
def divide = { a, b -> a / b }
println "Add 5 + 3: ${add(5,3)}"
println "Subtract 5 - 3: ${subtract(5,3)}"
println "Multiply 5 * 3: ${multiply(5,3)}"
println "Divide 6 / 2: ${divide(6,2)}"
Basic arithmetic operations using closures.
Groovy Factorial
def factorial
factorial = { n ->
if (n <= 1) return 1
else return n * factorial(n-1)
}
println "Factorial of 5: ${factorial(5)}"
Recursive factorial function.
Groovy Fibonacci Sequence
def fibonacci
fibonacci = { n ->
if (n == 0) return 0
if (n == 1) return 1
return fibonacci(n-1) + fibonacci(n-2)
}
(0..9).each { println fibonacci(it) }
Generate Fibonacci numbers recursively.
Groovy List Comprehension
def numbers = 1..10
def squares = numbers.findAll { it % 2 == 0 }.collect { it * it }
println squares
Square even numbers using collection operations.
Groovy Map Filtering
def scores = [Alice:10, Bob:5, Charlie:12]
def highScores = scores.findAll { k,v -> v >= 10 }
println highScores
Filter a map based on values.
Groovy Anonymous Closures
def add = { x, y -> x + y }
println add(3,7)
Using closures as anonymous functions.
Groovy Reduce Example
def numbers = [1,2,3,4,5]
def sum = numbers.inject(0) { acc, val -> acc + val }
println sum
Sum elements of a list using inject (reduce).
Groovy Zip Lists
def list1 = [1,2,3]
def list2 = [4,5,6]
def sums = [list1, list2].transpose().collect { it.sum() }
println sums
Combine two lists element-wise using transpose.
Groovy Tuple Destructuring
def pair = [3,7]
def (x,y) = pair
println x + y
Destructuring a tuple and performing operations.
Frequently Asked Questions about Groovy
What is Groovy?
Groovy is a powerful, dynamic JVM language that integrates seamlessly with Java, offering concise syntax, scripting capabilities, functional programming patterns, and strong support for automation, DSL creation, and Gradle build scripts. It blends Java familiarity with Python/Ruby-like expressiveness.
What are the primary use cases for Groovy?
Gradle build scripts. Jenkins declarative & scripted pipelines. Automation & scripting on the JVM. Rapid backend prototyping. Testing frameworks (Spock). Domain-specific language (DSL) creation
What are the strengths of Groovy?
Concise compared to Java. Perfect for Gradle/Jenkins automation. Dynamic or static typing flexibility. Rich testing ecosystem (Spock). Excellent for DSLs & config-driven systems
What are the limitations of Groovy?
Slower than pure Java due to dynamic typing. Performance overhead. Less mainstream than Java/Kotlin. Smaller ecosystem vs Java. Tooling sometimes less polished
How can I practice Groovy typing speed?
CodeSpeedTest offers 10+ real Groovy code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.