kotlin study notes: "and". And ": and as" and!!

Keywords: Java

? nullable type

An important difference between kotlin and Java's type system is kotlin's display support for nullable types

In other words, you can declare a variable and use nullable type? To indicate that the variable is nullable

For example:


int StrLen(String s){return s.length}
//This function is not safe because if the parameter s passed in is null, a null pointer exception will be reported


fun StrLen(s:String?):Int = s.length  //Cannot call length method directly
//1. The nullable type is used here. You can load any type to indicate that the variable of this type can be null
//2. A variable of nullable type cannot call its method directly when it is used
//3. The value of nullable type cannot be passed to non nullable type
*For example, Val X: string? = null var Y: String = x / / assigning X of nullable type to y of non nullable type will result in an error: Type mismatch
*It is also not allowed to pass a controllable type value to a function with a non empty type parameter, such as StrLen(x) to StrLen(x:String)

?. Safe call operator

fun StrLen(s:String?):Int = s.length  //Cannot call length method directly
//If null check is added, s.length can be called directly, as follows:
fun StrLen(s:String?):Int  = if(s!=null) s.length else 0
//However, if every nullable type is checked in this way, it will be very cumbersome. In this case, the safe call operator is used

s?.length Equivalent to if(s!=null) s.length else null
//Execute method if s is not empty, return null if s is empty

?: Elvis operator (null merge operator)

Use the?: operator to set the return value when the check result is empty

fun foo(s:String?){
     val t:String  =  s ?:  "" //If?: if the value on the left is not empty, return the value on the left; if it is empty, return ""

//It can be used in this way
a?. peroson?. name ?: "UnKnown" //If?: left is empty, return "UnKnown"
//Use with colleagues of throw operator
//Returns name if it is not empty, and throws a meaningful error instead of NullPointException if it is empty
val name = a?.person?.name ?: throw illegalargumentexception("UnKnown name")//If the name is null, a custom exception will be reported to prevent the following code from calling and directly reporting a null pointer exception
println(name.length)//Null pointer exception will be reported if name is null

as? Safe conversion operator

Try to convert the value to the given type and return null if the type is not suitable

foo as? Type -> foo is  Type  retrun (foo as Type)
             -> foo !is Type  return null

//as? And?: joint use
object as? Person ?: "not human"
object as? Person ?: return false

!! Non null assertions (Kotlin does not recommend non null assertions, usually we use?: to prevent the program from crashing due to null pointer exceptions when running)

Throw NullPointerException null pointer exception if value is null

var s:String = s!!  //If s is null, a null pointer exception will be thrown, and the exception will point to the line using
println(s)//If s is null, a null pointer exception will be thrown
//Use break!! to locate the variable of an exception when a null pointer exception is thrown
//But never use assertions continuously!!
//student!!.person!!.name / / if the null pointer is abnormal, you cannot judge whether the student is null or the person is null, so do not use assertions continuously!!

let function and?. are used together to handle nullable expressions

let function is called only when the left expression is not empty

person?.let{ //Internal it must be non empty}
//If person is empty, let function will not be called
//If person is not empty, let function will be called, so?. it is convenient to handle nullable expression with let function
person?.let { println(it.name) //Output name}

That's a simple summary
Reference: Kotlin practice

Posted by Third_Degree on Wed, 08 Jan 2020 07:42:36 -0800