πΉ Null Safety in Kotlin
Dodging the Null Pointer Exception, the Kotlin wayπ€π€
β€ Concept of Strict Null Safety in Kotlin
β’ Safe Call Operator ( ?. ) in Kotlin
β’ Elvis Operator ( ?: ) in Kotlin
β’ Not Null Assertion ( !! ) in Kotlin.
- Kotlin allows to store βNullβ values in a variable.
- In Kotlin, the variables can be made of Null types by just adding a β?β symbol to the right side of the data type.
- A nullable type variable means it can also hold βNullβ as a value in it.
- A non-nullable type variable means that it can not hold βNullβ as a value in it.
- The safe call operator is simply a question mark followed by a dot ( ?. )
- The safe call operator in Kotlin, is one of the methods to impose βNull Safety Checksβ on the nullable variables.
- If the left-hand side of the operator is null, then it will return null value.
- If the left-hand side of the operator is not null, then it will return the result of the right-hand side expression.
- The elvis operator is represented by a question mark followed by a colon ( ?: )
- The elvis operator in Kotlin, is one of the methods to impose βNull Safety Checksβ on the nullable variables.
- If the first operand is not null, then this operand will be returned by the elvis operator.
- If the first operand is null, then second operand will be returned by the elvis operator.
- The Not Null Assertion is represented by a double exclamation mark (!!)
- The Not Null Assertion in Kotlin, is another tool to deal with nullity.
- This operator explicitly casts nullable variables to non-nullable variables.




Top comments (0)