Ran into some very strange behavior. Why does it m...
# getting-started
p
Ran into some very strange behavior. Why does it matter when I call
::class
?
Copy code
val c1 = Int::class.java to 42::class.java 
c1.first to c1.second // (int, int)

val c2 = Int::class.java to 42
c2.first to c2.second::class.java // (int, class java.lang.Integer)
and is there any way for me to make the boxed class object (
class java.lang.Integer
) from the primitive class object (
int
)?
j
Kotlin doesn't make a difference between primitive JVM types and their boxed variant in the language itself, but they still exist under the cover. If you're calling
::class
on a value that was previously boxed (when being stored in a generic
Pair
object), it's not the same as calling it on an unboxed literal
If you want to convert between boxed and primitive types, you might want to check out javaPrimitiveType and javaObjectType
p
thanks! that makes sense i found i can use an apache util:
Copy code
ClassUtils.primitiveToWrapper(x)
looking at their implementation, they just keep a hardcoded map of primitives to boxed types.. nothing clever about it
@Joffrey for that, is there a method to give me KClass if I have Class? something like:
Copy code
String::class.java.kotlin
oh nvm, this literally works. 🤐
😆 3
e
there is also
Copy code
Int::class.javaObjectType
Int::class.javaPrimitiveType