Boxing represents a primitive value as an object. In Kotlin, a generic Array<Int> becomes a Java java.lang.Integer[] and stores boxed integers. section 8.2.5
Choose the array type according to interoperability and storage needs:
- Use Array<Int> when an object array is required, such as when calling Java APIs that expect object arrays. section 8.2.5
- Use IntArray to compile to a Java int[] and avoid boxing, storing values in the primitive representation. section 8.2.5
- Create primitive arrays with IntArray(size), intArrayOf(...), or an initialization lambda. section 8.2.5
Boxed versus primitive arrays
val boxed = Array<Int>(3) { it } val unboxed = IntArray(3) { it } The first declaration produces boxed integers; the second produces a primitive int[] representation. section 8.2.5
val fiveZeros = IntArray(5) val fiveZerosToo = intArrayOf(0, 0, 0, 0, 0)
Compare two concise ways to create an IntArray without boxing. section 8.2.5


