Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,240 --> 00:00:05,910
Welcome back and this video, I would like to start with a little history lesson in order to get to
2
00:00:05,910 --> 00:00:12,390
knuckleballs, because nobles are a type that is only necessary because of a decision of Tony Aware,
3
00:00:13,020 --> 00:00:18,720
which was made in 1965 as part of his ALGOL w language development.
4
00:00:18,730 --> 00:00:20,040
So he invented this language.
5
00:00:20,220 --> 00:00:27,750
And then he also invented this now a reference which now finally results in a lot of null reference
6
00:00:27,750 --> 00:00:30,540
exceptions or multipoint or exceptions.
7
00:00:30,540 --> 00:00:38,760
So errors that come up quite often in programming and they are called by himself as a billion dollar
8
00:00:38,760 --> 00:00:41,070
mistake or his billion dollar mistake.
9
00:00:41,250 --> 00:00:44,550
So it was the invention of the null reference in nineteen sixty five.
10
00:00:44,730 --> 00:00:50,130
At the time, I was designing the first comprehensive type system for references in an object oriented
11
00:00:50,130 --> 00:00:50,670
language.
12
00:00:50,670 --> 00:00:58,290
Algol w my goal was to ensure that all use of references should be absolutely safe with checking performed
13
00:00:58,290 --> 00:00:59,880
automatically by the compiler.
14
00:01:00,090 --> 00:01:05,880
But I couldn't resist the temptation to put it in the reference simply because it was so easy to implement.
15
00:01:06,470 --> 00:01:12,390
This has led to innumerable errors, vulnerabilities and system crashes, which have probably cost a
16
00:01:12,390 --> 00:01:15,960
billion dollars of pain and damage in the last 40 years.
17
00:01:16,170 --> 00:01:22,830
And I believe that is quite a lot more than just a billion dollars because it takes so much time very
18
00:01:22,830 --> 00:01:25,050
often to figure out what the problem is.
19
00:01:25,320 --> 00:01:28,100
And now it's a lot easier than it was in the past.
20
00:01:28,140 --> 00:01:30,870
I can imagine it was really a pain back in the day.
21
00:01:31,200 --> 00:01:37,350
And yeah, now there are a bunch of cool tools which make life so much easier when it comes to no pointers
22
00:01:37,590 --> 00:01:39,030
or null references.
23
00:01:39,280 --> 00:01:43,410
So we're going to look at one of them, which are unknowables in Scotland.
24
00:01:43,590 --> 00:01:50,720
And if you come from a programming language like Swift, then you know this term as a optional.
25
00:01:50,820 --> 00:01:53,310
So Hainsworth is called optional here.
26
00:01:53,340 --> 00:01:54,540
It's called the nullable.
27
00:01:55,170 --> 00:02:01,340
So the idea is just that it's a null type that were a type that allows to be a null type.
28
00:02:01,350 --> 00:02:06,030
So just a type that allows variables to have a null as its value.
29
00:02:06,180 --> 00:02:09,810
So not zero, but a null and then some programming languages.
30
00:02:09,810 --> 00:02:13,890
It's called nil in objective C, for example, but in.
31
00:02:14,960 --> 00:02:22,400
Java and in Courtland's called not so let's go back to our code, and here I cleaned up again, so I
32
00:02:22,400 --> 00:02:24,010
have only my main function.
33
00:02:24,890 --> 00:02:30,500
So Kotlin supporters notability as part of its type system, that means that you have the ability to
34
00:02:30,590 --> 00:02:34,160
declare whether a variable can hold a value or not.
35
00:02:34,370 --> 00:02:40,790
And by supporting the lability in the type system, the compiler can detect possible null pointer exception
36
00:02:40,790 --> 00:02:45,410
errors at compile time and reduce the possibility of having them thrown at runtime.
37
00:02:45,530 --> 00:02:51,590
And that is super useful because you can see no pointer exceptions before they happen, so to speak.
38
00:02:51,770 --> 00:02:55,820
So before they happen on your phone, for example, when developing apps.
39
00:02:56,190 --> 00:02:56,510
Right.
40
00:02:56,510 --> 00:03:00,280
So let's see how we can even create such a situation.
41
00:03:00,310 --> 00:03:04,910
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
42
00:03:04,910 --> 00:03:06,380
to have the value of Dennis.
43
00:03:06,710 --> 00:03:12,710
And I'm going to explicitly state that this is going to be a string and it will make a little more sense
44
00:03:12,710 --> 00:03:15,510
once we go to the nullable types.
45
00:03:16,130 --> 00:03:20,060
So what I can do is I can assign a new value here.
46
00:03:20,070 --> 00:03:24,290
So instead of Dennis, I could rename myself and my name would be Adam.
47
00:03:24,890 --> 00:03:30,840
But what I cannot do is I cannot assign null to my name variable.
48
00:03:31,370 --> 00:03:32,010
Why is that?
49
00:03:32,030 --> 00:03:39,260
Well, because as you can see here, now cannot be a value of a non null type string.
50
00:03:39,630 --> 00:03:46,610
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
51
00:03:46,610 --> 00:03:47,340
to be empty.
52
00:03:47,360 --> 00:03:48,830
So it has to have a value.
53
00:03:49,520 --> 00:03:54,110
So what can we do if we want to have a variable that accepts null as a value?
54
00:03:54,800 --> 00:03:57,800
Well, we can make it a nullable value.
55
00:03:57,860 --> 00:04:00,410
So I'm just going to call this one nullable.
56
00:04:01,900 --> 00:04:07,990
Name and it doesn't have to have this nullable at the beginning, so this is just a name that I give
57
00:04:07,990 --> 00:04:14,070
it and I explicitly say again that it's a string, but now I'm adding a question mark.
58
00:04:14,710 --> 00:04:22,060
So this question mark now allows me to assign an empty value there so I can go ahead and say nullable
59
00:04:22,060 --> 00:04:23,150
name is going to be no.
60
00:04:24,010 --> 00:04:28,380
So here I don't get a compilation error and it totally accepts it.
61
00:04:28,390 --> 00:04:34,060
That's because I said, OK, this will be of type string nullable or elf type nullable string.
62
00:04:34,070 --> 00:04:41,080
So we have this new variable and I call this one nullable name and it gets a value at the beginning
63
00:04:41,410 --> 00:04:42,940
and then I assign null to it.
64
00:04:42,940 --> 00:04:46,280
But of course I could have also assign null to it straight from the get go.
65
00:04:46,330 --> 00:04:48,290
OK, that could have been an option as well.
66
00:04:49,300 --> 00:04:51,130
So now I have a nullable.
67
00:04:52,210 --> 00:04:59,050
And let's have a look at the difference between the two, so what is really the idea behind all of that?
68
00:04:59,440 --> 00:05:03,800
Now, let's say I want to go ahead and get the length of my name.
69
00:05:04,090 --> 00:05:08,690
Well, I can very easily do so by using name that length.
70
00:05:09,520 --> 00:05:17,470
Now, if I go ahead and do the same thing with my nullable name, I suddenly get an error because it
71
00:05:17,470 --> 00:05:17,860
says.
72
00:05:18,860 --> 00:05:25,520
Only safe or none now asserted calls are allowed on the nullable receiver of type string.
73
00:05:26,770 --> 00:05:28,210
So how can we fix that?
74
00:05:28,900 --> 00:05:32,120
Well, there is one way and there is another way.
75
00:05:32,380 --> 00:05:36,580
So let's go ahead with the first way, which is the old fashioned way.
76
00:05:36,590 --> 00:05:43,420
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
77
00:05:43,420 --> 00:05:44,040
use Nullable.
78
00:05:44,440 --> 00:05:53,920
So what you could do is you could check if nullable name is not equal now, only then go ahead and do
79
00:05:53,920 --> 00:05:54,220
that.
80
00:05:56,560 --> 00:05:57,670
And otherwise.
81
00:05:58,910 --> 00:06:03,440
Just return now, so that would be an option to do it.
82
00:06:04,370 --> 00:06:12,110
OK, so you can see we can now access the length if the nullable name is not null, otherwise it will
83
00:06:12,110 --> 00:06:12,830
return null.
84
00:06:14,220 --> 00:06:21,930
All right, so that's the old fashioned way, and this is not necessary in Scotland because we have
85
00:06:21,930 --> 00:06:24,450
a much shorter approach to this.
86
00:06:24,780 --> 00:06:32,490
So instead of doing all of that here, what we can do is we can go ahead and create this variable,
87
00:06:32,490 --> 00:06:37,290
lend to as nullable name that length.
88
00:06:37,560 --> 00:06:39,720
But now we add a question mark here.
89
00:06:41,060 --> 00:06:46,880
OK, so this line here is the same as this line over here.
90
00:06:48,420 --> 00:06:52,130
Now, the problem, of course, is that our nullable name here is in fact now.
91
00:06:52,530 --> 00:06:54,940
So now I will get rid of this here.
92
00:06:55,380 --> 00:06:58,280
And as you can see now, it's not much anymore.
93
00:06:58,290 --> 00:07:01,370
So we can even get rid of this whole line.
94
00:07:01,380 --> 00:07:04,260
But I'm just going to comment about.
95
00:07:05,940 --> 00:07:11,790
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
96
00:07:11,790 --> 00:07:12,090
right.
97
00:07:12,390 --> 00:07:17,930
Or you can just add a question mark to the nullable variable that you have here.
98
00:07:18,000 --> 00:07:19,520
And it does the same thing for you.
99
00:07:20,160 --> 00:07:25,440
So it says, OK, either it's going to work and if it works, then give the length.
100
00:07:25,470 --> 00:07:27,990
So if the nullable name is in fact, not now.
101
00:07:28,320 --> 00:07:34,530
So if there is a value in there, like in our case, it's Dennis, then just store the result in linta
102
00:07:35,430 --> 00:07:38,370
and otherwise store null and then to.
103
00:07:39,630 --> 00:07:40,530
All right, back here.
104
00:07:41,610 --> 00:07:50,130
Now, you can go ahead and not only get properties of nullable variables, but also you can use methods
105
00:07:50,130 --> 00:07:50,590
on them.
106
00:07:50,610 --> 00:07:55,350
So in this case, I'm using the two lower case method on my nullable name.
107
00:07:55,530 --> 00:07:57,810
And if I run this, then what?
108
00:07:57,810 --> 00:07:58,980
It will return.
109
00:07:59,340 --> 00:08:02,460
If it's empty, it will just return null.
110
00:08:02,910 --> 00:08:08,700
So otherwise it's going to return the name doneness in my case, so that we are Stenness.
111
00:08:08,970 --> 00:08:10,700
But let's say we make it now.
112
00:08:10,710 --> 00:08:15,030
So I uncommented this line here and I run it again then.
113
00:08:15,040 --> 00:08:18,060
Well, it's not even accepting it, so I can't even run the code.
114
00:08:18,510 --> 00:08:19,200
It's even.
115
00:08:20,620 --> 00:08:24,250
Not even printing it, so that's the thing about Nullable.
116
00:08:26,240 --> 00:08:28,520
Variables and using the question mark here.
117
00:08:30,260 --> 00:08:32,370
Which is the safe call operator.
118
00:08:33,020 --> 00:08:40,789
So now let's say we only want to do something or print something, if the variable is in fact not know
119
00:08:41,990 --> 00:08:46,440
what we can then do, we can use the safe call operator with let.
120
00:08:47,090 --> 00:08:48,860
So let me show you that real quick.
121
00:08:50,620 --> 00:08:51,310
So here.
122
00:08:53,640 --> 00:08:55,920
I'm just going to use the nullable name again.
123
00:08:57,080 --> 00:09:04,700
Then the question mark, so I need to use the safe call operator, not let, and then in curly brackets,
124
00:09:04,700 --> 00:09:10,580
the code that I want to execute only if it's not now, so only if there is a value in there.
125
00:09:10,610 --> 00:09:12,320
I want to execute something.
126
00:09:12,590 --> 00:09:16,400
So this is a very short way of doing another if else statement here.
127
00:09:17,260 --> 00:09:18,990
OK, so let me run it again.
128
00:09:22,030 --> 00:09:22,960
And there we are.
129
00:09:22,990 --> 00:09:26,020
It says five, because the length of my name is five.
130
00:09:27,270 --> 00:09:33,500
So now let's say it's now let's test it and we are there's nothing, so it says nothing.
131
00:09:33,510 --> 00:09:37,890
It didn't do anything because our nullable name, in fact, was null.
132
00:09:38,730 --> 00:09:39,180
All right.
133
00:09:39,180 --> 00:09:41,340
So that's the first part of Nullable.
134
00:09:41,340 --> 00:09:48,070
In the next video, we are going to look at the Elvis operator and not no assertion operator.
135
00:09:48,300 --> 00:09:56,190
So, yeah, let's just check those out, because then we have a rather full understanding of what those
136
00:09:56,640 --> 00:09:59,090
multiples do and how we can use them.
137
00:09:59,550 --> 00:10:00,720
So see you in the next video.
13438
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.