Kotlin null safety tutorial in hindi

kotlin

#1

Hello Friends,

In this tutorial, we will learn about null safety, as kotlin’s type system is aimed at eliminating the danger of null references from code.

Code of this tutorial:

fun main(args: Array<String>) {

    //Non null variable
    var name: String = "Anand"

    //Variable can have null values
    var name1: String? = null

    //Null safe check, It will not call
    //length method if value is null
    //else it will print length of name1
    print(name1?.length)

    try {
        //Not null safe variable, So if
        //value will be null, It will raise
        //Null pointer exception, to handel it
        //I have used  try catch block
        print(name1!!.length)
    } catch(e: Exception) {
        print(e)
    }
}