Safe Call Operator (?.) in Kotlin
Definition
- This operator helps in writing a program without NPEs (Null Pointer Exceptions) and is represented by (?.)
- This operator eliminates exception and returns the response as null when a null value is used for any operation.
Syntax
//When the nullable_value is null, the block inside the let will not be executed.
var result = nullable_value?.let{
//Block of code
}
For example,
//While doing operations or iterating with a collection which may have null values, this operator can be used to eliminate Null Pointer Exception.
var result = value_in_collection?.let{
//"it" is a variable generated by Kotlin and it holds the particular non-null value of the collection.
println(it)
}
Safe Call Operator Example Program in Kotlin
//Safe Call Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
//List containing null values
val namesList = listOf("Sheela", null, "Leela")
println("List containing null values is : $namesList")
//Iterating the list
println("\nIterating the non-null values in list")
for (value in namesList) {
//Safe Call Operator allows only the non-null values inside the let block
value?.let {
//Printing the non-null value
println(it)
}
}
}
Sample Output
List containing null values is : [Sheela, null, Leela]
Iterating the non-null values in list
Sheela
Leela
Kotlin Operators
- Kotlin Operators Overview
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Logical Operators in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Not Null Assertion Operator in Kotlin
- Safe Call Operator (?.) in Kotlin
- Elvis Operator (?:) in Kotlin
- Range Operator (..) in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin