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[]…

--

--

Matthew Dolan

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