- Kotlin for Enterprise Applications using Java EE
- Raghavendra Rao K
- 180字
- 2021-06-10 18:49:19
Expressions over statements
A statement is an element of a program that represents an action. An expression is a part of the statement that gets evaluated to a value. Statements introduce mutability. The more statements a program contains, the more mutability it will have. Mutability in code increases the chance that it is erroneous. Expressions, on the other hand, do not produce mutability. In purely functional constructs, there are no statements, there are only expressions. More expressions in a program mean less mutability and code that is more concise.
Consider the code for 5_Expressions.kts:
val number = 5
val evenOrOdd = if(number % 2 == 0) "is even number" else "is odd number"
println("$number $evenOrOdd")
The output is as follows:
In this case, the code has expressions and we are not mutating any state. We are simply assigning the result of an expression to a variable and printing it.
Similarly, try...catch is also an expression. try...catch is used for handling the exceptions. The last statement in the try block becomes the expression for the try block.
- iOS 9 Game Development Essentials
- Python自動化運維快速入門(第2版)
- PyTorch自然語言處理入門與實戰(zhàn)
- Mastering macOS Programming
- Hands-On Swift 5 Microservices Development
- JavaScript入門經典
- Swift細致入門與最佳實踐
- PHP編程基礎與實例教程
- Hands-On Nuxt.js Web Development
- 大學計算機基礎
- Java Web開發(fā)實例大全(基礎卷) (軟件工程師開發(fā)大系)
- Simulation for Data Science with R
- 大學計算機基礎實訓教程
- ROS機器人編程實戰(zhàn)
- jQuery Mobile Web Development Essentials(Second Edition)