Junior — Middle
Is it possible to declare arrays as constants using the keyword const val in Kotlin?
sobes.tech AI
Answer from AI
In Kotlin, the keyword const val is used to declare compile-time constants of primitive types and strings, whose values are known at compile time. Arrays are objects, and they cannot be declared as const val.
However, you can declare an array as val, which means the reference to the array is immutable, but the contents of the array can be changed:
val arr = arrayOf(1, 2, 3)
arr[0] = 10 // allowed
If you need an immutable array, you can use List from the standard library, for example listOf(), which returns an immutable list.