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.

Genie Lamp Free a silly app for android!!!

Genie Lamp Free is a funny voice recognition app where you rub the lamp and make a wish to a genie and the genie gives a reply based on what you say.  The app has over 5,000 downloads on google play store. Genie Lamp Free is where you can go to try the game. Its awesome because its free and you might get a few kicks out of it.

So go download GenieLampFree and leave a review for the developer jacs@pps on google play store.  As far as I know its the only genie in a lamp app that you can actually talk to.  They have a lot of great apps on there who knows what those guys are up to. The app is just for entertainment purposes and only available for android devices so I guess that’s too bad for all the apple gurus out there. Thanks for reading and go download that app. 😉

Excalibur Strength is awesome!!!

This post is about a game that is available on the Play Store and it is free with in app purchases.  The game is called Excalibur Strength and you can get it here Link to Excalibur Strength Presently the game has 4 and a half stars but I personally give it a 5.  The story behind the game is that the sword, Excalibur by it self is in the afterlife and it has to fend off all of the envious souls that couldn’t lift the sword from the stone in life and that is where Excalibur gets the strength and why it is known as one of the most powerfull weapons in the universe.  Excalibur Strength is a classic hoard game with unlimited waves. The game has 7 special moves and the monetary system is bones. You can use bones to unlock new weapons and special features.  You can unlock the whole game for a purchase of 20 dollars worth of bones, or you can just play the game until you have earned enough bones to unlock what youd like.  Personally I love the back story of the game about envious souls and the strength of Excalibur being derived from eternally battling the souls.  Its a decent game with pretty good graphics and audio.  The audio is very good, you can switch between 3 different songs to play while your playing.  So go download the game and rate it. Its only for android and it is a great all around action game. You’ll love it.

%d bloggers like this: