Use of Boolean? in if expression

前端 未结 11 1859
陌清茗
陌清茗 2020-12-13 23:14

If I have a nullable Boolean b, I can do the following comparison in Java:

Boolean b = ...;
if (b != null && b) {
   /* Do something */
         


        
11条回答
  •  时光说笑
    2020-12-14 00:08

    You can compare nullable boolean with true, false or null using equality operator:

    var b: Boolean? = null
    if (b == true) {
        // b was not null and equal true
    } 
    if (b == false) {
       // b is false 
    }
    if (b != true) { 
       // b is null or false 
    }
    

提交回复
热议问题