// syntax.groovy — the Groovy shortcuts that make code shorter than Java. // def or a type: both fine def quantity = 24 int lines = 3 String customer = 'Acme' // Semicolons and parentheses are optional println customer println(customer.toUpperCase()) // Everything is an object, even numbers println 10.class.simpleName // Integer 3.times { print 'hip ' }; println 'hooray' // methods on numbers // Decimal numbers are BigDecimal by default: exact money maths println 0.1 + 0.2 // 0.3 (Java double would print 0.30000000000000004) println((0.1 + 0.2).class.simpleName) // BigDecimal // Optional return: the last expression is returned def lineTotal(qty, price) { qty * price } println lineTotal(quantity, 8.50) // 204.00 // Named and default parameters def greet(String name, String greeting = 'Hello') { "$greeting, $name" } println greet('Globex') println greet('Initech', 'Welcome') // == compares values (like Java's equals) def a = 'boomi' def b = new String('boomi') println a == b // true println a.is(b) // false — identity, like Java's == // Multiple assignment def (first, second) = ['PO-1', 'PO-2'] println "$first and $second" // Assert: power assertions show what went wrong assert lineTotal(2, 5) == 10 println "lines=$lines"