Groovy Basics

What Is Groovy?

  • It is a scripting language that runs on the JVM.
  • Its syntax is very similar to Java.
  • It is compatible with Java, and Java class files can be used as Groovy classes as they are.
  • It was first released in 2003 and has been managed by the Apache Software Foundation since 2015.

Using Groovy

  • Gradle scripts are written in Groovy or Kotlin, and Java projects mainly use Groovy.
  • It is also used for writing test code.

Hello World

class HelloWorld {
   static void main(String[] args) {
      println 'Hello World'
   }
}

Execution result:

Hello World

Optional syntax

Adding libraries (Import Statement)

The following objects are automatically imported, so they can be omitted.

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

Semicolons

In Groovy, semicolons are not required and can be omitted.

println 'hello';
println 'hello'

Methods (Parentheses)

In Groovy, parentheses, (), can be omitted when calling methods.

println('hello')
println 'hello'

Return Type and the return Keyword

In Groovy, you do not need to specify a return type, and you do not need to use the return keyword on the last line of a method.

def getPi() {
    3.14
}

assert getPi() in BigDecimal
assert getPi() == 3.14

Getters and Setters

In Groovy, getters and setters do not need to be explicitly defined.

class Person {
    String firstName
    String lastName
 
    def getName() {
        firstName + ' ' + lastName
    }
 
    static void main(args) {
        def person = new Person()
        person.firstName = 'Kwan Young'
        person.lastName = 'Shin'
        assert person.firstName == 'Kwan Young'
        assert person.lastName == 'Shin'
        println person.getName()
    }
}

Access Modifiers

In Java, if an access modifier is omitted, access is limited to the same package. In Groovy, if it is omitted, it is treated as public.

Checked Exceptions

In Java, checked exceptions such as IOException had to be wrapped in a try/catch statement, but in Groovy checked exceptions are wrapped as RunTimeException, so you do not need to apply a try/catch statement.

Source: https://springsource.tistory.com/85 [Rednics Blog]

Data Type Inference

Groovy can specify data types when declaring variables, but it can also infer, or omit, data types by using the def keyword.

String message = 'hello'
def message = 'hello'

API

String

In Groovy, strings can be written with single quotes, ', or double quotes, ".

  • Single quotes, '

    • Used to output a simple string.
  • Double quotes, "

    • Used to output strings, and dynamic content can be displayed inside the string by using $ variables.
  • Multi-line strings

    • To use multiple lines, use three single quotes to open and close, ''' ''', or three double quotes to open and close, """ """.

Branching

def age = 19
def person = (age < 19)? '성인': '청소년'
println("$person입니다.")

References