Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Sometimes, we want to determine a variable’s concrete type. For example, we need the type information when we receive some value in raw type from another system.

Usually, there are two common scenarios for determining a variable’s type. One case is checking the object to see if it’s of our expected type and performing some operations. The other scenario is to obtain the name of a variable’s type as a string.

In this tutorial, we’ll explore how to determine a variable’s type in Kotlin. Of course, we’ll cover both cases mentioned above.

2. Checking a Variable’s Type

We’ll address how to check a variable’s type through an example.

2.1. An Example: the Player Class

First, let’s create some classes:

package com.baeldung.typeOfVariable

open class Person(val name: String, val age: Int)

interface Ranking

class Player(name: String, age: Int, val numberOfWins: Int) : Person(name, age), Ranking

As the code above shows, in the com.baeldung.typeOfVariable package, we’ve defined a Person class, a Ranking interface, and a Player class. Moreover, the Player class inherits the Person class and implements the Ranking interface.

Next, let’s create some variables:

private val myInt: Any = 42
private val myString: Any = "I am a string"
private val myPlayer: Any = Player("Jackson", 42, 100)

As we can see, we’ve created three variables: one Int, one String, and one Player. We should note that we’ve declared the three variables using the same type: Any. It’s worth mentioning that Kotlin’s Any is pretty similar to Java’s Object, which means it’s the supertype of all other classes. In other words, we’ve declared the three variables in the raw type.

For simplicity, we’ll use unit test assertions to verify if we get the expected type. Therefore, let’s create a simple enum type to make the verification easier:

enum class VariableType {
    INT, STRING, PLAYER, UNKNOWN
}

Next, let’s see how to check a variable’s type.

2.2. The is Operator

We can use the is operator to check if a variable is of the expected type: var is ExpectedType.

So next, let’s use the is operator in an extension function of the Any class to check the variable’s type and return the corresponding VariableType enum instance:

fun Any.getType(): VariableType {
    return when (this) {
        is Int -> INT
        is String -> STRING
        is Player -> PLAYER
        else -> UNKNOWN
    }
}

Now, let’s write a test to verify if it can give us the expected type:

assertTrue { INT == myInt.getType() }
assertTrue { STRING == myString.getType() }
assertTrue { PLAYER == myPlayer.getType() }

If we run the test, it passes.

It’s worth mentioning that, as our Player class is a subtype of Person and Ranking, if we apply the is operator on the Player instance and the two supertypes, we’ll get true as well:

assertTrue { myPlayer is Person }
assertTrue { myPlayer is Ranking }

Therefore, the is operator is the way to go if we want to check if an object is an expected type. However, sometimes, we don’t have an expected type to check. Instead, we need to get the name of an object’s type.

Next, let’s see how to achieve it.

3. Getting a Variable’s Type Name

We can get a type name from the object’s KClass object or its Class object. Next, let’s see them in action.

3.1. Kotlin Type Name

KClass defines the simpleName and qualifiedName attributes to carry the type name of an object.

One straightforward way to get the KClass instance from a Kotlin object is using the class reference, myObject::class.

So next, let’s verify if class references can report the expected type names:

assertTrue { "String" == myString::class.simpleName }
assertTrue { "Int" == myInt::class.simpleName }
assertTrue { "Player" == myPlayer::class.simpleName }

The test passes if we give it a run. If we need the type’s qualified name instead of the simple name, we can read the KClass.qualifiedName field:

assertTrue { "kotlin.String" == myString::class.qualifiedName }
assertTrue { "kotlin.Int" == myInt::class.qualifiedName }
assertTrue { "com.baeldung.typeOfVariable.Player" == myPlayer::class.qualifiedName }

3.2. Java Type Name

Alternatively, we can get the type name from the corresponding Java Class object.

So, the key is how to get the Class object in Kotlin. We can obtain an object’s Class object through myObject::class.java in Kotlin

Next, let’s create a test to check if we can get the expected type name via the Class object:

assertTrue { "java.lang.String" == myString::class.java.typeName }
assertTrue { "java.lang.Integer" == myInt::class.java.typeName }
assertTrue { "com.baeldung.typeOfVariable.Player" == myPlayer::class.java.typeName }

If we execute the test, it passes.

4. Conclusion

In this article, we’ve learned how to get the type name of a variable in Kotlin.

As always, the complete source code used in the article can be found over on GitHub.

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!