Why the below code gives error? It should print "Bonjour" as output
fun main()
{
var a:String? = null
var b:String? = "Hello"
var result1 = a?:"Gracias"
var result2 = b?:"Bonjour"
println(result2)
}
Why the below code gives error? It should print "Bonjour" as output
fun main()
{
var a:String? = null
var b:String? = "Hello"
var result1 = a?:"Gracias"
var result2 = b?:"Bonjour"
println(result2)
}
b?:"Bonjour"means, “ifbis not null, then whateverbis, but if it’s null, then"Bonjour". Sincebis not null, it evaluates to whatbis, which is"Hello".By the way, the term “error” in the programming world means the code will not compile, not that it’s giving you unexpected results when you run it.