Scala 中的 Int 和 Integer 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1269170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the differences between Int and Integer in Scala?
提问by pr1001
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
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
^
Compare that to an Int:
将其与 Int 进行比较:
scala> j
res30: Int = 3
scala> j > 3
res31: Boolean = false
What are the differences between Integer and Int? I see the deprecation warning but it's unclear to me why it was deprecated and, given that it has been, why it doesn't have a > method.
整数和整数之间有什么区别?我看到了弃用警告,但我不清楚它为什么被弃用,并且鉴于它已经被弃用,为什么它没有 > 方法。
采纳答案by Richard Dallaway
"What are the differences between Integer and Int?"
“整数和整数有什么区别?”
Integer is just an alias for java.lang.Integer. Int is the Scala integer with the extra capabilities.
Integer 只是 java.lang.Integer 的别名。Int 是具有额外功能的 Scala 整数。
Looking in Predef.scala you can see this the alias:
查看 Predef.scala 你可以看到这个别名:
/** @deprecated use <code>java.lang.Integer</code> instead */
@deprecated type Integer = java.lang.Integer
However, there is an implicit conversion from Int to java.lang.Integer if you need it, meaning that you can use Int in methods that take an Integer.
但是,如果需要,可以从 Int 隐式转换为 java.lang.Integer,这意味着您可以在采用 Integer 的方法中使用 Int。
As to why it is deprecated, I can only presume it was to avoid any confusion over which kind of integer you were working with.
至于为什么它被弃用,我只能假设这是为了避免对您正在使用的整数类型产生任何混淆。
回答by Kim Stebel
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.
Integer 从 java.lang.Integer 导入,仅用于与 Java 兼容。既然是Java类,当然不能有名为“<”的方法。编辑:您可以通过声明从 Integer 到 Int 的隐式转换来缓解这个问题。
implicit def toInt(in:Integer) = in.intValue()
You'll still get deprecation warning though.
不过,您仍然会收到弃用警告。
回答by Valentein
I think the problem you're seeing has has to do boxing/unboxing of value types and the use of the Java class Integer.
我认为您所看到的问题必须对值类型进行装箱/拆箱以及 Java 类 Integer 的使用。
I think the answer is here: Boxing and unboxing in Scala. There is no implict unboxing in Scala. You've defined ias the Java class Integer but in the i > 3, the 3 is being treated and an int.
我认为答案就在这里:在 Scala 中装箱和拆箱。Scala 中没有隐式拆箱。您已将i定义为 Java 类 Integer 但在i > 3 中, 3 正在被处理,并且是一个 int。
回答by Daniel C. Sobral
Integeris a Java class, java.lang.Integer. It's different from Java's primitive type int, which is not a class. It can't have <defined, because Java does not allow operators to be defined for classes.
Integer是一个 Java 类,java.lang.Integer. 它不同于 Java 的原始类型int,它不是一个类。它不能<定义,因为 Java 不允许为类定义运算符。
Now, you might wonder why such a type exist at all? Well, primitive types cannot be passed as references, so you can't pass an intto a method expecting java.lang.Object, equivalent to Scala's AnyRef, for example. To do that, you put that intinside an Integerobject, and then pass the Integer.
现在,您可能想知道为什么会存在这样的类型?好吧,原始类型不能作为引用传递,因此您不能将 an 传递int给一个方法 expecting java.lang.Object,AnyRef例如相当于 Scala 的。要做到这一点,你把它int放在一个Integer对象中,然后传递Integer.
回答by deepak
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 "<".
Integer 从 java.lang.Integer 导入,仅用于与 Java 兼容。既然是Java类,当然不能有名为“<”的方法。
EDIT: You can mitigate this problem by declaring an implicit conversion from Integer to Int.
编辑:您可以通过声明从 Integer 到 Int 的隐式转换来缓解这个问题。

