Kotlin Null Safety

Kotlin null safety is a procedure to eliminate the risk of null reference from the code.

Nullable Types:

Nullable types are declared by putting a ? behind the String as:
var str: String? = "Hello" // variable is declared as nullable  
str = null
print(str)

Output:
null

Non Nullable Types:

non-nullable types are normal strings that are declared as String types 

What happens when we assign a null value to a non-nullable string?
var str: String = "Hello"  
str = null // compile error
print(str)
Output:
It will generate a compile time error.

Checking for null in conditions:

Kotlin's If the expression is used for checking the condition and returns a value.

var str: String? = "Hello"     // variable is declared as nullable  
var len = if(str!=null) str.length else -1
println("str is : $str")
println("str length is : $len")

str = null
println("str is : $str")
len = if(str!=null) str.length else -1
println("b length is : $len")

Output:
str is : Hello
str length is : 5
str is : null
b length is : -1


Smart cast

To use these nullable types we have an option to use smart casts. Smart cast is a feature in which the Kotlin compiler tracks conditions inside if expression. If the compiler founds a variable is not null of type nullable then the compiler will allow accessing the variable.

When we try to access a nullable type of String without a safe cast it will generate a compile error.

var string: String? = "Hello!"

print(string.length) // Compile error

if(string != null) { // smart cast
print(string.length) // It works now!
}
While using is or !is for checking the variable, the compiler tracks this information and internally cast the variable to the target type. This is done inside the scope if is or !is returns true.

Use of is for smart cast
val obj: Any = "The variable obj is automatically cast to a String in this scope"  
if(obj is String) {
// No Explicit Casting needed.
println("String length is ${obj.length}") //String length is 64
}
Use of !is for smart cast
val obj: Any = "The variable obj is automatically cast to a String in this scope"  
if(obj !is String) {
println("obj is not string")
}
else
// No Explicit Casting needed.
println("String length is ${obj.length}") //String length is 64
}

Safe cast operator: as?

Kotlin provides a safe cast operator as? for safely casting to a type. It returns a null if the casting is not possible rather than throwing a ClassCastException exception.
val location: Any = "Kotlin"  

val safeString: String? = location as? String
val safeInt: Int? = location as? Int
println(safeString)
println(safeInt)

Output:
Kotlin
null

Elvis Operator (?:)

Elvis operator (?:) is used to return the not null value even if the conditional expression is null. It is also used to check the null safety of values.

var str: String? = null  
var str2: String? = "May be declare nullable string"

//var len1: Int = if (str != null) str.length else -1
var len1: Int = str ?.length ?: -1 // Elvis Operator

//var len2: Int = if (str2 != null) str2.length else -1
var len2: Int = str2 ?.length ?: -1 // Elvis Operator

println("Length of str is ${len1}")
println("Length of str2 is ${len2}")

Output:

Length of str is -1
Length of str2 is 30



Comments