Groovy super easy introduction

Introducing Groovy

Groovy is a dynamic programming language that runs on the Java platform developed in 2003 and features direct scripting.

Groovy project creation

First, create a simple Java Bean.


public class Cat{

    /**The name of the cat*/
    private String name;

    /**The age of the cat*/
    private int age;

    /**Constuct*/
    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**Get the cat name*/
    String getName() {
        return name
    }

    /**Set the cat name*/
    void setName(String name) {
        this.name = name
    }

    /**Get the cat age*/
    int getAge() {
        return age
    }

    /**Get the cat age*/
    void setAge(int age) {
        this.age = age
    }
}

Comparison of Groovy and Java

Comparing Groovy and Java, it has the following features.

  1. The semicolon at the end of the line can be omitted.
  2. return can be omitted.
  3. Getter / setter is created automatically.
  4. The comparison of objects by == is automatically compared by ʻequals`, and there is no need for null check.

public class Cat {
    private String name  /**The name of the cat*/
    private int age /**The age of the cat*/
    /**Constuct*/
    public Cat(String name, int age) {
        this.name = name
        this.age = age
    }
}

Cat cat = new Cat("wow", 1);
print cat.name;

NullPointerException check

public class Cat {
    private String name  /**The name of the cat*/
    private int age /**The age of the cat*/
    /**Constuct*/
    public Cat(String name, int age) {
        this.name = name
        this.age = age
    }
}

Cat cat = new Cat("wow", 1);
print cat.name;
Cat cat1 = null
print cat == cat1

Compile successful!

スクリーンショット 2020-02-09 16.22.21.png

Recommended Posts

Groovy super easy introduction
Ractor super introduction
[For super beginners] DBUnit super introduction
[For super beginners] Ant super introduction
[Super easy] Ruby environment construction
Easy library introduction with Maven!
[For super beginners] Maven super introduction
[For super beginners] Mirage SQL super introduction
[Super Introduction] About Symbols in Ruby
Jekyll super introduction starting from 0 # 1 --Environment construction
Super easy deployment of applications using Waypoint