Kotlin get type as string
I can't find how to get the type of a variable (or constant) as , like , with Kotlin language. How to accomplish this?String
typeof(variable)
I can't find how to get the type of a variable (or constant) as , like , with Kotlin language. How to accomplish this?String
typeof(variable)
You can use one of the methods that best suits your needs:
val obj: Double = 5.0
System.out.println(obj.javaClass.name) // double
System.out.println(obj.javaClass.kotlin) // class kotlin.Double
System.out.println(obj.javaClass.kotlin.qualifiedName) // kotlin.Double
You can fiddle with this here.
There is a simpler way using property and avoiding Kotlin prefix. simpleName
val lis = listOf(1,2,3)
lis
is from type . So one can useArrayList
println(lis.javaClass.kotlin.simpleName) // ArrayList
or, more elegantly:
println(lis::class.simpleName) // ArrayList