What does ?: do in Kotlin? (Elvis Operator)

后端 未结 8 870
青春惊慌失措
青春惊慌失措 2020-11-29 01:15

I can\'t figure out what ?: does in for example this case

val list = mutableList ?: mutableListOf() 

and why can it be modifi

8条回答
  •  甜味超标
    2020-11-29 01:34

    Simply we can say that, you have two hands. You want to know, is your left hand working right now?. If left hand not working, return empty else busy

    Example for Java:

    private int a;
    if(a != null){
        println("a is not null, Value is: "+a)
    }
    else{
        println("a is null")
    }
    

    Example for Kotlin:

    val a : Int = 5
    val l : Int = if (a != null) a.length else "a is null"
    

提交回复
热议问题