In the Java language programming, the non null Judgment of object is an eternal topic. For example, we often need to judge a string as follows:
if(str!=null&&!str.equals(""))
{
......
}
Typing such a statement really makes you sick, and sometimes you forget the "!" in the input "!str.equals" statement. Causes the code to have a logical error.
and agile Groovy language development does not require us to worry about such problems. The same judgment statement, we just need to enter the following code:
def str = null
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
The execution result of this statement segment is:
STR is NULL
As can be seen, the IF (str) Judgment statement is not executed when STR is null. You might ask, what happens when str = '?
def str = ''
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
The results of the execution are:
STR is NULL
In this way, we can rewrite the beginning of the Java code into the following code:
if(str)
{
......
}
This is a lot simpler. Isn't it?