Kotlin Kotlin for Java Developers IF, TRY and others are expressions, not statements


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 {
catch (ex : Exception)