Answers for "kotlin Null Safety"

3

!! in kotlin

//The not-null assertion operator
val l = b!!.length
Posted by: Guest on September-05-2020
0

elvis operator kotlin example

val l = b?.length ?: -1
Posted by: Guest on August-24-2020
0

null check in kotlin

if(a != null)  {
//do something
}
Posted by: Guest on February-08-2021
0

kotlin Null Safety

// Variable types in Kotlin don't allow the assignment of null. 
// Declare a nullable varible by adding ? at the end of its type.
var neverNull: String = "This can't be null"            
neverNull = null                                        // Error
var nullable: String? = "You can keep a null here"      
nullable = null                                         // Ok
var inferredNonNull = "The compiler assumes non-null"   
inferredNonNull = null                                  // Error
fun strLength(notNull: String): Int {                   
    return notNull.length
}
strLength(neverNull)                                    // Ok
strLength(nullable)                                     // Error
Posted by: Guest on April-21-2021

Browse Popular Code Answers by Language