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

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

问题描述

我正在处理一个我声明为Integer的变量,并发现>不是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.

关于为何不推荐使用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.

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

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