liveBook · Manning

IntArray

As explained in Kotlin in Action, 2E

IntArray is Kotlin's specialized array type whose elements are 32-bit integers stored without boxing.

IntArray is Kotlin’s specialized array for 32-bit integers. Unlike Array<Int>, it maps to Java’s int[] and stores elements without boxing, making it the practical choice when primitive-array interoperability or storage efficiency matters. section 8.2.5

Create and initialize an IntArray

val fiveZeros = IntArray(5) val fiveZerosToo = intArrayOf(0, 0, 0, 0, 0) val squares = IntArray(5) { i -> (i + 1) * (i + 1) } println(squares.joinToString()) // 1, 4, 9, 16, 25 Use IntArray(size) for default primitive values, intArrayOf(...) for explicit contents, or the size-and-lambda constructor for calculated elements. section 8.2.5

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

Compare the size constructor with intArrayOf using two equivalent arrays of five zero values. section 8.2.5

fun main() {
   val squares = IntArray(5) { i -> (i+1) * (i+1) }
   println(squares.joinToString())
   // 1, 4, 9, 16, 25
}

See a calculated IntArray initialized with consecutive square numbers and printed as a readable sequence. section 8.2.5

Choose the form that matches the data source:

  • Convert an existing array or collection of boxed integers with toIntArray(). section 8.2.5
  • Prefer collections by default unless primitive-array representation or Java interoperability gives you a reason to use IntArray. section 8.3

How Kotlin in Action, 2E covers it

How deep it goes
Explains boxing, Java representation, three construction forms, and conversion, while recommending collections by default.
Examples
Demonstrates zero-filled arrays, explicit integer values, and square numbers generated by an initializer lambda.
What it assumes
Assumes familiarity with Kotlin arrays and collections while contrasting generic arrays with primitive representations.
What you can do after
Builds primitive integer arrays from sizes, values, initializer lambdas, or existing boxed collections.
How it connects
Connects primitive arrays with Array<Int>, boxing, Java arrays, collections, and array conversion.

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.