How to get data type in Kotlin

Viewed 1309

In javascript:

var a = 10;
console.log(typeof a); // It's return data type 'Number'

How to get data type in Kotlin?

var a:Int = 10
println(/* What is the code? */)

I just started learning Kotlin. I searched the document but could not find it.

2 Answers

You can get the class of a variable like this

var a : Int = 10
println(a::class.simpleName) // Int
// or
println(a::class.qualifiedName) // kotlin.Int

Print as a string like this

println("${a::class.simpleName}")    // "Int"
println("${a::class.qualifiedName}") // "kotlin.Int"
Related