pr1*_*001 51 int types integer scala
我正在处理一个我声明为Integer的变量,并发现>不是Integer的成员.这是一个简单的例子:
scala> i
warning: there were deprecation warnings; re-run with -deprecation for details
res28: Integer = 3
scala> i > 3
<console>:6: error: value > is not a member of Integer
i > 3
^
Run Code Online (Sandbox Code Playgroud)
将它与Int进行比较:
scala> j
res30: Int = 3
scala> j > 3
res31: Boolean = false
Run Code Online (Sandbox Code Playgroud)
Integer和Int有什么区别?我看到了弃用警告,但我不清楚为什么它被弃用,并且鉴于它已经存在,为什么它没有>方法.
Ric*_*way 43
"Integer和Int之间有什么区别?"
Integer只是java.lang.Integer的别名.Int是具有额外功能的Scala整数.
在Predef.scala中查看您可以看到别名:
/** @deprecated use <code>java.lang.Integer</code> instead */
@deprecated type Integer = java.lang.Integer
Run Code Online (Sandbox Code Playgroud)
但是,如果需要,可以从Int到java.lang.Integer进行隐式转换,这意味着您可以在采用Integer的方法中使用Int.
至于为什么它被弃用,我只能假设它是为了避免混淆你正在使用哪种整数.
Integer从java.lang.Integer导入,仅用于与Java兼容。由于它是Java类,因此当然不能有一个名为“ <”的方法。编辑:您可以通过声明从Integer到Int的隐式转换来缓解此问题。
implicit def toInt(in:Integer) = in.intValue()
Run Code Online (Sandbox Code Playgroud)
您仍然会收到弃用警告。
| 归档时间: |
|
| 查看次数: |
28523 次 |
| 最近记录: |