9

为什么在 Groovy 中以下是正确的?

0 > null

它是由选择还是仅仅是 compareTo 实现的结果?

我正在使用 Groovy 2.0.5。

4

1 回答 1

13

在 Groovynull中是最低的元素,所以一切都是> null

assert                    'tim' > null
assert                        0 > null
assert                       -1 > null
assert Double.NEGATIVE_INFINITY > null

这意味着这样的事情可以工作:

[ 1, null, 3 ].sort()

否则会发生什么?如果你想让它工作,你必须说“null 比任何东西都低”“null 比任何东西都高” ......

Groovy 选择了前者

于 2012-11-23T16:19:57.590 回答