[英]Int vs Integer: type mismatch, found: Int, required: String
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互操作性的原因。
This question is almost a duplicate of: Scala can't multiply java Doubles? 这个问题几乎是重复的: Scala不能乘以java双打? - you can look at my answer as well, as the idea is similar. - 你也可以看看我的答案 ,因为这个想法很相似。
As Eastsun already hinted, the answer is an implicit conversion from an java.lang.Integer
(basically a boxed int
primitive) to a scala.Int
, which is the Scala way of representing JVM primitive integers. 正如Eastsun已经暗示的那样,答案是从java.lang.Integer
(基本上是一个盒装的int
原语)到scala.Int
的隐式转换 ,它是表示JVM原始整数的Scala方式。
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.Int
where java.lang.Integer
is needed seems to work just fine due to autoboxing. 使用scala.Int
需要java.lang.Integer
代码似乎可以正常工作,因为自动装箱。 So the following works: 以下是有效的:
def foo(d: java.lang.Integer) = println(d)
val z: scala.Int = 1
foo(z)
Also, as michaelkebe said, do not use the Integer
type - which is actually shorthand for scala.Predef.Integer
as 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.Integer
tried to mimic Java's syntactic sugar where a + "my String"
means string concatenation, a
is an int
. 您得到的错误可能是scala.Predef.Integer
试图模仿Java的语法糖,其中a + "my String"
表示字符串连接, a
是int
。 Therefore the +
method in the scala.Predef.Integer
type only does string concatenation (expecting a String
type) and no natural integer addition. 因此, scala.Predef.Integer
类型中的+
方法仅执行字符串连接(期望String
类型)并且不添加自然整数。
-- Flaviu Cipcigan - Flaviu Cipcigan
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
First of all you should use java.lang.Integer
instead of Integer
. 首先,您应该使用java.lang.Integer
而不是Integer
。
Currently I don't know why the error occurs. 目前我不知道为什么会发生错误。
a
is an instance of java.lang.Integer
and this type doesn't have a method named +
. a
是java.lang.Integer
一个实例,此类型没有名为+
的方法。 Furthermore there is no implicit conversion to Int
. 此外,没有隐式转换为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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.