基础知识
定义变量(define various)
val name = "None";
var age = "25";
变量类型(various type)
| 关键字 | 类型 |
|---|
| Byte,Short,Int,Long | 整型 |
| UByte,UShort,UInt,ULong | 无符号整型 |
| Float,Double | 浮点型 |
| String | 字符串 |
| Char | 字符 |
| Boolean | 布尔类型 |
val name:String = "None";
val age:Int = "25";
val salary:Double = "3000.00";
val male:Char = '♂'
集合(Collection)
| 关键字 | 类 型 | 描述 | 创建 |
|---|
| Array | 数组 | | arrayOf(1,2,3,4) |
| List | 列表 | 只读 | listOf("triangle", "square", "circle") |
| MutableList | 列表 | 可修改 | mutableListOf("triangle", "square", "circle") |
| Set | set | 只读 | setOf("apple", "banana", "cherry", "cherry") |
| MutableSet | set | 可修改 | mutableSetOf("apple", "banana", "cherry", "cherry") |
| Map | map | 只读 | mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) |
| MutableMap | map | 可修改 | mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100) |
流程控制
判断(if,when)
val count = 3;
if(count>3){
println("count>3")
}else{
printLn("count<=3")
}
when(count){
1->println("count ==1")
2,3->print("count >1")
else ->print("count>3")
}
循环(for,while)
val numbers = 1..100;
for(a in numbers){
println(a)
}
var i = 0;
while(i<100){
print("${numbers.elementAt(i)}\t")
i++;
}
函数
fun systemPrint(str:String){
print(str)
}
fun main(){
systemPrint("Hello Kotlin")
}