Check out a free preview of the full Introduction to Kotlin and Android Development course

The "Null Safety" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates the function of Null Safety to distinguish between references that can hold null (nullable references) and those that cannot (non-null references). Kotlin's type system is aimed at eliminating the danger of null references


Transcript from the "Null Safety" Lesson

>> Okay, so something that is probably new to some of you, null safety. So, in Kotlin, when you express, let me pull this for a minute so I don't spoil what's happening. When you have a variable like this and you say that my name is a string, it must contain a string.

It cannot accept null, like in Java or JavaScript or other languages. So if it's a string, it must be string. If not, it's not compiling. I need a value. But sometimes you know that sometimes, hey, I don't have a value or maybe I accept nulls. Well, in that case, if you accept a name with nulls, you use a string question mark.

Question mark says that the type that we have before, the character is actually optional. So it accepts nulls, okay? So that means that now we can assign a null, by default, it will have that value. So the problem with this, so it sounds, okay, simple to understand. So here I have an example with myLastname, that it handle a null value, is operators.

Because actually, if you wanna get the length of the string, it's not working, like so. Because if you do this, I mean in Java or in JavaScript, it works. But you might have the well known friend, null pointer exception or null reference exception in C#, for example. That is actually pretty annoying, right?

Well, Kotlin will force you to manage the situation, because now myLastName can accept nulls. I need to do something because maybe there is no last name to ask for a length. So I need to operate. This is not gonna work, and it's not going to compile. I need to make a decision, okay, about the null situation.

So the first possible decision is to use an operator that has a weird name to remember, that not null assertion operator. It's actually double exclamation mark. For those of you coming from Swift, there we have a similar operator with one exclamation mark, here it's two exclamation mark. In this case, we are saying that we know or as developers, we are guaranteeing the developer that it has a value and it's not null.

And this is like on Java, just asking for the length, and I don't care if it's null or not. Of course, if it's null, your app will crash, okay? But it is a way to bypass the compiler, and saying, compiler, I know you don't like this, but trust me, okay?

Go ahead and try to get the length, okay? So we try to avoid the usage of this operator as much as possible. We use these will control exceptions, okay? What other operators we have? We have another one called the safe call, the safe call, it's actually like this.

It's using question mark dot, so in this case, the safe call will actually call length only if it's not null. And if it's null, it's going to return null to the color of the expression. So actually, the type of length, if I zoom in a little bit, so you can see the type.

It's Int?. So the length can be an integer or a null, because if the last name is null, I don't have a length, okay? So that's the safe call, similar to other languages. It's the same syntax. And finally, we have an operator for default values. So for example, if we use a safe operator but I actually want an integer.

I want an integer, I don't want an optional integer. I can set the default value, and we set the default value with ?:, okay, so and then the body from a 0. So if this expression is null, it will use 0, the default value, okay? Does it make sense?

This is like in Swift or in JavaScript, now we have this operator, the ??. Well, here, it's ?:, and I have a question for you. Can you guess the name of this operator? You know it because you're laughing, so you know the answer. This operator has a name in Kotlin, what's the name?

>> I had to look at the picture to figure this out, but it's the Elvis operator.
>> The Elvis operator. Why is called the Elvis operator? I'm talking about that one, okay? Because if you turn your head anti-clockwise, it should be 90 degrees. It seemed for someone that it looks like the Elvis hair, okay?

Fortunately, it's not called the Donald Trump operator, right, because it can be applied as well. We aren't talking about politics, I'm talking about hair here. So it's called the Elvis operator because it looks like Elvis Presley hair, okay, the curly hair. I don't know. Anyway, a funny fact about Kotlin.

Okay, now you will see the hair when you look at that operator, I promise you, because I'm seeing the hair, every time I'm seeing. Okay, so those are the null safety operators available in Kotlin.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now