Kotlin
Java

null
Kotlin

Created By: Geoffrey Challen
/ Updated: 2021-08-30

Today's 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 nothing safely. Let's try to make sense of those statements, and also continue to practice writing String algorithms.

Java's Nothing That Is (A Big Problem)

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. Why? 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: null. Here's an example using Strings, the one object that we have been working with:

Any Java object can be initialized to null. Because Kotlin interoperates with Java, this is where the problem creeps in...

The Billion Dollar Mistake

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.

Demonstrate problems with null, and discuss NullPointerExceptions.

Kotlin to the Rescue

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 null. 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.

Sometimes 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 null? Here's how we do that:

Note how we used the type String? on the variable value. By appending an ? to any type name in Kotlin we indicate that the variable can also store null. You can think of the ? as hinting that maybe the variable stores the type, but maybe it stores null (nothing)!

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.

Because null can be used to initialize any value, Kotlin's type inference fails if we try and initialize a value with null:

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 null! Instead, when we need a nullable variable we will need to specify the type explicitly, even if we initialize it with a value:

Safely Working with null

From this point forward we're going to try and keep null in the back of our minds. Always. 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 non-nullable type:

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:

Demonstrate how to check for null at the beginning of a function, and return an appropriate value. Discuss flow typing.

Safe Property Access

One of the biggest problems with null in Java is errors caused by trying to use dot-notation on a variable that contains null: (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:

Discuss the safe call operator and flow typing.

More Practice With Strings

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:

  • "restful" and "fluster" are anagrams
  • "evil" and "vile" are also anagrams

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.

Show how to implement anagram using just character counting, and without arrays or sorting. Point out that this is inefficient and how we could improve it in the future.

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...

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough