Skip to content
Philip Ford edited this page Jan 12, 2017 · 30 revisions

Enough Groovy to be dangerous

Default Imports

The following packages are automatically imported by Groovy, so you don't have to import them:

import java.lang.*
import java.util.*
import java.io.*
import java.net.*
import groovy.lang.*
import groovy.util.*
import java.math.BigInteger
import java.math.BigDecimal

Operators

The Elvis Operator

A more concise ternary operator: ?:

def sampleText
 
// Normal ternary operator.
def ternaryOutput = (sampleText != null) ? sampleText : 'Hello Groovy!'
 
// The Elvis operator in action. We must read: 'If sampleText is not null assign
// sampleText to elvisOuput, otherwise assign 'Viva Las Vegas!' to elvisOutput.
def elvisOutput = sampleText ?: 'Viva Las Vegas!'

Clone this wiki locally