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

Yazar, bilişim ve teknoloji alanında derlediği faydalı içerikleri bu blogta paylaşmaktadır.