Kotlin数据类型是基于原始或非原始Java数据类型构建的吗? [英] Are Kotlin data types built off primitive or non-primitive Java data types?
问题描述
我是Kotlin的新手,正在玩数据类型。我拿了一个 Int
类型,然后试着将它作为 Double
转换为 num as 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
类型然后尝试将其作为Double
投射,将num称为Double
< ...>但是,此失败,说Int不能转换为Double
。我假设这是因为它是基于Integer
类构建的,而不是原始的int
数据类型。
I took an
Int
type and then tried to cast it as aDouble
by sayingnum as Double
<...> However, this failed, saying that Int cannot be cast toDouble
. I am assuming this is because it is built off theInteger
class rather than the rawint
data type.
不,有两点需要注意:
-
Kotlin定位其数字类型(
Int
,Long
,Double
等)由于没有相互嵌套,这些类型之间存在无子类型关系。这就是为什么演员intNum为Double
在Kotlin中没有成功。这也是为什么这些类型之间没有隐式转换的原因。相反,使用相应的功能完成数字转换(例如.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 castintNum 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.
另请参阅:对类似问题的回答,(链接)
See also: an answer to a similar question, (link)
这篇关于Kotlin数据类型是基于原始或非原始Java数据类型构建的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!