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
