Kotlin中如何区分基础类型和包装类型

陆佃

关注

阅读 143

2022-02-17


背景:

Kotlin没有区分基础类型和包装类型,而是通过初始值是否为null来区分的。如果为null,则是包装类型,否则是基础类型。

详细对照关系如下:


java写法

kotlin写法


java写法

kotlin写法

基础类型

byte byteValue;

var byteValue: Byte = 0

包装类型

Byte byteValue;

val byteValue: Byte? = null

short shortValue;

var shortValue: Short = 0

Short shortValue;

val shortValue: Short? = null

int intValue;

var intValue: Int = 0

Integer intValue;

val intValue: Int? = null

long longValue;

var longValue: Long = 0

Long longValue;

val longValue: Long? = null

float floatValue;

var floatValue: Float = 0.toFloat()

Float floatValue;

val floatValue: Float? = null

double doubleValue;

var doubleValue: Double = 0.toDouble()

Double doubleValue;

val doubleValue: Double? = null

char charValue;

var charValue: Char = ' '

Character charValue;

val charValue: Char? = null

boolean booleanValue;

var booleanValue: Boolean = false

Boolean booleanValue;

val booleanValue: Boolean? = null

Kotlin反编译代码对照:

Kotlin中如何区分基础类型和包装类型_包装类

Kotlin中如何区分基础类型和包装类型_java_02


精彩评论(0)

0 0 举报