# Groovy 关系运算符

2018-12-29 14:00 更新

 运算符 描述 例子 == 测试两个对象之间的等同性 2 == 2将得到true != 测试两个对象之间的差异 3！= 2将得到true < 检查左对象是否小于正确的操作数。 2 <  3将得到true <= 检查左对象是否小于或等于右操作数。 2 <  3将得到true > 检查左对象是否大于右操作数。 3 > 2将得到true >= 检查左对象是否大于或等于右操作数。 3 = 2将得到true

class Example {
static void main(String[] args) {
def x = 5;
def y = 10;
def z = 8;

if(x == y) {
println("x is equal to y");
} else
println("x is not equal to y");

if(z != y) {
println("z is not equal to y");
} else
println("z is equal to y");

if(z != y) {
println("z is not equal to y");
} else
println("z is equal to y");

if(z<y) {
println("z is less than y");
} else
println("z is greater than y");

if(x<=y) {
println("x is less than y");
} else
println("x is greater than y");

if(x>y) {
println("x is greater than y");
} else
println("x is less than y");

if(x>=y) {
println("x is greater or equal to y");
} else
println("x is less than y");
}
}

x is not equal to y
z is not equal to y
z is not equal to y
z is less than y
x is less than y
x is less than y
x is less than y

App下载