1. Overview
In this quick article, we’re going to see the difference between IntArray and Array
For the impatient, Kotlin compiles the Array
Now, let’s dive into details!
2. Different Array Representations
When targeting the JVM, the Kotlin compiler will represent its arrays as JVM arrays. At the language level, Kotlin provides two sets of arrays:
- The Array
type, in which the type parameter can be any Kotlin type, such as Array or Array - Specialized primitive arrays, such as IntArray
We have these two forms mainly because the JVM can create and manipulate arrays in two ways:
- The newarray opcode creates an array of primitive types (int, for example) and manipulates it with *astore (iastore for an array of int) and *aload opcodes
- The anewarray opcode creates an array of reference types and manipulates it with aastore and aaload opcodes
The specialized primitive opcodes allow the JVM to optimize the creation and manipulation of primitive arrays.
Now that we know a little more about theory, let’s compare the bytecode representations of these two types of arrays.
3. The Array Bytecode Representation
In order to see the difference in action, let’s consider a simple snippet:
val arrayOfInts = arrayOf<Int>(42)
Here, the inferred type is, of course, Array
>> kotlinc Arrays.kt
Now, we can use the javap tool to check out the generated bytecode. Anyway, this is how JVM creates the array:
>> javap -c -p ArraysKt
0: iconst_1
1: anewarray #8 // class java/lang/Integer
**So, the Array
In order to set the first array element to 42, the JVM does a conversion:
6: iconst_0
7: bipush 42
9: invokestatic #12 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
12: aastore
The JVM converts the literal 42 to an Integer instance by calling the Integer.valueOf(int) static method. So, every time we add something to an Array
4. The IntArray Bytecode Representation
Let’s create an IntArray in Kotlin and initialize it with one element:
val intArray = intArrayOf(42)
After compilation, we can see that the JVM creates this array like:
15: iconst_1
16: newarray int
As shown above, the JVM uses the specialized newarray instruction to create an array of primitive int values. Therefore, the IntArray has translated to an int[] at the bytecode level.
As we might expect, loading and storing into such arrays are implemented without any unnecessary boxing or unboxing:
20: iconst_0
21: bipush 42
23: iastore
Here, the JVM stores the literal 42 as-is into the array as the first element without any boxing!
5. Conclusion
In this article, we learned how primitive arrays and their corresponding Array