What is the difference between IntArray and Array in Kotlin?

IntArray In Kotlin

In Kotlin, IntArray is a specialized array for holding values. It is a type alias for Array<Int>, but with a specialized implementation for better performance, especially when it comes to arithmetic operations on its elements. This specialized implementation of the array provides better performance and lower memory overhead compared to a regular Array<Int>.

Code

fun main(args : Array<String>) {
// IntArray is a pre-defined type in Kotlin which is used to store elements of type Int
// Constructor of Class IntArray Initialized with value 5 and now its size is 5.
// This means that variable intArray can store 5 elements of type Int
val intArray = IntArray(5)
// We can initialize the elements of an IntArray using indices
intArray[0] = 1
intArray[1] = 2
intArray[2] = 3
intArray[3] = 4
intArray[4] = 5
// We can also initialize an IntArray using arrayOf function
val intArray2 = intArrayOf(6, 7, 8, 9, 10)
// We can access the elements of an IntArray using indices
println("The first element of intArray is: ${intArray[0]}")
println("The second element of intArray2 is: ${intArray2[1]}")
// Output:
// The first element of intArray is 1
// The second element of intArray2 is 7
}

Array In Kotlin

In Kotlin, Array is a general-purpose array class that can hold elements of any type. When working with an array of non-nullable elements, such as integers, it is recommended to use IntArray instead Array<Int> for improved performance and memory usage.

Code

fun main(args : Array<String>) {
// Array is a generic type in Kotlin which can be used to store elements of any type
// Declaring an Array of Int type with 5 elements, and initializing each element with 0
val array = Array<Int>(5) { 0 }
// We can initialize the elements of an Array using indices
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
// We can also initialize an Array using arrayOf function
val array2 = arrayOf(6, 7, 8, 9, 10)
// We can access the elements of an Array using indices
println("The first element of array is: ${array[0]}")
println("The second element of array2 is: ${array2[1]}")
// Output:
// The first element of array is 1
// The second element of array2 is 7
}

Quick comparison between IntArray and Array

In Kotlin, both IntArray and Array are used to store elements of different data types. However, there are some differences between them:

  • Type: IntArray is specifically used to store elements of type integer, whereas Array can be used to store elements of any type.

  • Initialization: IntArray can be initialized using the IntArray() constructor or the intArrayOf() function, while Array can be initialized using the Array() constructor or the arrayOf() function.

  • Performance: IntArray is faster and more efficient than Array, as it is specifically designed to store elements of type integer, and has a better performance compared to Array.

  • Syntax: IntArray uses a different syntax for accessing elements compared to Array. To access an element in an IntArray, we use indices, while in Array, we use the get() method.

IntArray and Array are both used to store elements in Kotlin, but IntArray is faster and more efficient for storing elements of type integers, while Array can be used for storing elements of any type.

Free Resources