First, air safety
1.1 Nullability
In Kotlin, the type system can distinguish between receiving a null reference (reference may be null) or not receiving (non-empty primer
use). For example, String types of conventional variables can not point to null
var name: String = leavesC
//Compile Error
//name = null
If desired variable can store a null reference, after the need to explicitly name with regards to the type of tag it:
var name: String? = leavesC
name = null
Question mark can be added to the back of any type to indicate the type of variable to store null references:??? Int, Doubld, Long, etc.
Kotlin to be empty type of explicit support to help prevent problems NullPointerException caused by abnormal, the compiler does not allow empty variables can do null checks directly call its properties
fun check(name: String?): Boolean {
// compiler does not allow null do not name a direct call to check on their properties
return name.isNotEmpty()
}
We need to explicitly check for null
fun check(name: String?): Boolean {
if (name != null) {
return name.isNotEmpty()
}
return false
}
1.2, security call operator:?.
Safety call operator:?. Allows a null check and a method call into one operation, if the value of the variable is not empty, the method or property is called, otherwise null is returned directly
For example, the following two are exactly the same wording:
fun check(name: String?) {
if (name != null) {
println(name.toUpperCase())
} else {
println(null)
}
}
fun check(name: String?) {
println(name?.toUpperCase())
}
1.3, Elvis operator:?:
Elvis operator:?: Direct return for replacing the default null value, Elvis operator takes two operands, if the first operand is not null, the result is calculating the value of the calculation result, if the first?. operand is null, the result of the operation is the second operand
For example, the following two are exactly the same wording:
fun check(name: String?) {
if (name != null) {
println(name)
} else {
println(default)
}
}
fun check(name: String?) {
println(name ?: default)
}
1.4, secure conversion operators: as?
Safety conversion operator:? As to convert the values to the specified type, if the values are not suitable for this type of null is returned
fun check(any: Any?) {
val result = any as? String
println(result ?: println(is not String))
}
1.5, non-empty assertion: !!
Non-empty assertion is used to convert the value of any non-empty type, if you do a non-empty assertions null value, an exception is thrown
fun main(args: Array) {
var name: String? = leavesC
check(name) //7
name = null
check(name) //KotlinNullPointerException
}
fun check(name: String?) {
println(name!!.length)
}
1.6, let function
let function can be used in the expression is not performed if the specified code block null
fun main(args: Array) {
var name: String? = leavesC
check(name) //leavesC
name = null
check (name) // nothing will output
}
fun check(name: String?) {
name?.let {
println(name)
}
}
1.7, the air type of expansion
Nullable type is an extension function is defined more processing null value, which may allow the call receiver to null, and a null in the function, rather than after not null then call a method to ensure its variable
For example, the following method may be normal and null pointer exception call does not occur
val name: String? = null
println(name.isNullOrEmpty()) //true
isNullOrEmpty () method signature below, you can see that this is to be empty type CharSequence? defined extension functions, methods have been dealt with methods of the caller is null,
@kotlin.internal.InlineOnly
public inline fun CharSequence?.isNullOrEmpty(): Boolean {
contract {
returns(false) implies (this@isNullOrEmpty != null)
}
return this == null || this.length == 0
}
Reproduced in: https: //juejin.im/post/5ceb95a16fb9a07eda02f5ed