IntArray vs Array<Int> in Kotlin

Kotlin

Kotlin Problem Overview


I'm not sure what the difference between an IntArray and an Array<Int> is in Kotlin and why I can't used them interchangeably:

missmatch

I know that IntArray translates to int[] when targeting the JVM, but what does Array<Int> translate to?

Also, you can also have String[] or YourObject[]. Why Kotlin has classes of the type {primitive}Array when pretty much anything can be arranged into an array, not only primitives.

Kotlin Solutions


Solution 1 - Kotlin

Array<Int> is an Integer[] under the hood, while IntArray is an int[]. That's it.

This means that when you put an Int in an Array<Int>, it will always be boxed (specifically, with an Integer.valueOf() call). In the case of IntArray, no boxing will occur, because it translates to a Java primitive array.


Other than the possible performance implications of the above, there's also convenience to consider. Primitive arrays can be left uninitialized and they will have default 0 values at all indexes. This is why IntArray and the rest of the primitive arrays have constructors that only take a size parameter:

val arr = IntArray(10)
println(arr.joinToString()) // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

In contrast, Array<T> doesn't have a constructor that only takes a size parameter: it needs valid, non-null T instances at all indexes to be in a valid state after creation. For Number types, this could be a default 0, but there's no way to create default instances of an arbitrary type T.

So when creating an Array<Int>, you can either use the constructor that takes an initializer function as well:

val arr = Array<Int>(10) { index -> 0 }  // full, verbose syntax
val arr = Array(10) { 0 }                // concise version

Or create an Array<Int?> to avoid having to initialize every value, but then you'll be later forced to deal with possible null values every time you read from the array.

val arr = arrayOfNulls<Int>(10)

Solution 2 - Kotlin

It is worth noting that using the spread (*) operator on a vararg will return an IntArray. If you need an Array<Int>, you can convert your IntArray using .toTypedArray().

Solution 3 - Kotlin

Arrays in Kotlin are classes (not "special" types like Java).

Kotlin's stdlib provides special purpose classes for JVM primitive arrays in order to improve Java language integration and performance.

The rule of thumb would be to use Array<T> except if it cause a problem when mixing with existing Java code, or should be called from Java classes. For the record, I never had to use IntArray.

You can check the Language's documentation regarding this matter here: https://kotlinlang.org/docs/reference/basic-types.html#arrays

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfrankelotView Question on Stackoverflow
Solution 1 - Kotlinzsmb13View Answer on Stackoverflow
Solution 2 - KotlinEran BoudjnahView Answer on Stackoverflow
Solution 3 - KotlinjaguilillaView Answer on Stackoverflow