Kotlin学习记录1: Assignments, Statements, Expressions 区别

Assignments and Expressions

Assignments are not expressions, and only expressions are allowed in this context

I use this sentence at the beginning of this article because it is indeed a first important and fundamental difference from the Java language. As a Java developer, you may not ever pay attention to the difference between assignments and expressions. However, if you do want to move forward and adapt to functional programming languages like Kotlin, figuring out their differences may become a necessity in the first place.

This article is a good reference to start with:
Kotlin programmer dictionary: Statement vs Expression

As we can see from the above example, the IDE instantly shows a compiler error when performing this operation. Actually, in Java, such operation is legal and acceptable because a value assignment is an expression in Java but not an expression in Kotlin. We will discuss it in details later. The following is a Java example:

public class TestJava {
    int c = 3;
    int d = 4;
    int e = c = d;
}

If you want to do the same thing like above in Kotlin, you should convert the body of the function to:

    var c = 3
    var d = 4
    var e = d.also { c = it }

The most significant feature for an expression is that each expression has a return value.

An expression is every part of code that returns value.

An expression in a programming language is a combination of one or more:

explicit values constants variables operators functions

the programming language interprets and computes to produce another value.

Statements and Expressions

Statements and expressions are not two exclusive concepts (they have some overlaps in some cases). It should be noticed that a standalone expression is also a statement like println(). A statement can be considered as sentences for declaring a certain state. All usages of control structures in Java are not expressions, while Kotlin allowed if, when and try to return values

Example of expressions in the Kotlin language

In Kotlin, most control structures, except for the loops (for, do, and do/while) are expressions so that you can combine control structures with other expressions.

  • 1+3 returns 4
  • sum(1,1) returns 2 every function invocation (= function calls) is an expression (in Kotlin every function returns at least Unit)
  • max(2*3,4) returns 6 an expression can contain another expression
  • if (a < b) a else b returns a or b In Kotlin, if is an expression unlike Java
  • println("Hello world") returns Unit

Example of statements in the Kotlin language

Statements are everything that make up a complete unit of execution. A statement is always a top-level element in its enclosing block and doesn’t have its own value

  • val id = 310 Variable declaration
  • var age = 19; age = 20 Variable or property assignment
  • class A { } Local class declaration (inside block { })
  • fun A() { } function declaration (inside block { })
  • if() { } control declaration (inside block { })
  • fun A() {val id = 310; val city = Shanghai } nested statements: two assignments statements, and one block statement as a whole
  • println("Hello world")
  • return if (a > b) a else b the return statement

Assignments and Statements

Assignments are expressions in Java (a = b = 1 is legal in Java) and become statements in Kotlin (a = b = 1 is illegal in Kotlin). In Kotlin, statements contain assignments (an assignment is a kind of statement) and they have a wider range than assignments. Assignments are specifically related to assigning values. Statements can also include control structures declarations, function declarations, and class declarations which are all inside curly braces enclosing blocks.

Conclusion

The following diagram sort of demonstrates the relationships among assignments, expressions, and statements in Kotlin (not exactly).


你可能感兴趣的:(Kotlin学习记录1: Assignments, Statements, Expressions 区别)