Kotlin basic syntax
Package declaration
The beginning of the code file is generally the declaration of the package
package com.runoob.main
import java.util.*
fun test() {}
class Runoob {}
Kotlin source files do not need matching directories and packages, source files can be placed in any file directory.
In the above example, the full name of test() is com.runoob.main.test, and the full name of Runoob is com.runoob.main.Runoob
If no package is specified, the default package is the default
Import by default
There are multiple packages that will be imported into each Kotlin file by default
kotlin.*
kotlin.annotation.*
kotlin.collections.*
kotlin.comparisons.*
kotlin.io.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
Function definition
The function definition uses the keyword fun, and the parameter format is: parameter: type
fun sum(a: Int, b: Int): Int { // Int 参数,返回值 Int
return a + b
}
Expression as the body of the function, the return type is automatically inferred
fun sum(a: Int, b: Int) = a + b
public fun sum(a: Int, b: Int): Int = a + b // public 方法则必须明确写出返回类型
Functions with no return value (similar to void in Java)
fun printSum(a: Int, b: Int): Unit {
print(a + b)
}
// 如果是返回 Unit类型,则可以省略(对于public方法也是这样):
public fun printSum(a: Int, b: Int) {
print(a + b)
}
Variable length parameter function
Variable-length parameters of functions can be identified with the vararg keyword
fun vars(vararg v:Int){
for(vt in v){
print(vt)
}
}
// 测试
fun main(args: Array<String>) {
vars(1,2,3,4,5) // 输出12345
}
lambda (anonymous function)
Examples of using lambda expressions
// 测试
fun main(args: Array<String>) {
val sumLambda: (Int, Int) -> Int = {x,y -> x+y}
println(sumLambda(1,2)) // 输出 3
}
Define constants and variables
Variable definition: var keyword
var <标识符> : <类型> = <初始化值>
Definition of immutable variables: val keyword, variables that can only be assigned once (similar to final modified variables in Java)
val <标识符> : <类型> = <初始化值>
Constants and variables can have no initialization value, but they must be initialized before being referenced
The compiler supports automatic type judgment, that is, the type can not be specified in the declaration, and the compiler judges
val a: Int = 1
val b = 1 // 系统自动推断变量类型为Int
val c: Int // 如果不在声明时初始化则必须提供变量类型
c = 1 // 明确赋值
var x = 5 // 系统自动推断变量类型为Int
x += 1 // 变量可修改
Annotation
Kotlin supports single-line and multi-line comments, examples are as follows
// 这是一个单行注释
/* 这是一个多行的
块注释。 */
Unlike Java, block comments in Kotlin allow nesting
String template
$ Represents a variable name or variable value
$varName represents the variable value
${varName.fun()} represents the method return value of a variable
var a = 1
// 模板中的简单名称:
val s1 = "a is $a"
a = 2
// 模板中的任意表达式:
val s2 = "${s1.replace("is", "was")}, but now is $a"
NULL checking mechanism
Kotlin's null safety design requires null judgment processing when using parameters that are declared to be nullable. There are two ways to deal with it. Add after the field!! Throw a null exception like Java, and add it after the other field? Processing return value is null or cooperate?: short judgment processing
//类型后面加?表示可为空
var age: String? = "23"
//抛出空指针异常
val ages = age!!.toInt()
//不做处理返回 null
val ages1 = age?.toInt()
//age为空返回-1
val ages2 = age?.toInt() ?: -1
When a reference may have a null value, the corresponding type declaration must be clearly marked as nullable.
When the string content in str is not an integer, return null
fun parseInt(str: String): Int? {
// ...
}
The following example demonstrates how to use a function whose return value can be null
fun main(args: Array<String>) {
if (args.size < 2) {
print("Two integers expected")
return
}
val x = parseInt(args[0])
val y = parseInt(args[1])
// 直接使用 `x * y` 会导致错误, 因为它们可能为 null.
if (x != null && y != null) {
// 在进行过 null 值检查之后, x 和 y 的类型会被自动转换为非 null 变量
print(x * y)
}
}
Type detection and automatic type conversion
We can use the is operator to detect whether an expression is an instance of a certain type (similar to the instanceof keyword in Java)
fun getStringLength(obj: Any): Int? {
if (obj is String) {
// 做过类型判断以后,obj会被系统自动转换为String类型
return obj.length
}
//在这里还有一种方法,与Java中instanceof不同,使用!is
// if (obj !is String){
// // XXX
// }
// 这里的obj仍然是Any类型的引用
return null
}
or
fun getStringLength(obj: Any): Int? {
if (obj !is String)
return null
// 在这个分支中, `obj` 的类型会被自动转换为 `String`
return obj.length
}
Even ok
fun getStringLength(obj: Any): Int? {
// 在 `&&` 运算符的右侧, `obj` 的类型会被自动转换为 `String`
if (obj is String && obj.length > 0)
return obj.length
return null
}
Interval
The range expression is formed by the rangeTo function with the operator form .. supplemented by in and !in.
The interval is defined for any comparable type, but for integer primitive types, it has an optimized implementation. Here are some examples of using intervals
for (i in 1..4) print(i) // 输出“1234”
for (i in 4..1) print(i) // 什么都不输出
if (i in 1..10) { // 等同于 1 <= i && i <= 10
println(i)
}
// 使用 step 指定步长
for (i in 1..4 step 2) print(i) // 输出“13”
for (i in 4 downTo 1 step 2) print(i) // 输出“42”
// 使用 until 函数排除结束元素
for (i in 1 until 10) { // i in [1, 10) 排除了 10
println(i)
}