Over a million developers have joined DZone.

Learning Kotlin: Smart Casts

DZone's Guide to

Learning Kotlin: Smart Casts

Learn more about Kotlin and Smart Casts in this tutorial on using Smart Casts and the when method for easier-to-read code.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

The goal of this Koan is to show how smart the Kotlin compiler is. When you use something like the is keyword to handle type checking, the compiler will know the type later on and be able to use it intelligently.

So, if we want to check types in Java, we would use something like this where we would use instanceof to check the type and then cast it to the right type.

public class JavaCode8 extends JavaCode {
public int eval(Expr expr) {
if (expr instanceof Num) {
return ((Num) expr).getValue();
if (expr instanceof Sum) {
Sum sum = (Sum) expr;
return eval(sum.getLeft()) + eval(sum.getRight());
throw new IllegalArgumentException("Unknown expression");

In Kotlin, we check the type of compiler handles being cast for us, but, before we get to that, we also need to learn about the when   Kotlin form of the Switch keyword in C# or Java. It offers similar functionality, as shown in this example:

when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")

It also supports multiple values on the same branch:

when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")

Where it gets awesome is the extra actions it supports. For example, when values can be functions, not just constants:

when (x) {
parseInt(s) -> print("s encodes x")
else -> print("s does not encode x")

You can also use in or !in to check values in a range/collection:

when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")

It really is very cool, so let us see how we use Smart Casts and when together and how it compares with the Java code above:

fun eval(e: Expr): Int =
when (e) {
is Num -> e.value
is Sum -> eval(e.left) + eval(e.right)

Really nice and, I think, more readable than the Java code!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

java ,Kotlin ,tutorial ,Smart Casts ,when

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}