Kotlin判断null比较let布尔值Boolean
class MyData {val count: Int? = 2023val number: Int? = null
}fun main(args: Array<String>) {val data = MyData()val year = 2022if (data.count != null) {if (data.count > year) {println("data.count != null")}}if (data.count!! > year) {println("data.count!!")}var b1 = data.count?.let { it > 2022 } ?: falseprintln(b1)var b2 = data.number?.let { it > 2022 } ?: falseprintln(b2)var b3 = data.count?.let { it > 9999 } ?: falseprintln(b3)
}
data.count != null
data.count!!
true
false
false
kotlin,null let expression_zhangphil的博客-CSDN博客当a不为null时,执行大括号里的语句(it肯定不为null)如果a == null ,则为 null。= null,则a.b()如果 a 不是 b,则为 null。当a==null时,执行b。当a不为null时,执行大括号里的语句(it肯定不为null)如果a == null ,则为 null。= null,则a.b()如果 a 不是 b,则为 null。当a==null时,执行b。kotlin的let,with,run,apply,also,异同区别_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/130538081