- 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.
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- TypeScript Blueprints
- SoapUI Cookbook
- 程序員面試筆試寶典
- MongoDB for Java Developers
- Ray分布式機器學習:利用Ray進行大模型的數據處理、訓練、推理和部署
- Mastering Swift 2
- Symfony2 Essentials
- Apache Mahout Clustering Designs
- Getting Started with Laravel 4
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- Oracle 18c 必須掌握的新特性:管理與實戰
- Python之光:Python編程入門與實戰
- 利用Python進行數據分析
- Natural Language Processing with Java and LingPipe Cookbook