Kotlin 数据类型是基于原始还是非原始 Java 数据类型构建的? [英] Are Kotlin data types built off primitive or non-primitive Java data types?

查看:41
本文介绍了Kotlin 数据类型是基于原始还是非原始 Java 数据类型构建的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Kotlin 的新手,正在研究数据类型.我取了一个 Int 类型,然后尝试通过说 num as Double 将它转换为 Double,这是一个在 java 中有效的调用(非句法上,但你明白了).但是,这失败了,说 Int 不能转换为 Double.我假设这是因为它是基于 Integer 类而不是原始 int 数据类型构建的.我说得对吗,最有效的值转换方法是什么?有一个 .toDouble() 函数,但这似乎效率低下且笨拙.

I am new to Kotlin and was playing around with the data types. I took an Int type and then tried to cast it as a Double by saying num as Double, a call that is valid in java (non syntactically but you get the point). However, this failed, saying that Int cannot be cast to Double. I am assuming this is because it is built off the Integer class rather than the raw int data type. Am I correct, and what is the most efficient way to cast values? There is a .toDouble() function, but this seems inefficient and unwieldy.

推荐答案

我选择了一个 Int 类型,然后通过说 num as Double <...> 尝试将它转换为 Double,这失败了,说 Int 不能转换为 Double.我假设这是因为它是基于 Integer 类而不是原始 int 数据类型构建的.

I took an Int type and then tried to cast it as a Double by saying num as Double <...> However, this failed, saying that Int cannot be cast to Double. I am assuming this is because it is built off the Integer class rather than the raw int data type.

不,有两点需要注意:

  • Kotlin 将其数字类型(IntLongDouble 等)定位为不相互嵌套,有这些类型之间没有子类型关系.这就是为什么在 Kotlin 中转换 intNum as Double 没有成功的原因.这也是为什么这些类型之间没有隐式转换的原因.相反,数字转换是用相应的函数完成的(例如.toDouble())

  • Kotlin positions its numeric types (Int, Long, Double etc.) as not being nested into each other, there is no subtyping relationship between these types. That's why the cast intNum as Double does not succeed in Kotlin. That's also why there's no implicit conversions between these types. Instead, the numeric conversion is done with the corresponding functions (e.g. .toDouble())

Kotlin 中的数字类型用法尽可能编译为 JVM 原语.某些用法需要装箱类型(例如,可以为空的 Int? 需要装箱,使用 Int 作为类型参数的泛型类型实现也是如此),但编译器决定它们是否每种情况都需要.

The numeric type usages in Kotlin are compiled into JVM primitives where possible. Some usages require boxed types (e.g. a nullable Int? requires boxing, and so does a generic type implementation with an Int as a type argument), but the compiler decides whether they are necessary for each case.

<...> 转换值的最有效方法是什么?有一个 .toDouble() 函数,但这似乎效率低下且笨拙.

<...> What is the most efficient way to cast values? There is a .toDouble() function, but this seems inefficient and unwieldy.

最有效的方法是使用数字转换函数,如.toDouble().实际上,这些函数是内在化的,使用时没有函数调用开销.它们的编译与 javac 为 Java 数字转换或隐式转换生成的内容非常接近.您可以检查 Kotlin 编译器生成的字节码以了解它的内幕以及特定转换是否引入任何开销.

The most efficient way is to use the numeric conversion functions like .toDouble(). In fact, these functions are intrinsified, and there is no function call overhead when you use them. They are compiled closely to what javac would produce for a Java numeric cast or an implicit conversion. You can inspect the bytecode that the Kotlin compiler produces to find out what it's under the hood and whether a specific conversion introduces any overhead.

另见:对类似问题的回答,(link)

See also: an answer to a similar question, (link)

这篇关于Kotlin 数据类型是基于原始还是非原始 Java 数据类型构建的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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