The Programmers Guide To Kotlin - The Basics |
Written by Mike James | |||||
Monday, 01 August 2022 | |||||
Page 4 of 4
Null SafetyOne of the most important features of Kotlin is that it eliminates the null reference error and you can’t meet null safety early enough. References can be either non-nullable or nullable. If you declare a variable in the usual way you get a non-nullable: var myVariable:sometype = something and you cannot set the variable to null as: myVariable = null throws a compiler error. The compiler tracks operations that could generate a null, and flags any operation that could possibly set a non-nullable variable to null. For example, if a function could return a null, you cannot assign it to a non-nullable. If you need a nullable reference then you have to explicitly declare it using ? as in: var myVariable:sometype? = something Now myVariable can be set to null, i.e.: myVariable = null works without a compiler error or warning. To avoid accidentally using a null reference the compiler will throw an error if you try to use a nullable without first checking that it is non-null. var a:Int = 1 var b = a+1 works without any problems as a can never be null as it is a non-nullable, but: var a:Int? = 1 var b = a + 1 generates a compiler error because you are adding one to a without checking that it isn't null.
To use a nullable variable you can test for null using an if statement: var a: Int? = 1 var b:Int = 1 if(a! = null){ b = a+1 } This compiles and you can be sure that a isn't null when the addition is performed. Alternatively, if you are accessing a property then you can use the safe call operator ?. For example: var b = a?.plus(1) Here, if a isn't null then b is set to 2, but if a is null then b is set to null. There are a few small things to notice about this example – by type inference b is a nullable type: var b:Int? = a?.plus(1) and nullable simple types are "boxed". That is, the integer a is a full object with methods and properties. In this case we use the plus infix function rather than an operator so that we can use the safe call operator. If you only want to perform an operation if something is non-null you can use the let method: a?.let { println(a)} the block of code in the curly brackets is only executed if a is non-null. There is also the cutely named Elvis operator ?: which will replace a null value with something that is non-null. For example: println(a?:0) will display the value of a if it is non-null and 0 otherwise. Finally, if you want to throw a null reference exception you can force one using !!. For example: println(a!!) will either print the value of a, or throw an exception if a is null. My guess is that you would only use this to test things. These null handling features make null a useful, rather than a dangerous, value. For example, if you cast something to a different type you will generate a classcastException if it isn't possible. It is much safer to use the safe cast as? which will return null if the cast isn't possible. So: variable as? type will evaluate to null if the cast to type isn't possible. The advantage of generating a null is that you now have lots of ways of working with it that are guaranteed not to end in an exception. Notice that a great deal of what makes the Kotlin null-safe is provided by the compiler, which does its best not to let you write code that works with null values that could result in an exception. If the compiler flags a null problem then don't just make the simplest fix. Try to work out what the role of the null is and whether you need it at all. Using non-nullable values is still the safest option. Summary
This article is an extract from: Programmer's Guide To Kotlin Third Edition
You can buy it from: Amazon Contents
<ASIN:B0D8H4N8SK>
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info
|
|||||
Last Updated ( Tuesday, 02 August 2022 ) |