Kotlin的Float,Int等是否已针对JVM中的内置类型进行了优化? [英] Are Kotlin's Float, Int etc optimised to built-in types in the JVM?

查看:154
本文介绍了Kotlin的Float,Int等是否已针对JVM中的内置类型进行了优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Kotlin, and AFAICT its syntax only supports the object versions of Int, Float etc without the corresponding int and float primitives of Java. But does the compiler or JVM optimise to use the primitive types if possible? I'm concerned that if I use local variables in a function called from a game main loop it might cause GC stutter if the JVM has to create an object each time instead of using a primitive type.

推荐答案

引用文档:

其中一些类型可以具有特殊的内部表示形式-例如,数字,字符和布尔值可以在运行时表示为原始值-但对用户而言,它们看起来像普通的类.在本节中,我们描述Kotlin中使用的基本类型:数字,字符,布尔值,数组和字符串.

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.

是的,编译器的确以

So yes, the compiler does optimise in a way that the JVM primitive types are used at runtime. There are certain exceptions of course:

在Java平台上,除非我们需要可为空的数字引用(例如Int?)或涉及泛型,否则数字实际上是作为JVM基本类型存储的.在后一种情况下,数字用方框括起来.

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:

There's also a hint in the source documentation, e.g. Int:

代表32位有符号整数.在JVM上,此类型的非空值表示为原始类型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.

这篇关于Kotlin的Float,Int等是否已针对JVM中的内置类型进行了优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆