Home - pford68/groovy-examples GitHub Wiki

Enough Groovy to be dangerous

Contents

Basics

  • Very similar to Java, but less verbose.
  • Groovy is a superset of Java, which means a Java program will run fine in Groovy environment, but vice-versa may or may not run, depending upon whether itโ€™s written in groovy or Java.
  • Runs on the JVM, and Groovy scripts can use Java classes.
  • Files have .groovy extensions
  • Semicolons are optional at the end of lines.

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

The def keyword

It means that datatype or return type of a property/function/method is unspecified, and that Groovy will attempt to infer the type.

  • It does declare a standalone function per se. It is a return type.
  • If you use a visibility modifier, the presence of def is unnecessary.

Functions

  • Can be invoked without parentheses around the parameter list.
  • Can take default parameters:
    def say(msg = 'Hello', name = 'world') {
      "$msg $name!"
    }
    
    // We can invoke 3 signatures:
    // say(), say(msg), say(msg, name)
    assert 'Hello world!' == say()
    // Right most parameter with default value is eliminated first.
    assert 'Hi world!' == say('Hi')
    assert 'Howdy, mrhaki!' == say('Howdy,', 'mrhaki')
    
  • The return statement is optional. If it is omitted, the last expression in the function body is returned.

Operators

For more on operators, see Operators

Equality

Object Equality

The == operator works with object equality as well. Groovy overloads the == operator and maps it to the equals() method.1

  • This is very different from Java, so when developers are switching back and forth between Groovy and Java mistakes are bound to happen. In Java we use the == operator to see if variables are referring to the same object instance.
  • In Groovy we use the == operator to see if two objects are the same, in Java we would use the equals() method for this.
  • To test if two variables are referring to the same object instance in Groovy we use the is() method.
  • The != operator is also overloaded and maps to the !equals() statement.
Integer myInt = 42
Integer anotherInt = myInt
Integer newInt = 42
Integer different = 101
 
assert myInt == anotherInt  // In Java: myInt != null && myInt.equals(anotherInt)
assert myInt.is(anotherInt)  // In Java: myInt == anotherInt
 
assert myInt == newInt
 
assert myInt != different

Truth

TBD. See http://groovy-lang.org/semantics.html#Groovy-Truth

Visibility

  • Everything has public visibility by default.
    • This means that if you omit the visibility modifier for a class/method, the visibility is public.
  • To assign package (default) visibility to a class or method, you must annotate it with @PackageScope.

References

Notes

  1. http://mrhaki.blogspot.com/2009/09/groovy-goodness-check-for-object.html