比较 groovy 中的 null 和 number [英] Comparing null and number in groovy

查看:16
本文介绍了比较 groovy 中的 null 和 number的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在 Groovy 中会出现以下情况?

Why is the following true in Groovy?

0 > null

是自己选择还是只是实现了 compareTo 的结果?

Is it by choice or is it just a consequence of the implementation of compareTo?

我使用的是 Groovy 2.0.5.

I'm using Groovy 2.0.5.

推荐答案

在 Groovy 中 null 是最低可能的元素,所以一切都是 >空

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()

否则会怎样?如果你想让它起作用,你必须说null 比任何东西都低"null 比任何东西都高"...

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 选择了前者

Groovy chose the former

这篇关于比较 groovy 中的 null 和 number的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆