This is the main function call.
fun main(){
val someValue = null
println(someValue ?: “This was null”)
}
i am not able to understand how the line
println(someValue?: “This was null”)
is printing This was null
This is the main function call.
fun main(){
val someValue = null
println(someValue ?: “This was null”)
}
i am not able to understand how the line
println(someValue?: “This was null”)
is printing This was null
?:
uses the right-hand value if the left-hand value is null
. In your case someValue
is null
, so the string "This was null"
is used.
See the Elvis operator documentation.
To make it easier to understand it’s basically an equivalent for Java’s:
String someValue = null;
System.out.println(someValue != null ? someValue : "This was null");