一、关系运算符
开篇第一句:关系运算符允许对象的比较。
以下是在Groovy中可用的关系运算符:
二、实例讲解
示例代码如下:
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
三、小结
到这里,关系运算符总结完结,希望对有需要的朋友有所帮助,接下来会继续总结逻辑运算符。
有兴趣的朋友可以点赞收藏关注我,让你在Groovy语言中不在迷路~