- Android Development with Kotlin
- Marcin Moskala Igor Wojda
- 306字
- 2021-07-02 18:48:37
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
a = true
true
Boolean
a = "foo" + "bar"
"foobar"
String
a = min(2, 3)
2
Integer
a = computePosition().getX()
Value returned by the getX method
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), interfaces (interface), variables (val, var), functions (fun), loop logic (break, continue) and so on. Expressions can also be treated as a statement when the value returned by the expression is ignored (do not assign a value to the variable, do not return it from a function, do not use it as part of other expressions, and so on).
Kotlin is an expression-oriented language. This means that many constructs that are statements in Java are treated as expressions in Kotlin. The first major difference is the fact that Java and Kotlin have different ways of treating control structures. In Java they are treated as statements, while in Kotlin all control structures are treated as expressions, except for loops. This means that in Kotlin we can write very concise syntax using control structures. We will see examples in upcoming sections.
- C語言程序設(shè)計(第2 版)
- 微服務(wù)設(shè)計原理與架構(gòu)
- R語言編程指南
- 你不知道的JavaScript(中卷)
- 自然語言處理Python進階
- 運用后端技術(shù)處理業(yè)務(wù)邏輯(藍橋杯軟件大賽培訓(xùn)教材-Java方向)
- Android應(yīng)用案例開發(fā)大全(第二版)
- 從零開始學(xué)Linux編程
- Illustrator CC平面設(shè)計實戰(zhàn)從入門到精通(視頻自學(xué)全彩版)
- Arduino可穿戴設(shè)備開發(fā)
- Oracle數(shù)據(jù)庫編程經(jīng)典300例
- 進入IT企業(yè)必讀的324個Java面試題
- Keil Cx51 V7.0單片機高級語言編程與μVision2應(yīng)用實踐
- Java EE項目應(yīng)用開發(fā)
- Visual C#(學(xué)習(xí)筆記)