scala Int vs Integer:类型不匹配,找到:Int,需要:String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1566315/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:41:20  来源:igfitidea点击:

Int vs Integer: type mismatch, found: Int, required: String

scala

提问by pcjuzer

I type these to the scala interpreter:

我将这些输入到 Scala 解释器中:

val a : Integer = 1;
val b : Integer = a + 1;

And I get the message:

我收到消息:

<console>:5: error: type mismatch;
 found   : Int(1)
 required: String
       val b : Integer = a +1
                            ^

Why? How can I solve this? This time I need Integers due to Java interoperability reasons.

为什么?我该如何解决这个问题?这次由于 Java 互操作性原因我需要整数。

回答by Flaviu Cipcigan

This question is almost a duplicate of: Scala can't multiply java Doubles?- you can look at my answeras well, as the idea is similar.

这个问题几乎是重复的:Scala 不能乘以 java Doubles?- 你也可以看看我的答案,因为想法是相似的。

As Eastsunalready hinted, the answer is an implicit conversionfrom an java.lang.Integer(basically a boxed intprimitive) to a scala.Int, which is the Scala way of representing JVM primitive integers.

正如Eastsun已经暗示的那样,答案是从 an (基本上是盒装原语)到 a的隐式转换,这是表示 JVM 原语整数的 Scala 方式。java.lang.Integerintscala.Int

implicit def javaToScalaInt(d: java.lang.Integer) = d.intValue

And interoperability has been achieved - the code snipped you've given should compile just fine! And code that uses scala.Intwhere java.lang.Integeris needed seems to work just fine due to autoboxing. So the following works:

并且已经实现了互操作性 - 您提供的代码应该可以很好地编译!由于自动装箱,scala.Intjava.lang.Integer需要的地方使用的代码似乎工作得很好。所以以下工作:

def foo(d: java.lang.Integer) = println(d)
val z: scala.Int = 1
foo(z)

Also, as michaelkebesaid, do not use the Integertype - which is actually shorthand for scala.Predef.Integeras it is deprecated and most probably is going to be removed in Scala 2.8.

此外,正如michaelkebe所说,不要使用Integer类型 - 这实际上是简写,scala.Predef.Integer因为它已被弃用,并且很可能将在 Scala 2.8 中删除。

EDIT: Oops... forgot to answer the why. The error you get is probably that the scala.Predef.Integertried to mimic Java's syntactic sugar where a + "my String"means string concatenation, ais an int. Therefore the +method in the scala.Predef.Integertype only does string concatenation (expecting a Stringtype) and no natural integer addition.

编辑:哎呀...忘了回答为什么。你得到的错误可能是scala.Predef.Integer试图模仿 Java 的语法糖,其中a + "my String"意味着字符串连接,a是一个int. 因此类型中的+方法scala.Predef.Integer只进行字符串连接(期望String类型),没有自然整数加法。

-- Flaviu Cipcigan

——弗拉维乌·西普西根

回答by Eastsun

Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> implicit def javaIntToScala(n: java.lang.Integer) = n.intValue

javaIntToScala: (java.lang.Integer)Int

scala> val a: java.lang.Integer = 1

a: java.lang.Integer = 1

scala> val b: java.lang.Integer = a + 1

b: java.lang.Integer = 2

回答by michael.kebe

First of all you should use java.lang.Integerinstead of Integer.

首先,您应该使用java.lang.Integer而不是Integer.

Currently I don't know why the error occurs.

目前我不知道为什么会发生错误。

ais an instance of java.lang.Integerand this type doesn't have a method named +. Furthermore there is no implicit conversion to Int.

a是 的实例,java.lang.Integer并且此类型没有名为 的方法+。此外,没有隐式转换为Int.

To solve this you can try this:

要解决此问题,您可以尝试以下操作:

scala> val a: java.lang.Integer = 1
a: java.lang.Integer = 1

scala> val b: java.lang.Integer = a.intValue + 1
b: java.lang.Integer = 2