I have a Java method that accepts Class
parameter. I need to pass Integer.class
to it, but from Kotlin code. I tried Int::class.java
, however this does not work, because int.class
is passed to the function. My question is, how do I access Integer.class
from Kotlin.
Java
void foo(Class clazz);
Kotlin
foo(Int::class.java) // does not work, int.class gets passed to foo
To convert a string to integer in Kotlin, use String. toInt() or Integer. parseInt() method. If the string can be converted to a valid integer, either of the methods returns int value.
[Int] Represents a 32-bit signed integer. On the JVM, non-nullable values of this type are represented as values of the primitive type int. Integer is a Java Class. If you were to search the Kotlin spec for "Integer", there is no Kotlin Integer type.
You must use the KClass#javaObjectType to get the wrapper type class in Kotlin:
Returns a Java
Class
instance corresponding to the givenKClass
instance. In case of primitive types it returns corresponding wrapper classes.
For example:
// v--- java.lang.Integer
println(Int::class.javaObjectType)
// v--- int
println(Int::class.java)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With