What is the difference between a.ne(null) and a != null in Scala?

后端 未结 2 648
心在旅途
心在旅途 2021-01-04 02:38

I have been always using

a != null

to check that a is not a null reference. But now I\'ve met another way used:



        
2条回答
  •  猫巷女王i
    2021-01-04 03:22

    Besides that said @drexin and @Jack, ne defined in AnyRef and exists only for referential types.

    scala> "null".ne(null)
    res1: Boolean = true
    
    scala> 1.ne(null)
    :5: error: type mismatch;
     found   : Int
     required: ?{val ne: ?}
    Note that implicit conversions are not applicable because they are ambiguous:
     both method int2Integer in object Predef of type (Int)java.lang.Integer
     and method intWrapper in object Predef of type (Int)scala.runtime.RichInt
     are possible conversion functions from Int to ?{val ne: ?}
           1.ne(null)
    
    scala> 1 != null
    res2: Boolean = true
    

提交回复
热议问题