在常规中比较空和数字 [英] Comparing null and number in groovy
本文介绍了在常规中比较空和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
0> null
是由选择决定,还是仅仅是执行compareTo的结果?
我使用Groovy 2.0.5。
解决方案
$ c> null 是可能的最小元素,所以一切都是> null
assert'tim'> null
assert 0> null
assert -1> null
断言Double.NEGATIVE_INFINITY> null
这意味着可以这样做:
[1,null,3] .sort()
<否则会发生什么?如果你想让它起作用,你必须说null比任何东西都要低或null比任何东西都高 ...
Groovy选择了前者
Why is the following true in Groovy?
0 > null
Is it by choice or is it just a consequence of the implementation of compareTo?
I'm using Groovy 2.0.5.
解决方案
In Groovy null
is the lowest possible element, so everything is > null
assert 'tim' > null
assert 0 > null
assert -1 > null
assert Double.NEGATIVE_INFINITY > null
This means things like this can work:
[ 1, null, 3 ].sort()
Otherwise what would happen? If you want this to work, you have to say "null is lower than anything" or "null is higher than anything"...
Groovy chose the former
这篇关于在常规中比较空和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文