16

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

1 Answer 1

26

You must use the KClass#javaObjectType to get the wrapper type class in Kotlin:

Returns a Java Class instance corresponding to the given KClass 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)
Sign up to request clarification or add additional context in comments.

1 Comment

For the more details you can see here.

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.