Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 821
Goldengirl Avatar asked Nov 15 '16 09:11

Goldengirl


People also ask

Can we convert int to Integer in Java?

There is another method of the Integer class called “parseInt()” that is also used to convert Integer to int. In this method, a string is accepted as an argument and gives an int value as an output.

Can you turn an int into a string Java?

We can convert int to String in java using String. valueOf() and Integer. toString() methods.


1 Answers

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
like image 59
shreya chakraborty Avatar answered Oct 27 '22 01:10

shreya chakraborty