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.
[00:00:51] 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.
[00:01:45] 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.
[00:02:07] 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.
[00:02:39] 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?
[00:02:57] 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.
[00:03:20] 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.
[00:03:42] 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.
[00:04:14] 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?
>> 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?
[00:05:24] 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.
[00:05:41] 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.