Thursday 23 April 2020

Kotlin : Null Safety Operators (Post 20)

Hi ,

I am here again to share another important topic of Kotlin , which is  null safety . In Kotlin , it is defined to avoid NullPointerException .
There are following types of Null Safe Operators which can be used in our code to avoid NPE :


  • Safe Call (?.)
  • Safe Call with let ( ?.let)  
  • Elvis Operator (?:)
  • Non-null Assertion Operator (!!)

Safe Call (?.) 


It can be used when null value will not affect the programming flow . 
Variable will return the value if not null else return null value. 

var name : String? = null
var name2 : String = "Hello"
        
println("Length of NAME:"+ name?.length) //using safe call operator for nullable
println("Length of NAME2:"+ name2.length)

Output :


Length of NAME:null
Length of NAME2:5

Here we can see that Name2 length is 5 , but name is having null value so length is also returning null.

======================================================================

Safe Call with let ( ?.let)  

Executes the let block only when , if the variable value is NOT NULL

var name : String? = null
var name2 : String = "Hello"

name?.let {  println("Length of NAME:"+ name?.length) }

name2.let {   println("Length of NAME2:"+ name2.length) }

Output :


Length of NAME2:5

Since name is having null value so for that let block will not execute , so it will not print the statement , only name2 , let statement will execute and show the output.

========================================================================

The Elvis Operator 

It is represented by a question mark followed by a colon: ?: and it can be used with this syntax:
first operand ?: second operand
If first operand isn't null, then it will be returned. If it is null, then the second operand will be returned. This can be used to guarantee that an expression won't return a null value, as you'll provide a non-nullable value if the provided value is null.

var name: String? = null
        
val length = name?.length ?: -1
println("Length of NAME: " + length)

Output :


Length of NAME: -1

Here value of name is null so last statement is executed !

========================================================================

Non-null Assertion Operator(!!) 

This can be used when it is sure that variable value must not be NULL , it will throw NullPointerException , when variable is found Null .

var name : String? = null
var name2 : String = "Hello"

println("Length of NAME2:"+ name2!!.length)
println("Length of NAME:"+ name!!.length) //this will throw NPE
       

Output :

Length of NAME2:5

Unable to start activity ComponentInfo: kotlin.KotlinNullPointerException

As operator explains that throw NPE in case variable will contain null value , so when getting the length of name variable , which is having null value , it is throwing NullPointerException.

So this is the basic practice of how Null Safety has been handled in Kotlin . I will share another post in this series very soon , till then Happy Coding πŸ˜€πŸ˜€









No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...