Sobes.tech
Junior — Middle

Can you explain what an Elvis operator is in programming?

sobes.tech AI

Answer from AI

Elvis operator is a shorthand syntax for null checking and providing a default value. It looks like ?: and is used to return the left operand if it is not null, or the right operand if the left is null.

Example in Kotlin:

val name: String? = null
val displayName = name ?: "Guest"
println(displayName) // Will print "Guest"

Here, if name is null, displayName will be set to the string "Guest".

Can you explain what an Elvis operator is in… - sobes.tech