what is this operator called and what is it used for <=>

后端 未结 4 968
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 23:00

I recently came across this magical operator when digging into Groovy: <=>

Groovy has really made me happy with elvis operators ?. and ?: which I use constantly

相关标签:
4条回答
  • 2021-01-18 23:25

    Like many others mention, it's called the spaceship operator. Here's my test:

    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
    
    0 讨论(0)
  • 2021-01-18 23:33

    You got a list of operators here. It is called the "Spaceship" operator. It handles null without problem.

    0 讨论(0)
  • 2021-01-18 23:36

    It's called the spaceship operator and is also commonly used for comparison in Ruby.

    http://www.objectpartners.com/2010/02/08/the-groovy-spaceship-operator-explained/

    0 讨论(0)
  • 2021-01-18 23:38

    Name : Spaceship operator

    Method that it uses : a.compareTo(b) //where a and b are the variables that has been used

    Class : java.lang.Comparable

    And this link explains about that operator in a bit more . Click Here

    0 讨论(0)
提交回复
热议问题