liveBook · Manning

Primitive data type

As explained in Kotlin in Action, 2E

A Java primitive type is one of Java's built-in value types, such as int or boolean, that is represented directly rather than as an object reference.

A primitive data type stores a basic value directly rather than an object reference. Java separates primitive types such as int from reference types, but Kotlin presents both primitive-like values and wrappers through one type, such as Int. section 8.1.1 section 8.1

The same Kotlin type across contexts

Use Int for ordinary values and collections: val i: Int = 1 val list: List<Int> = listOf(1, 2, 3) Kotlin normally compiles the standalone Int to Java int. As a generic type argument, List<Int> uses boxed java.lang.Integer values because JVM generics require reference types. section 8.1.1

Kotlin also lets number values use methods. For example, coerceIn restricts progress to a valid range while keeping the code concise. section 8.1.1

The representation changes when null is possible. Int? must use a boxed Java type because Java primitives cannot hold null; nullable values therefore follow normal Kotlin null-safety checks. section 8.1.3

Nullable primitive values

data class Person(val name: String, val age: Int? = null) { fun isOlderThan(other: Person): Boolean? { if (age == null || other.age == null) return null return age > other.age } } An unknown age produces null rather than an unsafe comparison. section 8.1.3

Generic collections box primitive values. For large unboxed collections, use specialized arrays such as IntArray; Array<Int> instead stores boxed integers. section 8.1.3 section 8.2.5

val fiveZeros = IntArray(5)
val fiveZerosToo = intArrayOf(0, 0, 0, 0, 0)

Compare two concise ways to create an IntArray containing five zeros without boxing its elements. section 8.2.5

How Kotlin in Action, 2E covers it

How deep it goes
Traces Kotlin types from source syntax through JVM primitive or wrapper representations, but defers broader array details.
Examples
Uses Int, Boolean, nullable Person ages, range clamping, boxed lists, and unboxed IntArray construction.
What it assumes
Assumes basic Kotlin declarations and Java familiarity while teaching primitive-reference distinctions and boxing behavior.
What you can do after
Evaluate Kotlin declarations and choose between collections, boxed generic values, and specialized primitive arrays.
How it connects
Connects primitive data types with nullability, Java wrapper types, generics, collections, arrays, and JVM interoperability.

Also explained in

Compare how 6 books explain primitive data type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.