Part 02 / Topic 4 of 8
Expressions versus statements
A statement does something but has no value of its own; an expression evaluates to a
value you can use. A val/var declaration is a statement — you can't write val x = (val y = 5).
What sits on the right of =, though, is almost always an expression in Kotlin, including a call
that looks like it's "just printing something":
val printResult: Unit = println("This call is itself an expression of type Unit")
println(printResult)
This call is itself an expression of type Unit
kotlin.Unit
println(...) really does return a value — it's just of type Unit, Kotlin's "nothing meaningful
to return" type (closer to Java's void, except Unit is an actual type you can name and store,
not a keyword meaning "no type"). You will almost never write val x: Unit = ... in real code;
seeing that it compiles and prints kotlin.Unit is what proves the "almost everything is an
expression" idea instead of just asserting it. Control-flow expressions like if/when returning
a value are Part 4's topic, once there's an actual branch to return from.
Full program: examples/04_operators.kt.
Done here? The practice set for Part 2 — including the solutions — is on the part page.