This lesson is all about nothing!
It turns out that nothing can cause us some trouble!
But, because we're using Kotlin, we have a language designed around dealing with
Let's try to make sense of those statements, and also continue to practice writing
Wait: why are we suddenly talking about Java?
To understand how Kotlin handles
null, we need to take a brief digression and discuss a different programming language: Java.
Because Kotlin is designed to improve on but also to interoperate with Java code.
So in some ways
null is a problem that Kotlin inherits from Java, even if Kotlin provides us with much better solutions for working with this special value safely.
Java has a special value that is used to indicate that an object is uninitialized:
Here's an example using
Strings, the one object that we have been working with:
Any Java object can be initialized to
Because Kotlin interoperates with Java, this is where the problem creeps in...
null seems harmless and maybe even kind of cute!
But it has left a trail of damage and tears in its wake.
In fact, famous computer scientist Tony Hoare,
who is credited for inventing
null as part of the programming language ALGOL, refers to it as his "billion dollar mistake".
Let's look at why.
Again, this example examines Java code: we'll get back to Kotlin in just a minute.
null has caused so many problems over the years, that Kotlin makes working with
null safely a core design goal.
One of the ways that it does it does this is that, unlike Java, Kotlin carefully tracks what variables can and can't hold
null in our programs.
Consider the following example:
If you try to run the code above you'll see that it fails.
The reason is that, while
value can store
Strings, and we can change its value to another
String, it cannot store
So, for each variable in our program, Kotlin knows whether or not it can hold
null and will fail if we try to assign
null to a variable that can't contain it.
null is a useful value in our programs to indicate that a variable is unset or uninitialized.
So: what if we want a
String that can contain
Here's how we do that:
Note how we used the type
String? on the variable
By appending an
? to any type name in Kotlin we indicate that the variable can also store
You can think of the
? as hinting that maybe the variable stores the type, but maybe it stores
This also works for the other kinds of variables we have been using:
Types that end with
? and can contain
null are referred to as nullable in Kotlin.
null can be used to initialize any value, Kotlin's type inference fails if we try and initialize a value with
A value that is initialized with
null has the inferred type
Nothing, which is fairly useless since a
Nothing variable can't store anything but
Instead, when we need a nullable variable we will need to specify the type explicitly, even if we initialize it with a value:
From this point forward we're going to try and keep
null in the back of our minds.
Whenever we have a variable that could be
null, we need to make sure that it isn't
null before we do anything with it!
Happily Kotlin provides us with a lot of help here!
We'll get to know Kotlin's
null safely capabilities bit-by-bit as we go.
But here's a few tips to get us started.
When declaring a method we have two options.
First, we can prevent the method from accepting
null by using a
Alternatively, if the method must accept a nullable type, we can simply check for it at the beginning. Let's see how to do that:
One of the biggest problems with
null in Java is errors caused by trying to use dot-notation on a variable that contains
(Again, this is Java code.)
In Kotlin, whenever a variable could be
null, plain dot notation will not work:
Instead, we have to use something called the safe call operator. Let's see how that works:
Now let's continue developing our algorithmic and
String manipulation capabilities.
Let's apply our skills to determining whether two
Strings are anagrams.
An anagram is created by rearrange the letters from one word to form another:
For our implementation we will not ignore whitespace and capitalization. Some anagrams do: for example, "New York Times" and "monkeys write" are anagrams, but the first string has two spaces while the second has only one. To us those would not be anagrams. When we are done, we can discuss how to make our approach more flexible.
Note that there are better ways to implement this algorithm. Perhaps we'll return to it later and experiment with one or even two alternate approaches. That's part of what makes computer science so exciting! There is always more than one way to solve any problem...
Need more practice? Head over to the practice page.