Member-only story

Effective Kotlin: Item 28 — Prefer lists to arrays

Matthew Dolan
2 min readSep 24, 2018

--

Joshua Bloch in item 28 of Effective Java talks about why you should prefer lists to arrays by firstly highlighting some of their differences. Interestingly with Kotlin some of the differences no longer hold true because of its changes to the generic type system.

In Java, arrays are covariant meaning that where Sub is a subtype of Super an array of type Sub[] is a subtype of Super[] which can lead to an ArrayStoreException at runtime if you store a different subtype of Super in an array:

// runtime exception
Object[] objectArray = new Long[1];
objectArray[0] = "Compiles but throws an ArrayStoreException";

Arrays in Kotlin as with Java generics are invariant so you cannot perform the same cast as you can in Java — Array<Type1> is neither a subtype nor a supertype of Array<Type2> for two distinct types. Kotlin’s arrays allow the use of generics which Java doesn’t.

Of course, you can perform an unchecked, and not safe, cast with an Array such that the following code compiles in Kotlin where it wouldn’t in Java because of this use of generics:

val stringLists = Array<List<String>>(1) { emptyList() }
val
objects = stringLists as Array<List<Any>> // unchecked cast
objects[0] = listOf(42)
val s = stringLists[0][0] // runtime exception here

However, the only guaranteed valid cast with an Array is to a star-projection which will cast the get return values to Any.

--

--

Matthew Dolan
Matthew Dolan

Written by Matthew Dolan

Matt Dolan has been eating doughnuts and developing with Android since the dark days of v1.6.

No responses yet