Kotlin数据类型是基于原始或非原始Java数据类型构建的吗?

时间:2022-01-04 16:26:34

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.

我是Kotlin的新手,正在玩数据类型。我接受了一个Int类型,然后尝试将它作为一个Double转换为num,这是一个在java中有效的调用(非语法但你明白了)。但是,这失败了,说Int不能被强制转换为Double。我假设这是因为它是由Integer类而不是原始int数据类型构建的。我是否正确,投资价值最有效的方法是什么?有一个.toDouble()函数,但这看起来效率低,不实用。

2 个解决方案

#1


38  

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.

我接受了一个Int类型然后尝试将它作为Double转换为num表示为Double <...>然而,这失败了,说Int不能转换为Double。我假设这是因为它是由Integer类而不是原始int数据类型构建的。

No, and there are two important points to note:

不,有两点需要注意:

  • 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将其数字类型(Int,Long,Double等)定位为不相互嵌套,这些类型之间没有子类型关系。这就是为什么作为Double的演员intNum在Kotlin中没有成功。这也是为什么这些类型之间没有隐式转换的原因。相反,数字转换是使用相应的函数完成的(例如.toDouble())

  • 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.

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

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

<...>投射价值的最有效方法是什么?有一个.toDouble()函数,但这看起来效率低,不实用。

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.

最有效的方法是使用.toDouble()之类的数字转换函数。实际上,这些函数是内在的,并且在使用它们时没有函数调用开销。它们与javac为Java数字转换或隐式转换生成的内容密切相关。您可以检查Kotlin编译器生成的字节码,以找出它的内幕,以及特定转换是否会引入任何开销。

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

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

#2


11  

This is because Kotlin does not work like Java in widening numbers.

这是因为Kotlin在扩大数字方面不像Java那样工作。

There are no implicit widening conversions for numbers in Kotlin. for example, you can write something in Java as below:

Kotlin中的数字没有隐含的扩大转换。例如,你可以用Java编写一些东西,如下所示:

int a = 1;
double b = a;

However, you can't write something in Kotlin. for example:

但是,你不能在Kotlin写东西。例如:

val a:Int = 1  
//             v--- can't be widening
val b:Double = a

This is because everything in Kotlin is object, there is no primitive types, so you should convert an Int to a Double explicitly, for example:

这是因为Kotlin中的所有东西都是对象,没有原始类型,所以你应该明确地将Int转换为Double,例如:

val a:Int = 1  
//               v--- convert it explicitly by `toDouble()` method
val b:Double = a.toDouble()

#1


38  

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.

我接受了一个Int类型然后尝试将它作为Double转换为num表示为Double <...>然而,这失败了,说Int不能转换为Double。我假设这是因为它是由Integer类而不是原始int数据类型构建的。

No, and there are two important points to note:

不,有两点需要注意:

  • 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将其数字类型(Int,Long,Double等)定位为不相互嵌套,这些类型之间没有子类型关系。这就是为什么作为Double的演员intNum在Kotlin中没有成功。这也是为什么这些类型之间没有隐式转换的原因。相反,数字转换是使用相应的函数完成的(例如.toDouble())

  • 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.

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

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

<...>投射价值的最有效方法是什么?有一个.toDouble()函数,但这看起来效率低,不实用。

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.

最有效的方法是使用.toDouble()之类的数字转换函数。实际上,这些函数是内在的,并且在使用它们时没有函数调用开销。它们与javac为Java数字转换或隐式转换生成的内容密切相关。您可以检查Kotlin编译器生成的字节码,以找出它的内幕,以及特定转换是否会引入任何开销。

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

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

#2


11  

This is because Kotlin does not work like Java in widening numbers.

这是因为Kotlin在扩大数字方面不像Java那样工作。

There are no implicit widening conversions for numbers in Kotlin. for example, you can write something in Java as below:

Kotlin中的数字没有隐含的扩大转换。例如,你可以用Java编写一些东西,如下所示:

int a = 1;
double b = a;

However, you can't write something in Kotlin. for example:

但是,你不能在Kotlin写东西。例如:

val a:Int = 1  
//             v--- can't be widening
val b:Double = a

This is because everything in Kotlin is object, there is no primitive types, so you should convert an Int to a Double explicitly, for example:

这是因为Kotlin中的所有东西都是对象,没有原始类型,所以你应该明确地将Int转换为Double,例如:

val a:Int = 1  
//               v--- convert it explicitly by `toDouble()` method
val b:Double = a.toDouble()