Help us understand the problem. What is going on with this article?

Groovy超簡単入門

Groovyの紹介

Groovyは2003年開発されたJavaプラットフォームで動作する動的プログラミング言語であり、直接スクリプトで実施することを特徴としています。

Groovyプロジェクト作成

まずは、簡単な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
    }
}

GroovyとJavaの比較

GroovyとJavaを比べると以下の特徴を持っています。

  1. 行末のセミコロンは省略できる。
  2. returnは省略できる。
  3. getter/setterは自動で作られる。
  4. == によるオブジェクトの比較は、自動的に equals による比較となり、null チェックの必要がない。
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 チェック

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

コンパイル成功!

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした