一、标识符
- 区分大小写:
myname与myName是两个变量。
- 首字符:
可以是下划线(_)或字母,但不能是数字。
- 除首字符外的其他字符:
可以是下划线(_)、字母和数字。
- 硬关键字不能作为标识符,软关键字、修饰符关键字 在它们的适应场景之外可以作为标识符使用。但尽可能避免使用软关键字、修饰符关键字做标识符。
- 特定标识符field和it,可在它们适应场景之外作为标识符使用。但尽可能避免使用field和it作为做标识符。
field标识符用于属性访问器中,访问属性支持字段。
it标识符用于Lambda表达式中,在省略参数列表时作为隐式参数,即不需要声明就可以使用的参数。
注意: 如果一定使用关键字作为标识符,可以在关键字前后增加反引号(`)。另外,kotlin语言中字母采用的是双字节Unicode编码,这就意味着可以使用中文、日文、韩文作为标识符了。
二、关键字
- 硬关键字
as、as?、break、class、continue、do、else、false、for、fun、if、in、!in、interface、is、!is、null、object、package、return、super、this、throw、true、try、typealias、val、var、when和while。
- 软关键字
by、catch、constructor、delegate、dynamic、field、file、finally、get、import、init、param、property、receiver、set、setparam和where
- 修饰符关键字
abstract、annotation、companion、const、crossinline、data、enum、external、final、infix、inner、internal、lateinit、noinline、open、operator、out、override、private、protected、public、reified、sealed、suspend、tailrec和vararg
网友评论