arrayOf(1) vs arrayOf<Int>(1) vs intArrayOf(1) in Kotlin

Viewed 35
  • Array is an Integer[] (object) under the hood,
  • IntArray is an int[] (primitive)

Why arrayOf is printing Integer and not using int primitive

Example

var a = arrayOf(1,2,3)
var b = arrayOf<Int>(4,5,6)
var c = intArrayOf(7,8,9)

println(a) //[Ljava.lang.Integer;@5d099f62
println(b) //[Ljava.lang.Integer;@31cefde0
println(c) //[I@439f5b3d
1 Answers

There is no class hierarchy between IntArray and Array. arrayOf() can only return the Array type. If there were an overload of arrayOf() that used Ints and returned IntArray, there would be no way to create an Array<Int>/ Java Integer[]. intArrayOf() is completely non-ambiguous.

Related