Kotlin 的 Float、Int 等是否针对 JVM 中的内置类型进行了优化?

Are Kotlin's Float, Int etc optimised to built-in types in the JVM?

我是 Kotlin 新手,AFAICT 其语法仅支持 Int、Float 等对象版本,不支持 Java 的相应 int 和 float 原语。但是,如果可能的话,编译器或 JVM 会优化使用原始类型吗?我担心如果我在从游戏主循环调用的函数中使用局部变量,如果 JVM 每次都必须创建一个对象而不是使用原始类型,则可能会导致 GC 卡顿。

引用 docs

Some of the types can have a special internal representation - for example, numbers, characters and booleans can be represented as primitive values at runtime - but to the user they look like ordinary classes. In this section we describe the basic types used in Kotlin: numbers, characters, booleans, arrays, and strings.

所以是的,编译器确实以在运行时使用 JVM primitive types 的方式进行了优化。当然也有一些例外:

On the Java platform, numbers are physically stored as JVM primitive types, unless we need a nullable number reference (e.g. Int?) or generics are involved. In the latter cases numbers are boxed.

源文档中也有提示,例如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.