Welcome back.
And this video, I would like to start with a little history lesson in order to get novels because nobles
are a type that is only necessary because of a decision of Tony Aware, which was made in 1965 as part
of his Algol W language development.
So he invented this language.
And then he also invented this now, a reference which now finally results in a lot of null reference
exceptions or multipoint or exceptions.
So errors that come up quite often in programming and they are called by himself as a billion dollar
mistake or his billion dollar mistake.
So it was the invention of the null reference in 1965.
At the time, I was designing the first comprehensive type system for references in an object oriented
language.
I'll go W. My goal was to ensure that all use of references should be absolutely safe, with checking
performed automatically by the compiler.
But I couldn't resist the temptation to put in a null reference simply because it was so easy to implement.
This has led to innumerable errors, vulnerabilities and system crashes, which have probably caused
a billion dollars of pain and damage in the last 40 years, and I believe that is quite a lot more than
just a billion dollars because it takes so much time very often to figure out what the problem is.
And now it's a lot easier than it was in the past.
I can imagine it was really a pain back in the day.
And yeah, now there are a bunch of cool tools which make life so much easier when it comes to know
pointers or null references.
So we're going to look at one of them, which are another Bowles in Scotland.
And if you come from a programming language like Swift, then you know this term as a optional so and
swift is called optional here.
It's called the Nullarbor.
So the idea is just that it's a novel type that where a type that allows to be an Noel type, so just
a type that allows variables to have a null as its value.
So not a zero, but a null.
And in some programming languages, it's called nil and Objective C, for example.
But in.
Java and in Scotland, it's cold now, so let's go back to our code, and here I cleaned up again,
so I have only my main function.
So Scotland supporters and culpability as part of its type system, that means that you have the ability
to declare whether a variable can hold a null value or not.
And by supporting the liability in the type system to compiler can detect possible now pointer exception
errors at compile time and reduce the possibility of having them thrown at runtime.
And that is super useful because you can see null pointer exceptions before they happen, so to speak.
So before they happen on your phone, for example, when developing apps.
All right, so let's see how we can even create such situations.
So first of all, I'm going to create a new variable and I'm going to call this one name, and it's
going to have the value of Dennis.
And I'm going to explicitly state that this is going to be a string and it will make a little more sense
once we go to the multiple types.
So what I can do is I can assign a new value here.
So instead of Dennis, I could rename myself and my name would be Adam.
But what I cannot do is I cannot assign null to my name variable.
Why is that?
Well, because as you can see here, null cannot be a value of a non null type string.
So if you create a variable, as we've done in the past, then it's a non null type and it doesn't accept
to be empty, so it has to have a value.
So what can we do if we want to have a variable that accepts null as a value?
Well, we can make it a knowable value.
So I'm just going to call this one Nullarbor.
Name, and it doesn't have to have this Nullarbor at the beginning, so this is just a name that I give
it.
And I explicitly say again that it's a string.
But now I'm adding a question mark.
So this question mark now allows me to assign an empty value there so I can go ahead and say Nullarbor
name is going to be now.
So here I don't get a compilation error and it totally accepts it.
That's because I said, OK, this will be of type string Nullarbor or alpha type multiple string.
So we have this new variable and I call this one syllable name and it gets a value at the beginning
and then I assign null to it.
But of course, I could have also assigned null to it straight from the get go.
It could have been an option as well.
So now I have a noble.
And let's have a look at the difference between the two.
So what is really the idea behind all of that now, let's say I want to go ahead and get the length
of my name.
Well, I can very easily do so by using named at length.
Now, if I go ahead and do the same thing with my Nullarbor name, I suddenly get an error because it
says.
Only safe or non-animal asserted calls are allowed on the Nullarbor receiver of type string.
So how can we fix that?
Well, there is one way and there is another way.
So let's go ahead with the first way, which is the old fashioned way.
So this is how you would have done it in the past if you hadn't or when you didn't have the option to
use another boat.
So what you could do is you could check if Nullarbor name is not equal MOE.
Only then go ahead and do that.
And otherwise.
Just return null.
So that would be an option to do it.
Okay, so you can see we can now access the length if the Nullarbor name is not null, otherwise it
will return null.
All right, so that's the old fashioned way.
And this is not necessary and current because we have a much shorter approach to this.
So instead of doing all of that here, what we can do is we can go ahead and get this variable lenta
as novel name that length.
But now we add a question mark here.
OK, so this line here is the same as this line over here.
Now, the problem, of course, is that our another name here is, in fact now.
So now I will get rid of this here.
And as you can see now, it's not mal anymore, so we can even get rid of this whole line.
But I'm just going to comment it out.
OK, so this is a lot of work you need to put in a lot of work, you need to write all of this code,
right?
Or you can just add a question mark to the Nullarbor variable that you have here, and it does the same
thing for you.
So it says, OK, either it's going to work, and if it works, then give the length.
So if the noble name is in fact not now, so if there is a value in there, like in our case, it's
Dennis, then just store the result in lenta and otherwise store null and then to.
All right, like here.
Now you can go ahead and not only get properties of multiple variables, but also can use methods on
them.
So in this case, I'm using the two lower case method on my Nullarbor name.
And if I run this, then what?
It will return if it's empty and we'll just return null.
So otherwise, it's going to return the name Dennis, in my case, so that we are, says Dennis.
But let's say we make it now.
So I uncomment this line here and I run it again.
Then, well, it's not even accepting it, so I can't even run the code.
It's even.
Not even printing it.
So that's the thing about the Nullarbor.
Variables and using the question mark here.
Which is the safe call operator.
So now let's say we only want to do something or print something if the variable is in fact not know
what we can then do.
We can use the safe call operator with that.
So let me show you that real quick.
So here.
I'm just going to use the Nullarbor name again.
Then the question mark, so I need to use the save call operator that led.
And then in curly brackets, the code that I want to execute only if it's not now.
So only if there is a value in there, I want to execute something.
So this is a very short way of doing another, if else, statement here.
OK, so let me run it again.
And there we are.
It says, five, because the length of my name is five.
So now let's say it's now let's test it, and there we are.
There's nothing, so it says nothing.
It didn't do anything because our another name, in fact, was null.
All right.
So that's the first part of Nullarbor us.
In the next video, we are going to look at the Elvis operator and not null assertion operator.
So, yeah, let's just check those out, because then we have a rather full understanding of what those
multiples do and how we can use them.
So see you in the next video.