That Missing Guide To Kotlin Part 2
In part one of our Kotlin series we began exploring the newly adopted language. We saw a few examples of how it can make our code more concise and user friendly than Java including type inference and inline constructors. In part two we’ll continue to show how Kotlin can make your code easier to work with while focusing on one dreaded Java roadblock: The NullPointerException.
If you’ve written anything in Java before then you’re familiar with NPE’s. It occurs when you attempt to interact with a variable’s value, but it turns out there is no value to interact with. Your compiler can’t see that there’s no value available, but at run-time your system checks for it and then crashes when it can’t be found.
Kotlin to the rescue
While Kotlin is 100% interoperable with Java (99% really as we’ll see soon) it’s fundamentally different in a few aspects. One of these is nullability. In Java every variable has a default value. This is the value it can fall back on if you never explicity say what value it contained. For example when you declare a boolean variable, unless you specify its value, it will be false. For any variable that isn’t a primitive type, the default value will be null. This can be really useful at times, but it allows us to accidentally try to grab a value when there isn’t one.
In Kotlin its possible to completely avoid this situation. Every variable that is declared is either a nullable type or a non-null type. What this means is that along with declaring what type of value (Int, Boolean, custom class) your variable holds, you declare whether it is ever allowed to be null or not. Here are two varaibles declared in Kotlin. The first can never be null, while the second is nullable.
As you can see, the question mark is used to show a variable has the possibility of being null. If there’s no question mark in the variables declaration, then you are safe to use it in every situation without risking it throwing an NPE. This means that you’ll never have to write if statements checking if a variable is null to play things safe. Goodbye useless lines of code
Nullablility In Action
When using non-null type variables, we don’t have to check if there is a value present. But for other situations we still must take precautions. One way that Kotlin allows us to do this concisely is through safe calls. A safe call looks similar to the traditional method of retrieving a value, except we include a question mark at the end of the variable. So
By doing this we can almost read the code as if it’s English. We’re asking “does the pet exist?”, and if the answer to that question is yes then we move on to the name characteristic of our pet. If there is no pet that has been created at this point in our code, instead of the app crashing our variable will be given a value of null.
This becomes incredibly powerful when we start chaining safe calls. Let’s say we want to check if a company has an HR department with an employee name Toby who at tuna for lunch. Instead of writing a null check for each of these before interacting with them we can write
If along the way any of these things doesn’t exist, then the code will finish the statement returning null and not interacting with any of the variables past the break. Without an HR department the code won’t crash, but instead our employee variable will be set equal to null.
Control Is A Good Thing
This is probably the most common feature of Kotlin that you see people talk about, and it’s because of how powerful it is. Not having to deal with null values is amazing, but even when we have to our code can be concise and clean.
The language can also smart cast nullable variables into non-null given the proper verification at first. So if you had a nullable variable and then explicitly check if it wasn’t null, then you wouldn’t need to use any ?’s moving forward when accessing that variable.
Kotlin isn’t perfect, but when it comes to dealing with null values it has everything that Java has to offer with some additional perks and changes. We’ll continue exploring some of Kotlin’s advantages in part 3. In the mean time if you have any questions about how nullability works let us know in the comments below!