Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing null and number in groovy

Tags:

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.

like image 764
Simon Avatar asked Nov 23 '12 16:11

Simon


1 Answers

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

like image 81
tim_yates Avatar answered Oct 16 '22 02:10

tim_yates