Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does printing IntArray show an address rather than the values of the array?

Tags:

kotlin

I am a little confused as to why when I try to print out an array of any primitive type such as the below, it returns an address rather than the actual values in the array.

fun notInPlaceRevers(array : IntArray): IntArray{
  val arrayB = IntArray(5)

  for (i in 0 until array.size)
      array[i] =  array[(array.size -1)  - i]

  return arrayB
}



fun main(args: Array<String>) {
  val initialArray = intArrayOf(2,3,4,5,6)

  print("${notInPlaceRevers(initialArray)}")
}

It prints out this --> [I@3834d63f

Now I have tested and it prints out the value in each index when I do a foreach loop such as

for(element in initialArray)
  print("$element")

Does this have to do with how kotlin is like java where it passes a copy of a reference of the object, when passing an object to a function. So when printing a primitive array I get back the reference?

Also is there another simpler way to print a primitive array?

like image 315
Yony Fernandez Avatar asked Oct 08 '18 23:10

Yony Fernandez


People also ask

Why can't we directly print array in Java?

We cannot print array elements directly in Java, you need to use Arrays. toString() or Arrays. deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.


2 Answers

It prints out this --> [I@3834d63f

It is not related to Kotlin. That is how Java work.

Does this have to do with how kotlin is like java where it passes a copy of a reference of the object, when passing an object to a function. So when printing a primitive array I get back the reference?

I assume you are not running Kotlin native or Kotlin JS. It is compiled to Java byte code and run on JVM. So it follows the rules of Java.

Also is there another simpler way to print a primitive array? See Mon Vrai Nom's answer

However, I would recommend you use List instead. There is only little real benefits to use array directly, just like you use ArrayList in Java instead. Not only you do have fixed size on List, you also have many more extensions methods.

Here is an example.

fun notInPlaceRevers(list : List<Int>): List<Int> {
    return list.mapIndexed { i , _ -> list.size - 1 - i }
}

fun main(args: Array<String>) {
    val initialList = listOf(2, 3, 4, 5, 6)
    print("${notInPlaceRevers(initialList)}")
}

It prints

[4, 3, 2, 1, 0]
like image 104
Joshua Avatar answered Oct 16 '22 22:10

Joshua


Does this have to do with how kotlin is like java where it passes a copy of a reference of the object, when passing an object to a function. So when printing a primitive array I get back the reference?

It's related to the JVM platform, but not in the way you describe.

Kotlin implements arrays as Java arrays. An array is an object, so it has the toString() method. What you see is the output of the default implementation of toString present in the Object class:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

[I is the name of the special int[] class and what follows after @ is simply the return value of hashCode() in hex representation. Coincidentally, it's derived from the address at which this object was initially allocated. The address of the object may change with each GC cycle, but its hashCode() never changes because that would be broken.

Also is there another simpler way to print a primitive array?

Yes. Kotlin defines the extension function contentToString:

val array = arrayOf("apples", "oranges", "lime")
println(array.contentToString()) // [apples, oranges, lime]
like image 3
Marko Topolnik Avatar answered Oct 16 '22 21:10

Marko Topolnik