Not Null Assertion(!!) Operator in Kotlin
Definition
- This operator converts the type of a boxed variable to not-null type.
- This is represented by "!!"
- When the asserted variable holds a null value, Null Pointer Exception occurs.
Syntax
//"value" variable is asserted to a not null value.
val variable_name = value!!.length
Not Null Assertion Operator(!!) Example Program
//Not Null Assertion Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
//Declaring boxed String
var name: String? = "Little Drops"
//Printing the length of the asserted value
println("Length of $name is ${name!!.length}")
//Setting null to the name variable
name = null
//This statement will create Null Pointer Exception
println("Length of $name is ${name!!.length}")
}
Sample Output
Length of Little Drops is 12
Exception in thread "main" kotlin.KotlinNullPointerException
at NotNullAssertionOperatorKt.main(NotNullAssertionOperator.kt:20)
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
- Elvis Operator (?:) in Kotlin
- Logical Operators in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin