Use of Boolean? in if expression

前端 未结 11 1898
陌清茗
陌清茗 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条回答
  •  猫巷女王i
    2020-12-14 00:11

    For Kotlin what i normally use is

    if (object?.booleanProperty ==true)
       { 
         //do stuff 
       }
    

    this would only work when the property is true and the object is not null. For the reverse:

    if (!object?booleanProperty !=true)
       { 
         //do Stuff
       }
    

提交回复
热议问题