Goldengirl
Goldengirl

Reputation: 967

How to convert a scala Int to a java Integer?

I am reasonable new to scala and working with scala and Java together.

I am trying to pass a scala Int into a method that accepts an Integer(java.long.Integer). Since they are of different types the compiler gives an error.

 /* name- Option[String], id- Option[Integer] , mask- Option[String]*/
 new findingFrame(name,id, mask)

 case class findingFrame(name: String,
                         id: Option[java.lang.Integer],
                         mask : Option[String])

I tried using .instanceOf [java.lang.Integer], but this doesn't work either..

I am not sure how to solve this.. Can somebody help me please? Thank you.

Upvotes: 13

Views: 18165

Answers (6)

Paul Lysak
Paul Lysak

Reputation: 1294

If automatic conversion didn't work for you (for example, if you have some intermediate steps and type checker can't immediately find what should be the final type) you can use int2Integer (or long2Long for Long) from scala.language.implicitConversions which is imported by default (https://docs.scala-lang.org/tour/implicit-conversions.html)

Upvotes: 0

rleibman
rleibman

Reputation: 247

I think most other answers cover it, your issue is with the use of Option, not with the difference between scala and java integers. There's many things you can do with options, for example: Given:

val optionOfInteger = Option(5)
  • (ugly) assume your option always has a value, just use get (val i = optionOfInteger.get)
  • Apply a default value: (val i = optionOfInteger.getOrElse(0))
  • You can map the Option of integer into option of something else (val optionOfString = optionOfInteger.map(_.toString))
  • You can do both of the last two in one call ( val str = optionOfInteger.fold("Nothing")(_.toString) )
  • You can also think of Option as a collection of one, so all the nice stuff you can do with collections you can do with Options, including converting it to other type of collections, fold it, etc. A good use for it in your case, might be to make the decision to call or NOT to call the java method.

def myFn(findingFrame: FindingFrame) = { findingFrame.in.foreach{i => javaMethod(i) } }

In the above you could use map, or match/case instead.

Upvotes: 2

shreya chakraborty
shreya chakraborty

Reputation: 99

All you need to do is pass the int value to the constructor of Integer(). You can pass this new object now.

scala> val n = 20
n: Int = 20

scala> new Integer(n)
res0: Integer = 20

scala> res0.getClass
res1: Class[_ <: Integer] = class java.lang.Integer

Upvotes: 8

jacks
jacks

Reputation: 4753

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

scala> val id: Option[java.lang.Integer] = Some(i)
id: Option[Integer] = Some(1)

scala> id.get.getClass
res9: Class[_ <: Integer] = class java.lang.Integer

Upvotes: 1

Rockie Yang
Rockie Yang

Reputation: 4925

Scala using box for primitive types, it can pass directly to Java.

In the following code, type i is actually int, and int can passed to Random which requires long since int will convert to long automatically.

val i = 3
println(i.getClass)
println(new Random(i))

Int the following code, using Option[Integer], Option[Integer].get will get the value inside which is type Integer. And Integer can not passed directly to long, while we can use .toLong to convert it to Long which will automatically unbox to long.

val oi: Option[Integer] = new Some(3)
println(oi.getClass)

println(new Random(oi.get.toLong))

Upvotes: 2

Frank Lauterwald
Frank Lauterwald

Reputation: 174

I think your problem is not so much about scala.Int vs. java.lang.Integer but the use of Option types.

Short answer: this seems to work for me val i: Int = 7 new findingFrame("foo", Some(i), None)

Long answer: The scala Option types are wrappers that may or may not contain a value of its generic type. The idea is to avoid Java's null values. An Option may be either None (does not contain a value) or Some[T] (does contain a value) - None and Some are subclasses of Option here. So you just need to wrap your scala Int into a Some as in the code above.

Upvotes: 2

Related Questions