Kotlin basic grammar notes (1)

fun sum0(a:Int,b:Int) = a+b

fun sum1(a:Int,b:Int):Int = a+b

fun sum2(a:Int,b:Int):Int{
    return a+b
}

fun sum3(a:Int,b:Int):Unit{
    a+b
}

public fun sum4(a:Int,b:Int){

} // public methods must explicitly write the return type
 fun sum5 ( vararg v:Int){
     for (m in v){
         print (v)

    }
}
//lambda(anonymous function)
 //example of using lambda expression:
 fun main (args:Array<String>){
     var sumLambda: (Int , Int) -> Int = { x , y -> x+y }
 print ( sumLambda( 1 , 2 ))    

}

//Variable variable
 var m : Int = 0
 //Immutable variable is similar to final
 val s :Int = 1
 //The compiler supports automatic type judgment, that is, the type can not be specified when the declaration is made, and the compiler judges it.
val b = 1        // The system automatically infers the variable type as Int
 var x = 5         // The system automatically infers the variable type as Int
 var a = 1
 //$varName represents the variable value
 //
 //${varName.fun()} represents Variable's method return value:
 // Simple name in template:
 val s1 = "a is $ a "
 // Any expression in template:
 val s2 = " ${ s1 .




replace ( "is" , "was" ) } , but now is $ a "
 //
 // null
 // add after the field!! Throw a null exception like Java, add another field? Null or match?: Short judgment processing
 //Add ? after the type to indicate nullability
 var age : String? = "23"
 //Throw a null pointer exception
 val ages = age !!. toInt ()
 //No processing return null
 val ages1 = age ?. toInt ()
 //age returns -1 if age is null
 val ages2 = age ?. toInt () ?: - 1


// When a reference may be a null value, the corresponding type declaration must be explicitly marked as nullable.
//
 //When the string content in str is not an integer, return null:
 //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 Java instanceof keyword).
fun getStringLength (obj: Any): Int? {
     if (obj is String) {
         // After type judgment, obj will be automatically converted to String type by the system
 return obj . length
 }

            

    //There is another method here, which is different from instanceof in Java, using !is
     // if (obj !is String){
     // // XXX
     // }
     // The obj here is still a reference of type Any
 return null
 }
    

//or
 fun getStringLength2 (obj: Any): Int? {
     if (obj !is String)
         return null
 // In this branch, the type of `obj` will be automatically converted to `String`
 return obj . length
 }
 // Or even
 fun getStringLength3 (obj: Any): Int? {
     // On the right side of the `&&` operator, the type of `obj` is automatically converted to `String`
 if (obj is String && obj . length > 0 )
         return obj . length
 return null
 }
        
        

//区间
//区间表达式由具有操作符形式 .. 的 rangeTo 函数辅以 in 和 !in 形成。
//
//区间是为任何可比较类型定义的,但对于整型原生类型,它有一个优化的实现。以下是使用区间的一些示例:

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)
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324908892&siteId=291194637