- 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.
- The Complete Rust Programming Reference Guide
- 計算機圖形學(xué)編程(使用OpenGL和C++)(第2版)
- Visual Basic編程:從基礎(chǔ)到實踐(第2版)
- Java 9 Programming Blueprints
- Servlet/JSP深入詳解
- C語言程序設(shè)計同步訓(xùn)練與上機指導(dǎo)(第三版)
- Building Serverless Applications with Python
- Unity 2D Game Development Cookbook
- C專家編程
- Struts 2.x權(quán)威指南
- QPanda量子計算編程
- Hadoop大數(shù)據(jù)分析技術(shù)
- MongoDB Cookbook(Second Edition)
- 計算機應(yīng)用基礎(chǔ)案例教程(第二版)
- 編程風(fēng)格:程序設(shè)計與系統(tǒng)構(gòu)建的藝術(shù)(原書第2版)