Statements versus expressions
Kotlin utilizes expressions more widely than Java, so it is important to know the difference between a statement and an expression. A program is basically a sequence of statements and expressions. An expression produces a value, which can be used as part of another expression, variable assignment, or function parameter. An expression is a sequence of one or more operands (data that is manipulated) and zero or more operators (a token that represents a specific operation) that can be evaluated to a single value:
Let's review some examples of expressions from Kotlin:
Expression (produces a value) | Assigned value | Expression of type |
|
| Boolean |
|
| String |
|
| Integer |
| Value returned by the | Integer |
Statements, on the other hand, perform an action and cannot be assigned to a variable, because they simply don't have a value. Statements can contain language keywords that are used to define classes (class...