What is called this operator and what does it use for <=>

I recently stumbled upon this magic operator while digging in Groovy: <=>

Did Groovy really enjoy the elvis operators ?. and ?: which I use constantly now, and would like very much in Java. Using this new operator, I found this link . Comparators seem to be a lot easier. My question is how it handles null values ​​and how it compares a non Comparable object. This operator has a name, I could not find it on Google.

+3
source share
4 answers

. " ". null .

+5

:

, : a.compareTo(b)// a b -

: java.lang.Comparable

.

+2

, . :

def a
def b

println 1 <=> 0                 // 1
println 0 <=> 1                 // -1
println 1 <=> a                 // 1
println b <=> 0                 // -1
println a <=> b                 // 0
println "abc" <=> "def"         // -1
println "abc" <=> 1             // throw exception: java.lang.ClassCastException
+1

All Articles