Scala 中的 Int 和 Integer 有什么区别? [英] What is the differences between Int and Integer in Scala?

查看:24
本文介绍了Scala 中的 Int 和 Integer 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
         ^

将其与 Int 进行比较:

Compare that to an Int:

scala> j
res30: Int = 3

scala> j > 3
res31: Boolean = false

Integer 和 Int 有什么区别?我看到了弃用警告,但我不清楚为什么它被弃用了,鉴于它已经被弃用,为什么它没有 > 方法.

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.

推荐答案

"Integer 和 Int 有什么区别?"

"What are the differences between Integer and Int?"

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

Integer is just an alias for java.lang.Integer. Int is the Scala integer with the extra capabilities.

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

Looking in Predef.scala you can see this the alias:

 /** @deprecated use <code>java.lang.Integer</code> instead */
  @deprecated type Integer = java.lang.Integer

但是,如果需要,可以将 Int 隐式转换为 java.lang.Integer,这意味着您可以在采用 Integer 的方法中使用 Int.

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.

这篇关于Scala 中的 Int 和 Integer 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆