Here's an example of this, using their constructors: val intArray = IntArray(10) They are also easier to create - an Array requires a non-null value for each of its indexes, while IntArray initializes them automatically to 0 values. Primitive arrays are more performant, as they don't require boxing for every element. These can be created with their own (also non-generic, like the class itself) intArrayOf factory method: val intArray: IntArray = intArrayOf(1, 2, 3, 4, 5) (There are similarly named classes for the other primitive types as well, such as ByteArray, CharArray, etc.) IntArray is a special class that lets you use a primitive array instead, i.e. This is what you get when you use the generic arrayOf method to create an array: val arrayOfInts: Array = arrayOf(1, 2, 3, 4, 5) When using this with the Int type parameter, what you end up in the bytecode is an Integer instance, in Java parlance. ![]() Array vs IntArray What's the difference between Array and IntArray?Īrray uses the generic Array class, which can store a fixed number of elements for any T type. ![]() The best way I found is to have a look at some of the most frequently asked questions about Kotlin on StackOverflow. I am currently defending the third place on the top users list of the Kotlin tag on StackOverflow, and I wanted to make use of the bragging rights this gives me while I can. This content was originally published as a series of articles on (with some runnable code snippets!).
0 Comments
Leave a Reply. |