kotlin现已正式成为Android的一级开发语言。它由第三方公司JetBrains开发,代码块被编译成class文件后在Java虚拟机上运行。
它简洁、高级、安全(几乎杜绝了空指针这个全球崩溃率最高的异常),并与java100%兼容,可以直接调用使用Java语言编写的代码,可以无缝使用Java第三方的开源库。
它有三种运行方式:IntelliJ IDEA(官方IDE)、在线运行(https://try.kotlinlang.org)、使用Android Studio(在包上右击->选择新建Kotlin File/Class->新建File)。
变量和函数
变量
Kotlin中定义一个变量的方式是:在变量前声明val(value,不可变) 或var(variable,可变)。不需要声明变量类型,行末不需要加分号。
package com.example.helloworld
fun main(){
val a=10
println("a = "+a)
}
//a = 10
函数
语法规则:
fun 函数名(变量1名:变量1类型(首字母大写) , 变量2名:变量2类型(首字母大写)....): 返回类型(首字母大写){
return ...
}
一个比较大小的函数:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(LargerNumber(15,34))
}
fun LargerNumber(a:Int ,b: Int):Int{
return max(a,b)
}
//34
可多使用语法糖(编程语言添加的某种语法,其对语言功能无影响但更加方便编程),则上面的代码可以简写为一行:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(LargerNumber(15,34))
}
fun LargerNumber(a:Int ,b: Int):Int= max(a,b)
//34
又因为Kotlin有着出色的类型推导机制,所以还可以更简单——省略返回类型:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(LargerNumber(15,34))
}
fun LargerNumber(a:Int ,b: Int)=max(a,b)
//34
程序的逻辑控制
if条件语句
几乎和别的编程语言没什么大差别,但可以不断简化:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(LargerNumber(15,34))
}
fun LargerNumber(a:Int ,b: Int):Int{
if(a>b)
return a
return b
}
package com.example.helloworld
import kotlin.math.max
fun main(){
println(LargerNumber(15,34))
}
fun LargerNumber(a:Int ,b: Int)=if(a>b){
a
}else{
b
}
package com.example.helloworld
import kotlin.math.max
fun main(){
println(LargerNumber(15,34))
}
fun LargerNumber(a:Int ,b: Int)=if(a>b) a else b
when条件语句
它类似于swich语句,但它还可以进行类型判断,一下为用法举例和不断简化的过程。
注意:when语句不能少了最后的else ->语句!
package com.example.helloworld
import kotlin.math.max
fun main(){
println(getScore("Doro"))
}
fun getScore(name:String)= when (name){
"Tom"->99
"Doro"->100
"Man"->98
else ->60
}
//100
另一种写法:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(getScore("Doro"))
}
fun getScore(name:String)= when{
name=="Tom"->99
name=="Doro"->100
name=="Man"->98
else ->60
}
//60
更高级的写法:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(getScore("Tommy"))
println(getScore("Tom"))
}
fun getScore(name:String)= when{
name.startsWith("Tom")->99
name=="Doro"->100
name=="Man"->98
else ->60
}
输出:
99
99
它还可以进行类型判断:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(checkNumber(9.5))
}
fun checkNumber(num:Number){
when(num){
is Int-> println("number is Int")
is Double->println("number is Double")
else ->println("number not support")
}
}
输出:
number is Double
kotlin.Unit
再比如:
package com.example.helloworld
import kotlin.math.max
fun main(){
println(checkNumber(10))
}
fun checkNumber(num:Number){
when(num){
is Int-> println("number is Int")
is Double->println("number is Double")
else ->println("number not support")
}
}
输出:
number is Int
kotlin.Unit
循环语句
左右都闭区间的定义:val range=0…10
左闭右开区间的定义:val range=0 until 10
循环时默认自增1,可以使用step改变增幅
降区间的定义:10 down 1(左右都闭)
package com.example.helloworld
import kotlin.math.max
fun main(){
for(i in 0..10)
print(i)
println()
for(i in 0 until 10 step 2)
print(i)
}
fun getScore(name:String)= when{
name.startsWith("Tom")->99
name=="Doro"->100
name=="Man"->98
else ->60
}
输出:
012345678910
02468