Groovy is a dynamic programming language that runs on the Java platform developed in 2003 and features direct scripting.
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
}
}
Comparing Groovy and Java, it has the following features.
==
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!
Recommended Posts