liveBook · Manning

Primitive array

As explained in Kotlin in Action, 2E

A primitive array is an array specialized for values of a primitive type and represented as a Java primitive array without boxing.

A primitive array stores values of one primitive type without boxing. Kotlin provides specialized types such as IntArray, ByteArray, CharArray, and BooleanArray, compiled to Java primitive arrays such as int[], byte[], char[], and boolean[]. section 8.2.5

Create and initialize an IntArray

val fiveZeros = IntArray(5) val squares = IntArray(5) { i -> (i + 1) * (i + 1) } println(squares.joinToString()) // 1, 4, 9, 16, 25 The size constructor supplies default primitive values, while the lambda constructor computes each element. The intArrayOf factory offers a third option when the values are already known. section 8.2.5

Choose a primitive array when:

  • Java interoperability requires a primitive array, or performance benefits from avoiding boxed values. section 8.2.5
  • Avoid Array<Int> when unboxed storage matters: its elements become boxed java.lang.Integer values and its Java type is Integer[]. section 8.2.5

How Kotlin in Action, 2E covers it

How deep it goes
Explains specialized types, JVM representation, boxing contrast, and three construction forms with runnable examples.
Examples
Demonstrates zero-filled arrays and lambda-initialized squares using IntArray.
What you can do after
Build it from scratch using size constructors, initialization lambdas, or specialized factory functions.
How it connects
Connects primitive arrays with arrays, boxing, collections, vararg parameters, Java interoperability, and primitive types.

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.