Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

What does null mean? Imagine there are three janitors in the school from the latest lesson: John, Mary, and Peter. When they’re finished working, they make a note of how many rooms they cleaned. You know that John cleaned five rooms and Mary cleaned seven rooms, but there isn’t any information about Peter’s progress yet. He’s still working. In this example, Peter’s number of cleaned rooms is null. In programming, null means there is no value assigned to the variable. Look at the following diagram illustrating the meaning of the null:

1 element 0 elements Null

The null is not the same as zero. It is a special entity that represents the absence of data. You can’t compare a null to any other value. Peter may have cleaned 10 rooms, or he may have cleaned zero — you don’t know. So, you can’t say if Peter cleaned more or fewer rooms than John or Mary. You can’t perform any operations on null values except for checking if it is null or not.

Imagine that the supervisor wants to know how many rooms all the janitors have cleaned so far. The response differs depending on the rules. One option is that you may tell the supervisor an answer isn’t available yet. In the code, it may mean throwing an exception or printing a message to the standard error. Another option is to tell the supervisor how many rooms John and Mary cleaned, and ignore Peter’s work. In the code, it may mean that you will treat the null value as zero. In a project, this is your decision as a programmer. You have to choose how to handle nulls. If you try to perform an operation on a null value, your Kotlin code won’t compile.

Kotlin distinguishes between nullable and non-nullable types. A nullable type is a type that can hold null values, while a non-nullable type cannot. You can declare a nullable type by adding a question mark after the type name. For example, Int? is a nullable integer type. Many of the methods in the Kotlin standard library that return nullable types have an orNull suffix. For example, firstOrNull(), which you have used in the previous lesson, returns the first element of a collection, or null if the collection is empty. There’s also a first() method which, on the contrary, returns the first element of a collection or throws an exception if there is no such element. It has a non-nullable return type.

The last topic of this lesson is the most advanced one.

Kotlin has a full interoperability with Java. You can use the Java libraries in your Kotlin projects. It will work only when targeting the JVM (which is the default in the Kotlin Playground). But, the Java language doesn’t have the built-in null safety like Kotlin. That means that an API written in Java does not expose the nullability information. The Kotlin compiler cannot infer that. In the Java code, it’s a responsibility of the developers to treat the value as nullable or not. If they perform an operation on a null value then a NullPointerException is thrown.

There are several techniques which can help in dealing with the null values in the Java code, like special annotations or static code analysis. But they aren’t as powerful as the Kotlin null safety, which has been present since the beginning in the design of the language.

So, you may think that it’s safe to treat all types returned by Java APIs as nullable. While that’s true, it’s also extremely impractical. In real projects, APIs written in Java are used very frequently. In most cases, it is known whether the value is nullable or not, for instance, by reading the documentation. Kotlin provides a special type called platform type. It’s neither nullable nor non-nullable. Developers must decide how they will treat the value. The Kotlin compiler does not enforce any null checks on the platform types.

OK, that’s enough theory. Look at the sample project in the demo section.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo