定义包
包的声明应处于源文件顶部:
package my.demo
import java.util.*
// ……
目录与包的结构无需匹配:源代码可以在文件系统的任意位置。
参见包。
定义函数
带有两个 Int
参数、返回 Int
的函数:
sum of 3 and 5 is 8
Target platform: JVM
Running on kotlin v. 1.1.2
将表达式作为函数体、返回值类型自动推断的函数:
sum of 19 and 23 is 42
Target platform: JVM
Running on kotlin v. 1.1.2
函数返回无意义的值:
sum of -1 and 8 is 7
Target platform: JVM
Running on kotlin v. 1.1.2
Unit
返回类型可以省略:
sum of -1 and 8 is 7
Target platform: JVM
Running on kotlin v. 1.1.2
参见函数。
定义局部变量
一次赋值(只读)的局部变量:
a = 1, b = 2, c = 3
Target platform: JVM
Running on kotlin v. 1.1.2
可变变量:
x = 6
Target platform: JVM
Running on kotlin v. 1.1.2
参见属性和字段。
注释
正如 Java 和 JavaScript,Kotlin 支持行注释及块注释。
// 这是一个行注释
/* 这是一个多行的
块注释。 */
与 Java 不同的是,Kotlin 的块注释可以嵌套。
参见编写 Kotlin 代码文档 查看关于文档注释语法的信息。
使用字符串模板
a was 1, but now is 2
Target platform: JVM
Running on kotlin v. 1.1.2
参见字符串模板。
使用条件表达式
max of 0 and 42 is 42
Target platform: JVM
Running on kotlin v. 1.1.2
使用 if 作为表达式:
max of 0 and 42 is 42
Target platform: JVM
Running on kotlin v. 1.1.2
参见if 表达式。
使用可空值及 null 检测
当某个变量的值可以为 null 的时候,必须在声明处的类型后添加 ?
来标识该引用可为空。
如果 str
的内容不是数字返回 null:
fun parseInt(str: String): Int? {
// ……
}
使用返回可空值的函数:
42either 'a' or '7' is not a numbereither 'a' or 'b' is not a number
Target platform: JVM
Running on kotlin v. 1.1.2
或者
42Wrong number format in arg1: 'a'Wrong number format in arg2: 'b'
Target platform: JVM
Running on kotlin v. 1.1.2
参见空安全。
使用类型检测及自动类型转换
is 运算符检测一个表达式是否某类型的一个实例。 如果一个不可变的局部变量或属性已经判断出为某类型,那么检测后的分支中可以直接当作该类型使用,无需显式转换:
'Incomprehensibilities' string length is 21 '1000' string length is ... err, not a string '[java.lang.Object@6b884d57]' string length is ... err, not a string
Target platform: JVM
Running on kotlin v. 1.1.2
或者
'Incomprehensibilities' string length is 21 '1000' string length is ... err, not a string '[java.lang.Object@6b884d57]' string length is ... err, not a string
Target platform: JVM
Running on kotlin v. 1.1.2
甚至
'Incomprehensibilities' string length is 21 '' string length is ... err, is empty or not a string at all '1000' string length is ... err, is empty or not a string at all
Target platform: JVM
Running on kotlin v. 1.1.2
使用 for
循环
applebananakiwi
Target platform: JVM
Running on kotlin v. 1.1.2
或者
item at 0 is appleitem at 1 is bananaitem at 2 is kiwi
Target platform: JVM
Running on kotlin v. 1.1.2
参见 for 循环。
使用 while
循环
Target platform: JVM
Running on kotlin v. 1.1.2
参见 while 循环。
使用 when
表达式
Target platform: JVM
Running on kotlin v. 1.1.2
参见 when 表达式。
使用区间(range)
使用 in 运算符来检测某个数字是否在指定区间内:
fits in range
Target platform: JVM
Running on kotlin v. 1.1.2
检测某个数字是否在指定区间外:
-1 is out of rangelist size is out of valid list indices range too
Target platform: JVM
Running on kotlin v. 1.1.2
区间迭代:
12345
Target platform: JVM
Running on kotlin v. 1.1.2
或数列迭代:
135799630
Target platform: JVM
Running on kotlin v. 1.1.2
参见区间。
使用集合
对集合进行迭代:
applebananakiwi
Target platform: JVM
Running on kotlin v. 1.1.2
使用 in 运算符来判断集合内是否包含某实例:
apple is fine too
Target platform: JVM
Running on kotlin v. 1.1.2
使用 lambda 表达式来过滤(filter)和映射(map)集合:
Target platform: JVM
Running on kotlin v. 1.1.2