Scala 中 Int 和 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
^
将其与 Int 进行比较:
scala> j
res30: Int = 3
scala> j > 3
res31: Boolean = false
Integer 和 Int 有什么区别? 我看到了弃用警告,但我不清楚为什么它被弃用,并且鉴于它已经被弃用,为什么它没有 > > 方法。
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“Integer 和 Int 有什么区别?”
Integer 只是 java.lang.Integer 的别名。 Int 是具有额外功能的 Scala 整数。
查看 Predef.scala,您可以看到这个别名:
但是,如果需要的话,可以从 Int 到 java.lang.Integer 进行隐式转换,这意味着您可以在采用 Integer 的方法中使用 Int。
至于为什么它被弃用,我只能推测这是为了避免对您正在使用的整数类型产生任何混淆。
"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.
Looking in Predef.scala you can see this the alias:
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.
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.
Integer 从 java.lang.Integer 导入,只是为了与 Java 兼容。 既然是Java类,当然不能有名为“<”的方法。
编辑:您可以通过声明从 Integer 到 Int 的隐式转换来缓解此问题。
不过,您仍然会收到弃用警告。
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.
You'll still get deprecation warning though.
我认为您遇到的问题必须对值类型进行装箱/拆箱以及 Java 类 Integer 的使用。
我认为答案就在这里:Scala 中的装箱和拆箱。 Scala 中没有隐式拆箱。 您已将 i 定义为 Java 类 Integer,但在 i > 中 3,3 正在被处理并且是一个整数。
I think the problem you're seeing has has to do boxing/unboxing of value types and the use of the Java class Integer.
I think the answer is here: Boxing and unboxing in Scala. There is no implict unboxing in Scala. You've defined i as the Java class Integer but in the i > 3, the 3 is being treated and an int.
Integer
是一个 Java 类,java.lang.Integer
。 它与Java的原始类型int
不同,它不是类。 它不能定义<
,因为 Java 不允许为类定义运算符。现在,您可能想知道为什么存在这种类型? 好吧,原始类型不能作为引用传递,因此您不能将
int
传递给需要java.lang.Object
的方法,相当于 Scala 的AnyRef<例如,/代码>。 为此,您可以将该
int
放入Integer
对象中,然后传递Integer
。Integer
is a Java class,java.lang.Integer
. It's different from Java's primitive typeint
, which is not a class. It can't have<
defined, because Java does not allow operators to be defined for classes.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
int
to a method expectingjava.lang.Object
, equivalent to Scala'sAnyRef
, for example. To do that, you put thatint
inside anInteger
object, and then pass theInteger
.Integer 从 java.lang.Integer 导入,只是为了与 Java 兼容。 既然是Java类,当然不能有名为“<”的方法。
编辑:您可以通过声明从 Integer 到 Int 的隐式转换来缓解此问题。
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.