Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala中Int和Integer的区别是什么?_Scala_Types_Integer_Int - Fatal编程技术网

Scala中Int和Integer的区别是什么?

Scala中Int和Integer的区别是什么?,scala,types,integer,int,Scala,Types,Integer,Int,我正在处理一个声明为整数的变量,发现>不是整数的成员。下面是一个简单的例子: 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

我正在处理一个声明为整数的变量,发现>不是整数的成员。下面是一个简单的例子:

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
         ^

整数和整数之间有什么区别?我看到了弃用警告,但我不清楚为什么它会被弃用,并且,考虑到它已经被弃用,为什么它没有>方法。

Integer从java.lang.Integer导入,只是为了与java兼容。由于它是一个Java类,当然它不能有一个名为“的方法,我认为您看到的问题必须与值类型的装箱/拆箱以及Java类整数的使用有关


我认为答案就在这里:。Scala中没有隐式拆箱。您已经将I定义为Java类整数,但在I>3中,3被处理,而int是一个int。

Integer
是一个Java类,
Java.lang.Integer
。它不同于Java的基本类型
int
,即不是类。它不能有“Integer和Int之间的区别是什么?”

Integer只是java.lang.Integer的别名。Int是带有额外功能的Scala整数

在Predef.scala中可以看到别名:

 /** @deprecated use <code>java.lang.Integer</code> instead */
  @deprecated type Integer = java.lang.Integer
但是,如果需要,可以使用从Int到java.lang.Integer的隐式转换,这意味着您可以在接受整数的方法中使用Int


至于为什么不推荐使用它,我只能假设它是为了避免对您使用的是哪种整数产生任何混淆。

integer是从java.lang.integer导入的,只是为了与java兼容。因为它是一个java类,当然它不能有一个名为“谢谢,所有人,感谢有用的答案”的方法。你们都回答了我的问题,所以我会让评分最高的回复上升到顶部,并将其标记为答案。与此同时,我已经把你所有的回答都评了上去。现在链接断了。
 implicit def toInt(in:Integer) = in.intValue()
 /** @deprecated use <code>java.lang.Integer</code> instead */
  @deprecated type Integer = java.lang.Integer