https://kotlinlang.org logo
Title
f

Florian

06/28/2019, 7:09 PM
is it uncommon to use primitive array types (IntArray, BooleanArray etc) in Kotlin?
g

gildor

06/29/2019, 5:02 PM
As it was said, it's depends on your usecase, but in general because arrays are pretty limited in many aspects it's much more common to use List instead, and use arrays as performance optimization in some cases and usually do not expose it in public API
f

Florian

06/29/2019, 5:43 PM
Thank you
Leaving the primitive variations aside, is Integer[] faster than ArrayList<Integer>?
g

gildor

06/30/2019, 4:59 AM
Integer[] is boxed, non-primitive array, so ArrayList<Integer> would be very close by performance, you just pay price of additional object But int[] (or Kotlin IntArray) is better in terms of memory and allocation (get/set complexity is the same as List<Int>)
f

Florian

06/30/2019, 9:16 AM
I'm just trying to figure out what the point of Array<> is
when it's worse in pretty much every point
g

gildor

06/30/2019, 9:46 AM
Array<T> is just a platform primitive (part of memory with references to other objects), for example point of it to implement other, higher level collections, ArrayList (and many other data structures) are based on generic array
And this is cause quite rare usage of it. But it's still useful, after all you need some efficient way to keep list of references to other objects, but mostly it make sense for library authors
f

Florian

06/30/2019, 10:02 AM
I see, thanks for the explanation