1. Basic Syntax & Types Variables: Use def or explicit type. def x = 10 (dynamically typed) String name = "Groovy" (statically typed) Strings: Single, double, triple quotes. 'hello' (plain string) "hello ${name}" (GString, interpolation) '''multi-line string''' Numbers: int , long , float , double , BigDecimal . def i = 10 ( Integer ) def d = 10.5 ( BigDecimal by default for decimals) Boolean: true , false Implicit Semicolons: Optional at end of line. 2. Collections 2.1 Lists Declaration: def list = [1, 2, 3] Access: list[0] , list.get(0) Range: def range = 1..5 Methods: add() , remove() , each{} , collect{} , findAll{} . Example: list.each { print it } 2.2 Maps Declaration: def map = [a:1, b:2] or def map = ['a':1, 'b':2] Access: map.a , map['a'] Methods: each{} , eachWithIndex{} Example: map.each { key, value -> println "$key=$value" } 3. Closures Definition: Anonymous block of code that can be passed around. def greet = { name -> println "Hello, $name!" } greet("Groovy") Implicit it : For single parameter closures. def square = { it * it } println square(5) // 25 Delegate: Can change the object a closure operates on. Currying: Partially apply arguments to a closure. 4. Control Structures If/Else: Standard syntax. if (x > 0) { /* ... */ } else { /* ... */ } For Loop: for (i in 1..5) { /* ... */ } for (item in list) { /* ... */ } While Loop: Standard syntax. while (condition) { /* ... */ } Switch: Supports types, ranges, and collections. switch (x) { case 1: break case 2..5: break case [6,7]: break default: break } 5. Object-Oriented Groovy Classes: class Person { String name } Properties: Getters/setters automatically generated. class Person { String name def age // Property // Constructor Person(name, age) { this.name = name this.age = age } } def p = new Person("Alice", 30) println p.name // Access property p.age = 31 // Set property Methods: def greet() { "Hello, $name!" } Traits: Horizontal reuse of behavior. trait Flyable { def fly() { "Flying!" } } class Bird implements Flyable {} new Bird().fly() 6. Operators Elvis Operator: ?: (shorthand for ternary operator with truthy check) def name = null def display = name ?: "Guest" // display is "Guest" Safe Navigation Operator: ?. (avoids NullPointerExceptions) def person = null println person?.name // Prints null, no error Spread Operator: *. (applies a method to all items in a collection) def names = ["Alice", "Bob"] def lengths = names*.length() // [5, 3] Identity Operator: === (checks if two objects are the same instance) Comparison: == (calls equals() ), != , , , > , >= Power: ** (e.g., 2**3 == 8 ) 7. Metaprogramming Dynamic Methods: Add methods at runtime. String.metaClass.reverseAndUpper = { delegate.reverse().toUpperCase() } println "hello".reverseAndUpper() // OLLEH Missing Method/Property: Handle calls to non-existent methods/properties. class MyClass { def methodMissing(String name, args) { "Method '$name' called with $args" } } new MyClass().foo(1, 2) // Method 'foo' called with [1, 2] 8. Builders & DSLs Groovy's natural syntax makes it great for building Domain Specific Languages (DSLs). MarkupBuilder: For XML/HTML generation. def writer = new StringWriter() def html = new groovy.xml.MarkupBuilder(writer) html.html { head { title "My Page" } body { p "Hello" } } println writer.toString() JsonBuilder: For JSON generation. def builder = new groovy.json.JsonBuilder() builder.person { name "Alice" age 30 } println builder.toString() 9. File I/O Read File: def file = new File("test.txt") file.eachLine { line -> println line } def text = file.text Write File: file.write "Hello Groovy!" file.append "More text" URL Content: def url = new URL("http://example.com") println url.text 10. Common GDK Enhancements Collections: each , collect , findAll , groupBy , sort , unique . def numbers = [1, 2, 3, 4, 5] def evens = numbers.findAll { it % 2 == 0 } // [2, 4] def squares = numbers.collect { it * it } // [1, 4, 9, 16, 25] Strings: reverse() , capitalize() , toInteger() , isNumber() , minus() , plus() . "hello".capitalize() // Hello "123".toInteger() + 1 // 124 Numbers: upto() , downto() , times() . 5.times { println "Repeat" } 11. Scripting & CLI Groovy scripts can be executed directly: groovy myscript.groovy Args: Access command-line arguments via args array. Shebang: Use #!/usr/bin/env groovy for executable scripts.