What you'll learn

  • def vs explicit types
  • Optional semicolons, parentheses and return
  • Default and named parameters
  • BigDecimal by default, and why it matters for money
  • == vs is(), multiple assignment and power asserts

Groovy’s syntax is Java’s with the noise removed. This lesson covers the differences you’ll meet in every script.

Types are optional

def quantity = 24          // Groovy works out the type at runtime
int lines = 3              // or declare it, Java-style
String customer = 'Acme'

Use def in scripts and for local values; use explicit types in classes and method signatures where they document intent. (@CompileStatic can make Groovy fully type-checked when you want Java-like safety.)

Less punctuation

println customer               // no parentheses, no semicolon
println(customer.toUpperCase())

Parentheses can be left off for top-level method calls with arguments. Keep them when it helps readability, and always for calls without arguments: list.size().

Implicit return

def lineTotal(qty, price) { qty * price }   // the last expression is returned

Default and named parameters

def greet(String name, String greeting = 'Hello') { "$greeting, $name" }
greet('Globex')                 // Hello, Globex
greet('Initech', 'Welcome')     // Welcome, Initech

Named arguments arrive as a map — very common with constructors: new Customer(name: 'Acme', email: 'ap@acme.test') (see Classes and traits).

Exact decimals by default

println 0.1 + 0.2                 // 0.3
println((0.1 + 0.2).class)        // class java.math.BigDecimal

In Java, 0.1 + 0.2 with doubles is 0.30000000000000004. Groovy decimal literals are BigDecimal, so money calculations are exact out of the box.

Everything is an object

println 10.class.simpleName      // Integer
3.times { print 'hip ' }; println 'hooray'

== compares values

def a = 'boomi'
def b = new String('boomi')
a == b       // true  — Groovy's == calls equals()
a.is(b)      // false — identity, like Java's ==

This removes a whole class of Java bugs.

Multiple assignment

def (first, second) = ['PO-1', 'PO-2']

Power asserts

assert lineTotal(2, 5) == 11

When an assert fails, Groovy prints every value in the expression, so you can see why — wonderful for quick checks and tests.

The full example

// 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"

Try it yourself

Write a method fullName(first, last, middle = '') that returns "First Middle Last" without a double space when there’s no middle name.

Show a solution
def fullName(first, last, middle = '') {
    [first, middle, last].findAll { it }.join(' ')
}
assert fullName('Ada', 'Lovelace') == 'Ada Lovelace'
assert fullName('John', 'Doe', 'Q') == 'John Q Doe'

findAll { it } drops empty strings thanks to Groovy truth.

Key takeaways

  • def or explicit types; semicolons and many parentheses are optional.
  • The last expression is returned; parameters can have defaults.
  • Decimal literals are exact BigDecimals.
  • == compares values; .is() compares identity.

Example files