
上QQ阅读APP看书,第一时间看更新
Arrays in Kotlin are invariant
Arrays in Kotlin are not built on native types, but are instead based on a Java array. Although these are similar, they do behave slightly differently. In Java, we can assign an array of a type to an array of its parent type. Arrays in Kotlin are invariant, which means that an array of a specific type cannot be assigned to an array of its parent type. It is not possible to assign Array<Integer> to Array<Any>. This provides implicit type safety and prevents possible runtime errors in the application. Kotlin also provides specialized classes to create arrays of primitive data types, including ByteArray, ShortArray, and IntArray.