Groovy 3

Groovy Introduction

Groovy is a open source programming language that uses the Java Vitual Machine (JVM) and uses a Java-like syntax. Groovy can be used either dynamically typed (any variable can hold any data type) or statically typed (where the variable needs to specify the data type). Groovy fully supports functional programming which includes first-class functions and currying. Functional programming concepts like immutability and pure function are big advantages to build side-effect-free functions, so it is easier to maintain systems — with some other benefits.

Groovy was created in 2003 and has been an Apache Software Foundation project since 2015, currently its at version 3.0.3 (april 2020).

Groovy is a dynamically typed language meaning that we can use the def keyword instead of the type definition like Integer, Float, etc. This is almost defining all variables of type Object but Groovy does something a little different which I will discuss later.

If you are coming from a Java background then the learning curve will be very easy, if you are new to programming it's still going to be easier than learn something like Java which is a complex programming langauge.

Domain Specific Languages (DSL) is meant to simplify the code written in Groovy in such a way that it becomes easily understandable for the common user more on this later.

Install and Setup

I am not going to cover how to install and setup groovy on you laptop, or server as there are many internet resources that explain this and you can go to the Groovy website from where you download the latest version and the full Groovy documentation.

There are many was to create Groovy code using a plain old text edit to a fully blown IDE like JetBrains intellij.

Groovy Shell and Console

A few other programming languages already have or are introducing (even Java) a commandline or console option that allows you to program quickly and to use to play around with a particular function or feature that you what to jump into and test or simply learn without having to create a whole application. Groovy also has a compiler which allows you to compile the source code into Java Bytecode, so you can even use the java command to run Groovy compiled code.

Groovy has both a shell and a console as seen below

Groovy shell
groovysh
Groovy console
groovyconsole
Groovy compiling
groovyc -h                        // get help
groovyc person.groovy             // create a Java class

Java to Groovy

As I mentioned above that if you have a Java background learning groovy will be a breeze, one point to make is that groovy does not replace Java, its more of a syntax sugar around Java.

Below is a simple conversion from a Java Pojo (or bean) to a Groovy Pojo (or bean)

Java Class
import java.util.Date;

public class User {

    private String firstName;
    private String lastName;
    private String email;

    public User(){}

    public User(String first,String last){
        this.firstName = first;
        this.lastName = last;
    }

    public User(String first,String last,String email) {
        this.firstName = first;
        this.lastName = last;
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void printFullName(){
        System.out.println("FullName: " + firstName + " " + lastName);
    }

    @Override
    public String toString(){
        return "Person[first=" + firstName + ",last=" + lastName + "]";
    }
}
Groovy Class (without comments)
// A Lot less code than Java example above
@groovy.transform.ToString()                             
class User {
                                    
    String firstName                                     
    String lastName
    String email                                      

    void printFullName(){     
        println "FullName: $firstName $lastName"                                                                                     
    }
}

User user = new User(firstName:"Paul", lastName:"Valle")    
println user
Groovy Class (with comments)
// No import statments as by default it imports many packages already
@groovy.transform.ToString()                             // replace toString method with Groovy annotation
class User {

                                                         // No access modifiers are required
    String firstName                                     // semi-colons are not required
    String lastName
    String email 
                                                         // No need for any constructors

    void printFullName(){
                                                         // no need to use System.out as println is a default method
        println "FullName: $firstName $lastName"         // no need for a return statement last line is always returned
                                                         // parentheses are optional
                                                         // you can use String interpolation, no need for + (concatenation)
    }
    
    // No need for any getters and setters

}

// No need to create a main method, 
// it runs like a normal script
User user = new User(firstName:"Paul",lastName:"Valle")  // using named parameters, so no need for any constructors
println user