Kotlin · 1 / 1

Basic data types

Numbers, strings, Unit, Nothing and equality

Kotlin exposes typed values and methods; JVM storage is a separate concern. A non-null Int is commonly represented as a primitive, while nullable or generic use may box it. Other Kotlin targets have different runtime representations. Numbers.

Numeric conversion

val count: Int = 5
val total: Long = count.toLong()
val fromLiteral: Long = 5
val integerResult = 7 / 2 // 3
val fractionalResult = 7 / 2.0 // 3.5

An Int variable is not implicitly assignable to Long. An integer literal can acquire its expected integer type directly. Integer division discards the fractional part. Conversions.

Special types

Unit has one value and expresses the absence of a useful result. Nothing has no values: throw never returns normally, so it works in an Elvis expression such as input ?: error("Missing name"). Unit, Nothing.

Use == for value equality. Referential equality (===) is unsuitable for numeric values: JVM boxing may reuse cached objects. Integer.valueOf guarantees caching −128..127 and permits a larger cache, so identity outside that range is not a portable assumption. Equality, Integer.valueOf.

An inline value class can use its underlying representation, but using it as a generic or interface value may box it. It does not guarantee that an entire call chain allocates nothing. Value classes.