If you need to see the data type (or class) of an instance in the Kotlin REPL, you can use the javaClass
method to see that type. Here are a few examples:
>>> val map = mapOf( ... "A" to 1, ... "B" to 2, ... "C" to 3 ... ) >>> map.javaClass class java.util.LinkedHashMap >>> listOf(1,2,3).javaClass class java.util.Arrays$ArrayList
As shown, mapOf
creates a LinkedHashMap
, and listOf
creates an ArrayList
. At the time of this writing (August, 2018) you can’t see those data types in the REPL, so Kotlin’s javaClass
method is a nice way of showing them.
More Kotlin javaClass methods
As I just learned, for more complicated problems you may need to go a little deeper. I just ran into a problem where the arrayOf
and intArrayOf
functions ended up behaving differently with a Kotlin varags parameter, so I had to experiment, like this.
arrayOf
:
>>> val x = arrayOf(1,2,3) >>> println(x.javaClass.name) [Ljava.lang.Integer; >>> println(x.javaClass.kotlin) class kotlin.Array >>> println(x.javaClass.kotlin.qualifiedName) kotlin.Array
intArrayOf
:
>>> val x = intArrayOf(1,2,3) >>> println(x.javaClass.name) [I >>> println(x.javaClass.kotlin) class kotlin.IntArray >>> println(x.javaClass.kotlin.qualifiedName) kotlin.IntArray
As shown, these approaches give different results — Array
vs IntArray
— and helped me see the (disappointing) differences between arrayOf
and intArrayOf
:
println(x.javaClass.name) println(x.javaClass.kotlin) println(x.javaClass.kotlin.qualifiedName)
I use the word “disappointing” there because unfortunately the two arrays work differently with a varargs parameter in one of my functions. I assumed they would both have the type Array<Int>
, and that obviously isn’t the case.