Checking data type with when() example

Estimated read time 1 min read

In this short note, you can see an example of when() usage with data type. The type of data will determine the code to run.

 val x : Any = 12.75
    when(x){
        is Int -> println("$x is an Integer")
        is Double -> println("$x is a Double")
        is String -> println("$x is a String")
        else -> println("$x is not Int,Double or String")

    }

You can see the output below.

İbrahim Korucuoğlu

The author shares useful content he has compiled in the field of informatics and technology in this blog.

+ There are no comments

Add yours