IntArray vs Array<Int> in Kotlin: what's the difference and which to choose?
Hello, colleagues! I started learning Kotlin and encountered a misunderstanding: the language has two types for working with integer arrays — IntArray and Array. At first glance they are similar, but the compiler complains when I try to mix them.
Here's an example that confused me:
```kotlin
val arr1: IntArray = intArrayOf(1, 2, 3)
val arr2: Array = arrayOf(1, 2, 3)
// Trying to assign one to another — error!
// arr1 = arr2 // Type mismatch
```
Why is that? After all, both store integers. I read that IntArray is a primitive type, and Array is a reference type, but I don't understand how this affects performance and when to use which.
I also noticed that they have different methods. For example, IntArray has sum() and average() right out of the box, while Array requires writing .map { it.toDouble() }.
No answers yet.
Log in to leave an answer