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

后端 未结 8 858
青春惊慌失措
青春惊慌失措 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:28

    Basically, if the left side of Elvis returns null for some reason, returns the right side instead.

    i.e.

    val number: Int? = null
    println(number ?: "Number is null")
    

    So, if number is NOT null, it will print number, otherwise will print "Number is null".

提交回复
热议问题