How to Check Kotlin Variable Type
-
Find the Type of Kotlin Variable Using the
is
Keyword -
Find the Type of Kotlin Variable Using the
qualifiedName
Keyword
Knowing the variables’ data types of your code can be extremely beneficial. For instance, it can help ensure you are working with the correct data type, using the right functions on the variable, and improving the code’s efficiency.
Java allows finding the variable data type with the instanceOf
keyword. Likewise, JavaScript has the typeOf
keyword, but what about Kotlin?
This article teaches you how you can check Kotlin variables’ types in different methods, including the following:
is
operatorqualifiedName
operator
Find the Type of Kotlin Variable Using the is
Keyword
The first and most straightforward way of finding the variable type in Kotlin is to use the is
keyword. It does not give you the exact variable type but allows checking the type.
You can use the is
keyword to determine if the variable type is the same as what you think it is. You can use this for type conversion to ensure you’re converting the right data type.
You can also use it to run a function requiring a specific data type variable.
Here’s an example: we use the is
keyword to find the variable’s data type.
fun main() {
val variable = "Hey, there!"
if (variable is String) {
println("The variable is of a String type")
} else {
println("The variable is not of a String type")
}
}
We can also check the type of variables of an array by using the is
keyword with an if-else
block. The below program accepts an array of variables of different data types.
It then uses the if-else
block to check the type of each variable.
fun main(args: Array<String>) {
var nameString = "Selena"
var ageInt = 27
var salDouble = 25000.95
val eDetails: List<Any> = listOf(nameString,ageInt,salDouble)
for(e in eDetails) {
if (e is String) {
println("First Name: $e")
} else if (e is Int) {
println("Age: $e")
} else if (e is Double) {
println("Salary: $e")
} else {
println("No such attribute found")
}
}
}
Find the Type of Kotlin Variable Using the qualifiedName
Keyword
The other way to find the Kotlin variable type is with the qualifiedName
keyword.
This keyword is a property of Kotlin’s KClass
interface. It returns the fully-qualified name of the class.
Here’s a simple program demonstrating the is
keyword. The program will accept a variable, print it, and then print the variable type.
fun main() {
val variable = "Hey, there!"
println(variable)
println(variable::class.qualifiedName)
}
As you can see in the output, the program correctly identified the variable as a String
type. Now, let’s replace the variable with an integer and see if the program still detects it.
fun main() {
val variable = 13
println(variable)
println(variable::class.qualifiedName)
}
If you want to get only the variable data type and not the fully-qualified name, you can get that by using the simpleName
instead of qualifiedName
.
The difference between the two is that the simpleName
keyword will give only the data type’s simple name and not the class’s fully-qualified name. You will understand it when you have a look at the below code.
fun main() {
val variable = 13
println(variable)
println(Int::class.simpleName)
}
Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.
LinkedIn