Android-Daily-Interview icon indicating copy to clipboard operation
Android-Daily-Interview copied to clipboard

2019-12-23:Kotlin中的数据类型有隐式转换吗?为什么?

Open Moosphan opened this issue 5 years ago • 4 comments

Moosphan avatar Dec 23 '19 00:12 Moosphan

个人理解,kotlin中没有所谓的'基本类型',本质上不存在拆装箱过程,所有的基本类型都可以想象成Java中的包装类型,所以也不存在隐式转换,对应的都是强类型,一旦声明之后,所有转换都是显示转换。

1126174422 avatar Dec 23 '19 08:12 1126174422

kotlin 略懂皮毛

zhangandroid avatar Dec 31 '19 13:12 zhangandroid

有,如下,两个Byte类型相加得到一个int val b: Byte = 12 var c = b + b println(c.getClassname()) //class java.lang.Integer

renzhenming avatar Aug 01 '22 09:08 renzhenming

应该是有的。例如:val value = "this value is " + 6

6 是整型,会被转换为,”6“,加上去的。而这种代码在python上会直接报错。python 就需要处理为:str(6)

yline avatar Sep 02 '23 04:09 yline