13

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.

2
  • The lines of code you posted are confusing. What exactly is the Java method? What line of code exactly gives you an error? What is the exact error message? What does the case class have to do with it? It takes an Option[Integer] (a strange mixture of a Scala Option and a Java Integer). Commented Nov 15, 2016 at 9:17
  • The case class takes an Option[Integer] and not an Integer. An Option is not an Integer, so you cannot cast one to the other. Commented Nov 15, 2016 at 9:44

6 Answers 6

8

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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.

Comments

2

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))

Comments

1
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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.