Scala 中 Int 和 Integer 有什么区别?

发布于 2024-08-02 04:02:28 字数 521 浏览 4 评论 0原文

我正在使用一个已声明为整数的变量,并发现 > > 不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

像极了他 2024-08-09 04:02:28

“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 进行隐式转换,这意味着您可以在采用 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:

 /** @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.

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.

伴我心暖 2024-08-09 04:02:28

Integer 从 java.lang.Integer 导入,只是为了与 Java 兼容。 既然是Java类,当然不能有名为“<”的方法。
编辑:您可以通过声明从 Integer 到 Int 的隐式转换来缓解此问题。

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

不过,您仍然会收到弃用警告。

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.

怪异←思 2024-08-09 04:02:28

我认为您遇到的问题必须对值类型进行装箱/拆箱以及 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.

等数载,海棠开 2024-08-09 04:02:28

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 type int, 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 expecting java.lang.Object, equivalent to Scala's AnyRef, for example. To do that, you put that int inside an Integer object, and then pass the Integer.

隔岸观火 2024-08-09 04:02:28

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文