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).


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容