Groovy Tip 3 如何在if条件语句中判断对象为空
2008-04-17 06:49
881 查看
Groovy Tip 3 如何在if条件语句中判断对象为空
在Java语言编程中,对对象的非空判断是一个永恒的话题。例如,我们经常需要对一个字符串进行如下的判断:
if(str!=null&&!str.equals(""))
{
......
}
输入这样的语句的确使人生厌,而且有时候还会忘掉输入“!str.equals("")”语句中的“!”导致代码出现逻辑错误。
而敏捷的Groovy语言开发就不需要我们担心这样的问题。同样的判断语句,我们只需要输入下面的代码:
def str = null
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
这个语句段的执行结果为:
str is null
可以看出,if(str)判断语句,当str为null的时候,不执行。你可能要问,当str = ''的时候会怎样呢?
def str = ''
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
执行结果还是:
str is null
这样,我们可以把开头的那段Java代码改写成如下的代码了:
if(str)
{
......
}
这样就简洁多了。不是吗?
除了字符串对象,那其他对象的非空判断呢?我们来看下面的例子:
def map = ['key1':'value1']
if(map)
{
println'map is not null'
}
else
{
println'map is null'
}
map.remove('key1')
if(map)
{
println'this time,map is not null'
}
else
{
println'this time,map is null'
}
执行结果为:
map is not null
this time,map is null
同样,我们来看看List对象:
def list = []
if(list)
{
println'list is not null'
}
else
{
println'list is null'
}
list<<'a'
if(list)
{
println'here, list is not null'
}
else
{
println'here, list is null too'
}
输出结果为:
list is null
here, list is not null
如果是Domain对象呢?
class Empl
{
String name
}
执行下面的语句:
Empl em = new Empl()
if(em)
{
println'em is not null'
}
else
{
println'em is null'
}
结果为:
em is not null
可以看出,对于Domain对象,只要该对象不是null,则if(em)条件为true。
在Java语言编程中,对对象的非空判断是一个永恒的话题。例如,我们经常需要对一个字符串进行如下的判断:
if(str!=null&&!str.equals(""))
{
......
}
输入这样的语句的确使人生厌,而且有时候还会忘掉输入“!str.equals("")”语句中的“!”导致代码出现逻辑错误。
而敏捷的Groovy语言开发就不需要我们担心这样的问题。同样的判断语句,我们只需要输入下面的代码:
def str = null
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
这个语句段的执行结果为:
str is null
可以看出,if(str)判断语句,当str为null的时候,不执行。你可能要问,当str = ''的时候会怎样呢?
def str = ''
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
执行结果还是:
str is null
这样,我们可以把开头的那段Java代码改写成如下的代码了:
if(str)
{
......
}
这样就简洁多了。不是吗?
除了字符串对象,那其他对象的非空判断呢?我们来看下面的例子:
def map = ['key1':'value1']
if(map)
{
println'map is not null'
}
else
{
println'map is null'
}
map.remove('key1')
if(map)
{
println'this time,map is not null'
}
else
{
println'this time,map is null'
}
执行结果为:
map is not null
this time,map is null
同样,我们来看看List对象:
def list = []
if(list)
{
println'list is not null'
}
else
{
println'list is null'
}
list<<'a'
if(list)
{
println'here, list is not null'
}
else
{
println'here, list is null too'
}
输出结果为:
list is null
here, list is not null
如果是Domain对象呢?
class Empl
{
String name
}
执行下面的语句:
Empl em = new Empl()
if(em)
{
println'em is not null'
}
else
{
println'em is null'
}
结果为:
em is not null
可以看出,对于Domain对象,只要该对象不是null,则if(em)条件为true。
相关文章推荐
- MySQL 如何利用一条语句实现类似于if-else条件语句的判断
- 不使用乘除法,for,while,if,else,switch,case,条件判断语句(A?B:C) 实现:1+2+....+n
- 使用SQL语句如何实现条件判断
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case等关键字 以及条件判断语句(A?B:C)
- if判断条件之List 如何表示不为null
- python 列表处理,for循环语句,if条件判断
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
- 零基础学python-2.10 条件判断 if语句
- Shell 编程5(条件判断语句if)
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
- java的if else if语句的判断条件不能相同
- if条件判断语句
- C/C++学习(一)题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
- ORALCE的几种IF条件判断语句
- 求1+2+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A? B:C)
- 如何在Office Excel中通过设置If条件判断两列数据是否一致?
- C#条件判断-if语句简单结构
- Python之 If条件判断、While循环语句
- PHP 不用求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)