Relationship between classes like Kotlin Int and Java Integer (and int primitive)


What is the relationship between classes like Kotlin's Int, Float, Double and Java's Integer/int, Float/float, Double/double?

From what I understand of the Kotlin documentation, it seems that once compiled to jvm bytecode, the Kotlin classes no longer exist, and depending on nullability, the class will either be a primitive or a boxed class:

This is my understanding

Kotlin Int -> java int
Kotlin int? -> java Integer

Furthermore, It seems there is no way to get the Kotlin class of an object while running in the JVM, is this correct? The best I am able to do is get the Java class through the javaClass property ( I think it is a property?)

So if my understanding is correct, a Kotlin developer who wants to avoid object allocation would not work with nullable types, is this right?

One last question: Is this forum the best place to ask these beginner-level Kotlin questions? I am afraid of spamming the wrong place.


I think you ask your question in the right place.

Your undestanding is correct: nullable types derived from “primitives” are represented by boxed objects, and if you want to avoid allocation, you shlud stick to non-null types.
One important remark: when using generic classes, such as collections, even non-null types will be boxed, because of erasure that demands objects at run time. Thus, no magic here: Kotlin’s list of integers is the same as Java’s.