IF TRY and others are expressions not statements

suggest change

In Kotlin, if, try and others are expressions (so they do return a value) rather than (void) statements.

So, for example, Kotlin does not have Java’s ternary Elvis Operator, but you can write something like this:

val i = if (someBoolean) 33 else 42

Even more unfamiliar, but equally expressive, is the try expression:

val i = try {
    Integer.parseInt(someString)
}
catch (ex : Exception)
{
    42
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents