What is the differences between Int and Integer in Scala?

后端 未结 5 2064
无人及你
无人及你 2020-12-01 17:46

I was working with a variable that I had declared as an Integer and discovered that > is not a member of Integer. Here\'s a simple example:

scala> i
warni         


        
5条回答
  •  天涯浪人
    2020-12-01 18:29

    Integer gets imported from java.lang.Integer and is only for compatibility with Java. Since it is a Java class, of course it can't have a method called "<". EDIT: You can mitigate this problem by declaring an implicit conversion from Integer to Int.

     implicit def toInt(in:Integer) = in.intValue()
    

    You'll still get deprecation warning though.

提交回复
热议问题