Simon
Simon

Reputation: 331

Comparing null and number in 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.

Upvotes: 9

Views: 2768

Answers (1)

tim_yates
tim_yates

Reputation: 171114

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

Upvotes: 13

Related Questions