Kotlin Nullability with extension functions and examples

Nullability and Common Extension Functions in Kotlin Explained with Code Examples

Kotlin is a statically typed programming language that was designed to be a safer alternative to Java. One of the ways it achieves this safety is through nullability, which allows developers to prevent null pointer exceptions at runtime by making them compile-time errors. In this blog post, we will explore nullability in Kotlin and common extension functions that can help you work with null values more efficiently.

Nullability in Kotlin

Nullability is a concept in programming that refers to the ability of a variable or object to hold a null value. In Kotlin, nullability is controlled with two types: nullable and non-nullable. A nullable type can hold a null value, while a non-nullable type cannot.

To make a variable or object nullable in Kotlin, you simply add a question mark after its type. For example, the following code creates a nullable String:


var nullableString: String? = null


To make a variable or object non-nullable in Kotlin, you do not add a question mark after its type. For example, the following code creates a non-nullable String:


val nonNullableString: String = “Hello, World!”


If you try to assign a null value to a non-nullable type, the compiler will give you an error:


val nonNullableString: String = null // Error: Null can not be value of a non-null type String


Common Extension Functions for Nullability

There are several extension functions in Kotlin that can help you work with null values more efficiently. Let’s look at some of the most common ones:

1. safeCall

The safeCall function allows you to execute a method or property on a nullable object without the risk of a null pointer exception. If the object is null, the function returns null.

For example, the following code uses the safeCall function to print the length of a nullable String:


val nullableString: String? = null
println(nullableString?.length) // Prints null


2. Elvis Operator

The Elvis operator allows you to assign a default value to a nullable variable or object. If the value is null, the operator returns the default value instead.

For example, the following code uses the Elvis operator to assign a default value to a nullable String:


val nullableString: String? = null
val length = nullableString?.length ?: -1
println(length) // Prints -1


3. Let Function

The let function allows you to execute a block of code if a nullable variable or object is not null. Inside the block, the variable or object is called with the it keyword.

For example, the following code uses the let function to print the length of a nullable String if it is not null:


val nullableString: String? = “Hello, World!”
nullableString?.let { println(it.length) } // Prints 13


4. Nullable Type Conversion

The toIntOrNull and toDoubleOrNull functions allow you to convert a nullable String to an Int or Double, respectively. If the String is null or cannot be parsed, the functions return null.

For example, the following code converts a nullable String to an Int using the toIntOrNull function:


val nullableString: String? = “123”
val nullableInt: Int? = nullableString?.toIntOrNull()
println(nullableInt) // Prints 123


In conclusion, nullability is an essential concept in Kotlin that helps prevent null pointer exceptions. By using common extension functions like safeCall, Elvis operator, Let function, and nullable type conversion, you can work with null values more efficiently and safely. We hope this blog post has been helpful in understanding nullability in Kotlin and its common extension functions.

Author: John Rowan

I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.

Author: John Rowan

I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.

%d bloggers like this: