Elvis operator ?:
In Kotlin the Elvis operator ?: is a binary operator that returns its first operand if that operand is true, and otherwise evaluates and returns the second operand. It is not important the first operand to have the literal value true so the first operand could be returned. It just needs to be a value that is considered to true. You can say that the Elvis operator ?: is a variant of the ternary conditional operator.
val name: String? = "kronos" val nameStatus: String = name ?: "empty" println(nameStatus) val lastName: String? = null val lasmnameStatus: String = name ?: "empty" println(lastName)
not-null assertion operator !!
This operator tells the compiler that if it is null throw a null pointer exception (NPE
val name: String? = "kronos" println(name!!) val lastName: String? = null println(lastName!!)
- Kotlin – Safe-call operator for Nullable types and let
- Kotlin – Nullable and not-null types
- Kotlin – Functions Part 2