Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:02,399
kotlin is the most popular programming
2
00:00:02,399 --> 00:00:04,740
language for building Android apps and
3
00:00:04,740 --> 00:00:07,140
it can also do a lot of other things in
4
00:00:07,140 --> 00:00:09,240
this beginner's course Alex will teach
5
00:00:09,240 --> 00:00:12,360
you kotlin from the ground up Alex is an
6
00:00:12,360 --> 00:00:14,040
experienced developer who has created
7
00:00:14,040 --> 00:00:16,500
multiple Android apps in this course
8
00:00:16,500 --> 00:00:18,119
I'll cover everything you need to know
9
00:00:18,119 --> 00:00:19,680
in order to be able to create another
10
00:00:19,680 --> 00:00:22,140
programs or move to build apps on
11
00:00:22,140 --> 00:00:24,420
Android I will start from zero by
12
00:00:24,420 --> 00:00:26,220
downloading the software that we need to
13
00:00:26,220 --> 00:00:27,840
write code to creating our first program
14
00:00:27,840 --> 00:00:29,160
and then we're gonna move to more
15
00:00:29,160 --> 00:00:31,560
advanced topics we'll cover things like
16
00:00:31,560 --> 00:00:34,860
variables operators Loops arrays object
17
00:00:34,860 --> 00:00:37,260
orientated programming generics Lambda
18
00:00:37,260 --> 00:00:39,360
functions core routines
19
00:00:39,360 --> 00:00:41,579
everything will be covered in depth with
20
00:00:41,579 --> 00:00:43,980
great detail additionally I'll use
21
00:00:43,980 --> 00:00:45,540
beautiful animation while talking about
22
00:00:45,540 --> 00:00:46,800
a concept
23
00:00:46,800 --> 00:00:49,620
for who is this course this course is
24
00:00:49,620 --> 00:00:51,239
for complete beginners who have no
25
00:00:51,239 --> 00:00:53,039
experience with programming and also for
26
00:00:53,039 --> 00:00:55,379
experience programmers kotlin is perfect
27
00:00:55,379 --> 00:00:57,059
for beginners because it's very
28
00:00:57,059 --> 00:00:58,920
intuitive and concise
29
00:00:58,920 --> 00:01:00,719
and if you already have a background in
30
00:01:00,719 --> 00:01:02,820
other programming languages then this
31
00:01:02,820 --> 00:01:04,979
course would be easy for you so let's
32
00:01:04,979 --> 00:01:06,540
get started
33
00:01:06,540 --> 00:01:08,280
now it's time to download the installing
34
00:01:08,280 --> 00:01:09,840
ID and throughout this course I'm going
35
00:01:09,840 --> 00:01:12,299
to use an ID called IntelliJ ID which is
36
00:01:12,299 --> 00:01:14,400
an ID made by a company called jetbrains
37
00:01:14,400 --> 00:01:16,200
now you may be wondering what is an idea
38
00:01:16,200 --> 00:01:18,540
an IDs stands for an integrated
39
00:01:18,540 --> 00:01:20,520
development environment and it's
40
00:01:20,520 --> 00:01:21,720
basically the tool that you're going to
41
00:01:21,720 --> 00:01:23,640
use writer code in and intelligent it's
42
00:01:23,640 --> 00:01:25,140
like a text editor but a very
43
00:01:25,140 --> 00:01:27,000
sophisticated one because it has a lot
44
00:01:27,000 --> 00:01:28,680
of features like highlighting our code
45
00:01:28,680 --> 00:01:31,140
showing real-time errors and suggestions
46
00:01:31,140 --> 00:01:32,939
you can run and compiler coaches by a
47
00:01:32,939 --> 00:01:34,920
few clicks in other words it's making
48
00:01:34,920 --> 00:01:37,320
the process of writing code easier and
49
00:01:37,320 --> 00:01:39,420
better therefore let's download IntelliJ
50
00:01:39,420 --> 00:01:42,240
IDEA so I'm going to open my browser
51
00:01:42,240 --> 00:01:44,520
and in the search bar to type IntelliJ
52
00:01:44,520 --> 00:01:45,540
IDEA
53
00:01:45,540 --> 00:01:47,579
press enter
54
00:01:47,579 --> 00:01:51,180
and we click on this download link
55
00:01:51,180 --> 00:01:52,799
and on the downloading page you're going
56
00:01:52,799 --> 00:01:54,780
to see two options ultimate which is
57
00:01:54,780 --> 00:01:56,399
paid so you need to pay in order to
58
00:01:56,399 --> 00:01:58,320
download it and Community which is free
59
00:01:58,320 --> 00:02:01,020
and the difference is that the ultimate
60
00:02:01,020 --> 00:02:02,640
has more features and Community has less
61
00:02:02,640 --> 00:02:04,500
features but Community is not for our
62
00:02:04,500 --> 00:02:06,960
purposes also pay attention on the left
63
00:02:06,960 --> 00:02:08,818
hand side when I'm currently recording
64
00:02:08,818 --> 00:02:11,000
this video the latest version is
65
00:02:11,000 --> 00:02:13,500
2021.1.2 which was released on the 1st
66
00:02:13,500 --> 00:02:15,959
June of 2022. and if you look at the
67
00:02:15,959 --> 00:02:17,520
system requirements here if you click on
68
00:02:17,520 --> 00:02:20,819
that you will see that this IntelliJ ID
69
00:02:20,819 --> 00:02:23,040
only works for a 64-bit version of
70
00:02:23,040 --> 00:02:24,840
Windows so if you have a 32-bit version
71
00:02:24,840 --> 00:02:28,140
of Windows this will not work for you
72
00:02:28,140 --> 00:02:31,200
now if you have macro X or Linux you
73
00:02:31,200 --> 00:02:32,459
should download the particular version
74
00:02:32,459 --> 00:02:34,800
for your system and if you don't know
75
00:02:34,800 --> 00:02:37,379
how it just tell me in the comments and
76
00:02:37,379 --> 00:02:38,879
I've made two separate video and shall
77
00:02:38,879 --> 00:02:40,800
show you how to do now I'm gonna
78
00:02:40,800 --> 00:02:42,420
download the community version for
79
00:02:42,420 --> 00:02:43,860
Windows
80
00:02:43,860 --> 00:02:46,560
so click to download the community now
81
00:02:46,560 --> 00:02:48,780
our downloading is starting on the right
82
00:02:48,780 --> 00:02:51,120
hand side on the top and if you have a
83
00:02:51,120 --> 00:02:52,500
different browser you'll see this in an
84
00:02:52,500 --> 00:02:54,739
infrared place
85
00:02:54,739 --> 00:02:57,060
so once the downloading is finished just
86
00:02:57,060 --> 00:02:58,860
open this file I'm going to close the
87
00:02:58,860 --> 00:03:00,120
browser now because I don't need the
88
00:03:00,120 --> 00:03:01,800
browser and the installation will start
89
00:03:01,800 --> 00:03:04,280
immediately
90
00:03:06,720 --> 00:03:08,400
now it's asking us if you allow this app
91
00:03:08,400 --> 00:03:10,860
to make changes to a device click yes
92
00:03:10,860 --> 00:03:12,959
you need to click on next this is the
93
00:03:12,959 --> 00:03:14,879
for the location or IntelliJ is going to
94
00:03:14,879 --> 00:03:16,080
be installed you can keep the default
95
00:03:16,080 --> 00:03:17,400
location or you can change it if you
96
00:03:17,400 --> 00:03:18,420
want I'm going to keep the default
97
00:03:18,420 --> 00:03:21,120
location so I'm going to click on next
98
00:03:21,120 --> 00:03:22,980
and here I'm going to check this box to
99
00:03:22,980 --> 00:03:24,900
have a desktop shortcut I'm going to
100
00:03:24,900 --> 00:03:26,519
click on next now I'm going to click on
101
00:03:26,519 --> 00:03:27,959
install and IntelliJ is going to be
102
00:03:27,959 --> 00:03:29,340
installed on my computer
103
00:03:29,340 --> 00:03:30,900
as you can see on the left hand side
104
00:03:30,900 --> 00:03:34,080
IntelliJ already appeared
105
00:03:34,080 --> 00:03:35,700
now let's wait until the installation
106
00:03:35,700 --> 00:03:38,299
finishes
107
00:03:39,480 --> 00:03:41,159
when the installation finishes just
108
00:03:41,159 --> 00:03:42,780
check this run Intel GID Community
109
00:03:42,780 --> 00:03:44,340
Edition and click on finish now
110
00:03:44,340 --> 00:03:47,580
intelligently IntelliJ is going to open
111
00:03:47,580 --> 00:03:49,379
now it's asking us if you want to import
112
00:03:49,379 --> 00:03:51,120
IntelliJ settings so if you have the
113
00:03:51,120 --> 00:03:52,739
previous version of IntelliJ ID you can
114
00:03:52,739 --> 00:03:54,120
import the settings here but I'm going
115
00:03:54,120 --> 00:03:55,680
to click the not import settings and
116
00:03:55,680 --> 00:03:56,879
click on OK
117
00:03:56,879 --> 00:04:00,319
now IntelliJ ID is going to open
118
00:04:02,700 --> 00:04:05,400
and this is the welcome window of
119
00:04:05,400 --> 00:04:07,560
IntelliJ IDEA and here on the left hand
120
00:04:07,560 --> 00:04:09,540
side click on customize
121
00:04:09,540 --> 00:04:11,640
go here to where it says color theme so
122
00:04:11,640 --> 00:04:13,140
you can change this theme because the
123
00:04:13,140 --> 00:04:14,760
default team is this dark team I'm Gonna
124
00:04:14,760 --> 00:04:17,339
Change it to light because the IntelliJ
125
00:04:17,339 --> 00:04:19,380
team is better for tutorials but you can
126
00:04:19,380 --> 00:04:21,660
keep the dark team if you want and in a
127
00:04:21,660 --> 00:04:22,800
separate video I'm going to show you how
128
00:04:22,800 --> 00:04:24,900
to add more themes not just the ones
129
00:04:24,900 --> 00:04:26,639
provided by IntelliJ ID
130
00:04:26,639 --> 00:04:29,100
now click back on projects click on this
131
00:04:29,100 --> 00:04:32,580
plus button where it says a new project
132
00:04:32,580 --> 00:04:35,940
because we need to configure the jdk now
133
00:04:35,940 --> 00:04:39,360
and now here we have jdk and we have in
134
00:04:39,360 --> 00:04:42,360
red and angle brackets we have no JD no
135
00:04:42,360 --> 00:04:43,740
SDK
136
00:04:43,740 --> 00:04:46,380
and now here we need to add the jdk and
137
00:04:46,380 --> 00:04:49,020
jdk stands for Java development kit SDK
138
00:04:49,020 --> 00:04:50,940
stands for software development so
139
00:04:50,940 --> 00:04:53,400
software developer Kint is the Java
140
00:04:53,400 --> 00:04:55,380
development kit no matter how you call
141
00:04:55,380 --> 00:04:57,960
it this thing contains the tools that
142
00:04:57,960 --> 00:05:00,060
IntelliJ ADN needs in order to be able
143
00:05:00,060 --> 00:05:02,220
to compile and run our programs because
144
00:05:02,220 --> 00:05:04,380
without them IntelliJ edits like an
145
00:05:04,380 --> 00:05:06,780
empty text editor so it needs those
146
00:05:06,780 --> 00:05:09,360
tools in order to be able to work with
147
00:05:09,360 --> 00:05:10,860
our particular programming language
148
00:05:10,860 --> 00:05:14,280
which is kotlin now to download the jdk
149
00:05:14,280 --> 00:05:16,199
and we use the Java developing kit
150
00:05:16,199 --> 00:05:19,139
because code and Java are interoperable
151
00:05:19,139 --> 00:05:20,759
and that means that you can use the
152
00:05:20,759 --> 00:05:22,919
existing Tools in library which Java
153
00:05:22,919 --> 00:05:25,560
already has in our kotlin project so
154
00:05:25,560 --> 00:05:28,919
quickly here on this no SDK and click
155
00:05:28,919 --> 00:05:32,039
here where it says download jdk
156
00:05:32,039 --> 00:05:33,180
now
157
00:05:33,180 --> 00:05:35,940
on this window we select version 17 not
158
00:05:35,940 --> 00:05:37,740
version 18 because this is the latest
159
00:05:37,740 --> 00:05:40,080
version 18. this change version 17
160
00:05:40,080 --> 00:05:41,880
because this is the version which has
161
00:05:41,880 --> 00:05:43,500
the long-term support and that means
162
00:05:43,500 --> 00:05:45,180
that it will receive bug fixes and
163
00:05:45,180 --> 00:05:46,500
updates for several lists to come
164
00:05:46,500 --> 00:05:49,139
without you needing to switch to any new
165
00:05:49,139 --> 00:05:51,000
version when that new version is
166
00:05:51,000 --> 00:05:53,100
released for the average vendor we
167
00:05:53,100 --> 00:05:55,160
choose Amazon Creator and
168
00:05:55,160 --> 00:05:58,080
I'm gonna let the default location so
169
00:05:58,080 --> 00:05:59,639
I'm gonna click download to download the
170
00:05:59,639 --> 00:06:01,199
jdk now the jdk is going to be
171
00:06:01,199 --> 00:06:03,360
downloaded
172
00:06:03,360 --> 00:06:06,800
now the jdk has been installed and is
173
00:06:06,800 --> 00:06:10,440
installed on our IntelliJ IDEA and now
174
00:06:10,440 --> 00:06:12,120
we can create our projects but I'm not
175
00:06:12,120 --> 00:06:13,440
going to create a project so I'm not
176
00:06:13,440 --> 00:06:14,520
going to click on create I'm going to
177
00:06:14,520 --> 00:06:15,960
click on cancel and in the next video
178
00:06:15,960 --> 00:06:17,340
we're going to create our first project
179
00:06:17,340 --> 00:06:19,979
and we're also going to talk about a
180
00:06:19,979 --> 00:06:22,560
little bit the jdk and the jvm so see
181
00:06:22,560 --> 00:06:24,180
you in the next video now it's time to
182
00:06:24,180 --> 00:06:25,919
create our first project and there is a
183
00:06:25,919 --> 00:06:27,360
traditional programming that our first
184
00:06:27,360 --> 00:06:28,979
program should be to Output the text
185
00:06:28,979 --> 00:06:30,780
tail over to the console and this is
186
00:06:30,780 --> 00:06:32,400
what we're gonna do in this video
187
00:06:32,400 --> 00:06:34,500
therefore I'm going to open IntelliJ ID
188
00:06:34,500 --> 00:06:38,300
you should open your IntelliJ IDEA
189
00:06:44,340 --> 00:06:46,020
on the left hand side make sure to have
190
00:06:46,020 --> 00:06:47,940
project selected then click on this plus
191
00:06:47,940 --> 00:06:51,440
button to create a new project
192
00:06:52,979 --> 00:06:55,680
and here we have a few options the first
193
00:06:55,680 --> 00:06:58,080
is the name and here you need to name
194
00:06:58,080 --> 00:06:59,819
our project we need to give a name to
195
00:06:59,819 --> 00:07:01,680
our project and the name of the project
196
00:07:01,680 --> 00:07:03,660
should start with the capital letter and
197
00:07:03,660 --> 00:07:05,280
if it has multiple words every word
198
00:07:05,280 --> 00:07:07,139
should start to the capital letter
199
00:07:07,139 --> 00:07:10,500
so I'm gonna call this uh on on this the
200
00:07:10,500 --> 00:07:12,539
way of naming is called the Pascal case
201
00:07:12,539 --> 00:07:15,000
so I'm going to call this uh this
202
00:07:15,000 --> 00:07:16,139
project hello
203
00:07:16,139 --> 00:07:17,880
world
204
00:07:17,880 --> 00:07:20,520
and also you should not have y spaces in
205
00:07:20,520 --> 00:07:22,620
your name
206
00:07:22,620 --> 00:07:24,660
next we have the location and this is
207
00:07:24,660 --> 00:07:26,220
where the approach is going to be
208
00:07:26,220 --> 00:07:27,720
created you can change this location if
209
00:07:27,720 --> 00:07:28,860
you want but I'm going to keep the
210
00:07:28,860 --> 00:07:30,240
default location
211
00:07:30,240 --> 00:07:32,340
next we have language and here you can
212
00:07:32,340 --> 00:07:33,780
here you need to select the language
213
00:07:33,780 --> 00:07:35,819
which this project is going to be
214
00:07:35,819 --> 00:07:40,620
created so we select Catalan if you Java
215
00:07:40,620 --> 00:07:43,319
is selected just select other B system
216
00:07:43,319 --> 00:07:46,380
we select IntelliJ and next we have the
217
00:07:46,380 --> 00:07:49,620
jdk the Java development kit and if for
218
00:07:49,620 --> 00:07:51,720
a terrible reason it says here in angle
219
00:07:51,720 --> 00:07:53,940
brackets no LDK just click on that and
220
00:07:53,940 --> 00:07:56,220
go down here off down off to afternoon
221
00:07:56,220 --> 00:08:00,000
it should say detected as jdks and you
222
00:08:00,000 --> 00:08:02,699
will see the jdk down there you're going
223
00:08:02,699 --> 00:08:04,979
to see Amazon correct or version 17.
224
00:08:04,979 --> 00:08:06,780
just click on that and it's going to be
225
00:08:06,780 --> 00:08:08,699
added here
226
00:08:08,699 --> 00:08:11,400
next we have this add sample code and
227
00:08:11,400 --> 00:08:12,900
what this is going to do as the naming
228
00:08:12,900 --> 00:08:15,900
price is gonna generate some code for us
229
00:08:15,900 --> 00:08:17,880
but I don't want to have that code
230
00:08:17,880 --> 00:08:20,699
generated uh now because I want to that
231
00:08:20,699 --> 00:08:23,400
type that code myself and I want to
232
00:08:23,400 --> 00:08:25,080
explain that code then in the next video
233
00:08:25,080 --> 00:08:26,759
we're gonna check that to generate the
234
00:08:26,759 --> 00:08:28,740
code for us so I'm gonna click on create
235
00:08:28,740 --> 00:08:32,000
Now to create the project
236
00:08:43,440 --> 00:08:45,300
and here we have the tips you can keep
237
00:08:45,300 --> 00:08:48,140
the tips activated because it's uh
238
00:08:48,140 --> 00:08:51,540
useful information but I'm gonna check
239
00:08:51,540 --> 00:08:55,699
this actually I'm gonna just close it
240
00:08:57,300 --> 00:08:59,519
here we have those windows which says
241
00:08:59,519 --> 00:09:01,980
there is a that is a plug-in update
242
00:09:01,980 --> 00:09:03,240
installed
243
00:09:03,240 --> 00:09:06,260
I'm going to close this
244
00:09:07,440 --> 00:09:09,360
so what we have now on the left hand
245
00:09:09,360 --> 00:09:11,459
side is called the project Pane and here
246
00:09:11,459 --> 00:09:14,279
we have some folders which IntelliJ IDEA
247
00:09:14,279 --> 00:09:17,760
generated for us but now I want to I
248
00:09:17,760 --> 00:09:19,680
want to show you how to check for
249
00:09:19,680 --> 00:09:21,959
IntelliJ updates because in this way
250
00:09:21,959 --> 00:09:23,160
we'll have the latest features
251
00:09:23,160 --> 00:09:24,899
throughout this course and to check for
252
00:09:24,899 --> 00:09:26,940
IntelliJ updates you go to help
253
00:09:26,940 --> 00:09:28,740
and to go down here where it says check
254
00:09:28,740 --> 00:09:30,120
for updates and here you can check for
255
00:09:30,120 --> 00:09:32,160
IntelliJ updates and you'll see a window
256
00:09:32,160 --> 00:09:34,200
down here and if there is an update it's
257
00:09:34,200 --> 00:09:36,360
gonna say that that there is an update
258
00:09:36,360 --> 00:09:38,640
and you can install that that update
259
00:09:38,640 --> 00:09:40,500
but now it's saying that
260
00:09:40,500 --> 00:09:42,060
that you already have the latest version
261
00:09:42,060 --> 00:09:44,100
of IntelliJ ID and plugins installed
262
00:09:44,100 --> 00:09:46,500
because we have the latest updates but
263
00:09:46,500 --> 00:09:49,080
it's good to check for this because in
264
00:09:49,080 --> 00:09:50,339
this way we'll have the latest updates
265
00:09:50,339 --> 00:09:51,899
so throughout this course
266
00:09:51,899 --> 00:09:54,540
next just check if you want to check for
267
00:09:54,540 --> 00:09:56,459
kotlin updates so for the programming
268
00:09:56,459 --> 00:09:58,620
language you kotlin you go to tools
269
00:09:58,620 --> 00:10:01,200
kotlin and go here it says configure
270
00:10:01,200 --> 00:10:04,560
custom plugin updates and if there is a
271
00:10:04,560 --> 00:10:05,940
new update you will see it here and
272
00:10:05,940 --> 00:10:07,560
click on install and that update is
273
00:10:07,560 --> 00:10:10,080
going to be installed
274
00:10:10,080 --> 00:10:12,959
on IntelliJ IDEA so there is no update
275
00:10:12,959 --> 00:10:14,640
because we have the latest update I'm
276
00:10:14,640 --> 00:10:16,140
going to click on OK
277
00:10:16,140 --> 00:10:17,880
now let's focus on the left hand side
278
00:10:17,880 --> 00:10:19,680
and if you don't see if you see
279
00:10:19,680 --> 00:10:20,880
something like this and you don't see
280
00:10:20,880 --> 00:10:23,640
this in this expanded form just click on
281
00:10:23,640 --> 00:10:26,519
this greater sign sign in front of his
282
00:10:26,519 --> 00:10:29,399
SRC SRC stands for Source this is where
283
00:10:29,399 --> 00:10:31,680
our source code is gonna go
284
00:10:31,680 --> 00:10:34,200
then click on this greater sign in front
285
00:10:34,200 --> 00:10:37,019
of Main and make sure to have the folder
286
00:10:37,019 --> 00:10:39,959
kotlin selected on this folder kotlin
287
00:10:39,959 --> 00:10:42,240
this is working out our code but first
288
00:10:42,240 --> 00:10:43,920
we need to create a file
289
00:10:43,920 --> 00:10:45,839
and the file is the thing where we're
290
00:10:45,839 --> 00:10:48,300
going to put our code so we right click
291
00:10:48,300 --> 00:10:50,519
on this go to new
292
00:10:50,519 --> 00:10:52,500
and here we select on the right hand
293
00:10:52,500 --> 00:10:55,019
side we select quoting class file
294
00:10:55,019 --> 00:10:57,480
now we need to give a name to our file
295
00:10:57,480 --> 00:10:58,980
and I'm going to call this file main
296
00:10:58,980 --> 00:11:02,220
you'll see the reason for this later so
297
00:11:02,220 --> 00:11:04,079
I'm going to call this file Main and
298
00:11:04,079 --> 00:11:05,880
then also you should be in Pascal case
299
00:11:05,880 --> 00:11:07,320
so if you start with the capital letter
300
00:11:07,320 --> 00:11:09,420
and if it has multiple words every words
301
00:11:09,420 --> 00:11:11,459
result the capital letter
302
00:11:11,459 --> 00:11:13,860
and for the from these options that we
303
00:11:13,860 --> 00:11:15,600
have here we select file because we're
304
00:11:15,600 --> 00:11:17,640
going to create a setup file now press
305
00:11:17,640 --> 00:11:19,880
enter
306
00:11:20,040 --> 00:11:22,740
now the file was created for us as you
307
00:11:22,740 --> 00:11:25,200
can see in the folder uh kotlin now we
308
00:11:25,200 --> 00:11:27,480
have main.at have the file and also to
309
00:11:27,480 --> 00:11:30,899
open the file for us let's uh
310
00:11:30,899 --> 00:11:33,300
click there now if you don't have the
311
00:11:33,300 --> 00:11:35,279
file open then if you have something
312
00:11:35,279 --> 00:11:36,959
like this just double click on the file
313
00:11:36,959 --> 00:11:39,480
and the file is going to be open for you
314
00:11:39,480 --> 00:11:40,620
now
315
00:11:40,620 --> 00:11:42,839
here now we're gonna type some code now
316
00:11:42,839 --> 00:11:45,180
you need to type exactly what I type so
317
00:11:45,180 --> 00:11:47,399
type fun
318
00:11:47,399 --> 00:11:50,220
so while I am typing as you can see
319
00:11:50,220 --> 00:11:52,560
IntelliJ is trying to help us and is
320
00:11:52,560 --> 00:11:54,420
giving us some suggestions so he's
321
00:11:54,420 --> 00:11:57,540
saying do you mean fun that and if you
322
00:11:57,540 --> 00:11:58,860
want to fill in that photo you just
323
00:11:58,860 --> 00:12:01,860
press enter Because I want to to to type
324
00:12:01,860 --> 00:12:04,800
that fun keyword fun and it also added
325
00:12:04,800 --> 00:12:07,200
the space so fun space
326
00:12:07,200 --> 00:12:09,000
then you type Main
327
00:12:09,000 --> 00:12:11,760
and next we need to put parenthesis and
328
00:12:11,760 --> 00:12:13,320
as you can see display button now
329
00:12:13,320 --> 00:12:14,459
appeared I'm going to explain
330
00:12:14,459 --> 00:12:15,959
immediately why
331
00:12:15,959 --> 00:12:18,420
and we put the left parenthes and when
332
00:12:18,420 --> 00:12:19,800
we put the left parenthes the right
333
00:12:19,800 --> 00:12:20,940
parenthesis is going to be added
334
00:12:20,940 --> 00:12:22,200
automatically
335
00:12:22,200 --> 00:12:24,180
so you put less parenthes and the right
336
00:12:24,180 --> 00:12:26,100
parenthesis was added automatically then
337
00:12:26,100 --> 00:12:28,740
we get outside of the parenthesis we
338
00:12:28,740 --> 00:12:31,560
press space now we're going to add curly
339
00:12:31,560 --> 00:12:33,600
braces and again we put the left
340
00:12:33,600 --> 00:12:35,160
calibrates and the record library is
341
00:12:35,160 --> 00:12:36,420
going to be added automatically because
342
00:12:36,420 --> 00:12:39,180
IntelliJ is helping us
343
00:12:39,180 --> 00:12:42,000
now we would inside the curly braces and
344
00:12:42,000 --> 00:12:43,440
now we press enter
345
00:12:43,440 --> 00:12:46,200
and it uh
346
00:12:46,200 --> 00:12:48,720
added the curly brace on the third line
347
00:12:48,720 --> 00:12:52,019
and also indented this line for us
348
00:12:52,019 --> 00:12:54,959
now let's talk about what we have here
349
00:12:54,959 --> 00:12:58,079
now what we have here is what is called
350
00:12:58,079 --> 00:13:00,480
the main function and the main function
351
00:13:00,480 --> 00:13:03,120
is the entry point of our program so
352
00:13:03,120 --> 00:13:05,220
every program that you create
353
00:13:05,220 --> 00:13:08,160
it if it first needs this entry point in
354
00:13:08,160 --> 00:13:11,100
order to be able to compile your code
355
00:13:11,100 --> 00:13:13,980
but let's take each part of this main
356
00:13:13,980 --> 00:13:16,079
function individually to talk about
357
00:13:16,079 --> 00:13:20,040
so first to have this fun and fun is the
358
00:13:20,040 --> 00:13:23,459
key word for creating a function and uh
359
00:13:23,459 --> 00:13:25,380
you can use fun keyword to create your
360
00:13:25,380 --> 00:13:27,060
own functions but you're gonna see that
361
00:13:27,060 --> 00:13:29,700
in uh in future videos
362
00:13:29,700 --> 00:13:32,220
next we have Main and Main is the name
363
00:13:32,220 --> 00:13:34,380
of this function functions can have
364
00:13:34,380 --> 00:13:36,839
different names but main is a special
365
00:13:36,839 --> 00:13:39,660
one as I said because main is the entry
366
00:13:39,660 --> 00:13:42,060
point of our program every program that
367
00:13:42,060 --> 00:13:44,040
you create needs and needs a main
368
00:13:44,040 --> 00:13:45,899
function in order to be able to run your
369
00:13:45,899 --> 00:13:48,540
code and in fact if I change the name of
370
00:13:48,540 --> 00:13:51,420
this function to fun main let's say a
371
00:13:51,420 --> 00:13:53,519
the play path button immediately
372
00:13:53,519 --> 00:13:56,160
disappear because now we don't have an
373
00:13:56,160 --> 00:13:59,040
entry point and our program can run so
374
00:13:59,040 --> 00:14:01,320
this is a special function and functions
375
00:14:01,320 --> 00:14:03,959
can have different names as I said and
376
00:14:03,959 --> 00:14:05,399
as you'll see we're gonna create your
377
00:14:05,399 --> 00:14:07,019
own functions
378
00:14:07,019 --> 00:14:09,300
that you can create you can give
379
00:14:09,300 --> 00:14:10,980
different names to your functions now
380
00:14:10,980 --> 00:14:13,740
next we have parenthesis and you have
381
00:14:13,740 --> 00:14:15,480
parentheses because
382
00:14:15,480 --> 00:14:18,720
um functions can take parameters and can
383
00:14:18,720 --> 00:14:20,220
do some work with that with those
384
00:14:20,220 --> 00:14:22,139
parameters but our function doesn't take
385
00:14:22,139 --> 00:14:24,839
any parameter but we still need to
386
00:14:24,839 --> 00:14:27,180
provide the parentheses because they are
387
00:14:27,180 --> 00:14:29,519
part of the syntax and we can think of
388
00:14:29,519 --> 00:14:32,579
the syntax like the grammar rules that
389
00:14:32,579 --> 00:14:36,200
you need to to to respect in a specific
390
00:14:36,200 --> 00:14:40,139
language in order to be to be a to under
391
00:14:40,139 --> 00:14:43,380
to be understood to be understood and in
392
00:14:43,380 --> 00:14:45,300
the same way this is the syntax in the
393
00:14:45,300 --> 00:14:47,220
programming it's like the grammar rules
394
00:14:47,220 --> 00:14:50,040
in which the compiler as you will see
395
00:14:50,040 --> 00:14:52,860
what is the compiler later
396
00:14:52,860 --> 00:14:55,160
the compiler needs
397
00:14:55,160 --> 00:14:57,480
enforces you to respect those
398
00:14:57,480 --> 00:15:00,540
syntactical rules which is uh to have
399
00:15:00,540 --> 00:15:02,820
parenthesis for a function in order to
400
00:15:02,820 --> 00:15:04,740
be able to understand your code because
401
00:15:04,740 --> 00:15:07,920
if I delete the parenthesis here
402
00:15:07,920 --> 00:15:10,620
now we have an underline which says uh
403
00:15:10,620 --> 00:15:14,940
expecting parentheses so it's it's it it
404
00:15:14,940 --> 00:15:17,639
you need to respect the syntactical
405
00:15:17,639 --> 00:15:19,620
rules in order for
406
00:15:19,620 --> 00:15:22,860
your code to be to be understood next we
407
00:15:22,860 --> 00:15:25,440
have curly braces and we have left curly
408
00:15:25,440 --> 00:15:28,199
brace and right curl base and this is
409
00:15:28,199 --> 00:15:31,019
called the body of the function so this
410
00:15:31,019 --> 00:15:33,420
is where our code goes so
411
00:15:33,420 --> 00:15:35,639
this is also called the code block
412
00:15:35,639 --> 00:15:37,940
because because it's the block of code
413
00:15:37,940 --> 00:15:42,600
this is what our logic our uh our
414
00:15:42,600 --> 00:15:45,600
these are our logic our lines of code go
415
00:15:45,600 --> 00:15:48,240
they are we Define them inside the the
416
00:15:48,240 --> 00:15:50,639
the curly braces so this represents the
417
00:15:50,639 --> 00:15:52,440
body of the function
418
00:15:52,440 --> 00:15:54,600
and let's add some code and I'm going to
419
00:15:54,600 --> 00:15:57,660
type print line here so because when we
420
00:15:57,660 --> 00:15:59,220
need to output something in the console
421
00:15:59,220 --> 00:16:01,440
so we type here print line
422
00:16:01,440 --> 00:16:05,760
so we type print and while I'm dumping
423
00:16:05,760 --> 00:16:08,100
you associate suggested and we have the
424
00:16:08,100 --> 00:16:10,139
suggestion print line and we select the
425
00:16:10,139 --> 00:16:12,779
first one to fill in for us and now we
426
00:16:12,779 --> 00:16:14,639
go inside the parenthesis of the print
427
00:16:14,639 --> 00:16:15,839
line
428
00:16:15,839 --> 00:16:19,019
and we put double quotation marks and
429
00:16:19,019 --> 00:16:20,940
inside the double quotation marks we
430
00:16:20,940 --> 00:16:23,480
type hello
431
00:16:24,240 --> 00:16:25,980
world
432
00:16:25,980 --> 00:16:28,260
now we need to
433
00:16:28,260 --> 00:16:30,540
we need to run this code in order to run
434
00:16:30,540 --> 00:16:33,000
this code to compile this code we can do
435
00:16:33,000 --> 00:16:36,120
it in uh two ways first you can click on
436
00:16:36,120 --> 00:16:39,060
display button that we have here
437
00:16:39,060 --> 00:16:42,240
or you can right click and you can go
438
00:16:42,240 --> 00:16:44,339
here where it says runmen.kt and the
439
00:16:44,339 --> 00:16:47,160
code is going to be compiled and let's
440
00:16:47,160 --> 00:16:48,720
do it from here first so I'm going to
441
00:16:48,720 --> 00:16:50,519
click on display button to run our code
442
00:16:50,519 --> 00:16:52,440
and I'm going to see the console opening
443
00:16:52,440 --> 00:16:53,759
down and you're going to see the text
444
00:16:53,759 --> 00:16:58,139
hello old hello world outputted
445
00:16:58,139 --> 00:17:00,600
so the console opened and we have hello
446
00:17:00,600 --> 00:17:02,279
world we have the text that we type
447
00:17:02,279 --> 00:17:04,980
inside this uh thing that we have here
448
00:17:04,980 --> 00:17:07,559
this also function and we have this code
449
00:17:07,559 --> 00:17:09,480
output now in the console the console
450
00:17:09,480 --> 00:17:12,900
opened now I'm gonna I'm gonna close the
451
00:17:12,900 --> 00:17:14,699
console to explain what this print line
452
00:17:14,699 --> 00:17:16,859
is this print line that we have here is
453
00:17:16,859 --> 00:17:19,380
a function also but is that the function
454
00:17:19,380 --> 00:17:23,099
which takes some which takes input and
455
00:17:23,099 --> 00:17:25,380
the input is this text and this function
456
00:17:25,380 --> 00:17:29,280
the its job is to take whatever you pass
457
00:17:29,280 --> 00:17:32,580
as the input and to Output that in the
458
00:17:32,580 --> 00:17:35,340
console so this is what this print line
459
00:17:35,340 --> 00:17:37,200
is doing and this is a function that we
460
00:17:37,200 --> 00:17:40,080
didn't create we didn't type free fun
461
00:17:40,080 --> 00:17:43,440
print line and then you call it this is
462
00:17:43,440 --> 00:17:45,480
a function which is uh
463
00:17:45,480 --> 00:17:48,780
build into quantity this is a function
464
00:17:48,780 --> 00:17:50,580
which is already built by the kotlin
465
00:17:50,580 --> 00:17:53,460
programmers so we just call that
466
00:17:53,460 --> 00:17:55,140
function inside our code and we can use
467
00:17:55,140 --> 00:17:56,580
you need you don't need to create this
468
00:17:56,580 --> 00:17:58,919
function so this is the this is what
469
00:17:58,919 --> 00:18:00,480
this print line is doing so it's taking
470
00:18:00,480 --> 00:18:02,520
whatever you pass inside the parentheses
471
00:18:02,520 --> 00:18:05,820
and it's outputting that in the console
472
00:18:05,820 --> 00:18:09,600
so this is our discussion uh about uh
473
00:18:09,600 --> 00:18:12,120
the first program which is to Output
474
00:18:12,120 --> 00:18:15,419
some text in the console hello world and
475
00:18:15,419 --> 00:18:18,600
uh now I'm gonna show you that I know
476
00:18:18,600 --> 00:18:20,100
I'm going to show you an illustration to
477
00:18:20,100 --> 00:18:22,559
understand what is uh
478
00:18:22,559 --> 00:18:27,480
what is the jdk the jvm and the how
479
00:18:27,480 --> 00:18:29,760
kotlin and Java achieves this
480
00:18:29,760 --> 00:18:32,460
interoperability because Kathleen and
481
00:18:32,460 --> 00:18:34,140
Java are fully interoperable and that
482
00:18:34,140 --> 00:18:35,640
means that you can
483
00:18:35,640 --> 00:18:38,160
have a Java project which you can add
484
00:18:38,160 --> 00:18:40,140
kotlin code and you can compile that
485
00:18:40,140 --> 00:18:42,120
code and everything to work fine and
486
00:18:42,120 --> 00:18:43,919
also I'm going to explain what the what
487
00:18:43,919 --> 00:18:46,260
the word compile means
488
00:18:46,260 --> 00:18:47,820
and for that I'm going to bring an
489
00:18:47,820 --> 00:18:50,580
illustration here
490
00:18:50,580 --> 00:18:54,059
so this is the illustration and uh
491
00:18:54,059 --> 00:18:56,940
let's talk about what we have here first
492
00:18:56,940 --> 00:18:59,400
we have kotlin source code and kotlin
493
00:18:59,400 --> 00:19:02,280
source code is the code that we wrote is
494
00:19:02,280 --> 00:19:05,460
this squad that is meant to be human
495
00:19:05,460 --> 00:19:08,340
readable so this code is the code that
496
00:19:08,340 --> 00:19:11,940
we as humans can understand we can write
497
00:19:11,940 --> 00:19:14,940
this code we can read this quad but the
498
00:19:14,940 --> 00:19:16,740
computer does understand this high level
499
00:19:16,740 --> 00:19:19,620
uh high level uh quote that we wrote it
500
00:19:19,620 --> 00:19:21,600
needs something it need it needs machine
501
00:19:21,600 --> 00:19:23,580
code it needs binary code in order to be
502
00:19:23,580 --> 00:19:25,320
able to understand
503
00:19:25,320 --> 00:19:28,140
and this is where the jdk comes into
504
00:19:28,140 --> 00:19:29,820
action so
505
00:19:29,820 --> 00:19:32,880
our continent source code again this
506
00:19:32,880 --> 00:19:35,940
quad which is meant to be human readable
507
00:19:35,940 --> 00:19:39,600
then it's when when we press on that
508
00:19:39,600 --> 00:19:42,120
play button on that and when you press
509
00:19:42,120 --> 00:19:44,820
on that play button on this button and
510
00:19:44,820 --> 00:19:47,580
the code is compiled
511
00:19:47,580 --> 00:19:49,980
what happens under the hood is that that
512
00:19:49,980 --> 00:19:53,940
quoting source code is is uh taken by
513
00:19:53,940 --> 00:19:57,360
the Java development kit the jdk and
514
00:19:57,360 --> 00:19:59,400
through the kotlin compiler so the
515
00:19:59,400 --> 00:20:01,440
controlling compiler acts like a
516
00:20:01,440 --> 00:20:03,660
translator it takes that high level
517
00:20:03,660 --> 00:20:07,260
human readable and writable code
518
00:20:07,260 --> 00:20:10,500
it converts that code then into what is
519
00:20:10,500 --> 00:20:13,500
called Java byte code and this is not
520
00:20:13,500 --> 00:20:15,179
the final step as you can see in this
521
00:20:15,179 --> 00:20:17,160
illustration this this is just the
522
00:20:17,160 --> 00:20:20,179
intermediary step this is not uh
523
00:20:20,179 --> 00:20:23,100
converted into machine code
524
00:20:23,100 --> 00:20:27,480
then this Java byte code it's uh
525
00:20:27,480 --> 00:20:31,260
taken by the jvn so it's sent to the jvm
526
00:20:31,260 --> 00:20:34,679
and the jvm then takes that Java by code
527
00:20:34,679 --> 00:20:37,679
and convert the jbox.java by code into
528
00:20:37,679 --> 00:20:40,320
something which a machine which a
529
00:20:40,320 --> 00:20:43,620
specific machine can understand but
530
00:20:43,620 --> 00:20:44,820
let's
531
00:20:44,820 --> 00:20:49,020
let's first explain why why kotlin uh
532
00:20:49,020 --> 00:20:51,660
why the jdk takes this kotlin source
533
00:20:51,660 --> 00:20:53,580
code and then it converts this kotlin
534
00:20:53,580 --> 00:20:55,919
source code into Java byte code
535
00:20:55,919 --> 00:20:59,100
and this is doing this because different
536
00:20:59,100 --> 00:21:01,919
processors and if different operating
537
00:21:01,919 --> 00:21:05,640
systems need different instructions in
538
00:21:05,640 --> 00:21:08,940
order to understand your code so if we
539
00:21:08,940 --> 00:21:12,419
are not will not to have this Java byte
540
00:21:12,419 --> 00:21:16,980
code so if we had we'll uh compile our
541
00:21:16,980 --> 00:21:19,679
code on a specific platform that code
542
00:21:19,679 --> 00:21:22,020
can run only on that specific platform
543
00:21:22,020 --> 00:21:24,240
but because our quadrant source code is
544
00:21:24,240 --> 00:21:26,580
taken by the jdk and through the
545
00:21:26,580 --> 00:21:28,140
quarterly compiler and is converted into
546
00:21:28,140 --> 00:21:30,299
Java byte code
547
00:21:30,299 --> 00:21:34,380
and the Java byte code then it's
548
00:21:34,380 --> 00:21:37,380
it's uh it it's send it to the jvm and
549
00:21:37,380 --> 00:21:39,600
jvm stands for Java virtual machine and
550
00:21:39,600 --> 00:21:42,419
the jvm then because different platforms
551
00:21:42,419 --> 00:21:44,880
and different operating
552
00:21:44,880 --> 00:21:46,559
uh different platform and different
553
00:21:46,559 --> 00:21:49,919
processors need different instructions
554
00:21:49,919 --> 00:21:53,039
the jvm is the one which is doing the
555
00:21:53,039 --> 00:21:55,679
hard work of figure out figuring out
556
00:21:55,679 --> 00:21:58,080
what kind of instruction that specific
557
00:21:58,080 --> 00:22:01,020
platform needs so it converts that Java
558
00:22:01,020 --> 00:22:02,940
by code into machine code let's say
559
00:22:02,940 --> 00:22:05,659
which a Windows platform can understand
560
00:22:05,659 --> 00:22:09,840
then the same thing happens to Linux the
561
00:22:09,840 --> 00:22:13,080
J the jvm receives the same Java byte
562
00:22:13,080 --> 00:22:15,900
code and then it converts that Java by
563
00:22:15,900 --> 00:22:17,940
according to machine code which a Linux
564
00:22:17,940 --> 00:22:20,100
platform can understand and then is
565
00:22:20,100 --> 00:22:22,559
doing the same thing
566
00:22:22,559 --> 00:22:27,659
to a Mac OS so the make waves the jvm is
567
00:22:27,659 --> 00:22:30,059
gonna is gonna receive the same Java
568
00:22:30,059 --> 00:22:32,280
byte quad and it's going to convert that
569
00:22:32,280 --> 00:22:34,320
Java by code into machine code which
570
00:22:34,320 --> 00:22:38,039
Macos can understand so this is how uh
571
00:22:38,039 --> 00:22:40,679
kotlin and Java can achieve this call
572
00:22:40,679 --> 00:22:43,320
this thing called Write once and run
573
00:22:43,320 --> 00:22:46,080
everywhere this is because the
574
00:22:46,080 --> 00:22:51,020
the work of converting that machine code
575
00:22:51,020 --> 00:22:54,780
which which a specific platform Cananda
576
00:22:54,780 --> 00:22:57,840
said has shifted from the programmer to
577
00:22:57,840 --> 00:23:00,900
the platform which has the which
578
00:23:00,900 --> 00:23:03,900
implements that jvm so we just wrote Our
579
00:23:03,900 --> 00:23:07,559
quad we run our code through the jdk the
580
00:23:07,559 --> 00:23:10,260
jdk converted that code in Java byte
581
00:23:10,260 --> 00:23:12,659
code and then the platform which has the
582
00:23:12,659 --> 00:23:13,820
jvm
583
00:23:13,820 --> 00:23:17,640
installed uh it's doing the work of
584
00:23:17,640 --> 00:23:20,159
converting that Java by code that we
585
00:23:20,159 --> 00:23:22,260
wrote into something which that platform
586
00:23:22,260 --> 00:23:25,679
that specific platform can understand so
587
00:23:25,679 --> 00:23:29,280
and this is why uh Java and Kathleen are
588
00:23:29,280 --> 00:23:32,120
fully interoperable so you can add
589
00:23:32,120 --> 00:23:34,919
you can add Java code to an existing
590
00:23:34,919 --> 00:23:38,100
kotlin program and vice versa you cannot
591
00:23:38,100 --> 00:23:41,340
uh you can add the Java you can add the
592
00:23:41,340 --> 00:23:44,159
quota link code to an existing Java
593
00:23:44,159 --> 00:23:47,340
program and that is uh that is possible
594
00:23:47,340 --> 00:23:52,080
because uh the jdk is gonna is gonna
595
00:23:52,080 --> 00:23:54,539
convert through the compiler
596
00:23:54,539 --> 00:23:56,880
all of that code into the same Java by
597
00:23:56,880 --> 00:23:59,039
code and then that code is going to be
598
00:23:59,039 --> 00:24:01,620
run on the jvm and the jvm is going to
599
00:24:01,620 --> 00:24:03,480
convert that into machine code and that
600
00:24:03,480 --> 00:24:05,159
code is going to run on a specific
601
00:24:05,159 --> 00:24:08,340
platform so this is how Java and kotlin
602
00:24:08,340 --> 00:24:10,799
and this is why Java and collecting and
603
00:24:10,799 --> 00:24:13,500
are fully interoperable because
604
00:24:13,500 --> 00:24:17,039
they uh their core they are ultimately
605
00:24:17,039 --> 00:24:19,760
converted into the same Java by code and
606
00:24:19,760 --> 00:24:23,159
you can also achieve this right once run
607
00:24:23,159 --> 00:24:24,539
everywhere because
608
00:24:24,539 --> 00:24:27,659
that code then runs on the jvm Java
609
00:24:27,659 --> 00:24:29,520
virtual machine which is doing the hard
610
00:24:29,520 --> 00:24:32,340
work or figure out what kind of
611
00:24:32,340 --> 00:24:35,340
instructions uh specific platform needs
612
00:24:35,340 --> 00:24:38,400
in order to understand that code that we
613
00:24:38,400 --> 00:24:42,360
wrote and in this way that hard working
614
00:24:42,360 --> 00:24:45,840
has shifted from us to the people who
615
00:24:45,840 --> 00:24:47,460
implement the jvm
616
00:24:47,460 --> 00:24:51,179
so this is how the jdk and The jvm Works
617
00:24:51,179 --> 00:24:52,200
under the hood
618
00:24:52,200 --> 00:24:54,720
and if you find this confusing don't be
619
00:24:54,720 --> 00:24:57,360
just this is just a relaxed discussion
620
00:24:57,360 --> 00:25:02,340
about uh how the jdk what is the jdk and
621
00:25:02,340 --> 00:25:04,980
what is the kotlin compiler because I
622
00:25:04,980 --> 00:25:07,500
said our cotton I said previously that
623
00:25:07,500 --> 00:25:09,659
our code is compiled when I said that
624
00:25:09,659 --> 00:25:11,820
this actually what was happening our
625
00:25:11,820 --> 00:25:13,820
code was taken by the
626
00:25:13,820 --> 00:25:16,320
jdkn through the jdk was covered in Java
627
00:25:16,320 --> 00:25:19,799
bytecode then it was sent to the jvm of
628
00:25:19,799 --> 00:25:23,580
uh which is on my windows and then that
629
00:25:23,580 --> 00:25:25,440
is converted into machine code and then
630
00:25:25,440 --> 00:25:28,320
we see in the output hello world
631
00:25:28,320 --> 00:25:31,320
so this is our discussion about the
632
00:25:31,320 --> 00:25:32,520
first program how to create the first
633
00:25:32,520 --> 00:25:33,840
program hello world and this is the
634
00:25:33,840 --> 00:25:36,120
additional discussion about how the jdk
635
00:25:36,120 --> 00:25:38,820
how the quartering compiler and how the
636
00:25:38,820 --> 00:25:41,700
jvm works so see you in the next video
637
00:25:41,700 --> 00:25:44,159
so in the previous video we created our
638
00:25:44,159 --> 00:25:46,200
first program and even though it was a
639
00:25:46,200 --> 00:25:48,240
simple program you should celebrate
640
00:25:48,240 --> 00:25:50,039
because you created your first program
641
00:25:50,039 --> 00:25:52,020
but if you want to make your program to
642
00:25:52,020 --> 00:25:54,179
do something useful you need to store
643
00:25:54,179 --> 00:25:56,220
data and in order to store data we need
644
00:25:56,220 --> 00:25:58,260
to use variables and this is what we're
645
00:25:58,260 --> 00:26:00,179
gonna talk about in this video therefore
646
00:26:00,179 --> 00:26:03,559
I'm going to open IntelliJ IDEA
647
00:26:07,919 --> 00:26:10,020
I'm gonna create a new project so make
648
00:26:10,020 --> 00:26:11,820
sure to that on the left hand side you
649
00:26:11,820 --> 00:26:13,740
have project selected click on this plus
650
00:26:13,740 --> 00:26:17,000
button to create a new project
651
00:26:17,299 --> 00:26:20,580
I'm going to call it variables it should
652
00:26:20,580 --> 00:26:22,200
be in Pascal case if it has multiple
653
00:26:22,200 --> 00:26:23,640
words language we have already
654
00:26:23,640 --> 00:26:25,380
constantly selected for the build system
655
00:26:25,380 --> 00:26:28,140
IntelliJ and also make sure to have the
656
00:26:28,140 --> 00:26:30,480
jdk selected here
657
00:26:30,480 --> 00:26:32,520
and now I'm gonna check this out sample
658
00:26:32,520 --> 00:26:33,960
code because this is going to generate
659
00:26:33,960 --> 00:26:35,900
the code that we typed previously
660
00:26:35,900 --> 00:26:38,520
respectively the main function and the
661
00:26:38,520 --> 00:26:41,760
print line to and the hello world
662
00:26:41,760 --> 00:26:43,860
text is going to be now Auto generated
663
00:26:43,860 --> 00:26:47,460
for us by because we check this box add
664
00:26:47,460 --> 00:26:49,380
sample code and it's going to add the
665
00:26:49,380 --> 00:26:51,059
code automatically for us so I'm going
666
00:26:51,059 --> 00:26:53,658
to click on create
667
00:27:13,020 --> 00:27:14,580
now
668
00:27:14,580 --> 00:27:18,480
our project is ready to go and as you
669
00:27:18,480 --> 00:27:20,520
can see here it generated some code for
670
00:27:20,520 --> 00:27:22,860
us and this is similar to what we've did
671
00:27:22,860 --> 00:27:24,600
in the previous video where we created
672
00:27:24,600 --> 00:27:28,320
our we'll create our code ourselves but
673
00:27:28,320 --> 00:27:30,659
we have some additional code and uh I'm
674
00:27:30,659 --> 00:27:32,340
gonna delete this additional record
675
00:27:32,340 --> 00:27:34,919
because it will make things confusing so
676
00:27:34,919 --> 00:27:37,559
I'm going to delete this part
677
00:27:37,559 --> 00:27:41,600
sorry I'm gonna delete this part
678
00:27:41,700 --> 00:27:45,659
and also uh you have may have noticed
679
00:27:45,659 --> 00:27:48,840
that uh here in inside this parenthesis
680
00:27:48,840 --> 00:27:51,539
we now we have this args array and
681
00:27:51,539 --> 00:27:52,740
string
682
00:27:52,740 --> 00:27:55,740
we can delete this too because
683
00:27:55,740 --> 00:27:59,640
it will make I think things confusing
684
00:27:59,640 --> 00:28:02,039
and we're going to talk about what are
685
00:28:02,039 --> 00:28:04,500
the arrays and all of that in the next
686
00:28:04,500 --> 00:28:05,940
videos
687
00:28:05,940 --> 00:28:08,779
now I'm gonna
688
00:28:09,600 --> 00:28:11,940
all right now ready
689
00:28:11,940 --> 00:28:16,260
now let's run this code to see
690
00:28:16,260 --> 00:28:17,820
to see what is happening it's gonna
691
00:28:17,820 --> 00:28:21,779
output that text like it did in the
692
00:28:21,779 --> 00:28:24,260
previous video
693
00:28:28,620 --> 00:28:30,480
you should open the console in a short
694
00:28:30,480 --> 00:28:31,860
moment
695
00:28:31,860 --> 00:28:33,720
and indeed it opened the console and it
696
00:28:33,720 --> 00:28:36,000
output the text hello world like it did
697
00:28:36,000 --> 00:28:37,380
in the previous video so I'm going to
698
00:28:37,380 --> 00:28:40,320
close the console now also let's expand
699
00:28:40,320 --> 00:28:41,880
this SRC
700
00:28:41,880 --> 00:28:44,640
to see the file that it was created
701
00:28:44,640 --> 00:28:47,820
automatically for us this time
702
00:28:47,820 --> 00:28:49,740
and um
703
00:28:49,740 --> 00:28:52,080
let's change this hello world text to
704
00:28:52,080 --> 00:28:54,659
print another text let's say
705
00:28:54,659 --> 00:28:57,900
let's print the
706
00:28:57,900 --> 00:29:00,179
type your name there in my case is Alex
707
00:29:00,179 --> 00:29:02,460
so I'm going to run this quad
708
00:29:02,460 --> 00:29:05,159
and it will output my name in the
709
00:29:05,159 --> 00:29:07,520
console
710
00:29:10,140 --> 00:29:13,380
all right so it outputted Hello Alex
711
00:29:13,380 --> 00:29:16,980
like we typed here
712
00:29:16,980 --> 00:29:18,840
but um
713
00:29:18,840 --> 00:29:21,299
it's what we have here is pretty pretty
714
00:29:21,299 --> 00:29:23,880
inflexible it's a better way to be to to
715
00:29:23,880 --> 00:29:27,960
do that it will be to have something to
716
00:29:27,960 --> 00:29:31,919
store this name and then refer that that
717
00:29:31,919 --> 00:29:34,200
thing here to call that thing here and
718
00:29:34,200 --> 00:29:38,880
this will be a bit more more dynamic
719
00:29:38,880 --> 00:29:42,120
and to do that we need to use variables
720
00:29:42,120 --> 00:29:44,520
and variables are
721
00:29:44,520 --> 00:29:47,700
are like some containers which can hold
722
00:29:47,700 --> 00:29:49,559
data values
723
00:29:49,559 --> 00:29:52,140
and
724
00:29:52,140 --> 00:29:55,200
let's see how to create a variable okay
725
00:29:55,200 --> 00:29:59,299
Let's uh put the the the code below
726
00:29:59,299 --> 00:30:03,360
above our print Ln statement
727
00:30:03,360 --> 00:30:06,120
and if you don't know what a statement
728
00:30:06,120 --> 00:30:08,520
is a statement is of complete
729
00:30:08,520 --> 00:30:10,860
instruction of execution
730
00:30:10,860 --> 00:30:15,419
Now to create a variable we just type
731
00:30:15,419 --> 00:30:18,059
the keyword far
732
00:30:18,059 --> 00:30:20,940
which stands for variable
733
00:30:20,940 --> 00:30:22,620
all right
734
00:30:22,620 --> 00:30:25,200
next we need to give a name to our very
735
00:30:25,200 --> 00:30:27,179
a variable to be able to refer it later
736
00:30:27,179 --> 00:30:29,220
to call it later and I'm going to end
737
00:30:29,220 --> 00:30:31,559
the naming convention around the naming
738
00:30:31,559 --> 00:30:34,679
variable is called is called camel case
739
00:30:34,679 --> 00:30:38,520
and that means that the first
740
00:30:38,520 --> 00:30:41,820
letter in the name if it has multiple
741
00:30:41,820 --> 00:30:44,520
words so start with the lowercase letter
742
00:30:44,520 --> 00:30:46,919
so I'm going to type user
743
00:30:46,919 --> 00:30:49,679
and the next word name with the capital
744
00:30:49,679 --> 00:30:50,940
letter
745
00:30:50,940 --> 00:30:52,980
all right next we need to provide the
746
00:30:52,980 --> 00:30:55,860
type of the variable and that means what
747
00:30:55,860 --> 00:30:57,659
kind of data this variable is going to
748
00:30:57,659 --> 00:30:59,760
hold it's going to store to do that we
749
00:30:59,760 --> 00:31:02,220
need to put colon and next we need to
750
00:31:02,220 --> 00:31:04,799
say what type of data is going to store
751
00:31:04,799 --> 00:31:06,059
what
752
00:31:06,059 --> 00:31:09,120
what data is going to store and we want
753
00:31:09,120 --> 00:31:12,120
to store text and to store text we need
754
00:31:12,120 --> 00:31:14,159
to type here string
755
00:31:14,159 --> 00:31:16,020
and string means that this variable is
756
00:31:16,020 --> 00:31:17,940
going to store a single a sequence of
757
00:31:17,940 --> 00:31:19,740
characters or text
758
00:31:19,740 --> 00:31:20,960
now
759
00:31:20,960 --> 00:31:23,940
we need to assign a value to this
760
00:31:23,940 --> 00:31:25,919
variable and to do that we'll put we put
761
00:31:25,919 --> 00:31:28,500
here equals
762
00:31:28,500 --> 00:31:30,299
space
763
00:31:30,299 --> 00:31:33,419
and because we express the cells here
764
00:31:33,419 --> 00:31:35,820
that we're going to put text text or
765
00:31:35,820 --> 00:31:38,820
string we put a double pair of quotation
766
00:31:38,820 --> 00:31:42,779
marks and we type Alex
767
00:31:42,779 --> 00:31:45,539
all right so now we create our variable
768
00:31:45,539 --> 00:31:47,279
we
769
00:31:47,279 --> 00:31:52,440
give it a name a type and with with also
770
00:31:52,440 --> 00:31:54,600
sine of value to this variable next we
771
00:31:54,600 --> 00:31:57,020
need to refer this variable down here
772
00:31:57,020 --> 00:32:01,919
and to to to refer that variable I'm
773
00:32:01,919 --> 00:32:04,200
going to delete this text first and to
774
00:32:04,200 --> 00:32:07,860
refer the variable we put dollar sign
775
00:32:07,860 --> 00:32:10,440
and after the dollar sign we start
776
00:32:10,440 --> 00:32:14,039
typing the name of the variable user and
777
00:32:14,039 --> 00:32:17,039
as I'm typing the IntelliJ is giving us
778
00:32:17,039 --> 00:32:19,020
some suggestions and it said do you mean
779
00:32:19,020 --> 00:32:22,919
this username variable and it also has
780
00:32:22,919 --> 00:32:25,200
this V here for variable so I'm going to
781
00:32:25,200 --> 00:32:27,899
press enter to fill in that for me
782
00:32:27,899 --> 00:32:30,918
and if I
783
00:32:31,320 --> 00:32:33,600
run this code
784
00:32:33,600 --> 00:32:36,980
you'll see the exact
785
00:32:37,440 --> 00:32:39,000
singer
786
00:32:39,000 --> 00:32:41,399
Happening Here It outputted the text
787
00:32:41,399 --> 00:32:44,940
Hello Alex but this time we didn't type
788
00:32:44,940 --> 00:32:47,279
the text directly here which we we
789
00:32:47,279 --> 00:32:49,500
stored the text in a variable and we
790
00:32:49,500 --> 00:32:51,840
call that variable in uh in our
791
00:32:51,840 --> 00:32:53,580
printerland I'm going to add a space
792
00:32:53,580 --> 00:32:55,020
here
793
00:32:55,020 --> 00:32:56,820
to make things more clear
794
00:32:56,820 --> 00:32:59,880
now what we can do is to change the
795
00:32:59,880 --> 00:33:03,200
value of this variable by
796
00:33:03,200 --> 00:33:07,260
adding below of our variable typing the
797
00:33:07,260 --> 00:33:10,640
name of the variable again user name
798
00:33:10,640 --> 00:33:13,380
equals and now we can assign a new value
799
00:33:13,380 --> 00:33:17,519
to this variable and let's put
800
00:33:17,519 --> 00:33:19,679
let's put John here
801
00:33:19,679 --> 00:33:21,720
all right let's run this code to see
802
00:33:21,720 --> 00:33:25,019
what happens now now this time when this
803
00:33:25,019 --> 00:33:27,179
uh this instruction is going to be
804
00:33:27,179 --> 00:33:29,880
called it's going to print then the new
805
00:33:29,880 --> 00:33:33,360
value of the variable which is John
806
00:33:33,360 --> 00:33:36,240
so as you can see down here it output
807
00:33:36,240 --> 00:33:38,700
the text hello John so we can change the
808
00:33:38,700 --> 00:33:41,580
value of the variable after we assign a
809
00:33:41,580 --> 00:33:47,100
value to it but what you can't do is to
810
00:33:47,100 --> 00:33:50,700
is to type here username
811
00:33:50,700 --> 00:33:53,460
equals 5.
812
00:33:53,460 --> 00:33:57,059
and if you hover over here
813
00:33:57,059 --> 00:33:58,980
it says integer literature does not
814
00:33:58,980 --> 00:34:01,799
conform to the specific type string and
815
00:34:01,799 --> 00:34:04,200
in other words it's saying that we said
816
00:34:04,200 --> 00:34:06,779
explicit explicitly up here that is
817
00:34:06,779 --> 00:34:09,719
gonna store only text and down here we
818
00:34:09,719 --> 00:34:13,739
try to put a whole number an integer and
819
00:34:13,739 --> 00:34:15,960
that is not going to work because kotlin
820
00:34:15,960 --> 00:34:20,219
is a link language which is uh which is
821
00:34:20,219 --> 00:34:22,260
called statically typed
822
00:34:22,260 --> 00:34:25,560
and that means that it it will check the
823
00:34:25,560 --> 00:34:28,560
types of the variable at compile time
824
00:34:28,560 --> 00:34:31,260
not at run time and that means it does
825
00:34:31,260 --> 00:34:33,960
it you cannot run your code if you
826
00:34:33,960 --> 00:34:37,859
assign um a value to to a variable which
827
00:34:37,859 --> 00:34:40,739
has a different type that done done the
828
00:34:40,739 --> 00:34:43,859
type that you specify specified when you
829
00:34:43,859 --> 00:34:46,199
record the variable this is why we get
830
00:34:46,199 --> 00:34:47,460
this warning here
831
00:34:47,460 --> 00:34:50,599
and if I delete this
832
00:34:53,580 --> 00:34:56,699
now let's delete this
833
00:34:56,699 --> 00:34:59,220
all right
834
00:34:59,220 --> 00:35:00,480
okay
835
00:35:00,480 --> 00:35:03,500
the next type of variable that
836
00:35:03,500 --> 00:35:07,080
also actually not the next type the word
837
00:35:07,080 --> 00:35:08,940
over the next way of declaring a
838
00:35:08,940 --> 00:35:11,400
variable is using the keyword
839
00:35:11,400 --> 00:35:12,780
file
840
00:35:12,780 --> 00:35:14,820
so if you declare if you declare a
841
00:35:14,820 --> 00:35:18,240
variable as Val Val means that we cannot
842
00:35:18,240 --> 00:35:22,740
change its value after we assign a value
843
00:35:22,740 --> 00:35:25,560
to it so if you try if you try to assign
844
00:35:25,560 --> 00:35:27,300
a new value to our variable like we did
845
00:35:27,300 --> 00:35:31,140
here up here it will not work so let's
846
00:35:31,140 --> 00:35:34,200
actually let's actually just change here
847
00:35:34,200 --> 00:35:36,660
let's put Val
848
00:35:36,660 --> 00:35:40,560
okay and and after I did that you it
849
00:35:40,560 --> 00:35:43,560
immediately underlying red username and
850
00:35:43,560 --> 00:35:46,260
it says Val cannot be reassigned and
851
00:35:46,260 --> 00:35:48,900
that is because if you declare if you
852
00:35:48,900 --> 00:35:51,240
declare a variable as Val
853
00:35:51,240 --> 00:35:54,420
you cannot change its value later it
854
00:35:54,420 --> 00:35:57,660
will you can put a value to that
855
00:35:57,660 --> 00:36:00,839
variable only once and that's all we
856
00:36:00,839 --> 00:36:02,940
cannot change the very the value of the
857
00:36:02,940 --> 00:36:05,040
variable later
858
00:36:05,040 --> 00:36:08,400
so let's change this back to VAR
859
00:36:08,400 --> 00:36:12,920
to have the ability to change the value
860
00:36:13,140 --> 00:36:15,780
now let's look at another type of
861
00:36:15,780 --> 00:36:19,020
variable so I'm gonna type VAR
862
00:36:19,020 --> 00:36:20,700
again
863
00:36:20,700 --> 00:36:24,078
I'm going to call it age
864
00:36:25,380 --> 00:36:28,079
I'm going to put colon to specify a type
865
00:36:28,079 --> 00:36:30,359
here and now we're going to use another
866
00:36:30,359 --> 00:36:33,599
type which is int and int stands for
867
00:36:33,599 --> 00:36:36,599
integer or for whole numbers now we need
868
00:36:36,599 --> 00:36:38,099
to assign a value to it I'm going to
869
00:36:38,099 --> 00:36:42,180
call I'm going to put you know 10 or you
870
00:36:42,180 --> 00:36:44,220
know 20
871
00:36:44,220 --> 00:36:46,560
22
872
00:36:46,560 --> 00:36:49,440
all right so what we have now is a
873
00:36:49,440 --> 00:36:52,020
different type up here we have string
874
00:36:52,020 --> 00:36:53,760
and that means that we're going to we're
875
00:36:53,760 --> 00:36:54,900
going to store sequence of characters
876
00:36:54,900 --> 00:36:58,020
and down here we have integers and that
877
00:36:58,020 --> 00:36:59,880
means that we can store
878
00:36:59,880 --> 00:37:03,480
um only only numbers only whole numbers
879
00:37:03,480 --> 00:37:05,520
and there are different type of numbers
880
00:37:05,520 --> 00:37:07,560
which you're gonna look in the next
881
00:37:07,560 --> 00:37:08,579
video
882
00:37:08,579 --> 00:37:12,240
and if I try here to change this uh
883
00:37:12,240 --> 00:37:15,839
to change this so first first let's
884
00:37:15,839 --> 00:37:17,820
let's actually
885
00:37:17,820 --> 00:37:21,119
let's actually print this uh
886
00:37:21,119 --> 00:37:23,940
Hello Alex
887
00:37:23,940 --> 00:37:26,040
let's type your
888
00:37:26,040 --> 00:37:31,020
age is and here you put dollar sign
889
00:37:31,020 --> 00:37:33,839
and we call uh the name of the variable
890
00:37:33,839 --> 00:37:36,359
without the name of the variable age so
891
00:37:36,359 --> 00:37:39,119
we press enter to fill in that for us
892
00:37:39,119 --> 00:37:42,800
and if you run this code
893
00:37:47,880 --> 00:37:52,980
now says hello John your age is 22. so
894
00:37:52,980 --> 00:37:55,740
now it's using the value the values of
895
00:37:55,740 --> 00:37:58,200
this variable in our printerland and we
896
00:37:58,200 --> 00:38:01,260
can change for the variable also the its
897
00:38:01,260 --> 00:38:04,020
value because it's declared as a VAR not
898
00:38:04,020 --> 00:38:06,720
a Val so we can put here age equals
899
00:38:06,720 --> 00:38:08,940
let's say 25
900
00:38:08,940 --> 00:38:12,079
and if you run this code
901
00:38:12,540 --> 00:38:15,780
the change is reflected immediately in
902
00:38:15,780 --> 00:38:18,599
our code so now we see hello John your
903
00:38:18,599 --> 00:38:20,160
age is 25.
904
00:38:20,160 --> 00:38:24,320
what we can't do like in the previous
905
00:38:24,320 --> 00:38:27,180
example with the username Here We cannot
906
00:38:27,180 --> 00:38:30,240
put a string We cannot put hit
907
00:38:30,240 --> 00:38:31,920
Alex
908
00:38:31,920 --> 00:38:34,079
because we said explicitly here that
909
00:38:34,079 --> 00:38:36,480
this is gonna is gonna hold only
910
00:38:36,480 --> 00:38:38,820
integers only whole numbers so I'm gonna
911
00:38:38,820 --> 00:38:42,359
delete this let's put it again to 25 and
912
00:38:42,359 --> 00:38:45,119
the arrow error disappeared
913
00:38:45,119 --> 00:38:47,040
all right now
914
00:38:47,040 --> 00:38:49,619
let's look at what we have here because
915
00:38:49,619 --> 00:38:52,920
we have this underlines this uh
916
00:38:52,920 --> 00:38:56,940
uh great light out of our types and
917
00:38:56,940 --> 00:38:59,240
every time you see something like this
918
00:38:59,240 --> 00:39:01,800
that means that IntelliJ is trying to
919
00:39:01,800 --> 00:39:03,480
say something to you and if you hover
920
00:39:03,480 --> 00:39:05,900
over here
921
00:39:07,140 --> 00:39:10,079
actually let's hover over over the type
922
00:39:10,079 --> 00:39:13,079
it says explicitly given type it's
923
00:39:13,079 --> 00:39:14,640
redundant here
924
00:39:14,640 --> 00:39:18,240
and that means that uh it does not need
925
00:39:18,240 --> 00:39:22,020
to we we don't know we don't need to say
926
00:39:22,020 --> 00:39:25,260
explicitly here that is gonna hold
927
00:39:25,260 --> 00:39:27,060
of um
928
00:39:27,060 --> 00:39:28,680
sequence of characters it's going to
929
00:39:28,680 --> 00:39:31,200
hold string because it knows this you
930
00:39:31,200 --> 00:39:33,240
can because it quoting has something
931
00:39:33,240 --> 00:39:35,700
which is called type inference and that
932
00:39:35,700 --> 00:39:38,160
means that it can infer the type of the
933
00:39:38,160 --> 00:39:41,040
variable by the value we assigned to it
934
00:39:41,040 --> 00:39:43,140
in our case we assign some text so if I
935
00:39:43,140 --> 00:39:46,160
delete the type here
936
00:39:46,740 --> 00:39:48,420
now
937
00:39:48,420 --> 00:39:51,900
is still working and if I as you can see
938
00:39:51,900 --> 00:39:55,260
if I if I put here let's say if I try to
939
00:39:55,260 --> 00:39:58,160
put your username
940
00:39:58,560 --> 00:40:01,880
sorry username
941
00:40:03,720 --> 00:40:06,180
equals to tree
942
00:40:06,180 --> 00:40:09,300
is still treated as a
943
00:40:09,300 --> 00:40:12,680
string because it inferior it it
944
00:40:12,680 --> 00:40:15,540
inferred the type by the value assigned
945
00:40:15,540 --> 00:40:19,740
to it and similarly this uh this thing
946
00:40:19,740 --> 00:40:22,740
applies to to our
947
00:40:22,740 --> 00:40:25,980
int type so if I delete this
948
00:40:25,980 --> 00:40:28,380
is still working it's tilt it and it is
949
00:40:28,380 --> 00:40:30,900
still treating this as a as an end so if
950
00:40:30,900 --> 00:40:32,220
you try to put here
951
00:40:32,220 --> 00:40:36,599
age equals to a text I don't know again
952
00:40:36,599 --> 00:40:39,240
we have this yellow arrow unifarable it
953
00:40:39,240 --> 00:40:41,640
says type mismatch required int and
954
00:40:41,640 --> 00:40:44,460
found string and that means that it's
955
00:40:44,460 --> 00:40:46,260
still uh
956
00:40:46,260 --> 00:40:49,320
is still treating this uh variable as an
957
00:40:49,320 --> 00:40:51,839
in because as I said it has something
958
00:40:51,839 --> 00:40:54,300
which is called type inference and it
959
00:40:54,300 --> 00:40:56,160
can infer the type of the variable by
960
00:40:56,160 --> 00:40:57,960
the value assigned to it
961
00:40:57,960 --> 00:41:00,359
all right this is uh our discussion
962
00:41:00,359 --> 00:41:03,180
about variables there are more types of
963
00:41:03,180 --> 00:41:04,560
variables and we're going to look at
964
00:41:04,560 --> 00:41:08,400
that other types in the next videos and
965
00:41:08,400 --> 00:41:11,880
collectively they are called data types
966
00:41:11,880 --> 00:41:13,140
okay
967
00:41:13,140 --> 00:41:15,660
you can remove the space if you want
968
00:41:15,660 --> 00:41:17,460
so I'm going to end this video now and
969
00:41:17,460 --> 00:41:19,200
see you in the next video
970
00:41:19,200 --> 00:41:21,079
so while I was recording this course
971
00:41:21,079 --> 00:41:23,220
jetbrains have released a new version of
972
00:41:23,220 --> 00:41:24,540
IntelliJ IDEA
973
00:41:24,540 --> 00:41:27,480
and you will see in the course when you
974
00:41:27,480 --> 00:41:29,280
when I create a new project you will see
975
00:41:29,280 --> 00:41:31,440
a different window it's not that
976
00:41:31,440 --> 00:41:33,480
different it's slightly different than
977
00:41:33,480 --> 00:41:35,460
this window than the window that you
978
00:41:35,460 --> 00:41:37,680
will see when you download IntelliJ ID
979
00:41:37,680 --> 00:41:39,960
which is this version now which has this
980
00:41:39,960 --> 00:41:42,420
window and when you see this window here
981
00:41:42,420 --> 00:41:44,820
you just need to specify a name here you
982
00:41:44,820 --> 00:41:46,859
specify the location for the language
983
00:41:46,859 --> 00:41:49,619
selecting here you select for the build
984
00:41:49,619 --> 00:41:51,599
system IntelliJ and
985
00:41:51,599 --> 00:41:54,180
you make sure to have the jdk here and
986
00:41:54,180 --> 00:41:56,400
you also check this out sample code and
987
00:41:56,400 --> 00:41:57,780
that is going to generate the main
988
00:41:57,780 --> 00:42:00,240
function for you and you click on Create
989
00:42:00,240 --> 00:42:02,040
and the project is going to be created
990
00:42:02,040 --> 00:42:04,320
for you so I thought that it's good to
991
00:42:04,320 --> 00:42:07,260
tell you about this and uh if you'll see
992
00:42:07,260 --> 00:42:09,119
this window and if this window changes
993
00:42:09,119 --> 00:42:11,099
in the future I'm gonna release a new
994
00:42:11,099 --> 00:42:13,619
video and so I'm gonna show you how to
995
00:42:13,619 --> 00:42:15,540
if they changed specifically the window
996
00:42:15,540 --> 00:42:17,040
of creating a new project how to create
997
00:42:17,040 --> 00:42:18,540
a new project with that window but for
998
00:42:18,540 --> 00:42:20,520
now this is the window of the latest
999
00:42:20,520 --> 00:42:22,820
version
1000
00:42:24,480 --> 00:42:26,280
all right so in the last video we talked
1001
00:42:26,280 --> 00:42:28,380
about variables and we seen there that
1002
00:42:28,380 --> 00:42:31,140
you can store different data in our
1003
00:42:31,140 --> 00:42:33,599
variables and we looked at two different
1004
00:42:33,599 --> 00:42:36,660
data types or types in short and that
1005
00:42:36,660 --> 00:42:39,540
was the string type and that in type and
1006
00:42:39,540 --> 00:42:41,160
the string type was used for storing
1007
00:42:41,160 --> 00:42:43,680
sequence of characters or text and the
1008
00:42:43,680 --> 00:42:45,300
in-type was used for storing whole
1009
00:42:45,300 --> 00:42:47,579
numbers and in this video we're going to
1010
00:42:47,579 --> 00:42:50,339
take take a deeper look at the integer
1011
00:42:50,339 --> 00:42:52,560
data type and we're also going to extend
1012
00:42:52,560 --> 00:42:55,320
our understanding to more
1013
00:42:55,320 --> 00:42:57,960
types for storing whole numbers and that
1014
00:42:57,960 --> 00:43:01,740
is the byte the short and the long data
1015
00:43:01,740 --> 00:43:03,420
types for storing whole numbers you only
1016
00:43:03,420 --> 00:43:05,040
gonna see what is the difference between
1017
00:43:05,040 --> 00:43:08,400
this uh these types now let's open
1018
00:43:08,400 --> 00:43:09,839
IntelliJ ID
1019
00:43:09,839 --> 00:43:13,160
I'm going to open my IntelliJ ID
1020
00:43:16,680 --> 00:43:18,420
on the left side make sure that project
1021
00:43:18,420 --> 00:43:22,700
is created selected click on new project
1022
00:43:24,420 --> 00:43:26,220
on the left hand side measure the cotton
1023
00:43:26,220 --> 00:43:28,380
is selected and you don't need to give a
1024
00:43:28,380 --> 00:43:29,880
name to a project again it should be in
1025
00:43:29,880 --> 00:43:32,460
Pascal case I'm going to type
1026
00:43:32,460 --> 00:43:35,099
bite
1027
00:43:35,099 --> 00:43:36,660
short
1028
00:43:36,660 --> 00:43:38,400
and
1029
00:43:38,400 --> 00:43:41,000
long
1030
00:43:41,040 --> 00:43:42,119
oh
1031
00:43:42,119 --> 00:43:43,800
now go down here and it's going to
1032
00:43:43,800 --> 00:43:45,960
select IntelliJ and make sure that the
1033
00:43:45,960 --> 00:43:48,839
jdk is selected click on next to create
1034
00:43:48,839 --> 00:43:51,060
a new project and click on finish now
1035
00:43:51,060 --> 00:43:54,560
our project is going to be created
1036
00:44:00,060 --> 00:44:02,880
and down here again a setting IntelliJ
1037
00:44:02,880 --> 00:44:05,700
is setting up our project on the left
1038
00:44:05,700 --> 00:44:07,800
hand side we have our
1039
00:44:07,800 --> 00:44:11,280
project pane I can expand this SRC to
1040
00:44:11,280 --> 00:44:13,440
see our file our main file that we
1041
00:44:13,440 --> 00:44:16,500
created first now it was
1042
00:44:16,500 --> 00:44:18,839
generated for us automatically I'm gonna
1043
00:44:18,839 --> 00:44:20,400
delete this code and here we have
1044
00:44:20,400 --> 00:44:22,140
something which is called the comments
1045
00:44:22,140 --> 00:44:23,880
or a comment but they're going to look
1046
00:44:23,880 --> 00:44:26,940
at comments in a separate video
1047
00:44:26,940 --> 00:44:29,960
so I'm going to delete this
1048
00:44:30,000 --> 00:44:32,540
sorry
1049
00:44:32,640 --> 00:44:34,980
okay I'm gonna add this
1050
00:44:34,980 --> 00:44:38,220
space here I'm gonna delete this
1051
00:44:38,220 --> 00:44:41,520
you can keep the RX there it's not gonna
1052
00:44:41,520 --> 00:44:43,560
affect your code in any way but I want
1053
00:44:43,560 --> 00:44:45,540
to make it consistent with my previous
1054
00:44:45,540 --> 00:44:48,119
videos but you can keep the arcs there
1055
00:44:48,119 --> 00:44:51,060
it will make no difference in uh the way
1056
00:44:51,060 --> 00:44:55,020
your code is compiled now let's add the
1057
00:44:55,020 --> 00:44:56,940
code that you had previously
1058
00:44:56,940 --> 00:44:59,520
and previously we had the we had a
1059
00:44:59,520 --> 00:45:04,079
variable of type string for storing uh
1060
00:45:04,079 --> 00:45:06,060
text specifically we store our name
1061
00:45:06,060 --> 00:45:08,400
there so I'm gonna type the VAR keyword
1062
00:45:08,400 --> 00:45:09,359
because
1063
00:45:09,359 --> 00:45:12,480
uh in the last video we we established
1064
00:45:12,480 --> 00:45:13,859
that you want to change this value later
1065
00:45:13,859 --> 00:45:14,940
VAR
1066
00:45:14,940 --> 00:45:17,040
and uh he didn't give a name to our
1067
00:45:17,040 --> 00:45:19,079
value I'm going to call it name like in
1068
00:45:19,079 --> 00:45:20,460
the previous video for the type I'm
1069
00:45:20,460 --> 00:45:23,099
going to choose string
1070
00:45:23,099 --> 00:45:25,560
and uh I'm gonna put equals and now
1071
00:45:25,560 --> 00:45:27,300
Center sign of value to it I'm going to
1072
00:45:27,300 --> 00:45:30,119
put again our text
1073
00:45:30,119 --> 00:45:31,619
all right
1074
00:45:31,619 --> 00:45:33,420
and down here I'm going to declare a
1075
00:45:33,420 --> 00:45:34,859
variable but this time I'm going to
1076
00:45:34,859 --> 00:45:36,359
declare it as a Val
1077
00:45:36,359 --> 00:45:39,960
I'm gonna type I'm gonna type for the
1078
00:45:39,960 --> 00:45:42,300
name H
1079
00:45:42,300 --> 00:45:44,520
and for the type
1080
00:45:44,520 --> 00:45:48,300
I'm gonna put int for storing the whole
1081
00:45:48,300 --> 00:45:50,640
numbers I'm going to put 22.
1082
00:45:50,640 --> 00:45:54,000
and we see in there that we can delete
1083
00:45:54,000 --> 00:45:56,160
the type here because cotton has
1084
00:45:56,160 --> 00:45:57,960
something which is called type inference
1085
00:45:57,960 --> 00:46:00,000
and that means that it can infer the
1086
00:46:00,000 --> 00:46:01,980
type of the variable by the value
1087
00:46:01,980 --> 00:46:04,140
assigned to it so we can delete this
1088
00:46:04,140 --> 00:46:06,740
type here
1089
00:46:07,619 --> 00:46:10,260
and our code is going to still be valid
1090
00:46:10,260 --> 00:46:12,780
and if I hover over here
1091
00:46:12,780 --> 00:46:15,359
if you look at here is still treating
1092
00:46:15,359 --> 00:46:19,619
this variable as type of string so
1093
00:46:19,619 --> 00:46:22,740
but uh in this video I'm Gonna Leave the
1094
00:46:22,740 --> 00:46:23,880
string type
1095
00:46:23,880 --> 00:46:25,260
I'm gonna delete this because we're
1096
00:46:25,260 --> 00:46:28,380
gonna look at the integer more closely
1097
00:46:28,380 --> 00:46:30,839
and
1098
00:46:30,839 --> 00:46:33,300
here let's uh
1099
00:46:33,300 --> 00:46:35,280
let's change this value let's change its
1100
00:46:35,280 --> 00:46:36,900
name to
1101
00:46:36,900 --> 00:46:39,000
number
1102
00:46:39,000 --> 00:46:41,339
and it's also going to be an INT and
1103
00:46:41,339 --> 00:46:42,540
then let's take a deeper look first
1104
00:46:42,540 --> 00:46:44,940
let's see what is an integer we know
1105
00:46:44,940 --> 00:46:47,040
that it's a whole number so you can
1106
00:46:47,040 --> 00:46:51,359
store whole numbers but what is it's uh
1107
00:46:51,359 --> 00:46:53,819
it has a range you can put whatever
1108
00:46:53,819 --> 00:46:56,099
value you want actually there is a range
1109
00:46:56,099 --> 00:46:58,560
there is a minimum and maximum value
1110
00:46:58,560 --> 00:47:01,680
that you can put in a in an integer data
1111
00:47:01,680 --> 00:47:03,480
type so and to get that minimum and
1112
00:47:03,480 --> 00:47:06,440
maximum value we type
1113
00:47:07,079 --> 00:47:09,000
I'm going to put a space here to make
1114
00:47:09,000 --> 00:47:10,560
what the code Mercury I'm going to type
1115
00:47:10,560 --> 00:47:15,960
Val I'm gonna type Max integer
1116
00:47:15,960 --> 00:47:17,700
value
1117
00:47:17,700 --> 00:47:20,040
and for the type I'm going to choose int
1118
00:47:20,040 --> 00:47:22,260
or you can let it infer the type
1119
00:47:22,260 --> 00:47:23,400
you can
1120
00:47:23,400 --> 00:47:25,560
I'm gonna actually put the type here but
1121
00:47:25,560 --> 00:47:27,119
you can let it infer the type if you
1122
00:47:27,119 --> 00:47:31,800
want equals and now we type int Dot
1123
00:47:31,800 --> 00:47:33,420
and Intel J is giving us some
1124
00:47:33,420 --> 00:47:35,160
suggestions here I'm gonna choose max
1125
00:47:35,160 --> 00:47:36,720
value
1126
00:47:36,720 --> 00:47:39,540
and Discord
1127
00:47:39,540 --> 00:47:42,900
on the right here is gonna give us
1128
00:47:42,900 --> 00:47:44,520
actually the entire code is going to
1129
00:47:44,520 --> 00:47:48,420
give us the maximum value in a integer
1130
00:47:48,420 --> 00:47:50,700
and if you don't if this seems confusing
1131
00:47:50,700 --> 00:47:54,119
in that and this uppercase so
1132
00:47:54,119 --> 00:47:55,980
uh you know you don't need to worry
1133
00:47:55,980 --> 00:47:58,560
about that for now you just need to know
1134
00:47:58,560 --> 00:48:00,540
that in this way you can get the maximum
1135
00:48:00,540 --> 00:48:03,060
value to out to be out with in our
1136
00:48:03,060 --> 00:48:04,980
console and for the minimum value we're
1137
00:48:04,980 --> 00:48:07,700
going to type Val
1138
00:48:07,859 --> 00:48:10,578
mean
1139
00:48:11,180 --> 00:48:12,900
integer
1140
00:48:12,900 --> 00:48:14,700
value
1141
00:48:14,700 --> 00:48:17,880
for the type I'm going to choose int
1142
00:48:17,880 --> 00:48:20,940
now I'm going to type again int Dot and
1143
00:48:20,940 --> 00:48:22,680
again I'm going to choose from here mean
1144
00:48:22,680 --> 00:48:26,099
value now to print these two values I'm
1145
00:48:26,099 --> 00:48:29,220
gonna just delete this text here
1146
00:48:29,220 --> 00:48:33,180
and I'm going to type int
1147
00:48:34,980 --> 00:48:38,220
eger or in
1148
00:48:38,220 --> 00:48:41,220
maximum
1149
00:48:41,220 --> 00:48:43,800
value
1150
00:48:43,800 --> 00:48:45,240
is
1151
00:48:45,240 --> 00:48:48,000
I'm gonna put dollar sign to get the
1152
00:48:48,000 --> 00:48:49,980
value Max
1153
00:48:49,980 --> 00:48:51,720
integer value
1154
00:48:51,720 --> 00:48:53,460
right
1155
00:48:53,460 --> 00:48:56,060
and below this code we're going to type
1156
00:48:56,060 --> 00:48:59,900
another println
1157
00:49:00,180 --> 00:49:02,339
and here you're gonna put
1158
00:49:02,339 --> 00:49:04,140
two pair of quotation marks again and
1159
00:49:04,140 --> 00:49:07,640
I'm going to type int
1160
00:49:08,460 --> 00:49:12,359
minimum value is
1161
00:49:12,900 --> 00:49:15,980
dollar sign
1162
00:49:17,640 --> 00:49:19,260
again IntelliJ is giving us suggestions
1163
00:49:19,260 --> 00:49:20,940
you're going to choose minimum integer
1164
00:49:20,940 --> 00:49:22,260
value
1165
00:49:22,260 --> 00:49:24,900
now let's print this uh let's output
1166
00:49:24,900 --> 00:49:28,440
this code to see what is the minimum
1167
00:49:28,440 --> 00:49:30,359
maximum value which can be stored in an
1168
00:49:30,359 --> 00:49:33,960
integer data type
1169
00:49:33,960 --> 00:49:36,300
and if you want you can add a space here
1170
00:49:36,300 --> 00:49:39,500
to make things more clear
1171
00:49:49,020 --> 00:49:51,180
all right and as you can see down here
1172
00:49:51,180 --> 00:49:53,700
when the console opened that the maximum
1173
00:49:53,700 --> 00:49:56,940
value is 2 billion and 147 millions and
1174
00:49:56,940 --> 00:49:59,940
so on and the minimum values to billion
1175
00:49:59,940 --> 00:50:03,359
147 millions and so on and these are the
1176
00:50:03,359 --> 00:50:05,700
minimum and maximum values which can be
1177
00:50:05,700 --> 00:50:09,119
stored in an INT so that means that you
1178
00:50:09,119 --> 00:50:10,680
cannot put a value which is which is
1179
00:50:10,680 --> 00:50:12,319
bigger than this
1180
00:50:12,319 --> 00:50:16,440
maximum value or the watch or value
1181
00:50:16,440 --> 00:50:19,920
which is uh uh which is bigger than the
1182
00:50:19,920 --> 00:50:21,420
minimal values and so if you try to
1183
00:50:21,420 --> 00:50:24,480
assign and to confirm that if you try to
1184
00:50:24,480 --> 00:50:25,619
assign here
1185
00:50:25,619 --> 00:50:28,079
a value which is bigger than the maximum
1186
00:50:28,079 --> 00:50:33,119
integer value so let's uh type
1187
00:50:33,119 --> 00:50:36,300
number and
1188
00:50:36,300 --> 00:50:39,359
now we may be spotted the problem here
1189
00:50:39,359 --> 00:50:41,460
if I try to put here equals
1190
00:50:41,460 --> 00:50:43,980
it says
1191
00:50:43,980 --> 00:50:45,780
Val cannot be reassigned so let's
1192
00:50:45,780 --> 00:50:47,819
declare this as a bar because I want to
1193
00:50:47,819 --> 00:50:50,280
change this value now
1194
00:50:50,280 --> 00:50:52,680
I'm going to put equals and I'm just
1195
00:50:52,680 --> 00:50:55,460
going to copy this
1196
00:51:06,660 --> 00:51:09,859
I'm gonna put it here
1197
00:51:10,559 --> 00:51:13,260
now if I increase this number by one
1198
00:51:13,260 --> 00:51:16,920
let's say that I put 8 here now it
1199
00:51:16,920 --> 00:51:18,540
immediately shows an awning and if I
1200
00:51:18,540 --> 00:51:20,160
hover over here
1201
00:51:20,160 --> 00:51:22,140
it says that the integer literally does
1202
00:51:22,140 --> 00:51:24,420
not conform to the expected type int and
1203
00:51:24,420 --> 00:51:25,760
you may be wondering
1204
00:51:25,760 --> 00:51:29,400
how this makes any sense because here up
1205
00:51:29,400 --> 00:51:31,800
here we said explicit that this
1206
00:51:31,800 --> 00:51:34,380
variable is going to store integers or
1207
00:51:34,380 --> 00:51:36,240
whole numbers and down here we have a
1208
00:51:36,240 --> 00:51:38,400
whole number but the problem now is that
1209
00:51:38,400 --> 00:51:40,859
this whole number is bigger than the
1210
00:51:40,859 --> 00:51:42,839
maximum integer value which can be
1211
00:51:42,839 --> 00:51:45,480
stored in an end which is 2 billion one
1212
00:51:45,480 --> 00:51:47,520
four seven million and forty thousand
1213
00:51:47,520 --> 00:51:50,160
forty three three thousand six hundred
1214
00:51:50,160 --> 00:51:52,920
forty seven but now we have a bigger
1215
00:51:52,920 --> 00:51:56,359
number and um
1216
00:51:56,359 --> 00:51:59,700
to to store this number need to use a
1217
00:51:59,700 --> 00:52:01,800
different data type or a different type
1218
00:52:01,800 --> 00:52:03,660
and gonna see in the next videos
1219
00:52:03,660 --> 00:52:06,300
precisely what type we need to use and
1220
00:52:06,300 --> 00:52:08,640
what type we gonna use for storing a
1221
00:52:08,640 --> 00:52:10,740
bigger number than the integer on the
1222
00:52:10,740 --> 00:52:13,500
integer maximum value all right
1223
00:52:13,500 --> 00:52:16,740
also it's important to know because he
1224
00:52:16,740 --> 00:52:20,160
said he says here now the integer liter
1225
00:52:20,160 --> 00:52:21,780
so you may be wonder what is this leader
1226
00:52:21,780 --> 00:52:23,880
the liter is the value that you assign
1227
00:52:23,880 --> 00:52:25,859
to our variable and it's called the
1228
00:52:25,859 --> 00:52:28,260
literal because you assign we assign the
1229
00:52:28,260 --> 00:52:30,660
value directly here so we don't have a
1230
00:52:30,660 --> 00:52:32,880
what is called a symbolic name here and
1231
00:52:32,880 --> 00:52:34,559
we're going to see what is a symbolic
1232
00:52:34,559 --> 00:52:37,800
name in the next videos now I'm gonna
1233
00:52:37,800 --> 00:52:39,780
edit this video now I'm gonna change it
1234
00:52:39,780 --> 00:52:42,780
back to seven
1235
00:52:42,780 --> 00:52:44,880
and also you can delete the types here
1236
00:52:44,880 --> 00:52:47,520
the code is going to still work
1237
00:52:47,520 --> 00:52:51,119
the code is going to still be valid
1238
00:52:51,119 --> 00:52:55,339
and if you delete the type up here also
1239
00:52:56,339 --> 00:53:00,000
because the type can be inferred
1240
00:53:00,000 --> 00:53:02,579
like we talked about
1241
00:53:02,579 --> 00:53:04,140
now
1242
00:53:04,140 --> 00:53:05,880
I'm gonna add this video now and see you
1243
00:53:05,880 --> 00:53:08,420
in the next video
1244
00:53:08,520 --> 00:53:10,440
alright so in the last video we looked
1245
00:53:10,440 --> 00:53:12,059
at the integer data type and we saw
1246
00:53:12,059 --> 00:53:14,040
there that you can store a minimum and a
1247
00:53:14,040 --> 00:53:17,280
maximum value in an integer and uh in
1248
00:53:17,280 --> 00:53:18,240
this video we're going to look at the
1249
00:53:18,240 --> 00:53:20,280
next three data types or types for
1250
00:53:20,280 --> 00:53:22,140
storing whole numbers and those are the
1251
00:53:22,140 --> 00:53:24,359
byte the short and the long and let's
1252
00:53:24,359 --> 00:53:26,040
start with the buy to see what is the
1253
00:53:26,040 --> 00:53:27,660
minimum and maximum value which can be
1254
00:53:27,660 --> 00:53:29,760
stored in a in a byte so I'm going to
1255
00:53:29,760 --> 00:53:31,260
type Val
1256
00:53:31,260 --> 00:53:35,280
my marks byte
1257
00:53:35,280 --> 00:53:37,559
value for the type I'm going to choose
1258
00:53:37,559 --> 00:53:39,359
byte
1259
00:53:39,359 --> 00:53:42,319
equals
1260
00:53:42,960 --> 00:53:46,200
byte that
1261
00:53:46,200 --> 00:53:47,579
max value
1262
00:53:47,579 --> 00:53:49,920
below I'm going to type again Val this
1263
00:53:49,920 --> 00:53:53,040
time I'm gonna I'm gonna call it my mean
1264
00:53:53,040 --> 00:53:57,240
my um byte
1265
00:53:57,240 --> 00:53:59,099
value
1266
00:53:59,099 --> 00:54:03,020
for the type I'm going to choose byte
1267
00:54:03,359 --> 00:54:05,460
we're gonna put equals I'm gonna type
1268
00:54:05,460 --> 00:54:09,420
bite that mean value and now to print
1269
00:54:09,420 --> 00:54:11,720
this to print the value of those
1270
00:54:11,720 --> 00:54:14,640
variables I'm gonna copy this to
1271
00:54:14,640 --> 00:54:17,900
printerland statements
1272
00:54:20,280 --> 00:54:23,700
I'm going to paste it below here
1273
00:54:23,700 --> 00:54:28,379
I'm going to change the text to byte
1274
00:54:32,640 --> 00:54:35,819
and here we need to call our variables
1275
00:54:35,819 --> 00:54:38,280
to refer all variables so I'm going to
1276
00:54:38,280 --> 00:54:39,140
type
1277
00:54:39,140 --> 00:54:43,319
my Max byte value just press enter to
1278
00:54:43,319 --> 00:54:46,640
fill that for you
1279
00:54:46,980 --> 00:54:49,559
and below here I'm gonna
1280
00:54:49,559 --> 00:54:51,480
type my
1281
00:54:51,480 --> 00:54:53,160
mean byte value
1282
00:54:53,160 --> 00:54:55,740
now let's print this uh two varus the
1283
00:54:55,740 --> 00:54:57,240
console to see what is the minimum and
1284
00:54:57,240 --> 00:54:59,220
maximum value which can be stored in a
1285
00:54:59,220 --> 00:55:02,400
byte data type so I'm gonna run this
1286
00:55:02,400 --> 00:55:03,300
code
1287
00:55:03,300 --> 00:55:05,760
now the console should
1288
00:55:05,760 --> 00:55:09,140
open in a short moment
1289
00:55:11,099 --> 00:55:13,380
all right as you can see the byte
1290
00:55:13,380 --> 00:55:16,680
maximum value is 127 and the byte
1291
00:55:16,680 --> 00:55:20,220
minimum value is minus 128 and in
1292
00:55:20,220 --> 00:55:23,040
contrast with those minimum maximum
1293
00:55:23,040 --> 00:55:25,619
values for the integer type you can see
1294
00:55:25,619 --> 00:55:28,260
that these two are very very small
1295
00:55:28,260 --> 00:55:30,900
compared to that now let's look at the
1296
00:55:30,900 --> 00:55:32,220
next
1297
00:55:32,220 --> 00:55:34,980
the next type which is the short and I'm
1298
00:55:34,980 --> 00:55:37,140
gonna do the same
1299
00:55:37,140 --> 00:55:38,940
the same
1300
00:55:38,940 --> 00:55:44,000
I'm gonna type the same code AS sorry
1301
00:55:45,059 --> 00:55:46,680
the same code that you had previously
1302
00:55:46,680 --> 00:55:49,399
but uh
1303
00:55:49,559 --> 00:55:51,480
so I'm going to put a space to make it
1304
00:55:51,480 --> 00:55:53,160
more clear I'm going to close the
1305
00:55:53,160 --> 00:55:54,900
console
1306
00:55:54,900 --> 00:55:57,599
so let's see the short type right so I'm
1307
00:55:57,599 --> 00:55:59,099
going to type Val
1308
00:55:59,099 --> 00:56:00,540
my
1309
00:56:00,540 --> 00:56:03,319
marks
1310
00:56:04,800 --> 00:56:05,880
short
1311
00:56:05,880 --> 00:56:11,000
value I'm going to put x equals
1312
00:56:11,300 --> 00:56:15,300
choose short here for the type
1313
00:56:15,300 --> 00:56:17,040
equals
1314
00:56:17,040 --> 00:56:19,380
short
1315
00:56:19,380 --> 00:56:22,220
Dot
1316
00:56:23,160 --> 00:56:25,020
max value right
1317
00:56:25,020 --> 00:56:26,760
I'm going to press enter I'm going to
1318
00:56:26,760 --> 00:56:29,540
type again Val
1319
00:56:29,940 --> 00:56:32,099
my
1320
00:56:32,099 --> 00:56:33,599
mean
1321
00:56:33,599 --> 00:56:36,020
short
1322
00:56:38,520 --> 00:56:41,700
short value
1323
00:56:41,700 --> 00:56:45,379
and for the type again short
1324
00:56:45,480 --> 00:56:47,700
equals
1325
00:56:47,700 --> 00:56:50,400
short that mean
1326
00:56:50,400 --> 00:56:53,400
value now let's copy this
1327
00:56:53,400 --> 00:56:55,859
actually we have already copied that so
1328
00:56:55,859 --> 00:56:58,319
I'm going to paste that here to print
1329
00:56:58,319 --> 00:57:00,619
the
1330
00:57:01,200 --> 00:57:04,319
print our minimum maximum values
1331
00:57:04,319 --> 00:57:07,619
so I'm gonna change this to short this
1332
00:57:07,619 --> 00:57:09,800
text
1333
00:57:11,460 --> 00:57:14,160
I'm gonna change it here to
1334
00:57:14,160 --> 00:57:16,559
and now we need to call those variables
1335
00:57:16,559 --> 00:57:18,000
because as you can see right now they
1336
00:57:18,000 --> 00:57:20,579
are gray out and if you hover over this
1337
00:57:20,579 --> 00:57:22,440
is the variable my Max short value is
1338
00:57:22,440 --> 00:57:24,720
never used and that's a clever way to
1339
00:57:24,720 --> 00:57:27,359
see when a variable is used or not
1340
00:57:27,359 --> 00:57:29,160
all right let's change it
1341
00:57:29,160 --> 00:57:30,720
let's change the name here because we
1342
00:57:30,720 --> 00:57:32,339
want to refer those variables
1343
00:57:32,339 --> 00:57:38,040
my Max short value and
1344
00:57:38,040 --> 00:57:40,800
here you're gonna type my
1345
00:57:40,800 --> 00:57:42,119
mean
1346
00:57:42,119 --> 00:57:44,099
short value
1347
00:57:44,099 --> 00:57:46,800
right let's print this to see now what
1348
00:57:46,800 --> 00:57:48,359
is the minimum and maximum you can be
1349
00:57:48,359 --> 00:57:50,900
stored in a short
1350
00:57:56,160 --> 00:57:58,079
all right as you can see down here it
1351
00:57:58,079 --> 00:58:00,359
output that the short maximum values
1352
00:58:00,359 --> 00:58:01,619
minus
1353
00:58:01,619 --> 00:58:03,119
32
1354
00:58:03,119 --> 00:58:04,880
000 70
1355
00:58:04,880 --> 00:58:09,599
167 and for uh the short minimum value
1356
00:58:09,599 --> 00:58:14,400
is minus 32 770 168 right and if you
1357
00:58:14,400 --> 00:58:18,240
look at the the previous data types you
1358
00:58:18,240 --> 00:58:21,420
can see that the integers the the
1359
00:58:21,420 --> 00:58:24,059
biggest is the is the type which can
1360
00:58:24,059 --> 00:58:28,079
store the biggest number but uh there is
1361
00:58:28,079 --> 00:58:29,880
one actually which is bigger than the
1362
00:58:29,880 --> 00:58:32,640
integer so I'm gonna type now the next
1363
00:58:32,640 --> 00:58:34,680
dot that I put you gonna look at and
1364
00:58:34,680 --> 00:58:37,140
that is the long which is um
1365
00:58:37,140 --> 00:58:39,960
much bigger than the in integer so I'm
1366
00:58:39,960 --> 00:58:41,099
going to close the console I'm going to
1367
00:58:41,099 --> 00:58:43,880
type the same code
1368
00:58:44,160 --> 00:58:46,260
I'm going to type Val
1369
00:58:46,260 --> 00:58:48,980
my
1370
00:58:51,059 --> 00:58:53,520
Max
1371
00:58:53,520 --> 00:58:55,920
long value
1372
00:58:55,920 --> 00:58:59,720
for the type I'm going to choose long
1373
00:58:59,940 --> 00:59:02,880
and we're going to put equals long I'm
1374
00:59:02,880 --> 00:59:07,200
going to put that marks value again Val
1375
00:59:07,200 --> 00:59:08,940
my
1376
00:59:08,940 --> 00:59:10,319
mean
1377
00:59:10,319 --> 00:59:12,480
long value
1378
00:59:12,480 --> 00:59:15,359
for the type I'm going to choose long
1379
00:59:15,359 --> 00:59:18,359
equals again I'm going to choose now I'm
1380
00:59:18,359 --> 00:59:20,760
going to type again long
1381
00:59:20,760 --> 00:59:22,559
Dot
1382
00:59:22,559 --> 00:59:24,180
minimum volume
1383
00:59:24,180 --> 00:59:26,400
now I'm going to paste that code below
1384
00:59:26,400 --> 00:59:28,859
here to Output our
1385
00:59:28,859 --> 00:59:31,140
variables to Output the minimum maximum
1386
00:59:31,140 --> 00:59:33,480
bar which can be stored in a long data
1387
00:59:33,480 --> 00:59:36,000
type so I'm gonna
1388
00:59:36,000 --> 00:59:39,920
so I'm gonna change this to Long
1389
00:59:40,380 --> 00:59:42,299
I'm going to change this text also to
1390
00:59:42,299 --> 00:59:44,540
Long
1391
00:59:48,480 --> 00:59:50,579
now I need to refer this those two
1392
00:59:50,579 --> 00:59:54,559
variables up here from up here
1393
00:59:54,720 --> 00:59:56,280
so I'm gonna
1394
00:59:56,280 --> 00:59:59,940
delete this I'm going to type my
1395
00:59:59,940 --> 01:00:02,460
marks
1396
01:00:02,460 --> 01:00:04,559
long value so I'm going to choose that
1397
01:00:04,559 --> 01:00:06,420
press enter
1398
01:00:06,420 --> 01:00:08,220
and here you're going to choose uh I'm
1399
01:00:08,220 --> 01:00:11,240
going to type my
1400
01:00:12,720 --> 01:00:16,618
mean long
1401
01:00:17,460 --> 01:00:21,740
value let's output this uh
1402
01:00:21,900 --> 01:00:23,880
code to see
1403
01:00:23,880 --> 01:00:26,640
let's run actually our code to see what
1404
01:00:26,640 --> 01:00:27,960
is the minimum marketing what you can be
1405
01:00:27,960 --> 01:00:30,720
stored in a long data type and as you
1406
01:00:30,720 --> 01:00:33,780
can see the range for the longest much
1407
01:00:33,780 --> 01:00:36,000
bigger contrast that with the the
1408
01:00:36,000 --> 01:00:39,000
integer data type you can see that it's
1409
01:00:39,000 --> 01:00:42,780
a lot a lot bigger this number so the
1410
01:00:42,780 --> 01:00:46,079
longest has a more is
1411
01:00:46,079 --> 01:00:49,020
has a range which is much bigger than
1412
01:00:49,020 --> 01:00:52,260
the integer and
1413
01:00:52,260 --> 01:00:54,240
I'm going to end this video now and in
1414
01:00:54,240 --> 01:00:55,500
the next video we're going to look at
1415
01:00:55,500 --> 01:01:00,240
how we can use those types and um what
1416
01:01:00,240 --> 01:01:02,880
are the relationships between between
1417
01:01:02,880 --> 01:01:04,680
them right
1418
01:01:04,680 --> 01:01:06,599
so I'm going to end this video now and
1419
01:01:06,599 --> 01:01:07,799
see you in the next
1420
01:01:07,799 --> 01:01:10,020
all right now it's time to use those for
1421
01:01:10,020 --> 01:01:12,059
uh data types for storing whole numbers
1422
01:01:12,059 --> 01:01:14,700
in our code so I'm gonna type some code
1423
01:01:14,700 --> 01:01:17,640
below of those prison Talent statements
1424
01:01:17,640 --> 01:01:19,319
I'm going to type Val
1425
01:01:19,319 --> 01:01:21,000
because I want this very well to be
1426
01:01:21,000 --> 01:01:22,740
immutable I don't want to change this
1427
01:01:22,740 --> 01:01:23,880
value later
1428
01:01:23,880 --> 01:01:25,559
I'm going to call it my number it should
1429
01:01:25,559 --> 01:01:28,079
be in camel case like our last names and
1430
01:01:28,079 --> 01:01:31,020
for the type I'm not Define explicit
1431
01:01:31,020 --> 01:01:32,760
data type here I'm going to put equals
1432
01:01:32,760 --> 01:01:36,359
I'm going to put the literal value 28.
1433
01:01:36,359 --> 01:01:39,000
and if I hover over my number
1434
01:01:39,000 --> 01:01:41,819
you can see that the type which was
1435
01:01:41,819 --> 01:01:44,339
inferred is an INT now I might be
1436
01:01:44,339 --> 01:01:46,680
wondering how this makes any sense
1437
01:01:46,680 --> 01:01:48,839
because this 28 can easily fit in a byte
1438
01:01:48,839 --> 01:01:50,579
or in a short on a long
1439
01:01:50,579 --> 01:01:53,700
and that is because the default data
1440
01:01:53,700 --> 01:01:56,040
type which is automatically inferred is
1441
01:01:56,040 --> 01:01:59,339
the integer so actually let's confirm
1442
01:01:59,339 --> 01:02:02,040
that this can fit easily in other data
1443
01:02:02,040 --> 01:02:03,359
types
1444
01:02:03,359 --> 01:02:05,339
by running our code to see what at the
1445
01:02:05,339 --> 01:02:08,040
menu and you can see that that number
1446
01:02:08,040 --> 01:02:10,260
can easily fit in any of those data
1447
01:02:10,260 --> 01:02:13,319
types from this list and
1448
01:02:13,319 --> 01:02:15,540
that is not happening because the
1449
01:02:15,540 --> 01:02:17,520
automatically as I said the default type
1450
01:02:17,520 --> 01:02:20,040
which is in third is the integer and if
1451
01:02:20,040 --> 01:02:22,680
you want this type to be a byte we need
1452
01:02:22,680 --> 01:02:25,319
to explicitly say explicitly say here
1453
01:02:25,319 --> 01:02:27,000
buy it
1454
01:02:27,000 --> 01:02:29,760
now if I hover over here you can see
1455
01:02:29,760 --> 01:02:31,920
that now it's a byte
1456
01:02:31,920 --> 01:02:33,839
and if I want to be a short
1457
01:02:33,839 --> 01:02:37,880
we need to type explicitly here short
1458
01:02:38,760 --> 01:02:41,640
so if I hover over here now this is a
1459
01:02:41,640 --> 01:02:44,640
short and if I want to be I want this to
1460
01:02:44,640 --> 01:02:47,760
be along I just type again here long and
1461
01:02:47,760 --> 01:02:49,319
for the long there are two ways this is
1462
01:02:49,319 --> 01:02:50,819
the first way to type
1463
01:02:50,819 --> 01:02:54,420
the the type explicitly here
1464
01:02:54,420 --> 01:02:58,079
sorry hover over here it's long or
1465
01:02:58,079 --> 01:03:02,160
we can put IL suffix at the end of our
1466
01:03:02,160 --> 01:03:04,079
number
1467
01:03:04,079 --> 01:03:07,140
but if I put a lowercase letter you eat
1468
01:03:07,140 --> 01:03:09,000
it immediately show this warning which
1469
01:03:09,000 --> 01:03:12,059
says use an uppercase L instead of fail
1470
01:03:12,059 --> 01:03:13,920
and that is because this can easily be
1471
01:03:13,920 --> 01:03:16,260
confused with one so I'm gonna put a
1472
01:03:16,260 --> 01:03:18,000
napper KSL
1473
01:03:18,000 --> 01:03:20,280
and if I hover over here
1474
01:03:20,280 --> 01:03:24,540
now this is a long even though this can
1475
01:03:24,540 --> 01:03:25,859
be
1476
01:03:25,859 --> 01:03:28,500
can be easy can easily fit in any other
1477
01:03:28,500 --> 01:03:30,240
data type
1478
01:03:30,240 --> 01:03:33,240
and if I delete this cell and if I
1479
01:03:33,240 --> 01:03:35,339
exceed the maximum value which can be
1480
01:03:35,339 --> 01:03:37,680
stored in an integer let's say I put 28
1481
01:03:37,680 --> 01:03:39,900
or something here
1482
01:03:39,900 --> 01:03:41,880
and if I hover over here
1483
01:03:41,880 --> 01:03:43,859
you can see that now it inferred the
1484
01:03:43,859 --> 01:03:46,500
type to be as long because now this
1485
01:03:46,500 --> 01:03:49,140
number that I have here exceeds the
1486
01:03:49,140 --> 01:03:51,000
maximum value which can be stored in an
1487
01:03:51,000 --> 01:03:54,420
INT and um you may be wondering now when
1488
01:03:54,420 --> 01:03:56,160
to use one or the other want to use the
1489
01:03:56,160 --> 01:03:57,900
byte I want to use the short when to use
1490
01:03:57,900 --> 01:04:01,319
the long and you will find the the
1491
01:04:01,319 --> 01:04:04,680
answer with the practice where with the
1492
01:04:04,680 --> 01:04:09,799
working with them but if I run this code
1493
01:04:12,119 --> 01:04:14,640
the byte and the short will be used in
1494
01:04:14,640 --> 01:04:17,400
very specific cases and most of the time
1495
01:04:17,400 --> 01:04:19,559
you'll be working with the integer data
1496
01:04:19,559 --> 01:04:21,480
type right
1497
01:04:21,480 --> 01:04:23,579
now I'm gonna end this video and see you
1498
01:04:23,579 --> 01:04:25,460
on the next video we're gonna talk about
1499
01:04:25,460 --> 01:04:27,960
the next
1500
01:04:27,960 --> 01:04:30,900
data types which are the floating data
1501
01:04:30,900 --> 01:04:33,660
types and those are types which you can
1502
01:04:33,660 --> 01:04:36,839
hold the numbers which have a fractional
1503
01:04:36,839 --> 01:04:39,240
part or decimals so see you in the next
1504
01:04:39,240 --> 01:04:41,479
video
1505
01:04:44,040 --> 01:04:45,960
all right before we start our discussion
1506
01:04:45,960 --> 01:04:47,280
about the floating Point numbers
1507
01:04:47,280 --> 01:04:49,260
respectively about the float and double
1508
01:04:49,260 --> 01:04:51,960
data type it's important to know what
1509
01:04:51,960 --> 01:04:53,880
are the difference between the data
1510
01:04:53,880 --> 01:04:55,680
types for storing whole numbers under
1511
01:04:55,680 --> 01:04:58,740
the hood because we know now that there
1512
01:04:58,740 --> 01:05:00,599
is a minimum maximum value which can be
1513
01:05:00,599 --> 01:05:04,500
stored in those data types and also we
1514
01:05:04,500 --> 01:05:07,140
need to know what amount of space they
1515
01:05:07,140 --> 01:05:08,940
occupy under the hood and let's start
1516
01:05:08,940 --> 01:05:12,359
with a byte and our byte occupy eight
1517
01:05:12,359 --> 01:05:14,280
bits right
1518
01:05:14,280 --> 01:05:18,119
so abide occupy bits and we say that a
1519
01:05:18,119 --> 01:05:22,440
byte has a width of 8. and a short can
1520
01:05:22,440 --> 01:05:25,799
store La J range of numbers and occupy
1521
01:05:25,799 --> 01:05:29,400
16 bits and has a width of 16. an end
1522
01:05:29,400 --> 01:05:31,980
has a much larger range as you know an
1523
01:05:31,980 --> 01:05:35,220
occupies 32 bits and has a width of 32.
1524
01:05:35,220 --> 01:05:37,440
the point here that each data type
1525
01:05:37,440 --> 01:05:39,720
occupies a different amount of memory we
1526
01:05:39,720 --> 01:05:41,339
can see that an internet is the four
1527
01:05:41,339 --> 01:05:43,260
times the amount of space that abide
1528
01:05:43,260 --> 01:05:44,819
does for example
1529
01:05:44,819 --> 01:05:46,799
it's not particularly relevant to you to
1530
01:05:46,799 --> 01:05:48,900
know these numbers but it come up as an
1531
01:05:48,900 --> 01:05:50,520
interview question and it's useful to
1532
01:05:50,520 --> 01:05:53,339
know that a certain type takes up more
1533
01:05:53,339 --> 01:05:56,700
space than others all right now let's
1534
01:05:56,700 --> 01:05:58,440
close our project because I'm going to
1535
01:05:58,440 --> 01:06:01,220
create a new project
1536
01:06:03,780 --> 01:06:05,640
now I'm going to click here on new
1537
01:06:05,640 --> 01:06:07,680
project because we have our previous
1538
01:06:07,680 --> 01:06:10,339
project here
1539
01:06:14,160 --> 01:06:16,380
all right on the left hand side again
1540
01:06:16,380 --> 01:06:18,420
kotlin should be selected for the name
1541
01:06:18,420 --> 01:06:21,599
I'm going to call it um
1542
01:06:21,599 --> 01:06:23,579
it should be in Pascal case
1543
01:06:23,579 --> 01:06:26,480
float
1544
01:06:30,660 --> 01:06:33,379
and double
1545
01:06:33,420 --> 01:06:35,819
all right here is Eric for the best
1546
01:06:35,819 --> 01:06:38,640
system IntelliJ and for the produce jdk
1547
01:06:38,640 --> 01:06:41,640
select the indicator I'm going to click
1548
01:06:41,640 --> 01:06:42,900
on next and I'm going to click on finish
1549
01:06:42,900 --> 01:06:46,280
now approach is going to be created
1550
01:06:49,070 --> 01:06:52,189
[Music]
1551
01:06:59,579 --> 01:07:03,020
right let's delete this code
1552
01:07:07,500 --> 01:07:10,440
we can keep the arcs and the array
1553
01:07:10,440 --> 01:07:11,819
string here I'm going to keep it to see
1554
01:07:11,819 --> 01:07:14,940
that there is no but there's not it's
1555
01:07:14,940 --> 01:07:18,000
not making any difference in our
1556
01:07:18,000 --> 01:07:20,099
code
1557
01:07:20,099 --> 01:07:22,079
I'm going to drain the I'm gonna delete
1558
01:07:22,079 --> 01:07:25,440
the print again statement
1559
01:07:25,440 --> 01:07:27,119
and I'm gonna
1560
01:07:27,119 --> 01:07:29,400
expand this to the left because we don't
1561
01:07:29,400 --> 01:07:32,400
really need the project pane
1562
01:07:32,400 --> 01:07:36,079
actually I'm going to expand it full
1563
01:07:37,880 --> 01:07:40,440
then click here to hide the project pane
1564
01:07:40,440 --> 01:07:42,900
all right
1565
01:07:42,900 --> 01:07:44,520
um
1566
01:07:44,520 --> 01:07:46,859
floating Point numbers are used for
1567
01:07:46,859 --> 01:07:49,680
storing numbers which have a fractional
1568
01:07:49,680 --> 01:07:52,920
part which have decimals so floating
1569
01:07:52,920 --> 01:07:54,660
Point numbers are used when you need
1570
01:07:54,660 --> 01:07:57,420
more Precision in your calculations and
1571
01:07:57,420 --> 01:07:59,660
you'll as you'll see in the next video
1572
01:07:59,660 --> 01:08:01,440
this uh
1573
01:08:01,440 --> 01:08:05,880
has a practicality now to
1574
01:08:05,880 --> 01:08:08,220
declare a floating Point number just
1575
01:08:08,220 --> 01:08:09,720
type
1576
01:08:09,720 --> 01:08:11,819
Val
1577
01:08:11,819 --> 01:08:13,680
because I want this variable to be
1578
01:08:13,680 --> 01:08:14,760
multiple
1579
01:08:14,760 --> 01:08:17,640
I'm going to call it my number again
1580
01:08:17,640 --> 01:08:20,219
should be in camera case now first
1581
01:08:20,219 --> 01:08:22,080
letter first
1582
01:08:22,080 --> 01:08:24,899
word starts the color case later and the
1583
01:08:24,899 --> 01:08:26,520
following Awards should start with
1584
01:08:26,520 --> 01:08:29,239
capital letters
1585
01:08:29,279 --> 01:08:32,460
and for for the type I'm going to choose
1586
01:08:32,460 --> 01:08:35,520
let's actually not choose any type let's
1587
01:08:35,520 --> 01:08:38,279
just put equals and try to try here to
1588
01:08:38,279 --> 01:08:41,939
assign a value let's put 2.5
1589
01:08:41,939 --> 01:08:43,198
okay
1590
01:08:43,198 --> 01:08:46,799
now if you hover over here
1591
01:08:46,799 --> 01:08:49,439
and if you look at what type this value
1592
01:08:49,439 --> 01:08:51,120
has
1593
01:08:51,120 --> 01:08:53,580
what type this variable has you can see
1594
01:08:53,580 --> 01:08:55,500
that the type which was inferred is the
1595
01:08:55,500 --> 01:08:56,640
double
1596
01:08:56,640 --> 01:08:59,420
so
1597
01:08:59,460 --> 01:09:03,000
if we want to change the type we can
1598
01:09:03,000 --> 01:09:04,738
put here
1599
01:09:04,738 --> 01:09:08,299
float let's say to be a float
1600
01:09:09,060 --> 01:09:12,980
but look what happens when we do this
1601
01:09:13,319 --> 01:09:14,460
it says that the floating Point
1602
01:09:14,460 --> 01:09:16,319
literally does not Comfort the safety
1603
01:09:16,319 --> 01:09:20,219
type float and that is because like you
1604
01:09:20,219 --> 01:09:25,198
know in our example with the integer the
1605
01:09:25,198 --> 01:09:28,920
type which is inferred which is
1606
01:09:28,920 --> 01:09:32,279
automatically assumed by kotlin is
1607
01:09:32,279 --> 01:09:34,799
double in this case so even though it is
1608
01:09:34,799 --> 01:09:36,060
set here explicit that you're going to
1609
01:09:36,060 --> 01:09:38,219
store a float because it types here a
1610
01:09:38,219 --> 01:09:40,620
literal value it considers this to be a
1611
01:09:40,620 --> 01:09:43,679
double so to solve this you need to
1612
01:09:43,679 --> 01:09:46,580
delete this from here
1613
01:09:48,120 --> 01:09:50,460
and put here at the end of our number
1614
01:09:50,460 --> 01:09:51,839
and F
1615
01:09:51,839 --> 01:09:55,400
now if I hover over here
1616
01:09:55,500 --> 01:09:59,820
now the variable has the type float so
1617
01:09:59,820 --> 01:10:01,380
this is how you declare a float variable
1618
01:10:01,380 --> 01:10:04,080
into put if at the end of the number of
1619
01:10:04,080 --> 01:10:05,219
suffix
1620
01:10:05,219 --> 01:10:06,900
and the
1621
01:10:06,900 --> 01:10:09,780
for the for declaring a double you can
1622
01:10:09,780 --> 01:10:12,239
just uh
1623
01:10:12,239 --> 01:10:15,960
type Val let's call it my
1624
01:10:15,960 --> 01:10:18,679
second
1625
01:10:19,140 --> 01:10:21,719
number
1626
01:10:21,719 --> 01:10:25,679
equals and if I put 2.5 here
1627
01:10:25,679 --> 01:10:27,420
0.5
1628
01:10:27,420 --> 01:10:29,940
now if I hover hover over here like in
1629
01:10:29,940 --> 01:10:32,159
uh like I said previously it
1630
01:10:32,159 --> 01:10:33,840
automatically infer the type to be a
1631
01:10:33,840 --> 01:10:36,900
double so if you don't specific I if you
1632
01:10:36,900 --> 01:10:39,900
don't specify a type the automatically
1633
01:10:39,900 --> 01:10:41,580
inferred type will it will be a double
1634
01:10:41,580 --> 01:10:44,219
because this is the
1635
01:10:44,219 --> 01:10:47,340
type which is uh inferred
1636
01:10:47,340 --> 01:10:50,760
for the floating Point numbers and this
1637
01:10:50,760 --> 01:10:52,860
is similar to the integer type because
1638
01:10:52,860 --> 01:10:55,800
there we've seen that the type which is
1639
01:10:55,800 --> 01:10:58,800
automatically enforcedent so here we
1640
01:10:58,800 --> 01:11:00,060
have the same thing
1641
01:11:00,060 --> 01:11:01,739
so this is how you can declare a
1642
01:11:01,739 --> 01:11:03,840
floating Point number and this is how we
1643
01:11:03,840 --> 01:11:06,840
can declare a double now what is the
1644
01:11:06,840 --> 01:11:09,480
difference between this the different in
1645
01:11:09,480 --> 01:11:12,739
the Precision in which
1646
01:11:12,739 --> 01:11:15,840
depression with a half and we'll see in
1647
01:11:15,840 --> 01:11:18,420
the next example in the next video
1648
01:11:18,420 --> 01:11:19,800
we're going to be talking about
1649
01:11:19,800 --> 01:11:22,280
operators and operations
1650
01:11:22,280 --> 01:11:25,260
what is the what is the Precision of a
1651
01:11:25,260 --> 01:11:26,820
float and what is the position of a
1652
01:11:26,820 --> 01:11:29,580
double and the float comes from a
1653
01:11:29,580 --> 01:11:32,760
floating Precision number and double
1654
01:11:32,760 --> 01:11:34,800
comes for double Precision number and as
1655
01:11:34,800 --> 01:11:36,120
the name implies
1656
01:11:36,120 --> 01:11:40,620
the the double data type has a more has
1657
01:11:40,620 --> 01:11:43,140
more Precision in our calculation and
1658
01:11:43,140 --> 01:11:45,480
you're going to see an example of this
1659
01:11:45,480 --> 01:11:46,980
in the next
1660
01:11:46,980 --> 01:11:49,679
video because you need to First learn
1661
01:11:49,679 --> 01:11:52,080
about operators and operations before we
1662
01:11:52,080 --> 01:11:54,840
we introduce any kind of operator here
1663
01:11:54,840 --> 01:11:58,199
even though we we've already used an
1664
01:11:58,199 --> 01:11:59,520
operator which is the assignment
1665
01:11:59,520 --> 01:12:01,500
operator which is this operator here but
1666
01:12:01,500 --> 01:12:03,900
we're going to talk about this in the
1667
01:12:03,900 --> 01:12:04,980
next video
1668
01:12:04,980 --> 01:12:07,260
so I'm gonna end this video now and see
1669
01:12:07,260 --> 01:12:08,040
you in the next video we're going to
1670
01:12:08,040 --> 01:12:10,260
start our discussion about operators and
1671
01:12:10,260 --> 01:12:12,360
also in that video you're going to see
1672
01:12:12,360 --> 01:12:15,600
uh that the float has a smaller
1673
01:12:15,600 --> 01:12:17,460
Precision than a double and the double
1674
01:12:17,460 --> 01:12:19,739
is assumed to be
1675
01:12:19,739 --> 01:12:21,800
it's assumed to be the default ibicos
1676
01:12:21,800 --> 01:12:25,920
has a bigger procedure so
1677
01:12:25,920 --> 01:12:27,840
I'm gonna end this video now see you in
1678
01:12:27,840 --> 01:12:30,000
the next video but before we end our
1679
01:12:30,000 --> 01:12:32,460
video actually let's uh print let's
1680
01:12:32,460 --> 01:12:34,800
output those values in our console let's
1681
01:12:34,800 --> 01:12:36,480
output the values of the variables in
1682
01:12:36,480 --> 01:12:39,060
our console so I'm going to type println
1683
01:12:39,060 --> 01:12:40,860
statement
1684
01:12:40,860 --> 01:12:43,800
below for code below the creation of our
1685
01:12:43,800 --> 01:12:46,739
variables and inside the parenthesis I'm
1686
01:12:46,739 --> 01:12:48,480
going to put the double pair of
1687
01:12:48,480 --> 01:12:53,000
quotation marks I'm going to type my
1688
01:12:56,219 --> 01:12:57,719
number
1689
01:12:57,719 --> 01:13:00,500
equals
1690
01:13:00,719 --> 01:13:03,179
dollar sign to get a value
1691
01:13:03,179 --> 01:13:05,460
the content of my
1692
01:13:05,460 --> 01:13:07,620
number
1693
01:13:07,620 --> 01:13:09,540
and
1694
01:13:09,540 --> 01:13:10,860
my
1695
01:13:10,860 --> 01:13:11,670
second
1696
01:13:11,670 --> 01:13:13,800
[Music]
1697
01:13:13,800 --> 01:13:16,580
number
1698
01:13:17,100 --> 01:13:19,679
we put equals
1699
01:13:19,679 --> 01:13:22,940
again dollar sign
1700
01:13:24,540 --> 01:13:27,080
not here
1701
01:13:29,100 --> 01:13:31,620
my second number now let's finish those
1702
01:13:31,620 --> 01:13:34,040
values
1703
01:13:35,100 --> 01:13:38,000
in our console
1704
01:13:38,040 --> 01:13:41,940
and as you can see output is 2.5 and 2.5
1705
01:13:41,940 --> 01:13:44,400
but if I hover over here
1706
01:13:44,400 --> 01:13:47,219
you can see that the type for uh because
1707
01:13:47,219 --> 01:13:49,380
it's explicitly given here is the float
1708
01:13:49,380 --> 01:13:51,780
if I hover over here this is the double
1709
01:13:51,780 --> 01:13:54,480
but let's see what happens if I delete
1710
01:13:54,480 --> 01:13:57,239
the fractional part and I just actually
1711
01:13:57,239 --> 01:14:00,659
you can uh you can put a lowercase if
1712
01:14:00,659 --> 01:14:03,960
lowercase f here or an uppercase F like
1713
01:14:03,960 --> 01:14:07,460
this if you run this code again
1714
01:14:10,620 --> 01:14:12,480
you'll have the same output
1715
01:14:12,480 --> 01:14:14,640
again now let's uh
1716
01:14:14,640 --> 01:14:17,340
delete the fractional part and just let
1717
01:14:17,340 --> 01:14:21,120
an F there and here let's also delete
1718
01:14:21,120 --> 01:14:23,820
the fractional part and it's
1719
01:14:23,820 --> 01:14:27,600
let's run our code and you may be
1720
01:14:27,600 --> 01:14:31,040
know what is going to happen now
1721
01:14:35,040 --> 01:14:39,480
printed 2.0 for uh my number because we
1722
01:14:39,480 --> 01:14:40,860
said exclusively here that this is a
1723
01:14:40,860 --> 01:14:42,800
flow so it prints the fractional part
1724
01:14:42,800 --> 01:14:46,140
but uh okay in the case of my number you
1725
01:14:46,140 --> 01:14:49,500
only output is the 2 because now this is
1726
01:14:49,500 --> 01:14:51,600
inferred if you hover over here as an
1727
01:14:51,600 --> 01:14:54,960
INT so one
1728
01:14:54,960 --> 01:14:57,900
solution to this maybe to type here
1729
01:14:57,900 --> 01:15:00,199
double
1730
01:15:01,739 --> 01:15:04,560
and try to see what happens and if I
1731
01:15:04,560 --> 01:15:06,840
hover over this hover over here says the
1732
01:15:06,840 --> 01:15:08,820
interior in the integer literally does
1733
01:15:08,820 --> 01:15:10,020
not conform the expected to tie double
1734
01:15:10,020 --> 01:15:12,960
and it's correct because this is assumed
1735
01:15:12,960 --> 01:15:16,500
to be a need so you cannot uh you cannot
1736
01:15:16,500 --> 01:15:18,360
even though it said exclusive here that
1737
01:15:18,360 --> 01:15:21,300
this is going to store doubles here we
1738
01:15:21,300 --> 01:15:23,580
put an INT an integer value
1739
01:15:23,580 --> 01:15:26,280
so if I delete the type here
1740
01:15:26,280 --> 01:15:28,980
let's say if I try to put a d here
1741
01:15:28,980 --> 01:15:32,880
actually there is no D for the double
1742
01:15:32,880 --> 01:15:35,760
there is no like in the case of the
1743
01:15:35,760 --> 01:15:37,739
float you know if even if I try to put
1744
01:15:37,739 --> 01:15:40,020
the uppercase there is no
1745
01:15:40,020 --> 01:15:42,179
uh way to add the suffix to our
1746
01:15:42,179 --> 01:15:44,280
variables the only way is to put
1747
01:15:44,280 --> 01:15:49,620
directly here to point zero and now
1748
01:15:49,620 --> 01:15:51,260
this is
1749
01:15:51,260 --> 01:15:53,760
inferred as a double
1750
01:15:53,760 --> 01:15:55,620
as you can see here
1751
01:15:55,620 --> 01:15:58,820
and if I run this
1752
01:16:02,040 --> 01:16:06,420
now it output the 2.0 and 2.0 in both
1753
01:16:06,420 --> 01:16:08,040
four variables
1754
01:16:08,040 --> 01:16:10,380
all right I'm gonna end this video now
1755
01:16:10,380 --> 01:16:15,080
and uh see on the next video
1756
01:16:15,160 --> 01:16:17,280
[Music]
1757
01:16:17,280 --> 01:16:19,080
all right so far in our course you've
1758
01:16:19,080 --> 01:16:20,820
only dealt with data types which can
1759
01:16:20,820 --> 01:16:23,460
store numbers but there are two
1760
01:16:23,460 --> 01:16:25,199
different data types which you're gonna
1761
01:16:25,199 --> 01:16:28,140
look at in this video which you can use
1762
01:16:28,140 --> 01:16:30,719
for storing a different type of data and
1763
01:16:30,719 --> 01:16:33,000
we're going to see what kind of data we
1764
01:16:33,000 --> 01:16:35,159
can store but those data types are
1765
01:16:35,159 --> 01:16:36,960
called the Char data type and the
1766
01:16:36,960 --> 01:16:38,940
Boolean data type but first let's create
1767
01:16:38,940 --> 01:16:42,060
our project let's create a new project
1768
01:16:42,060 --> 01:16:44,580
I'm gonna give it a name
1769
01:16:44,580 --> 01:16:46,320
I'm going to call it char
1770
01:16:46,320 --> 01:16:47,580
and
1771
01:16:47,580 --> 01:16:49,620
Boolean again it should be in Pascal
1772
01:16:49,620 --> 01:16:51,840
case
1773
01:16:51,840 --> 01:16:53,699
make sure for the build system that you
1774
01:16:53,699 --> 01:16:55,500
select IntelliJ and measure that have
1775
01:16:55,500 --> 01:16:57,239
the produce jdk selected so I'm going to
1776
01:16:57,239 --> 01:16:58,800
click on next to create our project I'm
1777
01:16:58,800 --> 01:17:00,360
going to click on finish
1778
01:17:00,360 --> 01:17:04,040
now project is going to be created
1779
01:17:05,100 --> 01:17:06,300
on the left hand side we have the
1780
01:17:06,300 --> 01:17:07,620
project pane
1781
01:17:07,620 --> 01:17:10,020
we have our code Auto generated for us
1782
01:17:10,020 --> 01:17:12,780
even though this code is gonna we're
1783
01:17:12,780 --> 01:17:14,100
gonna delete this code which is inside
1784
01:17:14,100 --> 01:17:16,440
the curly braces because you don't need
1785
01:17:16,440 --> 01:17:20,780
a squad so I'm going to delete this code
1786
01:17:20,820 --> 01:17:23,400
and let's also hide the project pane
1787
01:17:23,400 --> 01:17:25,800
because you don't need a project pay
1788
01:17:25,800 --> 01:17:27,600
and
1789
01:17:27,600 --> 01:17:30,179
let's see how we can declare a chart
1790
01:17:30,179 --> 01:17:31,980
data type and after that we're gonna
1791
01:17:31,980 --> 01:17:33,840
talk about
1792
01:17:33,840 --> 01:17:36,239
how where you can use the shutter type
1793
01:17:36,239 --> 01:17:37,800
in a specific case and what specific
1794
01:17:37,800 --> 01:17:39,900
cases you can use so I'm going to type
1795
01:17:39,900 --> 01:17:42,360
Val because I want this variable to be
1796
01:17:42,360 --> 01:17:44,159
mutable I don't want to change valid
1797
01:17:44,159 --> 01:17:45,600
value later
1798
01:17:45,600 --> 01:17:47,520
I'm going to call it my
1799
01:17:47,520 --> 01:17:50,159
Char value
1800
01:17:50,159 --> 01:17:53,280
again it should be in camel case
1801
01:17:53,280 --> 01:17:55,380
and for the type I'm going to put colon
1802
01:17:55,380 --> 01:17:58,260
I'm going to choose char
1803
01:17:58,260 --> 01:18:00,060
press enter
1804
01:18:00,060 --> 01:18:03,060
let's put equals and let's now assign a
1805
01:18:03,060 --> 01:18:06,480
value to this variable so this time I'm
1806
01:18:06,480 --> 01:18:07,620
gonna type
1807
01:18:07,620 --> 01:18:10,860
a single pair of quotation marks not a
1808
01:18:10,860 --> 01:18:12,360
double pair of quotation marks like we
1809
01:18:12,360 --> 01:18:15,840
did in our string data type and in a
1810
01:18:15,840 --> 01:18:19,020
chart data type we can store only a
1811
01:18:19,020 --> 01:18:21,480
single sum symbol and that symbol can be
1812
01:18:21,480 --> 01:18:24,440
a letter a number or an
1813
01:18:24,440 --> 01:18:27,659
exclamation point on the mark
1814
01:18:27,659 --> 01:18:30,600
so I'm gonna put here a d a single
1815
01:18:30,600 --> 01:18:33,360
letter D and uh you may be wondering now
1816
01:18:33,360 --> 01:18:35,760
what is this what is a specific case
1817
01:18:35,760 --> 01:18:37,620
when you where you can use a charter
1818
01:18:37,620 --> 01:18:40,140
value a specific case can be let's say
1819
01:18:40,140 --> 01:18:41,760
if you want to store the last key press
1820
01:18:41,760 --> 01:18:46,560
the bio user so this is a specific case
1821
01:18:46,560 --> 01:18:48,239
where you can use the charter data type
1822
01:18:48,239 --> 01:18:50,820
now let's print this value in our
1823
01:18:50,820 --> 01:18:51,840
console
1824
01:18:51,840 --> 01:18:53,520
to see that
1825
01:18:53,520 --> 01:18:55,800
it should be in a lowercase letters
1826
01:18:55,800 --> 01:18:57,780
pinterland
1827
01:18:57,780 --> 01:19:02,640
and uh you can print it by the usual or
1828
01:19:02,640 --> 01:19:04,500
you put here a double pair of quotation
1829
01:19:04,500 --> 01:19:07,980
marks and we type a dollar sign and hero
1830
01:19:07,980 --> 01:19:10,080
we type the name of our variable match
1831
01:19:10,080 --> 01:19:11,400
our value
1832
01:19:11,400 --> 01:19:13,320
press enter
1833
01:19:13,320 --> 01:19:16,440
I messed up the code let's delete this
1834
01:19:16,440 --> 01:19:19,580
now if I run this
1835
01:19:24,120 --> 01:19:28,040
the console will start in a short moment
1836
01:19:28,860 --> 01:19:31,739
and you're gonna see the output
1837
01:19:31,739 --> 01:19:34,820
of the letter d
1838
01:19:38,640 --> 01:19:40,739
right so the console opened and now we
1839
01:19:40,739 --> 01:19:43,620
have output down here the letter d
1840
01:19:43,620 --> 01:19:48,480
so this is the usual way how one of uh
1841
01:19:48,480 --> 01:19:50,400
referring a variable that but you can
1842
01:19:50,400 --> 01:19:52,800
refer the variable even
1843
01:19:52,800 --> 01:19:55,460
like this
1844
01:19:55,920 --> 01:19:59,460
by not putting the dollar sign
1845
01:19:59,460 --> 01:20:03,260
deleted the dollar sign and also the
1846
01:20:03,719 --> 01:20:06,719
the quotation mark at the beginning now
1847
01:20:06,719 --> 01:20:09,440
if I run this
1848
01:20:14,699 --> 01:20:18,239
it's going to still output the battery
1849
01:20:18,239 --> 01:20:20,219
so we will see that we have now most of
1850
01:20:20,219 --> 01:20:21,659
the letter A so this is another way of
1851
01:20:21,659 --> 01:20:23,820
printing the value even in a variable
1852
01:20:23,820 --> 01:20:25,440
but this uh
1853
01:20:25,440 --> 01:20:28,739
it's not really the usual way to do it
1854
01:20:28,739 --> 01:20:30,300
now
1855
01:20:30,300 --> 01:20:32,400
um let's take a look at the next data
1856
01:20:32,400 --> 01:20:34,679
type and that is the Boolean data type
1857
01:20:34,679 --> 01:20:36,900
and to declare a boolean.type we type
1858
01:20:36,900 --> 01:20:38,640
again Val because again I want this to
1859
01:20:38,640 --> 01:20:41,580
be multiple and and I'm going to call it
1860
01:20:41,580 --> 01:20:42,960
my
1861
01:20:42,960 --> 01:20:46,520
in camel case it should be
1862
01:20:47,880 --> 01:20:50,820
Boolean value
1863
01:20:50,820 --> 01:20:52,920
for the type choose
1864
01:20:52,920 --> 01:20:54,179
Boolean
1865
01:20:54,179 --> 01:20:57,239
and a Boolean a Boolean data type Can
1866
01:20:57,239 --> 01:20:59,100
Only Store
1867
01:20:59,100 --> 01:21:03,179
a true or a false or a false value
1868
01:21:03,179 --> 01:21:07,820
so you can only assign here true
1869
01:21:08,699 --> 01:21:10,679
or false
1870
01:21:10,679 --> 01:21:13,380
so if you type now let's put in those
1871
01:21:13,380 --> 01:21:16,440
values but this time I'm going to put
1872
01:21:16,440 --> 01:21:19,500
the parenthesis and I'm going to put the
1873
01:21:19,500 --> 01:21:23,280
quotation marks I'm going to type my
1874
01:21:23,280 --> 01:21:24,360
char
1875
01:21:24,360 --> 01:21:26,460
let's
1876
01:21:26,460 --> 01:21:28,560
my char
1877
01:21:28,560 --> 01:21:31,260
value equals
1878
01:21:31,260 --> 01:21:33,300
dollar sign
1879
01:21:33,300 --> 01:21:36,120
my chart value
1880
01:21:36,120 --> 01:21:38,699
and
1881
01:21:38,699 --> 01:21:41,100
my
1882
01:21:41,100 --> 01:21:44,780
let's type the name here
1883
01:21:47,820 --> 01:21:50,159
Boolean value
1884
01:21:50,159 --> 01:21:54,360
equals and let's put the dollar sign
1885
01:21:54,360 --> 01:21:59,780
my Boolean value now if we run this code
1886
01:22:04,620 --> 01:22:06,600
now you can see that my chart value
1887
01:22:06,600 --> 01:22:10,320
which is this text here
1888
01:22:10,320 --> 01:22:13,080
has this value which is the content of
1889
01:22:13,080 --> 01:22:14,600
this variable that we're referring here
1890
01:22:14,600 --> 01:22:16,739
and uh
1891
01:22:16,739 --> 01:22:21,179
my Boolean value which is our variable
1892
01:22:21,179 --> 01:22:23,520
the name of our variable has the content
1893
01:22:23,520 --> 01:22:27,199
of false so it printed here false now
1894
01:22:27,199 --> 01:22:31,440
Boolean data types the Boolean Atlas are
1895
01:22:31,440 --> 01:22:33,360
going to be used in our code a lot
1896
01:22:33,360 --> 01:22:36,780
because the Boolean data types can be
1897
01:22:36,780 --> 01:22:39,420
used for controlling the flow of the
1898
01:22:39,420 --> 01:22:42,659
program so you can in you're gonna see
1899
01:22:42,659 --> 01:22:44,400
that in combination with the if
1900
01:22:44,400 --> 01:22:46,140
statement without which you're going to
1901
01:22:46,140 --> 01:22:48,540
look at in the next video on the next
1902
01:22:48,540 --> 01:22:52,020
videos uh you can control you can
1903
01:22:52,020 --> 01:22:53,400
actually control the flow of the program
1904
01:22:53,400 --> 01:22:55,739
because usually the flow the program is
1905
01:22:55,739 --> 01:22:58,800
from top to bottom and with the for with
1906
01:22:58,800 --> 01:23:01,440
the the Boolean data type and with an if
1907
01:23:01,440 --> 01:23:03,000
statement as you'll see you can control
1908
01:23:03,000 --> 01:23:04,920
uh actually what
1909
01:23:04,920 --> 01:23:08,699
you can control to you can control what
1910
01:23:08,699 --> 01:23:12,120
parts of the code are
1911
01:23:12,120 --> 01:23:14,880
uh compiled or not
1912
01:23:14,880 --> 01:23:16,739
based on a certain condition if that
1913
01:23:16,739 --> 01:23:18,540
function is false or if that condition
1914
01:23:18,540 --> 01:23:19,679
is true
1915
01:23:19,679 --> 01:23:21,719
so Boolean dial types are going to be
1916
01:23:21,719 --> 01:23:24,480
used as you'll see a lot in our code in
1917
01:23:24,480 --> 01:23:28,020
your coding career as you'll see
1918
01:23:28,020 --> 01:23:30,239
so these are the
1919
01:23:30,239 --> 01:23:31,560
two
1920
01:23:31,560 --> 01:23:34,860
data ties that can be used for storing
1921
01:23:34,860 --> 01:23:41,299
either a single character or a or
1922
01:23:42,300 --> 01:23:44,760
a character and this is the data type
1923
01:23:44,760 --> 01:23:46,800
which can be used for storing true or
1924
01:23:46,800 --> 01:23:49,860
false which is called the Boolean type
1925
01:23:49,860 --> 01:23:51,900
also you
1926
01:23:51,900 --> 01:23:55,140
May so already that you can delete the
1927
01:23:55,140 --> 01:23:57,420
types yet because as we talked about
1928
01:23:57,420 --> 01:23:59,760
kotlin has what is called the type
1929
01:23:59,760 --> 01:24:02,460
inference so can it infer the type by
1930
01:24:02,460 --> 01:24:03,320
looking
1931
01:24:03,320 --> 01:24:06,360
at what value assigned to it so in this
1932
01:24:06,360 --> 01:24:08,540
case
1933
01:24:08,580 --> 01:24:10,739
you know it knows that this is a type of
1934
01:24:10,739 --> 01:24:13,860
chart so here you know you can see that
1935
01:24:13,860 --> 01:24:15,600
the type is a Boolean
1936
01:24:15,600 --> 01:24:17,460
all right but before we end our video
1937
01:24:17,460 --> 01:24:19,260
let's do a little recap
1938
01:24:19,260 --> 01:24:22,140
about the eight data types that we
1939
01:24:22,140 --> 01:24:23,900
looked at into throughout these videos
1940
01:24:23,900 --> 01:24:27,179
and the eight data types are the byte
1941
01:24:27,179 --> 01:24:30,420
short int and along those are for
1942
01:24:30,420 --> 01:24:32,520
storing the whole numbers
1943
01:24:32,520 --> 01:24:34,440
and the next two data types are the
1944
01:24:34,440 --> 01:24:36,719
float and double for storing floating
1945
01:24:36,719 --> 01:24:39,179
Point numbers for numbers which have a
1946
01:24:39,179 --> 01:24:41,640
fractional part decimals
1947
01:24:41,640 --> 01:24:44,460
and the next two ones are the ones that
1948
01:24:44,460 --> 01:24:46,500
we looked at in do this video and those
1949
01:24:46,500 --> 01:24:48,600
are the Char and the Boolean
1950
01:24:48,600 --> 01:24:51,719
and those data types are also called the
1951
01:24:51,719 --> 01:24:55,080
primitive data types because it has to
1952
01:24:55,080 --> 01:24:56,940
do with their call like that because it
1953
01:24:56,940 --> 01:24:58,560
has to do with how they are stored in
1954
01:24:58,560 --> 01:24:59,580
memory
1955
01:24:59,580 --> 01:25:03,000
and a string data type is not really A
1956
01:25:03,000 --> 01:25:05,100
Primitive Delta because the store is
1957
01:25:05,100 --> 01:25:07,560
stored a little differently also as you
1958
01:25:07,560 --> 01:25:08,460
can see
1959
01:25:08,460 --> 01:25:11,280
the string data type has a special
1960
01:25:11,280 --> 01:25:12,900
language support
1961
01:25:12,900 --> 01:25:16,199
so you can consider this eight primitive
1962
01:25:16,199 --> 01:25:19,080
data types as the building blocks for
1963
01:25:19,080 --> 01:25:21,300
data manipulation and in the next video
1964
01:25:21,300 --> 01:25:23,460
we're gonna look at those this data
1965
01:25:23,460 --> 01:25:25,679
manipulation but by looking specifically
1966
01:25:25,679 --> 01:25:26,699
at
1967
01:25:26,699 --> 01:25:29,820
what operations we we can do with the
1968
01:25:29,820 --> 01:25:33,060
those data types because
1969
01:25:33,060 --> 01:25:35,880
in uh throughout this this video we only
1970
01:25:35,880 --> 01:25:38,219
looked at how we can store values and
1971
01:25:38,219 --> 01:25:40,440
how we can output this those values but
1972
01:25:40,440 --> 01:25:44,420
in uh in a real real app you'll need to
1973
01:25:44,420 --> 01:25:47,520
to manipulate that data to other to
1974
01:25:47,520 --> 01:25:48,840
subtract something and we're going to
1975
01:25:48,840 --> 01:25:51,179
look at all of this in the next video
1976
01:25:51,179 --> 01:25:54,020
see you next
1977
01:25:57,659 --> 01:25:59,699
all right so far in our videos we've
1978
01:25:59,699 --> 01:26:01,080
only looked at how we can store
1979
01:26:01,080 --> 01:26:03,719
different values in our variables how to
1980
01:26:03,719 --> 01:26:06,780
Output them to the console but in a real
1981
01:26:06,780 --> 01:26:08,340
app you'll need to do some mathematical
1982
01:26:08,340 --> 01:26:10,980
calculations so in this video I'm going
1983
01:26:10,980 --> 01:26:12,300
to learn about that we're going to look
1984
01:26:12,300 --> 01:26:14,940
at operators so first let's create a new
1985
01:26:14,940 --> 01:26:16,380
project I'm going to click on new
1986
01:26:16,380 --> 01:26:18,179
project I'm going to speed up the
1987
01:26:18,179 --> 01:26:19,920
process now a little bit because we did
1988
01:26:19,920 --> 01:26:21,540
this already
1989
01:26:21,540 --> 01:26:23,100
a few times so I'm going to call it
1990
01:26:23,100 --> 01:26:24,540
operators
1991
01:26:24,540 --> 01:26:26,820
I'm going to select IntelliJ click on X
1992
01:26:26,820 --> 01:26:28,380
and click on finish
1993
01:26:28,380 --> 01:26:30,420
now our project is going to be created
1994
01:26:30,420 --> 01:26:32,940
in a short moment
1995
01:26:32,940 --> 01:26:34,440
again on the left hand side you have the
1996
01:26:34,440 --> 01:26:36,360
project Pane and we have the main effect
1997
01:26:36,360 --> 01:26:38,340
function and the
1998
01:26:38,340 --> 01:26:40,739
code Auto generator for us
1999
01:26:40,739 --> 01:26:42,600
now we're going to delete the code
2000
01:26:42,600 --> 01:26:44,219
inside the curly braces inside the
2001
01:26:44,219 --> 01:26:46,260
calibrations of main function because it
2002
01:26:46,260 --> 01:26:47,580
only Discord
2003
01:26:47,580 --> 01:26:49,260
and also I'm going to hide the project
2004
01:26:49,260 --> 01:26:50,760
pane because I don't need the previous
2005
01:26:50,760 --> 01:26:54,120
Pane and first let's declare two
2006
01:26:54,120 --> 01:26:55,980
variables I'm going to declare the first
2007
01:26:55,980 --> 01:26:58,920
variable as a VAR because I want to
2008
01:26:58,920 --> 01:27:00,900
reassign this value later I'm going to
2009
01:27:00,900 --> 01:27:03,120
call it um
2010
01:27:03,120 --> 01:27:06,060
uh let's say um
2011
01:27:06,060 --> 01:27:09,000
X and in a real app you should use more
2012
01:27:09,000 --> 01:27:10,980
descriptiveness because
2013
01:27:10,980 --> 01:27:13,739
the name of the variable should say what
2014
01:27:13,739 --> 01:27:17,219
the variable is doing anyway I'm gonna
2015
01:27:17,219 --> 01:27:19,020
put equals I'm going to assign a value
2016
01:27:19,020 --> 01:27:20,340
of five
2017
01:27:20,340 --> 01:27:21,659
and the next variable I'm going to
2018
01:27:21,659 --> 01:27:24,000
declare it as a Val because I don't want
2019
01:27:24,000 --> 01:27:26,340
to change its value later
2020
01:27:26,340 --> 01:27:28,500
I'm going to call it I I'm going to put
2021
01:27:28,500 --> 01:27:30,239
a value of 3 here
2022
01:27:30,239 --> 01:27:31,920
and next I'm going to decode another
2023
01:27:31,920 --> 01:27:35,159
variable this is going to be also
2024
01:27:35,159 --> 01:27:38,400
or Val I'm going to call it result
2025
01:27:38,400 --> 01:27:41,960
and we're going to store the
2026
01:27:42,780 --> 01:27:45,960
sum of 5 and 3 here and to do that type
2027
01:27:45,960 --> 01:27:47,580
here x
2028
01:27:47,580 --> 01:27:49,620
and this is the first operator that
2029
01:27:49,620 --> 01:27:52,800
we're going to looked at look at and
2030
01:27:52,800 --> 01:27:55,440
that is the add or the plus operator so
2031
01:27:55,440 --> 01:27:58,139
I'm going to put here X Plus
2032
01:27:58,139 --> 01:27:59,280
I
2033
01:27:59,280 --> 01:28:01,320
now
2034
01:28:01,320 --> 01:28:04,380
let's print this result I'm going to
2035
01:28:04,380 --> 01:28:07,219
type println
2036
01:28:07,800 --> 01:28:10,260
I'm gonna put the double pair of
2037
01:28:10,260 --> 01:28:11,940
quotation marks I'm gonna type result
2038
01:28:11,940 --> 01:28:13,800
the text
2039
01:28:13,800 --> 01:28:16,620
equals dollar sign
2040
01:28:16,620 --> 01:28:19,020
to get the value of the variable here
2041
01:28:19,020 --> 01:28:20,699
I'm going to press enter to fill that
2042
01:28:20,699 --> 01:28:22,980
suggestion for me and now let's run this
2043
01:28:22,980 --> 01:28:24,780
code to see what is going to Output
2044
01:28:24,780 --> 01:28:26,580
you're gonna see the output 8 here
2045
01:28:26,580 --> 01:28:27,659
because
2046
01:28:27,659 --> 01:28:30,179
five plus three is eight
2047
01:28:30,179 --> 01:28:32,340
the console should open in a short
2048
01:28:32,340 --> 01:28:33,900
moment
2049
01:28:33,900 --> 01:28:37,260
right so it output the result equals to
2050
01:28:37,260 --> 01:28:39,120
eight because five plus three is eight
2051
01:28:39,120 --> 01:28:42,300
now what we have differently here and we
2052
01:28:42,300 --> 01:28:45,120
didn't uh we didn't have we didn't had
2053
01:28:45,120 --> 01:28:47,639
previously is that now we're using a
2054
01:28:47,639 --> 01:28:49,860
variable the variables here and we're
2055
01:28:49,860 --> 01:28:52,320
not using liters like we have here five
2056
01:28:52,320 --> 01:28:54,060
and three type directly
2057
01:28:54,060 --> 01:28:55,699
and um
2058
01:28:55,699 --> 01:29:01,320
it's important to to clarify some
2059
01:29:01,320 --> 01:29:04,980
names here and first is the name operand
2060
01:29:04,980 --> 01:29:08,219
and open an operand is any object which
2061
01:29:08,219 --> 01:29:10,800
which isn't affected by an operator in
2062
01:29:10,800 --> 01:29:13,739
our case the operands are X and I
2063
01:29:13,739 --> 01:29:16,739
because those those variables are
2064
01:29:16,739 --> 01:29:19,920
affected by the operator plus because it
2065
01:29:19,920 --> 01:29:23,940
sums their values and then it puts these
2066
01:29:23,940 --> 01:29:26,580
values in the variable result so those
2067
01:29:26,580 --> 01:29:29,760
two variables X and I are called operand
2068
01:29:29,760 --> 01:29:32,880
also the if you had here little revolves
2069
01:29:32,880 --> 01:29:38,639
if we put it here let's say 5 and 3.
2070
01:29:38,639 --> 01:29:41,040
directly these two all are also called
2071
01:29:41,040 --> 01:29:44,219
operands so literals or variables are
2072
01:29:44,219 --> 01:29:46,620
called the operands any
2073
01:29:46,620 --> 01:29:48,719
any objects which is affected by an
2074
01:29:48,719 --> 01:29:50,940
operator is called an operand because if
2075
01:29:50,940 --> 01:29:53,580
you now if you
2076
01:29:53,580 --> 01:29:56,400
now you're gonna see also eight here but
2077
01:29:56,400 --> 01:29:58,560
let's change it back to
2078
01:29:58,560 --> 01:30:00,179
to I
2079
01:30:00,179 --> 01:30:02,280
and X
2080
01:30:02,280 --> 01:30:05,040
and there is another way to
2081
01:30:05,040 --> 01:30:06,420
to
2082
01:30:06,420 --> 01:30:09,780
output the value of x plus I by using a
2083
01:30:09,780 --> 01:30:11,159
placeholder what is called the
2084
01:30:11,159 --> 01:30:12,840
placeholder and to do that let's first
2085
01:30:12,840 --> 01:30:15,780
delete this line of code
2086
01:30:15,780 --> 01:30:17,580
and
2087
01:30:17,580 --> 01:30:19,320
this now is red because we don't have
2088
01:30:19,320 --> 01:30:22,020
the variable declared I'm gonna delete
2089
01:30:22,020 --> 01:30:25,080
what is inside the quotation marks I'm
2090
01:30:25,080 --> 01:30:28,860
gonna I'm gonna type here X plus I the
2091
01:30:28,860 --> 01:30:32,400
text equals and now I'm gonna put here
2092
01:30:32,400 --> 01:30:34,620
I'm going to put here dollar sign and
2093
01:30:34,620 --> 01:30:36,900
I'm gonna put curly braces
2094
01:30:36,900 --> 01:30:39,060
and inside the color braces I'm going to
2095
01:30:39,060 --> 01:30:42,719
type Let's uh click here to make it
2096
01:30:42,719 --> 01:30:43,980
clear
2097
01:30:43,980 --> 01:30:45,960
X
2098
01:30:45,960 --> 01:30:48,840
plus I
2099
01:30:48,840 --> 01:30:51,300
so this is called the placeholder and
2100
01:30:51,300 --> 01:30:55,080
you can use this and in a placeholder we
2101
01:30:55,080 --> 01:30:56,840
can only put
2102
01:30:56,840 --> 01:31:00,420
expressions and an expression is a
2103
01:31:00,420 --> 01:31:02,340
construct which evaluates to a single
2104
01:31:02,340 --> 01:31:05,280
value so in our case
2105
01:31:05,280 --> 01:31:07,380
X plus I evaluates to a single value
2106
01:31:07,380 --> 01:31:11,159
because it Returns the value 8 so this
2107
01:31:11,159 --> 01:31:14,940
is why we can put here uh X plus I and
2108
01:31:14,940 --> 01:31:15,780
uh
2109
01:31:15,780 --> 01:31:18,300
even though if you if you if if you put
2110
01:31:18,300 --> 01:31:21,659
something which you will not uh
2111
01:31:21,659 --> 01:31:24,239
be an expression here IntelliJ is going
2112
01:31:24,239 --> 01:31:27,179
to warn you and I'm gonna see uh in the
2113
01:31:27,179 --> 01:31:28,920
following minutes
2114
01:31:28,920 --> 01:31:31,080
um an example of that now let's out for
2115
01:31:31,080 --> 01:31:33,000
this
2116
01:31:33,000 --> 01:31:34,260
and you're going to see the same result
2117
01:31:34,260 --> 01:31:37,139
but now we have the different text here
2118
01:31:37,139 --> 01:31:38,340
we're gonna have we're going to have X
2119
01:31:38,340 --> 01:31:40,620
plus I so now I have X Plus PSI equals
2120
01:31:40,620 --> 01:31:42,900
eight which is the correct result now
2121
01:31:42,900 --> 01:31:45,900
let's copy this four times and to do to
2122
01:31:45,900 --> 01:31:50,040
make that easier we can press Ctrl D or
2123
01:31:50,040 --> 01:31:52,440
command D on Mac
2124
01:31:52,440 --> 01:31:56,280
so I'm gonna press Ctrl D four times
2125
01:31:56,280 --> 01:31:59,100
now let's change this to
2126
01:31:59,100 --> 01:32:01,980
minus because we're going to look at the
2127
01:32:01,980 --> 01:32:04,380
subtraction
2128
01:32:04,380 --> 01:32:07,219
now here we're gonna look at I'm gonna
2129
01:32:07,219 --> 01:32:10,860
look at the multiplication operator
2130
01:32:10,860 --> 01:32:13,020
which is an Asterix operator so I need
2131
01:32:13,020 --> 01:32:14,940
to put here inside the curly braces the
2132
01:32:14,940 --> 01:32:17,699
Asterix because these were
2133
01:32:17,699 --> 01:32:21,120
the expression is evaluated and the next
2134
01:32:21,120 --> 01:32:22,800
is the division so we're going to put
2135
01:32:22,800 --> 01:32:26,540
here a photo forward slash
2136
01:32:26,760 --> 01:32:28,560
so I'm going to put here also forward
2137
01:32:28,560 --> 01:32:30,920
slash
2138
01:32:32,159 --> 01:32:36,360
forward slash right and you know this uh
2139
01:32:36,360 --> 01:32:40,199
this operators for a month class this
2140
01:32:40,199 --> 01:32:42,900
one but the one that is not so well
2141
01:32:42,900 --> 01:32:46,920
known outside the programming uh it's uh
2142
01:32:46,920 --> 01:32:49,500
it's called the modulus operator
2143
01:32:49,500 --> 01:32:51,719
and the modulus operator is operator
2144
01:32:51,719 --> 01:32:53,159
which
2145
01:32:53,159 --> 01:32:54,000
um
2146
01:32:54,000 --> 01:32:58,560
gives us the reminder of a division so
2147
01:32:58,560 --> 01:33:02,520
the models operator is a percent sign so
2148
01:33:02,520 --> 01:33:05,280
put per second sign here and percenter
2149
01:33:05,280 --> 01:33:07,260
sign here
2150
01:33:07,260 --> 01:33:08,520
right
2151
01:33:08,520 --> 01:33:12,440
now if you run this code
2152
01:33:17,400 --> 01:33:20,219
all right so now we have the output down
2153
01:33:20,219 --> 01:33:23,040
here and we have X plus I see eight
2154
01:33:23,040 --> 01:33:24,960
because five plus three is eight x minus
2155
01:33:24,960 --> 01:33:27,780
I is 2 because 5 minus uh
2156
01:33:27,780 --> 01:33:32,219
3 is 2 x times Y is 15 because 5 times 3
2157
01:33:32,219 --> 01:33:35,159
is 15 x divided by Y and this is
2158
01:33:35,159 --> 01:33:38,460
interesting is 1 but because those
2159
01:33:38,460 --> 01:33:41,219
variables are of typing they're inferred
2160
01:33:41,219 --> 01:33:43,679
automatically to be of type in it
2161
01:33:43,679 --> 01:33:45,420
dropped completely the fractional part
2162
01:33:45,420 --> 01:33:48,600
here so we don't have here 1.6 or
2163
01:33:48,600 --> 01:33:51,900
something you have only the integer part
2164
01:33:51,900 --> 01:33:55,219
which is one and here we have the the
2165
01:33:55,219 --> 01:33:57,840
modulus operator and this gives us the
2166
01:33:57,840 --> 01:34:01,380
remainder of 5 divided by 3 which is 1
2167
01:34:01,380 --> 01:34:03,719
and has a reminder of two
2168
01:34:03,719 --> 01:34:07,080
now if you want to to have here the
2169
01:34:07,080 --> 01:34:09,239
fractional part you need to declare uh
2170
01:34:09,239 --> 01:34:13,199
one of the variable uh as a type as a
2171
01:34:13,199 --> 01:34:16,199
floating Point number and that can be or
2172
01:34:16,199 --> 01:34:19,199
or either a float or a double so then
2173
01:34:19,199 --> 01:34:21,060
I'm going to declare the
2174
01:34:21,060 --> 01:34:23,880
the I variable as a double so I'm going
2175
01:34:23,880 --> 01:34:27,540
to put 3.0 Here and Now
2176
01:34:27,540 --> 01:34:29,639
all the variables all the results here
2177
01:34:29,639 --> 01:34:31,739
are going to be are going to be are
2178
01:34:31,739 --> 01:34:35,040
going to be of type double so if you run
2179
01:34:35,040 --> 01:34:36,000
this
2180
01:34:36,000 --> 01:34:37,679
you're gonna see
2181
01:34:37,679 --> 01:34:39,420
the fractional part for all the
2182
01:34:39,420 --> 01:34:41,960
variables here
2183
01:34:42,179 --> 01:34:45,179
now you can see now we have wipe one
2184
01:34:45,179 --> 01:34:47,900
8.0 2.0
2185
01:34:47,900 --> 01:34:54,000
15.0 and here we have 1.6666 and 7.
2186
01:34:54,000 --> 01:34:56,639
now it shows also the fractional part
2187
01:34:56,639 --> 01:34:59,400
and uh for the reminder all we've we've
2188
01:34:59,400 --> 01:35:03,600
also have the fractional part here so
2189
01:35:03,600 --> 01:35:07,440
um now the type of the if you want the
2190
01:35:07,440 --> 01:35:09,800
type of the expression it's a double
2191
01:35:09,800 --> 01:35:12,780
because we declare here as a double this
2192
01:35:12,780 --> 01:35:14,880
variable I as a double and the next one
2193
01:35:14,880 --> 01:35:17,460
is wide then automatically you don't
2194
01:35:17,460 --> 01:35:20,280
need to declare also the ex as a double
2195
01:35:20,280 --> 01:35:22,080
it will be
2196
01:35:22,080 --> 01:35:24,239
um so if you want widen automatically by
2197
01:35:24,239 --> 01:35:27,179
the fact that it required the I variable
2198
01:35:27,179 --> 01:35:29,900
as a double
2199
01:35:32,460 --> 01:35:33,780
all right
2200
01:35:33,780 --> 01:35:35,880
and let's continue
2201
01:35:35,880 --> 01:35:38,040
and often in programming you'll want to
2202
01:35:38,040 --> 01:35:40,020
increase
2203
01:35:40,020 --> 01:35:44,280
a variable by a certain number and to do
2204
01:35:44,280 --> 01:35:46,020
that let's first
2205
01:35:46,020 --> 01:35:49,920
let's declare again the variable result
2206
01:35:49,920 --> 01:35:51,659
down here
2207
01:35:51,659 --> 01:35:54,780
let's declare it as a VAR because I want
2208
01:35:54,780 --> 01:35:56,580
to resign it
2209
01:35:56,580 --> 01:35:59,340
let's call it result
2210
01:35:59,340 --> 01:36:03,380
and let's put again X
2211
01:36:03,420 --> 01:36:06,920
plus I here
2212
01:36:07,260 --> 01:36:10,380
now if you want to increase let's say
2213
01:36:10,380 --> 01:36:12,840
the the value of the variable by 2 we
2214
01:36:12,840 --> 01:36:15,239
can type something like a result equals
2215
01:36:15,239 --> 01:36:16,980
result
2216
01:36:16,980 --> 01:36:18,480
let's say that I want to increase this
2217
01:36:18,480 --> 01:36:20,820
variable by 2. I'm going to put result
2218
01:36:20,820 --> 01:36:23,280
equals result Plus 2.
2219
01:36:23,280 --> 01:36:24,780
and
2220
01:36:24,780 --> 01:36:28,980
this equals operator is different from
2221
01:36:28,980 --> 01:36:32,699
a mathematical equals because this equal
2222
01:36:32,699 --> 01:36:34,860
separate operator is not saying that
2223
01:36:34,860 --> 01:36:37,020
result equals result plus 2 because I
2224
01:36:37,020 --> 01:36:39,239
think it's incorrect what this equals
2225
01:36:39,239 --> 01:36:42,060
operator is is also called it's an
2226
01:36:42,060 --> 01:36:45,840
assignment operator and what is doing is
2227
01:36:45,840 --> 01:36:48,420
that it assigns
2228
01:36:48,420 --> 01:36:52,280
first it adds to to our variable result
2229
01:36:52,280 --> 01:36:57,120
and then it store stores that that
2230
01:36:57,120 --> 01:36:59,520
result that result of the that
2231
01:36:59,520 --> 01:37:02,699
expression in our variable result all
2232
01:37:02,699 --> 01:37:05,159
right so let's print this value to see
2233
01:37:05,159 --> 01:37:08,460
what we have here so if I print this
2234
01:37:08,460 --> 01:37:10,500
you should see
2235
01:37:10,500 --> 01:37:13,280
10 here
2236
01:37:14,159 --> 01:37:17,239
not print
2237
01:37:18,780 --> 01:37:22,699
I'm and I'm going to put here result
2238
01:37:22,739 --> 01:37:25,699
equals
2239
01:37:25,860 --> 01:37:27,540
dollar sign
2240
01:37:27,540 --> 01:37:28,860
result
2241
01:37:28,860 --> 01:37:30,239
and in front this code you're going to
2242
01:37:30,239 --> 01:37:32,718
see 10.
2243
01:37:38,040 --> 01:37:40,920
right so you have 10 but let's uh let's
2244
01:37:40,920 --> 01:37:42,719
delete the
2245
01:37:42,719 --> 01:37:45,179
the fractional part here to
2246
01:37:45,179 --> 01:37:47,580
have the type of the variable as
2247
01:37:47,580 --> 01:37:51,620
integers so let's run this code again
2248
01:37:54,540 --> 01:37:56,520
so now we have 10 here result equals 10
2249
01:37:56,520 --> 01:37:59,880
and let's copy this called the
2250
01:37:59,880 --> 01:38:03,139
again four times
2251
01:38:03,840 --> 01:38:06,179
let's add a space
2252
01:38:06,179 --> 01:38:09,000
so now I messed up the code and if you
2253
01:38:09,000 --> 01:38:10,800
mess up the code like I did here you can
2254
01:38:10,800 --> 01:38:13,020
do you can undo this thing by going to
2255
01:38:13,020 --> 01:38:17,400
edit and click on undo paste or the
2256
01:38:17,400 --> 01:38:22,679
shortcut is Ctrl Z and you'll see the
2257
01:38:22,679 --> 01:38:25,860
corresponding shortcut in a Mac here on
2258
01:38:25,860 --> 01:38:28,679
the right so please press here and now
2259
01:38:28,679 --> 01:38:30,780
our code is restored here as you can see
2260
01:38:30,780 --> 01:38:34,340
so let's copy this again
2261
01:38:35,040 --> 01:38:38,040
foreign
2262
01:38:40,080 --> 01:38:44,880
and let's space this down so one
2263
01:38:44,880 --> 01:38:47,840
two three
2264
01:38:48,060 --> 01:38:50,060
four
2265
01:38:51,239 --> 01:38:53,580
all right now let's change this this one
2266
01:38:53,580 --> 01:38:55,560
to minus
2267
01:38:55,560 --> 01:38:58,139
let's change this to multiplication
2268
01:38:58,139 --> 01:39:01,080
which is the Aesthetics operator
2269
01:39:01,080 --> 01:39:03,179
let's change this to division which is
2270
01:39:03,179 --> 01:39:06,139
the follow slash
2271
01:39:07,500 --> 01:39:10,320
and let's change this to modulus which
2272
01:39:10,320 --> 01:39:12,300
is the percent sign
2273
01:39:12,300 --> 01:39:14,580
now if you run this code let's run it
2274
01:39:14,580 --> 01:39:19,040
from the top right right hand side now
2275
01:39:22,940 --> 01:39:25,080
what you're going to see is that first
2276
01:39:25,080 --> 01:39:29,600
we have 10 because of now
2277
01:39:29,659 --> 01:39:33,179
the result has a value of 8 because X
2278
01:39:33,179 --> 01:39:36,900
plus sign is 5 plus 3 we have eight and
2279
01:39:36,900 --> 01:39:39,900
now here we we're adding 2 to our result
2280
01:39:39,900 --> 01:39:42,659
and then we print that value in that
2281
01:39:42,659 --> 01:39:44,639
variable in our
2282
01:39:44,639 --> 01:39:46,800
in our console and that is gonna output
2283
01:39:46,800 --> 01:39:49,440
10. the next we
2284
01:39:49,440 --> 01:39:52,500
decrement that variable by 2 and we have
2285
01:39:52,500 --> 01:39:53,280
eight
2286
01:39:53,280 --> 01:39:56,219
next we multiply
2287
01:39:56,219 --> 01:39:59,100
that that uh
2288
01:39:59,100 --> 01:40:02,699
variable by tools so we have 8 times 2
2289
01:40:02,699 --> 01:40:05,880
which is 16 which is the result here
2290
01:40:05,880 --> 01:40:09,659
next we have a result divided by two and
2291
01:40:09,659 --> 01:40:11,219
uh
2292
01:40:11,219 --> 01:40:16,320
16 divided by 2 is 8 so you have eight
2293
01:40:16,320 --> 01:40:17,639
eight here
2294
01:40:17,639 --> 01:40:19,440
now
2295
01:40:19,440 --> 01:40:22,320
now we have result
2296
01:40:22,320 --> 01:40:27,420
modulus 2 and 8 modulus 2 is 0 because 8
2297
01:40:27,420 --> 01:40:28,980
divided by
2298
01:40:28,980 --> 01:40:34,159
by 2 has no reminder right
2299
01:40:35,940 --> 01:40:37,800
next you can see that we have all of
2300
01:40:37,800 --> 01:40:39,600
those underlines here and if you hover
2301
01:40:39,600 --> 01:40:41,639
over one of them it's gonna say
2302
01:40:41,639 --> 01:40:43,920
replaceable with operator assignments so
2303
01:40:43,920 --> 01:40:46,920
there is a shorter wave to write the
2304
01:40:46,920 --> 01:40:49,500
same thing that we have here or there is
2305
01:40:49,500 --> 01:40:51,420
an abbreviation to write the same thing
2306
01:40:51,420 --> 01:40:53,219
that we have here and here and here and
2307
01:40:53,219 --> 01:40:55,560
here because they are so often used in
2308
01:40:55,560 --> 01:40:58,139
programming they are there is a
2309
01:40:58,139 --> 01:40:59,940
abbreviation for all of them so let's
2310
01:40:59,940 --> 01:41:03,780
click on replace with plus and equals
2311
01:41:03,780 --> 01:41:07,560
replace with minus n equals
2312
01:41:07,560 --> 01:41:09,360
replace with the
2313
01:41:09,360 --> 01:41:12,300
multiplication and equals
2314
01:41:12,300 --> 01:41:14,219
with division and equals and with
2315
01:41:14,219 --> 01:41:18,560
modulus n equals here
2316
01:41:20,360 --> 01:41:23,280
all right so this is the abbreviation of
2317
01:41:23,280 --> 01:41:26,639
what we had previously or a short way a
2318
01:41:26,639 --> 01:41:28,440
shorter way to write the same thing so
2319
01:41:28,440 --> 01:41:30,000
if you run this code now you're going to
2320
01:41:30,000 --> 01:41:32,219
see that it's going to Output the exact
2321
01:41:32,219 --> 01:41:34,520
uh
2322
01:41:35,460 --> 01:41:37,560
values that you had previously so we
2323
01:41:37,560 --> 01:41:41,820
have 10 8 16 8 again and zero so this is
2324
01:41:41,820 --> 01:41:43,940
a shorter way of
2325
01:41:43,940 --> 01:41:46,860
abbreviating uh oh actually this is a
2326
01:41:46,860 --> 01:41:48,239
way of abbreviating what we had
2327
01:41:48,239 --> 01:41:49,980
previously because as I said they are
2328
01:41:49,980 --> 01:41:52,440
very very often used in programming what
2329
01:41:52,440 --> 01:41:53,699
is
2330
01:41:53,699 --> 01:41:56,159
all right now
2331
01:41:56,159 --> 01:41:59,040
another important thing to know is
2332
01:41:59,040 --> 01:42:02,340
operator precedence so first let's type
2333
01:42:02,340 --> 01:42:06,000
again a println statement
2334
01:42:06,000 --> 01:42:08,280
and I'm going to put the worker
2335
01:42:08,280 --> 01:42:09,540
recognition marks and inside the
2336
01:42:09,540 --> 01:42:11,760
quotation marks I'm going to put the
2337
01:42:11,760 --> 01:42:13,980
text to three
2338
01:42:13,980 --> 01:42:17,580
so this is the text three plus three
2339
01:42:17,580 --> 01:42:20,940
times let's say um
2340
01:42:20,940 --> 01:42:22,440
four
2341
01:42:22,440 --> 01:42:24,900
now
2342
01:42:24,900 --> 01:42:27,119
also inside the quotation marks let's
2343
01:42:27,119 --> 01:42:30,060
put a dollar let's put equals here
2344
01:42:30,060 --> 01:42:32,040
the text and let's put a dollar sign
2345
01:42:32,040 --> 01:42:35,159
here and now let's put curly braces
2346
01:42:35,159 --> 01:42:37,020
and inside the curly braces I'm going to
2347
01:42:37,020 --> 01:42:39,000
put three
2348
01:42:39,000 --> 01:42:41,780
plus three
2349
01:42:41,940 --> 01:42:45,440
times four
2350
01:42:47,520 --> 01:42:50,760
let's click here to make it clear times
2351
01:42:50,760 --> 01:42:55,100
4. now if you run this code
2352
01:42:59,820 --> 01:43:01,619
now
2353
01:43:01,619 --> 01:43:06,559
you see that it outputed 15 because
2354
01:43:08,480 --> 01:43:11,400
multiplication has precedence over
2355
01:43:11,400 --> 01:43:13,619
addition so it first
2356
01:43:13,619 --> 01:43:17,639
multiplies 3 times 4 which is 12 and
2357
01:43:17,639 --> 01:43:21,239
then it adds 3 to our results so we have
2358
01:43:21,239 --> 01:43:24,060
15. but like in a math class you can
2359
01:43:24,060 --> 01:43:27,060
control which
2360
01:43:27,060 --> 01:43:29,520
expression is evaluated first by putting
2361
01:43:29,520 --> 01:43:33,800
parentheses so if I put parenthesis here
2362
01:43:35,580 --> 01:43:38,699
now this will be evaluated first three
2363
01:43:38,699 --> 01:43:41,760
plus three and then it will multiply the
2364
01:43:41,760 --> 01:43:43,739
result by four so if you run this code
2365
01:43:43,739 --> 01:43:45,900
you're gonna see three plus three is six
2366
01:43:45,900 --> 01:43:47,340
times four
2367
01:43:47,340 --> 01:43:49,199
is 24.
2368
01:43:49,199 --> 01:43:51,000
so you're going to see 24 right now so
2369
01:43:51,000 --> 01:43:53,520
you can see that operators have
2370
01:43:53,520 --> 01:43:55,320
precedence but can control that
2371
01:43:55,320 --> 01:43:57,600
precedence using parentheses like you
2372
01:43:57,600 --> 01:44:00,179
did here so multiplication and division
2373
01:44:00,179 --> 01:44:03,500
have precedence over
2374
01:44:03,500 --> 01:44:06,300
additional and subtraction
2375
01:44:06,300 --> 01:44:09,840
all right there are two other operators
2376
01:44:09,840 --> 01:44:12,119
which can be used for increasing or
2377
01:44:12,119 --> 01:44:14,460
decreasing the value of a variable
2378
01:44:14,460 --> 01:44:17,699
exactly by one and let's look at this
2379
01:44:17,699 --> 01:44:19,440
operator
2380
01:44:19,440 --> 01:44:22,500
but first let's uh use our variable X
2381
01:44:22,500 --> 01:44:24,360
that we declare above
2382
01:44:24,360 --> 01:44:29,360
and let's assign a value of 0 to it
2383
01:44:29,940 --> 01:44:32,820
and to use the
2384
01:44:32,820 --> 01:44:35,820
increment and decrement operator this is
2385
01:44:35,820 --> 01:44:37,679
how they are called
2386
01:44:37,679 --> 01:44:39,900
let's use the printerland statement
2387
01:44:39,900 --> 01:44:42,679
first to
2388
01:44:44,580 --> 01:44:46,800
let's put a double pair of quotation
2389
01:44:46,800 --> 01:44:47,940
marks here
2390
01:44:47,940 --> 01:44:49,800
and
2391
01:44:49,800 --> 01:44:51,960
let's type
2392
01:44:51,960 --> 01:44:57,320
x equals and let's put dollar sign
2393
01:44:57,360 --> 01:44:58,560
and let's type here
2394
01:44:58,560 --> 01:45:00,300
[Music]
2395
01:45:00,300 --> 01:45:01,800
X
2396
01:45:01,800 --> 01:45:04,199
and to use the increment operator we
2397
01:45:04,199 --> 01:45:06,540
type plus plus
2398
01:45:06,540 --> 01:45:10,320
right and what this is gonna do is going
2399
01:45:10,320 --> 01:45:11,580
to increment
2400
01:45:11,580 --> 01:45:14,040
the value of our variable which now is 0
2401
01:45:14,040 --> 01:45:16,800
by 1. and there is another way to
2402
01:45:16,800 --> 01:45:18,540
increment the value for a variable by
2403
01:45:18,540 --> 01:45:21,840
putting the increment upper operator or
2404
01:45:21,840 --> 01:45:24,780
the plus plus operator in front of our
2405
01:45:24,780 --> 01:45:26,820
variable not at the end of our variable
2406
01:45:26,820 --> 01:45:28,739
so we can
2407
01:45:28,739 --> 01:45:33,060
type again a println down here
2408
01:45:33,060 --> 01:45:35,219
let's put the quotation marks and I'm
2409
01:45:35,219 --> 01:45:38,159
going to put x equals and now I'm going
2410
01:45:38,159 --> 01:45:41,000
to put dollar sign
2411
01:45:42,840 --> 01:45:46,440
plus plus X but this now is not working
2412
01:45:46,440 --> 01:45:47,880
because
2413
01:45:47,880 --> 01:45:50,280
dollar sign only works if you refer a
2414
01:45:50,280 --> 01:45:52,139
variable so to do solve that you're
2415
01:45:52,139 --> 01:45:54,360
going to put this in a
2416
01:45:54,360 --> 01:45:56,340
parenthesis
2417
01:45:56,340 --> 01:45:59,780
actually in curly braces
2418
01:46:00,119 --> 01:46:01,980
so let's put this in color braces and
2419
01:46:01,980 --> 01:46:03,360
let's put the
2420
01:46:03,360 --> 01:46:07,280
above one also in curly braces
2421
01:46:10,860 --> 01:46:13,460
not here
2422
01:46:13,500 --> 01:46:16,619
all right and uh both of both of these
2423
01:46:16,619 --> 01:46:19,619
increment increment value of x by one
2424
01:46:19,619 --> 01:46:22,080
they differ in
2425
01:46:22,080 --> 01:46:23,760
what
2426
01:46:23,760 --> 01:46:25,619
value they return
2427
01:46:25,619 --> 01:46:28,619
so the first first Returns the value of
2428
01:46:28,619 --> 01:46:31,159
x which is zero and then it increments
2429
01:46:31,159 --> 01:46:35,280
and the next increment operator which is
2430
01:46:35,280 --> 01:46:38,340
before the name of the before the
2431
01:46:38,340 --> 01:46:42,179
variable it increments the variable and
2432
01:46:42,179 --> 01:46:44,580
then returns its value so what you're
2433
01:46:44,580 --> 01:46:46,380
gonna see if you output this code is
2434
01:46:46,380 --> 01:46:48,840
going to be interesting so if what if
2435
01:46:48,840 --> 01:46:51,619
you output this code
2436
01:46:52,139 --> 01:46:54,060
let's bring it down you're gonna see
2437
01:46:54,060 --> 01:46:55,980
Zero and two and maybe wondering how
2438
01:46:55,980 --> 01:46:59,040
this makes any sense because you have
2439
01:46:59,040 --> 01:47:02,540
incremented the value here and uh that's
2440
01:47:02,540 --> 01:47:06,080
that does this happens because
2441
01:47:06,080 --> 01:47:10,080
this first Returns the value of x so it
2442
01:47:10,080 --> 01:47:11,940
returns varices and then it prints the
2443
01:47:11,940 --> 01:47:14,940
value of 6 which is zero and then it
2444
01:47:14,940 --> 01:47:17,880
increments so now the X has the value of
2445
01:47:17,880 --> 01:47:20,940
1. when the next printerland statement
2446
01:47:20,940 --> 01:47:24,060
is executed now it with the variable x
2447
01:47:24,060 --> 01:47:26,760
is incremented by one so it has a value
2448
01:47:26,760 --> 01:47:29,639
of one and now it will be incremented
2449
01:47:29,639 --> 01:47:33,119
again but it will also be returned not
2450
01:47:33,119 --> 01:47:35,940
like in the previous uh increment
2451
01:47:35,940 --> 01:47:37,920
operator which is the other end so now
2452
01:47:37,920 --> 01:47:39,960
it will have the value of 2.
2453
01:47:39,960 --> 01:47:41,940
okay
2454
01:47:41,940 --> 01:47:45,000
and there is uh the same operator is
2455
01:47:45,000 --> 01:47:48,360
available available for uh decrementing
2456
01:47:48,360 --> 01:47:52,020
so if uh if you want to
2457
01:47:52,020 --> 01:47:53,520
decrement
2458
01:47:53,520 --> 01:47:55,980
a variable exactly by one I can put here
2459
01:47:55,980 --> 01:48:00,119
x let's put equals let's put the curly
2460
01:48:00,119 --> 01:48:03,719
braces actually the dollar sign
2461
01:48:03,719 --> 01:48:09,199
curly braces and can put x minus minus
2462
01:48:11,040 --> 01:48:14,100
and let's print
2463
01:48:14,100 --> 01:48:17,040
just call the printerland function again
2464
01:48:17,040 --> 01:48:20,400
I'm going to put x equals
2465
01:48:20,400 --> 01:48:23,100
dollar sign again
2466
01:48:23,100 --> 01:48:26,100
curly braces and let's put now
2467
01:48:26,100 --> 01:48:29,340
the decrement operator which is the
2468
01:48:29,340 --> 01:48:32,100
minus minus in front of our variable
2469
01:48:32,100 --> 01:48:33,179
now you're going to see something
2470
01:48:33,179 --> 01:48:37,280
similar but in the inverse way
2471
01:48:40,139 --> 01:48:42,900
so now we have now we have two and zero
2472
01:48:42,900 --> 01:48:48,000
and let's explain uh this also so again
2473
01:48:48,000 --> 01:48:50,639
this printerland statements executed now
2474
01:48:50,639 --> 01:48:54,960
the X has the value 2 and it returns to
2475
01:48:54,960 --> 01:48:57,300
and then it decrements so it prints the
2476
01:48:57,300 --> 01:48:59,520
value of 2 and then it decrements the
2477
01:48:59,520 --> 01:49:02,760
value of our variable X by one so now we
2478
01:49:02,760 --> 01:49:04,020
have one
2479
01:49:04,020 --> 01:49:06,719
when the next printerland statement is
2480
01:49:06,719 --> 01:49:09,900
executive or function is called
2481
01:49:09,900 --> 01:49:12,780
now the value is 1 so and it first
2482
01:49:12,780 --> 01:49:15,000
decrements the value and then it Returns
2483
01:49:15,000 --> 01:49:18,060
the the the the value of the variable so
2484
01:49:18,060 --> 01:49:19,560
now we have zero because it was
2485
01:49:19,560 --> 01:49:22,500
decremented this is why you have uh 0
2486
01:49:22,500 --> 01:49:25,739
and 2 and 2 and 0 here all right
2487
01:49:25,739 --> 01:49:29,760
so you see above here that I declare the
2488
01:49:29,760 --> 01:49:32,820
variable as a VAR so I can reassign this
2489
01:49:32,820 --> 01:49:35,219
value down here to zero
2490
01:49:35,219 --> 01:49:37,020
right this is our discussion about
2491
01:49:37,020 --> 01:49:39,119
operators there are more operators but
2492
01:49:39,119 --> 01:49:41,460
in order to talk about the next
2493
01:49:41,460 --> 01:49:44,760
operators we need to first talk about an
2494
01:49:44,760 --> 01:49:46,920
if statement
2495
01:49:46,920 --> 01:49:48,600
so now it's time to look at comments and
2496
01:49:48,600 --> 01:49:50,400
comments are nodes that you can add your
2497
01:49:50,400 --> 01:49:52,500
code and they are ignored by the
2498
01:49:52,500 --> 01:49:54,780
compiler and usually they are they are
2499
01:49:54,780 --> 01:49:55,920
used when you want to add some
2500
01:49:55,920 --> 01:49:58,739
description about the code so what you
2501
01:49:58,739 --> 01:50:00,360
want to indicate what the code is doing
2502
01:50:00,360 --> 01:50:03,659
because you may roll some code and you
2503
01:50:03,659 --> 01:50:06,060
come to that code a few weeks later and
2504
01:50:06,060 --> 01:50:07,800
forgot what it's doing and by putting
2505
01:50:07,800 --> 01:50:10,020
comments you can you can see
2506
01:50:10,020 --> 01:50:12,179
specifically what that code is doing and
2507
01:50:12,179 --> 01:50:13,920
when in kotlin we have two types of
2508
01:50:13,920 --> 01:50:15,900
comments the first is so-called
2509
01:50:15,900 --> 01:50:19,739
end-of-line comment and to add an end
2510
01:50:19,739 --> 01:50:22,260
offline comment we put two slices and
2511
01:50:22,260 --> 01:50:24,659
here we put our comma let's say
2512
01:50:24,659 --> 01:50:28,400
here we are
2513
01:50:29,040 --> 01:50:30,719
doing
2514
01:50:30,719 --> 01:50:33,060
ope
2515
01:50:33,060 --> 01:50:34,679
operations
2516
01:50:34,679 --> 01:50:36,780
so this is another End of Line comment
2517
01:50:36,780 --> 01:50:39,840
and this is a text which you simply says
2518
01:50:39,840 --> 01:50:42,780
here we are doing operations and this is
2519
01:50:42,780 --> 01:50:44,760
uh as I said an end of flying comment
2520
01:50:44,760 --> 01:50:47,100
the next comment that you can use is so
2521
01:50:47,100 --> 01:50:50,219
called The Block comment and to use a
2522
01:50:50,219 --> 01:50:53,400
block comment we put slash
2523
01:50:53,400 --> 01:50:57,420
Aesthetics and now you can press enter
2524
01:50:57,420 --> 01:51:01,739
or you can put Asterix and slash to add
2525
01:51:01,739 --> 01:51:05,460
to to add the the next uh to close this
2526
01:51:05,460 --> 01:51:07,860
comment and you press enter and as you
2527
01:51:07,860 --> 01:51:09,540
can see it added automatically this
2528
01:51:09,540 --> 01:51:12,780
Aesthetics and slash or you can type
2529
01:51:12,780 --> 01:51:14,880
this yourself and here you can put some
2530
01:51:14,880 --> 01:51:19,020
text which can be some it can be
2531
01:51:19,020 --> 01:51:20,880
something which is going to say what the
2532
01:51:20,880 --> 01:51:23,699
code is doing so so those are the two
2533
01:51:23,699 --> 01:51:25,199
types of comments that you can use in
2534
01:51:25,199 --> 01:51:26,100
kotlin
2535
01:51:26,100 --> 01:51:28,920
The End of Line comment and the block
2536
01:51:28,920 --> 01:51:31,800
the block comment and you can also use
2537
01:51:31,800 --> 01:51:33,719
comments to disable some code so if I
2538
01:51:33,719 --> 01:51:36,119
put two slices here
2539
01:51:36,119 --> 01:51:37,800
now that line of code is going to be
2540
01:51:37,800 --> 01:51:40,679
ignored by the compiler so this is our
2541
01:51:40,679 --> 01:51:42,360
discussion about comments see you in the
2542
01:51:42,360 --> 01:51:43,679
next video
2543
01:51:43,679 --> 01:51:45,420
alright before we continue discussion
2544
01:51:45,420 --> 01:51:47,699
about operators let's first introduce
2545
01:51:47,699 --> 01:51:51,480
the if then else statement and uh
2546
01:51:51,480 --> 01:51:53,760
let's first start talking about how
2547
01:51:53,760 --> 01:51:55,920
usually our code is executed and usually
2548
01:51:55,920 --> 01:51:57,960
our code is executed from top to bottom
2549
01:51:57,960 --> 01:52:01,139
and that means that our code is executed
2550
01:52:01,139 --> 01:52:05,040
line by line and that is the flow of the
2551
01:52:05,040 --> 01:52:06,900
program but with an if then else
2552
01:52:06,900 --> 01:52:09,480
statement you can break the flow of the
2553
01:52:09,480 --> 01:52:13,199
program by imposing a condition and you
2554
01:52:13,199 --> 01:52:16,100
can say to the computer to execute a
2555
01:52:16,100 --> 01:52:19,139
particular section of quad only if a
2556
01:52:19,139 --> 01:52:20,820
condition is true
2557
01:52:20,820 --> 01:52:23,280
so let's see how we can do that so let's
2558
01:52:23,280 --> 01:52:24,719
first create a variable I'm going to
2559
01:52:24,719 --> 01:52:28,739
type Val this is going to be multiple
2560
01:52:28,739 --> 01:52:30,840
I'm not going to reassign this value I'm
2561
01:52:30,840 --> 01:52:32,520
going to call it it's
2562
01:52:32,520 --> 01:52:35,699
active and I'm gonna put equals and I'm
2563
01:52:35,699 --> 01:52:37,199
going to sign a value of Truth way so
2564
01:52:37,199 --> 01:52:39,540
it's going to be of type Boolean on the
2565
01:52:39,540 --> 01:52:40,980
next line I'm going to type the if
2566
01:52:40,980 --> 01:52:43,139
keyword and now IntelliJ is giving us
2567
01:52:43,139 --> 01:52:44,760
some hints here
2568
01:52:44,760 --> 01:52:47,460
I'm going to press enter and inside the
2569
01:52:47,460 --> 01:52:50,699
parenthesis we put our condition so here
2570
01:52:50,699 --> 01:52:54,060
I'm going to type is active the variable
2571
01:52:54,060 --> 01:52:56,400
and now I'm going to type equals equals
2572
01:52:56,400 --> 01:52:59,280
notice that here now we have two equals
2573
01:52:59,280 --> 01:53:01,860
and here we type true
2574
01:53:01,860 --> 01:53:03,540
next
2575
01:53:03,540 --> 01:53:06,840
we put a left curly brace and now we can
2576
01:53:06,840 --> 01:53:09,840
add the right curly brace or you can
2577
01:53:09,840 --> 01:53:11,280
press
2578
01:53:11,280 --> 01:53:13,619
you can press enter and the right curly
2579
01:53:13,619 --> 01:53:15,600
brace is going to be added automatically
2580
01:53:15,600 --> 01:53:18,060
by IntelliJ for you so if I press enter
2581
01:53:18,060 --> 01:53:19,500
now
2582
01:53:19,500 --> 01:53:23,760
you can see that on the line 40 we the
2583
01:53:23,760 --> 01:53:25,860
right calibrase was added automatically
2584
01:53:25,860 --> 01:53:27,540
now
2585
01:53:27,540 --> 01:53:30,600
let's type here a println statement so
2586
01:53:30,600 --> 01:53:33,719
I'm going to type println
2587
01:53:33,719 --> 01:53:35,340
press enter
2588
01:53:35,340 --> 01:53:37,619
and quotation marks and here we're going
2589
01:53:37,619 --> 01:53:39,239
to type the text the
2590
01:53:39,239 --> 01:53:43,560
user is active right
2591
01:53:43,560 --> 01:53:45,060
and uh
2592
01:53:45,060 --> 01:53:46,800
if you run this code
2593
01:53:46,800 --> 01:53:50,360
let's see what happens
2594
01:53:52,500 --> 01:53:54,960
and we get the output the user is active
2595
01:53:54,960 --> 01:53:57,239
which is the text that we typed here and
2596
01:53:57,239 --> 01:54:00,420
let's first talk let's first talk about
2597
01:54:00,420 --> 01:54:01,739
what we have here
2598
01:54:01,739 --> 01:54:04,500
and uh
2599
01:54:04,500 --> 01:54:06,600
the if statement
2600
01:54:06,600 --> 01:54:09,659
checks to see as I said to see if this
2601
01:54:09,659 --> 01:54:12,060
condition inside the parenthesis is true
2602
01:54:12,060 --> 01:54:15,500
this if this expression returns true and
2603
01:54:15,500 --> 01:54:18,840
it does that by checking to see if the
2604
01:54:18,840 --> 01:54:21,900
operands are identical or if they are
2605
01:54:21,900 --> 01:54:24,480
the same so it checks to see if the
2606
01:54:24,480 --> 01:54:26,520
content of the variable it's acted which
2607
01:54:26,520 --> 01:54:30,719
is uh true it's equal to true and that
2608
01:54:30,719 --> 01:54:32,820
is that sounds convenient but it's true
2609
01:54:32,820 --> 01:54:33,719
because
2610
01:54:33,719 --> 01:54:36,600
uh our variable it's active has a value
2611
01:54:36,600 --> 01:54:38,760
of true and here we're checking to see
2612
01:54:38,760 --> 01:54:42,119
if our uh if those two are equal or if
2613
01:54:42,119 --> 01:54:43,860
they are the same and that is true and
2614
01:54:43,860 --> 01:54:47,820
this expression returns true and in
2615
01:54:47,820 --> 01:54:48,900
effect
2616
01:54:48,900 --> 01:54:51,360
the code inside the curly braces is
2617
01:54:51,360 --> 01:54:56,299
executed but if I change this to false
2618
01:54:56,880 --> 01:54:59,159
so if I change this value and if I run
2619
01:54:59,159 --> 01:55:00,659
this code now
2620
01:55:00,659 --> 01:55:03,600
this condition inside the parenthesis is
2621
01:55:03,600 --> 01:55:05,820
going to be false and the code is not
2622
01:55:05,820 --> 01:55:08,159
going to be executed so you can see now
2623
01:55:08,159 --> 01:55:10,619
that you don't see any output because
2624
01:55:10,619 --> 01:55:14,040
this condition is false and because the
2625
01:55:14,040 --> 01:55:15,600
condition is false the code inside the
2626
01:55:15,600 --> 01:55:18,600
curly braces is not going to be executed
2627
01:55:18,600 --> 01:55:22,500
but uh if you want to cover the
2628
01:55:22,500 --> 01:55:25,860
part where the condition is false so if
2629
01:55:25,860 --> 01:55:27,420
you want to execute some code even
2630
01:55:27,420 --> 01:55:29,400
though the condition is false you can
2631
01:55:29,400 --> 01:55:31,139
type here else
2632
01:55:31,139 --> 01:55:33,980
and here you also need to put the
2633
01:55:33,980 --> 01:55:36,060
calibrases so I'm going to put the left
2634
01:55:36,060 --> 01:55:38,159
curly brace I'm going to press enter and
2635
01:55:38,159 --> 01:55:39,900
the right calibrate so that added
2636
01:55:39,900 --> 01:55:41,280
automatically
2637
01:55:41,280 --> 01:55:44,760
for me now I'm gonna type println
2638
01:55:44,760 --> 01:55:46,500
and inside the parenthesis I'm going to
2639
01:55:46,500 --> 01:55:48,119
put
2640
01:55:48,119 --> 01:55:52,739
the user is not active so now what is
2641
01:55:52,739 --> 01:55:53,940
going to happen is that this condition
2642
01:55:53,940 --> 01:55:55,860
is going to fail it's gonna be false
2643
01:55:55,860 --> 01:55:58,460
because the two operands are not equal
2644
01:55:58,460 --> 01:56:01,080
respectively is active and true
2645
01:56:01,080 --> 01:56:03,480
and is going to be executed the else
2646
01:56:03,480 --> 01:56:05,639
part in this case it's going to be
2647
01:56:05,639 --> 01:56:07,619
executed the code which is inside the
2648
01:56:07,619 --> 01:56:09,420
curly braces of the else so if you run
2649
01:56:09,420 --> 01:56:10,860
this code you're going to see the output
2650
01:56:10,860 --> 01:56:14,420
the user is not active
2651
01:56:16,679 --> 01:56:19,080
so as you can see now it says the user
2652
01:56:19,080 --> 01:56:21,860
is not active because this condition is
2653
01:56:21,860 --> 01:56:24,239
false this returns this Boolean
2654
01:56:24,239 --> 01:56:27,300
expression return false and it jumps to
2655
01:56:27,300 --> 01:56:29,580
the else part so this is how you can use
2656
01:56:29,580 --> 01:56:31,800
the else part
2657
01:56:31,800 --> 01:56:34,619
now what we can
2658
01:56:34,619 --> 01:56:38,100
also do is to add another if statement
2659
01:56:38,100 --> 01:56:40,980
below of our if statement to chain more
2660
01:56:40,980 --> 01:56:42,719
if statements to check for more
2661
01:56:42,719 --> 01:56:45,500
conditions but to do that we need to use
2662
01:56:45,500 --> 01:56:48,840
another operator which is the called the
2663
01:56:48,840 --> 01:56:50,219
greater than operator which is an
2664
01:56:50,219 --> 01:56:51,900
operator which you may know from your
2665
01:56:51,900 --> 01:56:53,699
math class
2666
01:56:53,699 --> 01:56:55,020
and
2667
01:56:55,020 --> 01:56:56,699
through that let's change for the
2668
01:56:56,699 --> 01:56:58,860
variable so I'm going to type here a
2669
01:56:58,860 --> 01:56:59,880
value of
2670
01:56:59,880 --> 01:57:03,360
100 to make it an integer and also I'm
2671
01:57:03,360 --> 01:57:05,100
gonna change its name and to change the
2672
01:57:05,100 --> 01:57:06,900
name of the variable go right click on
2673
01:57:06,900 --> 01:57:10,159
the variable go to rename
2674
01:57:11,280 --> 01:57:14,580
to refactor and go to rename
2675
01:57:14,580 --> 01:57:16,739
and now if you change the name of the
2676
01:57:16,739 --> 01:57:18,960
variable here is going to be change
2677
01:57:18,960 --> 01:57:21,719
every where where the variable is called
2678
01:57:21,719 --> 01:57:24,659
so if I look what happens if I delete
2679
01:57:24,659 --> 01:57:26,040
the name here
2680
01:57:26,040 --> 01:57:28,679
it's changed also on the line 38 we're
2681
01:57:28,679 --> 01:57:30,239
referring the variable
2682
01:57:30,239 --> 01:57:31,860
and now I'm going to call the variable
2683
01:57:31,860 --> 01:57:35,280
my number and I'm going to press enter
2684
01:57:35,280 --> 01:57:36,780
now the name of the variable it's
2685
01:57:36,780 --> 01:57:38,940
updated
2686
01:57:38,940 --> 01:57:40,800
and now this condition is not correct
2687
01:57:40,800 --> 01:57:43,920
you don't need to put a greater than
2688
01:57:43,920 --> 01:57:45,780
sign so I'm going to put the greater
2689
01:57:45,780 --> 01:57:48,719
than sign and let's check for uh let's
2690
01:57:48,719 --> 01:57:51,440
say if my number is greater than
2691
01:57:51,440 --> 01:57:55,080
150 and this is false now because my
2692
01:57:55,080 --> 01:57:58,139
number has a value of 100 so this is
2693
01:57:58,139 --> 01:57:59,699
going to evaluate to false this
2694
01:57:59,699 --> 01:58:01,980
condition this expression
2695
01:58:01,980 --> 01:58:04,920
now let's add here
2696
01:58:04,920 --> 01:58:06,900
another if state if statement so we're
2697
01:58:06,900 --> 01:58:08,580
going to put here else
2698
01:58:08,580 --> 01:58:10,260
if
2699
01:58:10,260 --> 01:58:11,580
and
2700
01:58:11,580 --> 01:58:13,139
inside the bottom test you're going to
2701
01:58:13,139 --> 01:58:15,420
put the condition and now I'm going to
2702
01:58:15,420 --> 01:58:16,619
type
2703
01:58:16,619 --> 01:58:19,800
again my number my variable
2704
01:58:19,800 --> 01:58:21,360
or variable
2705
01:58:21,360 --> 01:58:26,000
now you're going to put let's say
2706
01:58:27,840 --> 01:58:31,080
or greater than 90.
2707
01:58:31,080 --> 01:58:33,599
now this condition is true so
2708
01:58:33,599 --> 01:58:35,639
let's add Also the curly braces because
2709
01:58:35,639 --> 01:58:37,920
we need the
2710
01:58:37,920 --> 01:58:39,420
we need the
2711
01:58:39,420 --> 01:58:42,540
to put our code inside there
2712
01:58:42,540 --> 01:58:44,400
let's put here
2713
01:58:44,400 --> 01:58:47,659
open until then also
2714
01:58:47,699 --> 01:58:49,860
let's type here
2715
01:58:49,860 --> 01:58:52,860
greater
2716
01:58:55,320 --> 01:58:57,300
than
2717
01:58:57,300 --> 01:58:59,159
90.
2718
01:58:59,159 --> 01:59:03,259
and here let's also change the test to
2719
01:59:05,280 --> 01:59:09,960
greater than 150
2720
01:59:09,960 --> 01:59:13,040
and here you can put
2721
01:59:14,040 --> 01:59:17,820
all the conditions
2722
01:59:26,159 --> 01:59:27,360
here you can put all the condition
2723
01:59:27,360 --> 01:59:29,340
failed because the else part is going to
2724
01:59:29,340 --> 01:59:31,679
be executed only if all the condition
2725
01:59:31,679 --> 01:59:35,280
that we have here which is uh for that
2726
01:59:35,280 --> 01:59:38,520
my number is greater than 150 and this
2727
01:59:38,520 --> 01:59:42,659
is false my number is has a value of 100
2728
01:59:42,659 --> 01:59:46,860
and if our lcf condition also evaluates
2729
01:59:46,860 --> 01:59:49,440
to False only then the else part is
2730
01:59:49,440 --> 01:59:51,659
going to be executed so let's run our
2731
01:59:51,659 --> 01:59:53,820
code to see what
2732
01:59:53,820 --> 01:59:56,179
what if
2733
01:59:56,179 --> 01:59:58,679
statement is going to be executed what
2734
01:59:58,679 --> 01:59:59,699
part
2735
01:59:59,699 --> 02:00:01,920
so you can see that we now get we get
2736
02:00:01,920 --> 02:00:04,139
greater than 90 and that means that this
2737
02:00:04,139 --> 02:00:06,840
condition is true because it's true that
2738
02:00:06,840 --> 02:00:10,199
my number has a value of 100 and it
2739
02:00:10,199 --> 02:00:11,880
checks to see if my number is greater
2740
02:00:11,880 --> 02:00:15,599
than 90 which is true so it executes the
2741
02:00:15,599 --> 02:00:18,659
code which is inside the else if part
2742
02:00:18,659 --> 02:00:21,179
right
2743
02:00:21,179 --> 02:00:25,080
now the next operator that you can use
2744
02:00:25,080 --> 02:00:27,000
is greater
2745
02:00:27,000 --> 02:00:30,300
than or equal to so you can put here
2746
02:00:30,300 --> 02:00:32,760
greater than or equal
2747
02:00:32,760 --> 02:00:33,960
now
2748
02:00:33,960 --> 02:00:36,480
what is gonna check
2749
02:00:36,480 --> 02:00:39,659
to see is to
2750
02:00:39,659 --> 02:00:42,599
is that what is going to check is to see
2751
02:00:42,599 --> 02:00:45,500
if the number that we're comparing to
2752
02:00:45,500 --> 02:00:47,820
respectively my number
2753
02:00:47,820 --> 02:00:53,639
is greater than 100 or equal to greater
2754
02:00:53,639 --> 02:00:57,239
than 150 or equal to 150 so let's change
2755
02:00:57,239 --> 02:01:00,960
this to let's say um
2756
02:01:00,960 --> 02:01:02,960
um
2757
02:01:03,480 --> 02:01:07,020
100 and uh
2758
02:01:07,020 --> 02:01:11,719
49 so look what happens now
2759
02:01:18,540 --> 02:01:21,000
so now it's executed the this part the
2760
02:01:21,000 --> 02:01:23,040
lc4 because this is greater the number
2761
02:01:23,040 --> 02:01:27,780
greater than 90. but if I put here
2762
02:01:27,780 --> 02:01:29,340
greater
2763
02:01:29,340 --> 02:01:33,380
let's change the value of my number to
2764
02:01:33,480 --> 02:01:37,580
um 150 to see what happens
2765
02:01:43,920 --> 02:01:45,900
and now we get the output greater than
2766
02:01:45,900 --> 02:01:49,020
150 and this is correct because now this
2767
02:01:49,020 --> 02:01:50,699
condition is true my number is greater
2768
02:01:50,699 --> 02:01:55,500
than equals to 150 and uh
2769
02:01:55,500 --> 02:01:59,099
my number has the value of 150 it's not
2770
02:01:59,099 --> 02:02:02,699
greater than 150 but it's greater than
2771
02:02:02,699 --> 02:02:07,139
or equal to so it's equal to 100 150 so
2772
02:02:07,139 --> 02:02:10,500
now this condition is uh satisfied and
2773
02:02:10,500 --> 02:02:12,900
the code inside this curly braces is
2774
02:02:12,900 --> 02:02:15,960
executed which is greater than 150. all
2775
02:02:15,960 --> 02:02:18,420
right now I'm gonna end this video and
2776
02:02:18,420 --> 02:02:20,520
in the next video video we're going to
2777
02:02:20,520 --> 02:02:22,800
continue our discussion about
2778
02:02:22,800 --> 02:02:25,619
um those operators
2779
02:02:25,619 --> 02:02:27,119
right so now let's continue our
2780
02:02:27,119 --> 02:02:28,920
discussion about the next operators that
2781
02:02:28,920 --> 02:02:31,080
you can use in kotlin and let's start
2782
02:02:31,080 --> 02:02:34,020
with the less than operator and to do
2783
02:02:34,020 --> 02:02:36,060
that let's first change the value of the
2784
02:02:36,060 --> 02:02:38,340
variable my number to 100.
2785
02:02:38,340 --> 02:02:41,219
and to use the less than operator you we
2786
02:02:41,219 --> 02:02:43,440
put here a less than sign which you
2787
02:02:43,440 --> 02:02:45,599
probably know from your math class
2788
02:02:45,599 --> 02:02:49,159
now if you run this code
2789
02:02:54,840 --> 02:02:58,080
I should change here the text to make it
2790
02:02:58,080 --> 02:03:02,179
consistent let's type here
2791
02:03:07,080 --> 02:03:10,400
so now if you run this
2792
02:03:13,380 --> 02:03:16,320
now get the output less than 150 and
2793
02:03:16,320 --> 02:03:18,000
this is true because the value of the
2794
02:03:18,000 --> 02:03:20,760
variable my number is less than 150 so
2795
02:03:20,760 --> 02:03:22,980
this condition now evaluates to true
2796
02:03:22,980 --> 02:03:25,020
the next operator that you can use is
2797
02:03:25,020 --> 02:03:28,139
less than or equal to similar to greater
2798
02:03:28,139 --> 02:03:30,060
than or equal to operator and to use the
2799
02:03:30,060 --> 02:03:31,199
less than or
2800
02:03:31,199 --> 02:03:33,300
equal to operator we put here a less
2801
02:03:33,300 --> 02:03:35,460
sign and the equal sign
2802
02:03:35,460 --> 02:03:38,480
right and let's change the value here to
2803
02:03:38,480 --> 02:03:41,820
150 to make it clear
2804
02:03:41,820 --> 02:03:45,239
now let's change also the value of the
2805
02:03:45,239 --> 02:03:47,280
variable to 150
2806
02:03:47,280 --> 02:03:51,199
and let's also change the text here
2807
02:04:00,000 --> 02:04:02,099
now if you run this code you're going to
2808
02:04:02,099 --> 02:04:04,619
see the output less than or equal to 150
2809
02:04:04,619 --> 02:04:05,760
because
2810
02:04:05,760 --> 02:04:08,780
now the else if part
2811
02:04:08,780 --> 02:04:11,219
and the condition of the else if
2812
02:04:11,219 --> 02:04:13,139
statement evalues to true
2813
02:04:13,139 --> 02:04:14,400
because
2814
02:04:14,400 --> 02:04:17,880
now the the value of my number it's not
2815
02:04:17,880 --> 02:04:20,639
the it's not less than 150 because it's
2816
02:04:20,639 --> 02:04:23,940
150 but it's less than or equal to 150
2817
02:04:23,940 --> 02:04:26,280
so this condition evolves to true
2818
02:04:26,280 --> 02:04:28,139
right
2819
02:04:28,139 --> 02:04:31,199
the next operator that you can use is
2820
02:04:31,199 --> 02:04:34,380
called The Logical
2821
02:04:34,380 --> 02:04:37,860
not equal operator and that operator is
2822
02:04:37,860 --> 02:04:40,860
used to check if two operands
2823
02:04:40,860 --> 02:04:43,260
respectively to if my number let's say
2824
02:04:43,260 --> 02:04:47,219
or and 150 are not the same let's see
2825
02:04:47,219 --> 02:04:48,900
how we can do that and to do that to
2826
02:04:48,900 --> 02:04:50,280
type here
2827
02:04:50,280 --> 02:04:53,300
an exclamation mark and an equal sign
2828
02:04:53,300 --> 02:04:56,820
and this operator here which is going to
2829
02:04:56,820 --> 02:04:59,280
make a mark is gonna check to see if
2830
02:04:59,280 --> 02:05:02,280
those two operands respectively my
2831
02:05:02,280 --> 02:05:05,639
number and 150 are not the same
2832
02:05:05,639 --> 02:05:08,520
and only then the condition is going to
2833
02:05:08,520 --> 02:05:09,719
evaluate true
2834
02:05:09,719 --> 02:05:11,280
so let's run this code to see what
2835
02:05:11,280 --> 02:05:13,460
happens
2836
02:05:23,219 --> 02:05:25,139
so now we get the output less than or
2837
02:05:25,139 --> 02:05:27,420
equal to 150 because we still have the
2838
02:05:27,420 --> 02:05:30,300
volume 150 so it jumps to the lcf part
2839
02:05:30,300 --> 02:05:33,780
but if I change this to
2840
02:05:33,780 --> 02:05:35,639
let's say
2841
02:05:35,639 --> 02:05:40,219
100 now look what happens
2842
02:05:43,619 --> 02:05:46,560
now it execute the if
2843
02:05:46,560 --> 02:05:48,599
now this condition evolves to true
2844
02:05:48,599 --> 02:05:51,960
because now this operator is checking to
2845
02:05:51,960 --> 02:05:55,080
see if the two operands if the value of
2846
02:05:55,080 --> 02:05:57,420
my number the value of my number is not
2847
02:05:57,420 --> 02:05:58,940
equal to
2848
02:05:58,940 --> 02:06:01,199
150 and that is true they are not equal
2849
02:06:01,199 --> 02:06:03,179
and this is why you get here the output
2850
02:06:03,179 --> 02:06:05,159
but let's change the output to make it
2851
02:06:05,159 --> 02:06:08,540
clear let's type here
2852
02:06:08,880 --> 02:06:11,880
they are not
2853
02:06:11,880 --> 02:06:13,739
tickle
2854
02:06:13,739 --> 02:06:15,540
so if you run this code you're going to
2855
02:06:15,540 --> 02:06:17,099
see the output I are not equal because
2856
02:06:17,099 --> 02:06:19,739
that condition now Evolution so this is
2857
02:06:19,739 --> 02:06:21,659
how you can use the
2858
02:06:21,659 --> 02:06:24,900
logical not equal operator
2859
02:06:24,900 --> 02:06:27,480
also with the logical not operator we
2860
02:06:27,480 --> 02:06:29,460
can change the value for Boolean
2861
02:06:29,460 --> 02:06:32,340
expression from True to false and from a
2862
02:06:32,340 --> 02:06:34,980
false to true in other words you can
2863
02:06:34,980 --> 02:06:37,619
negate its value and to do that let's
2864
02:06:37,619 --> 02:06:39,179
first declare the code that we had
2865
02:06:39,179 --> 02:06:41,940
previously so I'm going to cut again the
2866
02:06:41,940 --> 02:06:44,580
Val variable
2867
02:06:44,580 --> 02:06:47,520
it's active
2868
02:06:47,520 --> 02:06:51,739
I'm going to assign a value of true
2869
02:06:53,159 --> 02:06:55,560
I'm going to type the if statement again
2870
02:06:55,560 --> 02:06:57,000
I'm going to put
2871
02:06:57,000 --> 02:06:58,980
the same condition
2872
02:06:58,980 --> 02:07:02,178
equals equals true
2873
02:07:05,400 --> 02:07:07,380
here you can put the left curly brace
2874
02:07:07,380 --> 02:07:09,840
and press enter we can add the right
2875
02:07:09,840 --> 02:07:11,159
color base if you want but it will be
2876
02:07:11,159 --> 02:07:12,840
added automatically for you
2877
02:07:12,840 --> 02:07:15,780
I'm going to type else also the left
2878
02:07:15,780 --> 02:07:18,179
curl base press enter I'm going to put
2879
02:07:18,179 --> 02:07:21,260
the text that you had previously
2880
02:07:23,159 --> 02:07:24,960
all right now if you run this code
2881
02:07:24,960 --> 02:07:26,639
you're going to see the output the user
2882
02:07:26,639 --> 02:07:27,900
is active because this is going to
2883
02:07:27,900 --> 02:07:30,360
evaluate to show their
2884
02:07:30,360 --> 02:07:33,420
are equal they're the same
2885
02:07:33,420 --> 02:07:36,440
let's expand this
2886
02:07:38,580 --> 02:07:40,679
so you see the output user is active
2887
02:07:40,679 --> 02:07:42,119
because this condition average to show
2888
02:07:42,119 --> 02:07:44,820
and before you look at The Logical not
2889
02:07:44,820 --> 02:07:46,920
operator how we can get the value let's
2890
02:07:46,920 --> 02:07:49,159
first see how we can write the same
2891
02:07:49,159 --> 02:07:51,780
expression that we have here
2892
02:07:51,780 --> 02:07:54,960
in a shorter way by abbreviating so
2893
02:07:54,960 --> 02:07:57,960
because you have noticed probably
2894
02:07:57,960 --> 02:07:59,940
that we have another underline here and
2895
02:07:59,940 --> 02:08:02,219
if you hover over here
2896
02:08:02,219 --> 02:08:03,960
it says that the Boolean expression can
2897
02:08:03,960 --> 02:08:06,000
be simplified so let's click here
2898
02:08:06,000 --> 02:08:07,739
simplify Boolean expression to see what
2899
02:08:07,739 --> 02:08:08,639
happens
2900
02:08:08,639 --> 02:08:12,960
so now we get if it's active and then
2901
02:08:12,960 --> 02:08:15,900
print then execute the code inside the
2902
02:08:15,900 --> 02:08:19,199
curly braces so this thing now does the
2903
02:08:19,199 --> 02:08:21,239
exact same thing that we did previously
2904
02:08:21,239 --> 02:08:24,320
it's so it checks to see if the value of
2905
02:08:24,320 --> 02:08:27,960
its active is equal to true so it's a
2906
02:08:27,960 --> 02:08:29,820
short way of writing the same thing
2907
02:08:29,820 --> 02:08:33,719
and uh now get the output the user
2908
02:08:33,719 --> 02:08:35,760
exactly but with the equal not operator
2909
02:08:35,760 --> 02:08:36,960
you can put
2910
02:08:36,960 --> 02:08:41,040
the exclamation mark
2911
02:08:41,639 --> 02:08:45,420
in front of our its active exp
2912
02:08:45,420 --> 02:08:48,360
expression and it's now now it's going
2913
02:08:48,360 --> 02:08:49,860
to negate this value now it's going to
2914
02:08:49,860 --> 02:08:52,679
change its value from True to false so
2915
02:08:52,679 --> 02:08:55,760
now if you run this code
2916
02:08:57,860 --> 02:09:00,420
not going to see the output the user is
2917
02:09:00,420 --> 02:09:03,480
not active because now this logical not
2918
02:09:03,480 --> 02:09:04,860
operator
2919
02:09:04,860 --> 02:09:07,739
switch is switching its value to false
2920
02:09:07,739 --> 02:09:09,420
so now this is going to be this will
2921
02:09:09,420 --> 02:09:11,280
evaluate to false it and it's going to
2922
02:09:11,280 --> 02:09:12,599
execute the else part that we have with
2923
02:09:12,599 --> 02:09:14,820
the user is not active and you can do
2924
02:09:14,820 --> 02:09:16,380
the same for a
2925
02:09:16,380 --> 02:09:20,300
false so if you put here false
2926
02:09:20,580 --> 02:09:23,119
now
2927
02:09:23,340 --> 02:09:24,840
let's actually
2928
02:09:24,840 --> 02:09:26,880
execute this without the logical not
2929
02:09:26,880 --> 02:09:28,560
operator to see what happens now this
2930
02:09:28,560 --> 02:09:30,480
will not execute so it will execute the
2931
02:09:30,480 --> 02:09:32,959
else part
2932
02:09:33,179 --> 02:09:35,159
so you see the user is not active but if
2933
02:09:35,159 --> 02:09:37,560
I put the logical not operator in front
2934
02:09:37,560 --> 02:09:39,659
of its active now it's going to change
2935
02:09:39,659 --> 02:09:42,000
its value from false to true and now
2936
02:09:42,000 --> 02:09:44,520
this will evaluate to true and it will
2937
02:09:44,520 --> 02:09:46,020
execute the
2938
02:09:46,020 --> 02:09:47,400
oh
2939
02:09:47,400 --> 02:09:49,679
code inside the calibration of the if so
2940
02:09:49,679 --> 02:09:51,420
now you see that it says the user is
2941
02:09:51,420 --> 02:09:52,440
active
2942
02:09:52,440 --> 02:09:54,119
so this is how you can use the logical
2943
02:09:54,119 --> 02:09:57,480
not operator to flip a value from false
2944
02:09:57,480 --> 02:09:59,340
to true or from True to false and you
2945
02:09:59,340 --> 02:10:01,560
can do this for for every Boolean
2946
02:10:01,560 --> 02:10:02,940
expression
2947
02:10:02,940 --> 02:10:05,400
that you use so you can put the logical
2948
02:10:05,400 --> 02:10:07,920
note operator in front of this
2949
02:10:07,920 --> 02:10:09,900
actually not in front of this
2950
02:10:09,900 --> 02:10:11,520
um you can put the logical not operator
2951
02:10:11,520 --> 02:10:13,920
generally speaking in front of any
2952
02:10:13,920 --> 02:10:16,739
Boolean expression right
2953
02:10:16,739 --> 02:10:20,360
so this is our discussion about
2954
02:10:21,179 --> 02:10:22,739
operators so in the next video we're
2955
02:10:22,739 --> 02:10:24,360
going to look how we can combine
2956
02:10:24,360 --> 02:10:26,460
multiple Expressions to create a bigger
2957
02:10:26,460 --> 02:10:28,500
expression which you sell for returns uh
2958
02:10:28,500 --> 02:10:30,480
true or false
2959
02:10:30,480 --> 02:10:31,500
value
2960
02:10:31,500 --> 02:10:33,420
see you in the next
2961
02:10:33,420 --> 02:10:35,400
alright so I deleted the code that we
2962
02:10:35,400 --> 02:10:37,500
had previously you can keep the code
2963
02:10:37,500 --> 02:10:39,119
that you have to use if you want but I
2964
02:10:39,119 --> 02:10:41,460
deleted to make things more clear
2965
02:10:41,460 --> 02:10:42,900
and in this video we're going to look at
2966
02:10:42,900 --> 02:10:45,960
how we can combine multiple Expressions
2967
02:10:45,960 --> 02:10:48,800
which in essence can return a Boolean
2968
02:10:48,800 --> 02:10:51,420
value which can be either true or false
2969
02:10:51,420 --> 02:10:54,179
and uh
2970
02:10:54,179 --> 02:10:56,639
let's start with the first
2971
02:10:56,639 --> 02:10:59,099
operator which is called The Logical end
2972
02:10:59,099 --> 02:11:01,320
operator but less vertical two variables
2973
02:11:01,320 --> 02:11:03,060
I'm going to declare a Val variable I'm
2974
02:11:03,060 --> 02:11:04,860
going to call it
2975
02:11:04,860 --> 02:11:07,699
it's playing
2976
02:11:07,980 --> 02:11:09,360
I'm going to sign a value of true sweet
2977
02:11:09,360 --> 02:11:12,119
so it's going to be of type Boolean
2978
02:11:12,119 --> 02:11:14,400
and on the next line I'm gonna declare
2979
02:11:14,400 --> 02:11:17,099
another variable also of all and it's
2980
02:11:17,099 --> 02:11:19,199
going to be called score
2981
02:11:19,199 --> 02:11:22,199
and it's going to have a value of 80.
2982
02:11:22,199 --> 02:11:25,860
now down here we'll type if
2983
02:11:25,860 --> 02:11:28,440
and inside the parenthesis to type is
2984
02:11:28,440 --> 02:11:30,300
playing
2985
02:11:30,300 --> 02:11:33,119
equals equals true so this is the equals
2986
02:11:33,119 --> 02:11:37,080
to operator I didn't then talk really a
2987
02:11:37,080 --> 02:11:39,060
lot about the equals operator but equals
2988
02:11:39,060 --> 02:11:40,920
to operator
2989
02:11:40,920 --> 02:11:43,020
checks to see if the two operands are
2990
02:11:43,020 --> 02:11:45,659
the same so it in contrast to the one
2991
02:11:45,659 --> 02:11:48,000
equal operator the one equal operator
2992
02:11:48,000 --> 02:11:50,400
assigns what's on the right to do our
2993
02:11:50,400 --> 02:11:53,219
variable which is on the left so in this
2994
02:11:53,219 --> 02:11:55,020
case assign truth or variable is playing
2995
02:11:55,020 --> 02:11:59,520
so or it's a sign 80 to a variable score
2996
02:11:59,520 --> 02:12:02,040
the equals the two equals or the request
2997
02:12:02,040 --> 02:12:03,719
operator is different because it checks
2998
02:12:03,719 --> 02:12:06,540
to see if uh what we have on the left or
2999
02:12:06,540 --> 02:12:09,179
what have on the left in and on the
3000
02:12:09,179 --> 02:12:12,480
right are the same or are identical next
3001
02:12:12,480 --> 02:12:14,460
and we know from the previous video that
3002
02:12:14,460 --> 02:12:16,139
you can abbreviate this you can write
3003
02:12:16,139 --> 02:12:18,960
this in a shorter way by putting just is
3004
02:12:18,960 --> 02:12:21,480
playing this will be this is going to be
3005
02:12:21,480 --> 02:12:23,580
equivalent to what you had producer
3006
02:12:23,580 --> 02:12:25,440
because as you can see it says that you
3007
02:12:25,440 --> 02:12:27,599
can simplify the Boon expression
3008
02:12:27,599 --> 02:12:29,159
and now
3009
02:12:29,159 --> 02:12:32,040
gonna you use the logical end operator
3010
02:12:32,040 --> 02:12:34,139
and use the logic logical end operator
3011
02:12:34,139 --> 02:12:37,380
with type 2 Ampersand science
3012
02:12:37,380 --> 02:12:39,900
and here we type score now I'm going to
3013
02:12:39,900 --> 02:12:42,719
use again the equals equals operator
3014
02:12:42,719 --> 02:12:44,460
this time we need to use it there is not
3015
02:12:44,460 --> 02:12:48,619
a shorter way to check for a
3016
02:12:48,659 --> 02:12:52,199
new type if score equals to 100.
3017
02:12:52,199 --> 02:12:53,520
all right
3018
02:12:53,520 --> 02:12:55,619
so this expression inside the pattern
3019
02:12:55,619 --> 02:12:57,719
this is going to evaluate to True only
3020
02:12:57,719 --> 02:12:59,840
if both
3021
02:12:59,840 --> 02:13:02,460
Expressions respective is playing and
3022
02:13:02,460 --> 02:13:05,940
score equals equals to 100 evaluates to
3023
02:13:05,940 --> 02:13:09,420
true so only if those two conditions
3024
02:13:09,420 --> 02:13:12,360
are true only then the code inside the
3025
02:13:12,360 --> 02:13:14,099
parenthesis inside the curly braces
3026
02:13:14,099 --> 02:13:16,380
actually is going to be executed so
3027
02:13:16,380 --> 02:13:18,780
let's say that in a real app here you
3028
02:13:18,780 --> 02:13:20,940
can add the logic to open the next level
3029
02:13:20,940 --> 02:13:23,400
but here we go just gonna output some
3030
02:13:23,400 --> 02:13:26,540
text so I'm going to type here
3031
02:13:26,880 --> 02:13:29,040
next level
3032
02:13:29,040 --> 02:13:30,719
opened right
3033
02:13:30,719 --> 02:13:32,880
and also I'm gonna add the else part
3034
02:13:32,880 --> 02:13:35,179
here
3035
02:13:36,540 --> 02:13:39,300
and we're gonna put
3036
02:13:39,300 --> 02:13:42,300
also printerland statement
3037
02:13:42,300 --> 02:13:43,739
and inside the part of this is going to
3038
02:13:43,739 --> 02:13:45,780
type
3039
02:13:45,780 --> 02:13:47,340
steel art
3040
02:13:47,340 --> 02:13:48,780
the same
3041
02:13:48,780 --> 02:13:52,380
level now if you run this code
3042
02:13:52,380 --> 02:13:54,480
you're going to see the output still at
3043
02:13:54,480 --> 02:13:56,940
the same level because
3044
02:13:56,940 --> 02:14:00,119
not those conditions are not both true
3045
02:14:00,119 --> 02:14:04,260
is playing is true but it's playing is
3046
02:14:04,260 --> 02:14:07,079
equal to true but our score is not equal
3047
02:14:07,079 --> 02:14:09,000
to 100 so this is going to evaluate to
3048
02:14:09,000 --> 02:14:11,219
false and the entire expression inside
3049
02:14:11,219 --> 02:14:12,780
the pattern is going to turn false so
3050
02:14:12,780 --> 02:14:15,179
it's going to execute Tails part
3051
02:14:15,179 --> 02:14:19,260
now if we change the order the value of
3052
02:14:19,260 --> 02:14:22,579
our variable score to 100 now both
3053
02:14:22,579 --> 02:14:25,199
expressions are true is playing equal to
3054
02:14:25,199 --> 02:14:27,060
true and score
3055
02:14:27,060 --> 02:14:30,840
and the score is equal to 100.
3056
02:14:30,840 --> 02:14:32,880
so now both of our expression are true
3057
02:14:32,880 --> 02:14:38,360
and gonna see the text Next Level opened
3058
02:14:38,579 --> 02:14:40,440
and I will see that it output the next
3059
02:14:40,440 --> 02:14:42,300
level opened
3060
02:14:42,300 --> 02:14:45,780
so this is The Logical end operator and
3061
02:14:45,780 --> 02:14:48,480
you're going to be using uh The Logical
3062
02:14:48,480 --> 02:14:50,820
and operator a lot in your
3063
02:14:50,820 --> 02:14:52,739
code
3064
02:14:52,739 --> 02:14:54,239
all right so now it's time to continue
3065
02:14:54,239 --> 02:14:57,119
our discussion about operators and going
3066
02:14:57,119 --> 02:14:59,760
to look at the next operator which can
3067
02:14:59,760 --> 02:15:02,579
be used to combine multiple expression
3068
02:15:02,579 --> 02:15:05,340
and that is the logical or operator and
3069
02:15:05,340 --> 02:15:08,099
the logological or operator evaluates an
3070
02:15:08,099 --> 02:15:10,500
expression or multiple expression in
3071
02:15:10,500 --> 02:15:13,320
this case to true if one of the
3072
02:15:13,320 --> 02:15:15,480
condition is true or if both conditions
3073
02:15:15,480 --> 02:15:17,520
are true so let's see an example of this
3074
02:15:17,520 --> 02:15:19,440
but first let's comment the code that we
3075
02:15:19,440 --> 02:15:22,020
have here because to make things
3076
02:15:22,020 --> 02:15:23,400
confusing
3077
02:15:23,400 --> 02:15:26,400
so go to code and select comment.light
3078
02:15:26,400 --> 02:15:29,460
command with line comment or you can
3079
02:15:29,460 --> 02:15:34,079
press the shortcut Ctrl slash
3080
02:15:34,079 --> 02:15:37,079
now let's equal to variables down here
3081
02:15:37,079 --> 02:15:39,719
the first the first is going to be a Val
3082
02:15:39,719 --> 02:15:42,719
I'm going to call it num1
3083
02:15:42,719 --> 02:15:46,260
I'm going to sign a value of 5 to it
3084
02:15:46,260 --> 02:15:48,119
second is going to be also Val it's
3085
02:15:48,119 --> 02:15:49,800
going to be called num2 and I'm going to
3086
02:15:49,800 --> 02:15:53,940
send a value of 3 to it or four
3087
02:15:53,940 --> 02:15:55,679
now I'm going to type the if Dental
3088
02:15:55,679 --> 02:15:57,540
statement I'm going to type the if
3089
02:15:57,540 --> 02:15:59,159
keyword inside the parenthesis I'm going
3090
02:15:59,159 --> 02:16:02,159
to put the condition first is if num1 is
3091
02:16:02,159 --> 02:16:04,679
greater than zero and now to use the
3092
02:16:04,679 --> 02:16:07,320
logical or operator we type two vertical
3093
02:16:07,320 --> 02:16:09,060
bars here
3094
02:16:09,060 --> 02:16:11,520
now we put our next condition or our
3095
02:16:11,520 --> 02:16:15,000
next expression and that is num2
3096
02:16:15,000 --> 02:16:17,639
is also greater than zero and we put a
3097
02:16:17,639 --> 02:16:20,639
curly brace press enter two
3098
02:16:20,639 --> 02:16:22,500
to add the right color base
3099
02:16:22,500 --> 02:16:26,000
and put also the else part
3100
02:16:26,280 --> 02:16:27,840
and inside the
3101
02:16:27,840 --> 02:16:30,300
calibrases of the if let's print some
3102
02:16:30,300 --> 02:16:32,540
text
3103
02:16:33,599 --> 02:16:35,939
let's say
3104
02:16:35,939 --> 02:16:39,080
the condition is true
3105
02:16:43,620 --> 02:16:47,179
all right now if you run this code
3106
02:16:54,179 --> 02:16:55,500
you're going to see the output the
3107
02:16:55,500 --> 02:16:57,660
condition is true because now both
3108
02:16:57,660 --> 02:16:59,820
conditions are true and The Logical
3109
02:16:59,820 --> 02:17:03,000
operator returns true if both conditions
3110
02:17:03,000 --> 02:17:05,700
are true or if one of the condition is
3111
02:17:05,700 --> 02:17:08,879
true so let's see if we change this to
3112
02:17:08,879 --> 02:17:10,558
-3
3113
02:17:10,558 --> 02:17:12,898
and now our second condition num2
3114
02:17:12,898 --> 02:17:15,840
greater than zero is false so now this
3115
02:17:15,840 --> 02:17:17,280
is also going to evaluate to true
3116
02:17:17,280 --> 02:17:18,780
because
3117
02:17:18,780 --> 02:17:21,840
The Logical operator returns through if
3118
02:17:21,840 --> 02:17:23,638
one actually with one of the
3119
02:17:23,638 --> 02:17:26,160
conditioners or if both Constructor so
3120
02:17:26,160 --> 02:17:28,558
now get conditional show but if you
3121
02:17:28,558 --> 02:17:29,939
change this to
3122
02:17:29,939 --> 02:17:33,299
also to -5 now our both both conditions
3123
02:17:33,299 --> 02:17:34,379
are false
3124
02:17:34,379 --> 02:17:36,240
now you're gonna see that it's gonna be
3125
02:17:36,240 --> 02:17:38,280
executed the daily spot because this
3126
02:17:38,280 --> 02:17:39,740
condition is going to be false because
3127
02:17:39,740 --> 02:17:43,260
both conditions both Expressions num one
3128
02:17:43,260 --> 02:17:44,820
greater than zero and num two greater
3129
02:17:44,820 --> 02:17:46,859
than zero are false all right so let's
3130
02:17:46,859 --> 02:17:50,218
change it back to five
3131
02:17:50,218 --> 02:17:51,660
now if you're on this code you're gonna
3132
02:17:51,660 --> 02:17:52,679
see
3133
02:17:52,679 --> 02:17:54,840
the condition is true because at least
3134
02:17:54,840 --> 02:17:56,939
one of the condition is true which is
3135
02:17:56,939 --> 02:17:58,859
this one normal and greater than zero
3136
02:17:58,859 --> 02:18:00,959
even though the number two condition is
3137
02:18:00,959 --> 02:18:01,799
false
3138
02:18:01,799 --> 02:18:04,558
all right and this is how you can use
3139
02:18:04,558 --> 02:18:06,718
the logical or operator
3140
02:18:06,718 --> 02:18:08,580
all right now it's time to see how we
3141
02:18:08,580 --> 02:18:10,740
can use the if Daniel statement as an
3142
02:18:10,740 --> 02:18:14,280
expression and that means that the if
3143
02:18:14,280 --> 02:18:16,200
Daniel statement can return a value
3144
02:18:16,200 --> 02:18:18,359
which can be stored in a variable so
3145
02:18:18,359 --> 02:18:20,398
let's see how you can do that
3146
02:18:20,398 --> 02:18:23,398
but first
3147
02:18:23,398 --> 02:18:25,379
actually skip this code that we have
3148
02:18:25,379 --> 02:18:26,218
here
3149
02:18:26,218 --> 02:18:27,660
and uh
3150
02:18:27,660 --> 02:18:29,580
to use the if Daniel statement as an
3151
02:18:29,580 --> 02:18:30,660
expression
3152
02:18:30,660 --> 02:18:32,939
we declare of
3153
02:18:32,939 --> 02:18:33,780
um
3154
02:18:33,780 --> 02:18:35,580
a variable first so I'm going to create
3155
02:18:35,580 --> 02:18:37,439
a Val
3156
02:18:37,439 --> 02:18:38,820
and let's give it a name I'm going to
3157
02:18:38,820 --> 02:18:41,340
call it let's say text
3158
02:18:41,340 --> 02:18:44,398
and to use the if then else statement as
3159
02:18:44,398 --> 02:18:47,099
an expression we put equals and after
3160
02:18:47,099 --> 02:18:50,160
the equals we put our if then else
3161
02:18:50,160 --> 02:18:51,299
statement
3162
02:18:51,299 --> 02:18:52,580
now
3163
02:18:52,580 --> 02:18:55,379
we need to assign a value to our
3164
02:18:55,379 --> 02:18:58,080
variable text to use it as an expression
3165
02:18:58,080 --> 02:19:00,679
and the value is going to be the last
3166
02:19:00,679 --> 02:19:05,459
expression in the in the curly braces so
3167
02:19:05,459 --> 02:19:09,299
here if I type the text let's say uh
3168
02:19:09,299 --> 02:19:11,459
this
3169
02:19:11,459 --> 02:19:13,620
is text
3170
02:19:13,620 --> 02:19:15,058
one
3171
02:19:15,058 --> 02:19:16,200
and
3172
02:19:16,200 --> 02:19:18,780
down here if I type
3173
02:19:18,780 --> 02:19:23,519
quotation marks this is text tool
3174
02:19:23,519 --> 02:19:28,760
now if I add a print Ln here at the end
3175
02:19:30,000 --> 02:19:31,679
and if I print the value of the variable
3176
02:19:31,679 --> 02:19:34,558
text that we declare above
3177
02:19:34,558 --> 02:19:36,840
let's put a dollar sign and if I type
3178
02:19:36,840 --> 02:19:38,218
text here
3179
02:19:38,218 --> 02:19:40,200
now you can see our variable it says
3180
02:19:40,200 --> 02:19:43,558
it's of type string
3181
02:19:43,558 --> 02:19:47,358
so if I run this code now
3182
02:19:47,459 --> 02:19:50,220
you're gonna see first this println
3183
02:19:50,220 --> 02:19:51,899
inside the curly braces which says the
3184
02:19:51,899 --> 02:19:53,100
condition is true
3185
02:19:53,100 --> 02:19:54,420
and
3186
02:19:54,420 --> 02:19:56,520
after that you're gonna see this text
3187
02:19:56,520 --> 02:19:59,100
being assigned to our variable text and
3188
02:19:59,100 --> 02:20:01,859
it will be output out output it down
3189
02:20:01,859 --> 02:20:02,760
here
3190
02:20:02,760 --> 02:20:04,439
by the printerline statement so you see
3191
02:20:04,439 --> 02:20:06,300
this is text
3192
02:20:06,300 --> 02:20:08,100
two actually I was wrong that condition
3193
02:20:08,100 --> 02:20:10,439
is false because it will have minus five
3194
02:20:10,439 --> 02:20:11,580
and minus three and this condition but
3195
02:20:11,580 --> 02:20:15,060
it's the false and the last the last
3196
02:20:15,060 --> 02:20:17,160
expression in the
3197
02:20:17,160 --> 02:20:20,760
in the curly braces is assigned as a
3198
02:20:20,760 --> 02:20:23,760
value to our variable now
3199
02:20:23,760 --> 02:20:25,140
uh
3200
02:20:25,140 --> 02:20:27,479
what you can also do is to remove the
3201
02:20:27,479 --> 02:20:30,060
curly braces but it's recommended to use
3202
02:20:30,060 --> 02:20:33,359
to not use the curly braces if you have
3203
02:20:33,359 --> 02:20:37,260
one only one statement uh
3204
02:20:37,260 --> 02:20:40,080
in your if Dental statement so let's
3205
02:20:40,080 --> 02:20:42,600
delete the printerland here let's delete
3206
02:20:42,600 --> 02:20:44,460
the curly brace here
3207
02:20:44,460 --> 02:20:46,439
let's also delete the calibrase here
3208
02:20:46,439 --> 02:20:48,359
let's delete the printerland statement
3209
02:20:48,359 --> 02:20:50,520
here and let's also delete the curly
3210
02:20:50,520 --> 02:20:51,540
berries here
3211
02:20:51,540 --> 02:20:55,680
so now this will work the same to work
3212
02:20:55,680 --> 02:20:59,120
like previously but uh
3213
02:20:59,160 --> 02:21:01,140
so now you'll see the same output
3214
02:21:01,140 --> 02:21:02,520
without the condition without this take
3215
02:21:02,520 --> 02:21:03,780
the control result because now we don't
3216
02:21:03,780 --> 02:21:06,060
have the printerline statement so now
3217
02:21:06,060 --> 02:21:07,800
get this text too because this condition
3218
02:21:07,800 --> 02:21:10,500
evaluates to false and it assigns this
3219
02:21:10,500 --> 02:21:12,540
value to our variable text then down
3220
02:21:12,540 --> 02:21:14,280
here it prints so
3221
02:21:14,280 --> 02:21:17,040
also you see that this uh they are not
3222
02:21:17,040 --> 02:21:19,859
aligned here this our variable and two
3223
02:21:19,859 --> 02:21:22,979
format your code you just go to code
3224
02:21:22,979 --> 02:21:25,859
and click reform reformat code or place
3225
02:21:25,859 --> 02:21:29,580
Ctrl alt L on Windows or you can um
3226
02:21:29,580 --> 02:21:31,680
use another shot you can use the
3227
02:21:31,680 --> 02:21:33,600
corresponding shortcut on Linux or Mac
3228
02:21:33,600 --> 02:21:36,500
so click on code
3229
02:21:36,780 --> 02:21:38,939
and now we're gonna see that our code is
3230
02:21:38,939 --> 02:21:42,140
formatted if you run this
3231
02:21:46,380 --> 02:21:48,540
now see this is text too but let's
3232
02:21:48,540 --> 02:21:51,899
change this to five
3233
02:21:51,899 --> 02:21:54,180
now this conditional overlay to true and
3234
02:21:54,180 --> 02:21:56,580
this expression is going to be is going
3235
02:21:56,580 --> 02:21:59,520
to be assigned or variable text so if we
3236
02:21:59,520 --> 02:22:00,960
run this
3237
02:22:00,960 --> 02:22:03,500
hmm
3238
02:22:05,700 --> 02:22:08,040
you're gonna see this now our variable
3239
02:22:08,040 --> 02:22:10,740
text is assigned the value of this is
3240
02:22:10,740 --> 02:22:12,240
text one because this condition is true
3241
02:22:12,240 --> 02:22:14,040
and inside this value to make things
3242
02:22:14,040 --> 02:22:16,260
more clear clear you can
3243
02:22:16,260 --> 02:22:17,819
use a
3244
02:22:17,819 --> 02:22:20,340
you can you can type your code something
3245
02:22:20,340 --> 02:22:22,819
like this
3246
02:22:23,100 --> 02:22:25,800
now this does there's everything is the
3247
02:22:25,800 --> 02:22:27,060
exact same thing as previously so if you
3248
02:22:27,060 --> 02:22:29,720
run this code now
3249
02:22:33,000 --> 02:22:34,439
you're gonna see that now this it says
3250
02:22:34,439 --> 02:22:37,080
the same thing this is text one
3251
02:22:37,080 --> 02:22:39,840
now you may be wondering what type this
3252
02:22:39,840 --> 02:22:42,479
variable is so if you hover over it this
3253
02:22:42,479 --> 02:22:46,500
is the it is of type string but look
3254
02:22:46,500 --> 02:22:49,319
what can I do I can put here a number
3255
02:22:49,319 --> 02:22:51,920
so if this condition evaluates to false
3256
02:22:51,920 --> 02:22:54,960
this is going to be assigned or variable
3257
02:22:54,960 --> 02:22:58,519
test so if I run this now
3258
02:23:02,819 --> 02:23:06,540
now get this text one and let's uh put
3259
02:23:06,540 --> 02:23:09,000
here -5 for the condition to be false
3260
02:23:09,000 --> 02:23:12,200
and if you run this code now
3261
02:23:15,120 --> 02:23:17,460
now I'm going to see five and you may be
3262
02:23:17,460 --> 02:23:20,220
wondering how this makes any sense
3263
02:23:20,220 --> 02:23:22,580
because
3264
02:23:22,920 --> 02:23:24,600
previously our variable was of type
3265
02:23:24,600 --> 02:23:27,120
string but if you have hover over here
3266
02:23:27,120 --> 02:23:29,399
now it says Vault text and the type is
3267
02:23:29,399 --> 02:23:33,240
this thing comparable anyway but what is
3268
02:23:33,240 --> 02:23:36,479
uh simply doing here is looking at the
3269
02:23:36,479 --> 02:23:38,520
condition it's looking first at the if
3270
02:23:38,520 --> 02:23:39,540
statement
3271
02:23:39,540 --> 02:23:42,840
and if this condition is true then it
3272
02:23:42,840 --> 02:23:45,840
assigns our text or variable and then it
3273
02:23:45,840 --> 02:23:49,140
infers the type to be a string right
3274
02:23:49,140 --> 02:23:52,140
if this condition is false then this
3275
02:23:52,140 --> 02:23:54,780
part is skipped and then it assigns this
3276
02:23:54,780 --> 02:23:57,359
value of 5 to our variable text and not
3277
02:23:57,359 --> 02:24:00,479
and then our variable is going to be of
3278
02:24:00,479 --> 02:24:03,300
type int so there is no case in which
3279
02:24:03,300 --> 02:24:05,840
both
3280
02:24:06,660 --> 02:24:08,220
value is going to be assigned to our
3281
02:24:08,220 --> 02:24:10,319
variable so this is why you can use here
3282
02:24:10,319 --> 02:24:12,000
text and here
3283
02:24:12,000 --> 02:24:14,760
an integer even though but if you
3284
02:24:14,760 --> 02:24:17,580
declare explicit here that this variable
3285
02:24:17,580 --> 02:24:19,500
is going to store only strings only
3286
02:24:19,500 --> 02:24:22,620
sequence of characters or text now he'll
3287
02:24:22,620 --> 02:24:24,720
get an earning because it says the
3288
02:24:24,720 --> 02:24:26,760
integral transforms the expected stream
3289
02:24:26,760 --> 02:24:29,700
so in other words we cannot put here
3290
02:24:29,700 --> 02:24:32,340
but a number because we said that
3291
02:24:32,340 --> 02:24:33,720
explains here that is going to store
3292
02:24:33,720 --> 02:24:36,720
only strings but if you omit the
3293
02:24:36,720 --> 02:24:38,100
type
3294
02:24:38,100 --> 02:24:39,660
that is gonna work
3295
02:24:39,660 --> 02:24:42,660
so you can decide not to
3296
02:24:42,660 --> 02:24:45,899
depending on the context what to choose
3297
02:24:45,899 --> 02:24:48,600
so this way of writing the internal
3298
02:24:48,600 --> 02:24:50,520
statement in one single line like we
3299
02:24:50,520 --> 02:24:53,700
have here it's okay as long as we use
3300
02:24:53,700 --> 02:24:55,140
the if the general statement as an
3301
02:24:55,140 --> 02:24:56,939
expression or in other words if you're
3302
02:24:56,939 --> 02:25:00,180
assigning a value to a variable like in
3303
02:25:00,180 --> 02:25:01,200
our case
3304
02:25:01,200 --> 02:25:04,319
but if you have multiple instruction you
3305
02:25:04,319 --> 02:25:05,819
know
3306
02:25:05,819 --> 02:25:07,859
also if you have a single single
3307
02:25:07,859 --> 02:25:10,560
instruction you can also omit the curly
3308
02:25:10,560 --> 02:25:13,680
braces but most of the time it's
3309
02:25:13,680 --> 02:25:15,479
recommended to use the curly braces
3310
02:25:15,479 --> 02:25:18,000
because that allows the those allows us
3311
02:25:18,000 --> 02:25:21,780
to execute more than on uh instruction
3312
02:25:21,780 --> 02:25:24,960
on the statement in your code and it
3313
02:25:24,960 --> 02:25:27,060
also makes the code more clear so let's
3314
02:25:27,060 --> 02:25:29,160
change this code
3315
02:25:29,160 --> 02:25:31,500
from an expression to just a new general
3316
02:25:31,500 --> 02:25:33,359
statement to
3317
02:25:33,359 --> 02:25:35,880
see why
3318
02:25:35,880 --> 02:25:38,520
so I'm going to change this
3319
02:25:38,520 --> 02:25:42,140
I'm going to add the curly braces
3320
02:25:45,660 --> 02:25:49,920
from other code press Ctrl
3321
02:25:49,920 --> 02:25:52,979
alt l
3322
02:25:52,979 --> 02:25:56,880
all right and low solution it is
3323
02:25:56,880 --> 02:25:59,100
let's delete this
3324
02:25:59,100 --> 02:26:04,160
text let's add to print Elena
3325
02:26:04,680 --> 02:26:07,580
statements here
3326
02:26:09,600 --> 02:26:12,020
foreign
3327
02:26:12,020 --> 02:26:15,359
now if you run this code
3328
02:26:15,359 --> 02:26:17,700
you're gonna see this part executed else
3329
02:26:17,700 --> 02:26:19,500
part because this condition is false
3330
02:26:19,500 --> 02:26:22,800
fault of our condition respectively num1
3331
02:26:22,800 --> 02:26:25,260
great is not greater than zero and
3332
02:26:25,260 --> 02:26:27,720
also num2 is also not greater than zero
3333
02:26:27,720 --> 02:26:30,600
so this is going to return false
3334
02:26:30,600 --> 02:26:34,340
and it's going to execute the else part
3335
02:26:39,120 --> 02:26:41,700
all right but now if you change this to
3336
02:26:41,700 --> 02:26:43,380
five and now one of the conditions is
3337
02:26:43,380 --> 02:26:46,200
going to be true and as you know this
3338
02:26:46,200 --> 02:26:47,939
entire expression is going to wait is
3339
02:26:47,939 --> 02:26:50,100
going to evaluate to true now because at
3340
02:26:50,100 --> 02:26:52,200
least one of the the expression is true
3341
02:26:52,200 --> 02:26:55,399
so if you run this code
3342
02:27:00,540 --> 02:27:02,160
now I see this condition is true and
3343
02:27:02,160 --> 02:27:05,160
also we see all our second printerland
3344
02:27:05,160 --> 02:27:06,720
statement this condition is running some
3345
02:27:06,720 --> 02:27:07,560
text
3346
02:27:07,560 --> 02:27:11,840
but look what happens uh
3347
02:27:12,780 --> 02:27:14,399
if I uh
3348
02:27:14,399 --> 02:27:17,520
let's say if I
3349
02:27:17,520 --> 02:27:20,840
delete the else part
3350
02:27:22,020 --> 02:27:24,600
right and if I delete the curly braces
3351
02:27:24,600 --> 02:27:26,520
here
3352
02:27:26,520 --> 02:27:28,680
all right
3353
02:27:28,680 --> 02:27:31,939
and now if I run this code
3354
02:27:34,140 --> 02:27:36,479
you see the now both statements are
3355
02:27:36,479 --> 02:27:38,040
executed but as I said if you don't use
3356
02:27:38,040 --> 02:27:40,500
curly braces you want just only one of
3357
02:27:40,500 --> 02:27:41,399
the
3358
02:27:41,399 --> 02:27:43,080
uh
3359
02:27:43,080 --> 02:27:45,359
statements in is going to be executive
3360
02:27:45,359 --> 02:27:47,220
so why you have both of these because
3361
02:27:47,220 --> 02:27:50,340
the first is if you executed as part of
3362
02:27:50,340 --> 02:27:53,160
the if statement because this one is
3363
02:27:53,160 --> 02:27:55,560
true and the next one is executed
3364
02:27:55,560 --> 02:27:57,000
because it's part of the code because
3365
02:27:57,000 --> 02:28:00,660
it's red from top to bottom but if
3366
02:28:00,660 --> 02:28:04,020
I change this to false and now I want
3367
02:28:04,020 --> 02:28:07,880
both condition both statements
3368
02:28:07,880 --> 02:28:10,020
respectively with this one and this one
3369
02:28:10,020 --> 02:28:14,180
to not be executed look what happens
3370
02:28:14,640 --> 02:28:16,680
now get this condition experience on
3371
02:28:16,680 --> 02:28:19,800
text so the first
3372
02:28:19,800 --> 02:28:22,800
instruction in our if the next element
3373
02:28:22,800 --> 02:28:24,840
is omitted because it's part of the if
3374
02:28:24,840 --> 02:28:27,780
the statement but the second one it's
3375
02:28:27,780 --> 02:28:30,780
executed because it's part of the code
3376
02:28:30,780 --> 02:28:34,319
so it's going to be red anyway so this
3377
02:28:34,319 --> 02:28:35,760
is why you need to always use curly
3378
02:28:35,760 --> 02:28:38,040
braces in this way
3379
02:28:38,040 --> 02:28:40,200
the entire code is also is gonna be
3380
02:28:40,200 --> 02:28:43,319
either executed or omitted so this is
3381
02:28:43,319 --> 02:28:44,760
our discussion about the external
3382
02:28:44,760 --> 02:28:47,880
statement and you should always use the
3383
02:28:47,880 --> 02:28:51,660
curly braces and for this part you also
3384
02:28:51,660 --> 02:28:53,880
you should use the curly braces
3385
02:28:53,880 --> 02:28:56,720
and um
3386
02:28:56,760 --> 02:28:59,160
only omit the calibrations if you have
3387
02:28:59,160 --> 02:29:03,420
only single one single statement or one
3388
02:29:03,420 --> 02:29:05,880
single expression if you want to use uh
3389
02:29:05,880 --> 02:29:08,100
leave the statement as an exception to
3390
02:29:08,100 --> 02:29:10,680
assign a value to a variable but if you
3391
02:29:10,680 --> 02:29:13,680
have just one single statement let's say
3392
02:29:13,680 --> 02:29:15,960
you have something like this
3393
02:29:15,960 --> 02:29:20,720
economy the curly braces like this
3394
02:29:24,720 --> 02:29:27,780
so now you can see that everything works
3395
02:29:27,780 --> 02:29:30,240
fine
3396
02:29:30,240 --> 02:29:32,520
so you can use it like this if you have
3397
02:29:32,520 --> 02:29:34,200
one single statement like we have here
3398
02:29:34,200 --> 02:29:36,479
you just print some text to our console
3399
02:29:36,479 --> 02:29:40,260
or if you use it as an expression so if
3400
02:29:40,260 --> 02:29:41,760
you
3401
02:29:41,760 --> 02:29:45,300
sort of value in a variable
3402
02:29:45,300 --> 02:29:48,300
so see you in the next video
3403
02:29:48,300 --> 02:29:50,100
all right now it's time to look at the
3404
02:29:50,100 --> 02:29:52,080
next control flow statement that you can
3405
02:29:52,080 --> 02:29:55,380
use in kotlin besides the the if Daniel
3406
02:29:55,380 --> 02:29:57,780
statement and that is the when statement
3407
02:29:57,780 --> 02:29:59,760
or the one expression but first let's
3408
02:29:59,760 --> 02:30:02,220
create a new project
3409
02:30:02,220 --> 02:30:04,439
I'm going to call it Ctrl flow
3410
02:30:04,439 --> 02:30:07,140
it should be in Pascal case
3411
02:30:07,140 --> 02:30:12,680
select IntelliJ click on next and finish
3412
02:30:14,160 --> 02:30:15,960
right now let's delete the code inside
3413
02:30:15,960 --> 02:30:18,858
the curly braces
3414
02:30:19,080 --> 02:30:21,780
let's hide the project Pane and let's
3415
02:30:21,780 --> 02:30:24,479
first declare a variable it's going to
3416
02:30:24,479 --> 02:30:26,100
be a Val
3417
02:30:26,100 --> 02:30:27,540
it's going to be called
3418
02:30:27,540 --> 02:30:32,040
alarm and I'm gonna assign a value of 12
3419
02:30:32,040 --> 02:30:33,300
to it
3420
02:30:33,300 --> 02:30:36,600
now to to declare when statement we type
3421
02:30:36,600 --> 02:30:39,620
here when
3422
02:30:40,200 --> 02:30:42,780
we put parenthesis and inside the
3423
02:30:42,780 --> 02:30:44,700
parentheses we put
3424
02:30:44,700 --> 02:30:47,160
our argument in this case is our
3425
02:30:47,160 --> 02:30:49,500
variable alarm
3426
02:30:49,500 --> 02:30:51,240
next
3427
02:30:51,240 --> 02:30:54,720
put space and we put the curly braces
3428
02:30:54,720 --> 02:30:58,560
press enter and now we put the
3429
02:30:58,560 --> 02:31:00,479
expression here so here I'm going to put
3430
02:31:00,479 --> 02:31:03,660
the value let's say 12.
3431
02:31:03,660 --> 02:31:06,899
now I'm going to put a minus sign and a
3432
02:31:06,899 --> 02:31:09,899
greater than sign so I have this Arrow
3433
02:31:09,899 --> 02:31:13,979
and here I'm gonna type print Ln this is
3434
02:31:13,979 --> 02:31:17,100
the code that is going to be executed
3435
02:31:17,100 --> 02:31:20,939
so and I'm gonna type the time is
3436
02:31:20,939 --> 02:31:23,640
dollar sign
3437
02:31:23,640 --> 02:31:25,859
alarm
3438
02:31:25,859 --> 02:31:30,180
let's press Ctrl D to duplicate this
3439
02:31:30,180 --> 02:31:32,780
two times
3440
02:31:33,660 --> 02:31:36,500
or three times
3441
02:31:36,960 --> 02:31:39,600
and let's change the last one to one
3442
02:31:39,600 --> 02:31:41,840
else
3443
02:31:48,720 --> 02:31:49,800
all right
3444
02:31:49,800 --> 02:31:53,160
let's change those values here to
3445
02:31:53,160 --> 02:31:55,819
7
3446
02:31:58,319 --> 02:32:01,140
and 14.
3447
02:32:01,140 --> 02:32:03,180
now let's run this code to see what to
3448
02:32:03,180 --> 02:32:04,979
get in the output and after that we're
3449
02:32:04,979 --> 02:32:08,000
going to discuss our code
3450
02:32:09,359 --> 02:32:12,540
so getting the output the time is 12 and
3451
02:32:12,540 --> 02:32:15,300
we get that because what is what the
3452
02:32:15,300 --> 02:32:17,399
when statement is doing
3453
02:32:17,399 --> 02:32:19,920
it's using this argument that we passed
3454
02:32:19,920 --> 02:32:21,600
here in this case it's our variable
3455
02:32:21,600 --> 02:32:23,040
called alarm
3456
02:32:23,040 --> 02:32:26,460
and it's comparing this uh the value of
3457
02:32:26,460 --> 02:32:29,160
this argument with all the branches that
3458
02:32:29,160 --> 02:32:30,420
we have here respectively it's going to
3459
02:32:30,420 --> 02:32:32,640
compare it with the expression that we
3460
02:32:32,640 --> 02:32:35,280
have on the left because on the left we
3461
02:32:35,280 --> 02:32:36,960
have the expression
3462
02:32:36,960 --> 02:32:41,460
the this Arrow sign is gonna is is uh
3463
02:32:41,460 --> 02:32:45,000
saying execute execute this code on the
3464
02:32:45,000 --> 02:32:46,020
right
3465
02:32:46,020 --> 02:32:49,319
if this expression on the left is true
3466
02:32:49,319 --> 02:32:53,760
in this case if our Allah alarm equals
3467
02:32:53,760 --> 02:32:58,319
equals 12 and it's true we have a value
3468
02:32:58,319 --> 02:33:00,660
of 12 so this branch is executed and it
3469
02:33:00,660 --> 02:33:03,840
executes the code on the right which is
3470
02:33:03,840 --> 02:33:07,380
this this the alarm is 12 but if I
3471
02:33:07,380 --> 02:33:10,519
change this to 7 now
3472
02:33:12,300 --> 02:33:16,039
and if I run this code
3473
02:33:17,280 --> 02:33:19,680
I'm going to see the time is seven and
3474
02:33:19,680 --> 02:33:21,479
if I change this to
3475
02:33:21,479 --> 02:33:24,199
let's say
3476
02:33:25,380 --> 02:33:27,359
13.
3477
02:33:27,359 --> 02:33:30,660
and here we have no 13 Dallas part is
3478
02:33:30,660 --> 02:33:32,819
going to be executed similar to our if
3479
02:33:32,819 --> 02:33:35,660
Daniel statement
3480
02:33:37,140 --> 02:33:40,020
you know get the time is 13. so it was
3481
02:33:40,020 --> 02:33:43,500
it was executed the else part now now
3482
02:33:43,500 --> 02:33:47,819
and we can combine those uh Expressions
3483
02:33:47,819 --> 02:33:50,580
by putting a colon and and we can
3484
02:33:50,580 --> 02:33:52,560
combine them and putting them on on one
3485
02:33:52,560 --> 02:33:56,700
single line so I can put here colon
3486
02:33:56,700 --> 02:33:59,899
I can type here
3487
02:34:00,899 --> 02:34:03,300
seven
3488
02:34:03,300 --> 02:34:05,880
and fourteen now
3489
02:34:05,880 --> 02:34:09,000
I'm gonna delete those two
3490
02:34:09,000 --> 02:34:12,060
now it will check to see if our alarm
3491
02:34:12,060 --> 02:34:15,660
argument has the value either 12
3492
02:34:15,660 --> 02:34:19,319
or 7 or 14. so
3493
02:34:19,319 --> 02:34:22,620
um let's change it to 14 to see
3494
02:34:22,620 --> 02:34:24,180
so now if you run this code you're gonna
3495
02:34:24,180 --> 02:34:26,040
see that
3496
02:34:26,040 --> 02:34:29,280
this will evaluate to true and this code
3497
02:34:29,280 --> 02:34:30,960
on the right is going to be executed so
3498
02:34:30,960 --> 02:34:33,300
again the time is 14. and if I change
3499
02:34:33,300 --> 02:34:36,019
this to 7
3500
02:34:37,200 --> 02:34:39,720
again this level is true because one of
3501
02:34:39,720 --> 02:34:42,720
those expressions show so yeah the time
3502
02:34:42,720 --> 02:34:43,920
is seven
3503
02:34:43,920 --> 02:34:45,060
all right
3504
02:34:45,060 --> 02:34:48,359
what you can do next is we can check to
3505
02:34:48,359 --> 02:34:51,240
see if a number is in a certain range so
3506
02:34:51,240 --> 02:34:54,359
we can put another
3507
02:34:54,359 --> 02:34:56,580
another expression here and and here I
3508
02:34:56,580 --> 02:34:58,500
can type
3509
02:34:58,500 --> 02:35:00,000
in
3510
02:35:00,000 --> 02:35:03,060
the in keyword is used to check to see
3511
02:35:03,060 --> 02:35:05,760
if numbers is to see if a number is in a
3512
02:35:05,760 --> 02:35:09,060
certain range and to declare a range we
3513
02:35:09,060 --> 02:35:10,740
type
3514
02:35:10,740 --> 02:35:12,780
the range let's say that I want to check
3515
02:35:12,780 --> 02:35:14,700
from
3516
02:35:14,700 --> 02:35:16,680
let's say uh
3517
02:35:16,680 --> 02:35:18,560
one
3518
02:35:18,560 --> 02:35:22,380
from one to ten we put two dots dot dot
3519
02:35:22,380 --> 02:35:26,460
10. now what this is gonna do is gonna
3520
02:35:26,460 --> 02:35:30,180
check to see if our alarm argument in
3521
02:35:30,180 --> 02:35:31,740
the in this in this case our alarm
3522
02:35:31,740 --> 02:35:35,040
variable is in the range from 1 to 10.
3523
02:35:35,040 --> 02:35:38,760
now I'm gonna put a minus sign and a
3524
02:35:38,760 --> 02:35:41,280
gray and a greater than sign to have the
3525
02:35:41,280 --> 02:35:43,140
arrow and here I'm going to type the
3526
02:35:43,140 --> 02:35:44,819
code that I want to be executing in this
3527
02:35:44,819 --> 02:35:46,380
case it's just a printerland statement
3528
02:35:46,380 --> 02:35:49,938
and I'm gonna type
3529
02:35:49,979 --> 02:35:51,960
the number
3530
02:35:51,960 --> 02:35:55,080
is in
3531
02:35:55,080 --> 02:35:57,899
the range
3532
02:35:57,899 --> 02:36:00,300
1.7
3533
02:36:00,300 --> 02:36:05,060
now if you run this is only from here
3534
02:36:08,280 --> 02:36:12,000
we get the time is seven because uh
3535
02:36:12,000 --> 02:36:15,000
the first branch is checked and this
3536
02:36:15,000 --> 02:36:17,880
branch is true and uh in effect it
3537
02:36:17,880 --> 02:36:20,220
executes the code on the right and the
3538
02:36:20,220 --> 02:36:22,500
next branches are left offs are not
3539
02:36:22,500 --> 02:36:24,960
executed to and to change that let's put
3540
02:36:24,960 --> 02:36:29,300
this first so let's copy this
3541
02:36:32,580 --> 02:36:35,899
now if you run this code
3542
02:36:36,240 --> 02:36:38,580
now get the number is in range one two
3543
02:36:38,580 --> 02:36:41,280
ten and it's true our seven is in the
3544
02:36:41,280 --> 02:36:43,740
range from 1 to 10. what you can do is
3545
02:36:43,740 --> 02:36:45,660
also you can use the logical not
3546
02:36:45,660 --> 02:36:48,300
operator so I can put here
3547
02:36:48,300 --> 02:36:50,520
The Logical not operator which is an uh
3548
02:36:50,520 --> 02:36:52,560
exclamation mark
3549
02:36:52,560 --> 02:36:55,020
and this now will negate this value so
3550
02:36:55,020 --> 02:36:56,520
if you run this code now you'll get the
3551
02:36:56,520 --> 02:36:59,760
output 7. the time is seven so now we
3552
02:36:59,760 --> 02:37:00,840
get the output
3553
02:37:00,840 --> 02:37:03,479
the time is seven because this this
3554
02:37:03,479 --> 02:37:06,120
switch is this value from True to false
3555
02:37:06,120 --> 02:37:09,720
and execute this uh part
3556
02:37:09,720 --> 02:37:12,240
now
3557
02:37:12,240 --> 02:37:14,100
let's delete this
3558
02:37:14,100 --> 02:37:16,620
what I can also do is that we can put
3559
02:37:16,620 --> 02:37:20,899
curly braces so I can put curly braces
3560
02:37:21,899 --> 02:37:24,500
press enter
3561
02:37:27,540 --> 02:37:30,380
the same here
3562
02:37:33,780 --> 02:37:35,520
and
3563
02:37:35,520 --> 02:37:36,840
here
3564
02:37:36,840 --> 02:37:38,880
also
3565
02:37:38,880 --> 02:37:40,680
and it's recommended to use the
3566
02:37:40,680 --> 02:37:43,080
calibrases only if you have multiple
3567
02:37:43,080 --> 02:37:44,939
statements in your code in our case we
3568
02:37:44,939 --> 02:37:46,740
have one one single statement which is
3569
02:37:46,740 --> 02:37:48,960
this printerland which outputs some text
3570
02:37:48,960 --> 02:37:51,600
to the console and the the when
3571
02:37:51,600 --> 02:37:53,939
statement as I said can be used as an
3572
02:37:53,939 --> 02:37:56,520
expression so I can type here a Val
3573
02:37:56,520 --> 02:38:01,380
let's say your text and I can assign the
3574
02:38:01,380 --> 02:38:05,160
when expression to our text variable now
3575
02:38:05,160 --> 02:38:08,700
we need to give back a value to our text
3576
02:38:08,700 --> 02:38:11,880
so instead of printing this text I can
3577
02:38:11,880 --> 02:38:15,019
delete this
3578
02:38:17,220 --> 02:38:20,420
let's put it like this
3579
02:38:23,520 --> 02:38:27,359
now the last expression in our block of
3580
02:38:27,359 --> 02:38:29,939
code which is now we have a single
3581
02:38:29,939 --> 02:38:31,020
expression but you have multiple
3582
02:38:31,020 --> 02:38:32,520
Expressions say that we have here
3583
02:38:32,520 --> 02:38:35,960
another print Ln
3584
02:38:37,680 --> 02:38:40,760
let's say some text
3585
02:38:42,180 --> 02:38:43,979
so similar to our if then else
3586
02:38:43,979 --> 02:38:47,040
expression the last line of code the
3587
02:38:47,040 --> 02:38:49,439
last expression is going to be assigned
3588
02:38:49,439 --> 02:38:51,899
to our variable text now let's print
3589
02:38:51,899 --> 02:38:53,939
this down here
3590
02:38:53,939 --> 02:38:58,640
torque console so let's type println
3591
02:39:01,560 --> 02:39:05,220
and let's put director here text
3592
02:39:05,220 --> 02:39:08,358
now if you run this code
3593
02:39:12,180 --> 02:39:14,220
now I'm gonna see the text the number is
3594
02:39:14,220 --> 02:39:15,960
in the range you want to
3595
02:39:15,960 --> 02:39:19,979
10 because it checks to see if this
3596
02:39:19,979 --> 02:39:23,280
expression on the left is true then it
3597
02:39:23,280 --> 02:39:25,020
executes the car the code inside the
3598
02:39:25,020 --> 02:39:27,960
curly braces which is this expression
3599
02:39:27,960 --> 02:39:30,899
and because now we're using the one as
3600
02:39:30,899 --> 02:39:33,960
an expression it assigns this expression
3601
02:39:33,960 --> 02:39:37,319
this text or variable text then it
3602
02:39:37,319 --> 02:39:39,840
outputs that value with the print line
3603
02:39:39,840 --> 02:39:42,600
down here in our console so you get the
3604
02:39:42,600 --> 02:39:45,120
number is in the range one two
3605
02:39:45,120 --> 02:39:50,340
10. all right now but what if I want to
3606
02:39:50,340 --> 02:39:53,520
put here a less than sign so you what if
3607
02:39:53,520 --> 02:39:56,960
I want to put here if
3608
02:39:58,260 --> 02:40:02,399
is less than seven our alarm argument I
3609
02:40:02,399 --> 02:40:04,859
can do that the only way to do that is
3610
02:40:04,859 --> 02:40:07,800
by uh let's delete this now let's put
3611
02:40:07,800 --> 02:40:09,180
seven
3612
02:40:09,180 --> 02:40:11,780
not here
3613
02:40:14,819 --> 02:40:17,220
let's press Ctrl alt L to format the
3614
02:40:17,220 --> 02:40:19,439
code all right
3615
02:40:19,439 --> 02:40:21,720
now the only way to put a greater than
3616
02:40:21,720 --> 02:40:24,060
sign or less than sine or greater than
3617
02:40:24,060 --> 02:40:26,939
equal or greater or less than equal sign
3618
02:40:26,939 --> 02:40:29,460
is to delete the argument here but let's
3619
02:40:29,460 --> 02:40:32,040
first delete the curly braces to make
3620
02:40:32,040 --> 02:40:34,500
things more clear
3621
02:40:34,500 --> 02:40:37,920
so let's delete the curly braces
3622
02:40:37,920 --> 02:40:39,960
let's delete this now because here we
3623
02:40:39,960 --> 02:40:42,180
have two lines of course so we need to
3624
02:40:42,180 --> 02:40:43,920
use curly braces this is why I deleted
3625
02:40:43,920 --> 02:40:45,840
the penalion there and again you should
3626
02:40:45,840 --> 02:40:47,460
always use curly braces if you have
3627
02:40:47,460 --> 02:40:49,620
multiple lines of code but in our case
3628
02:40:49,620 --> 02:40:51,660
we have a simple line of code which is
3629
02:40:51,660 --> 02:40:54,300
the text on the right which is going to
3630
02:40:54,300 --> 02:40:57,060
be assigned to our variable text and
3631
02:40:57,060 --> 02:40:58,560
it's going to be output in the console
3632
02:40:58,560 --> 02:41:00,899
so always use highly braces if you have
3633
02:41:00,899 --> 02:41:02,640
multiple lines of code all right all
3634
02:41:02,640 --> 02:41:04,560
right now this is more concise and more
3635
02:41:04,560 --> 02:41:06,840
clear and let's say that I want to check
3636
02:41:06,840 --> 02:41:12,180
to see if our alarm is less than say 10.
3637
02:41:12,180 --> 02:41:14,760
um to do that we need to delete the
3638
02:41:14,760 --> 02:41:17,359
argument here
3639
02:41:18,180 --> 02:41:21,420
all right and now what can I do is I can
3640
02:41:21,420 --> 02:41:23,399
put here
3641
02:41:23,399 --> 02:41:25,080
our
3642
02:41:25,080 --> 02:41:28,080
alarm variable and here I can put less
3643
02:41:28,080 --> 02:41:29,399
than
3644
02:41:29,399 --> 02:41:33,120
or equal to 10. so now here we have a
3645
02:41:33,120 --> 02:41:36,060
Boolean expression on the left
3646
02:41:36,060 --> 02:41:38,600
sorry
3647
02:41:40,819 --> 02:41:43,080
so now we have a Boolean expression here
3648
02:41:43,080 --> 02:41:44,460
on the left
3649
02:41:44,460 --> 02:41:48,300
and this will check to see if our alarm
3650
02:41:48,300 --> 02:41:50,640
or variable alarm alarm has a value
3651
02:41:50,640 --> 02:41:53,160
Which is less than or equal to 10 which
3652
02:41:53,160 --> 02:41:55,439
is true we have a 7 here so now if you
3653
02:41:55,439 --> 02:41:56,580
run this call
3654
02:41:56,580 --> 02:41:59,280
but let's delete this because we need to
3655
02:41:59,280 --> 02:42:01,439
have now Boolean expression here because
3656
02:42:01,439 --> 02:42:04,560
here we cannot uh here we have just
3657
02:42:04,560 --> 02:42:07,080
literal types so here I need to put
3658
02:42:07,080 --> 02:42:12,180
let's say if let's type alarm
3659
02:42:12,180 --> 02:42:15,000
equals equals
3660
02:42:15,000 --> 02:42:17,660
8
3661
02:42:18,240 --> 02:42:20,760
or two vertical bars
3662
02:42:20,760 --> 02:42:22,260
alarm
3663
02:42:22,260 --> 02:42:25,520
equals equals
3664
02:42:27,120 --> 02:42:28,439
seven
3665
02:42:28,439 --> 02:42:31,700
so now if you run this code
3666
02:42:32,100 --> 02:42:33,780
now you get the output the numbers in
3667
02:42:33,780 --> 02:42:36,899
the range 1 to 10. because now this on
3668
02:42:36,899 --> 02:42:39,000
the this expression on the left is true
3669
02:42:39,000 --> 02:42:42,740
and it executes this text uh
3670
02:42:42,740 --> 02:42:45,359
it assigns this expression to our
3671
02:42:45,359 --> 02:42:48,240
variable text and it prints down here
3672
02:42:48,240 --> 02:42:50,280
the that value so you get here the
3673
02:42:50,280 --> 02:42:52,200
number is already one to ten
3674
02:42:52,200 --> 02:42:55,340
but uh
3675
02:42:55,380 --> 02:43:00,000
this condition is also true so if I swap
3676
02:43:00,000 --> 02:43:01,920
those two so if I put this one below
3677
02:43:01,920 --> 02:43:05,580
because as I said if one uh one of the
3678
02:43:05,580 --> 02:43:08,520
expression evolution true the next ones
3679
02:43:08,520 --> 02:43:11,760
the the one which are below so this in
3680
02:43:11,760 --> 02:43:14,520
this case this one is true or not be are
3681
02:43:14,520 --> 02:43:17,340
not going to be executed are skipped it
3682
02:43:17,340 --> 02:43:20,760
only executes this code and the
3683
02:43:20,760 --> 02:43:23,880
subsequent ones are omitted so let's put
3684
02:43:23,880 --> 02:43:25,080
this
3685
02:43:25,080 --> 02:43:26,640
below
3686
02:43:26,640 --> 02:43:29,520
it's good to remember this
3687
02:43:29,520 --> 02:43:32,899
so now if you run this code
3688
02:43:39,420 --> 02:43:42,359
now get the time is seven because now
3689
02:43:42,359 --> 02:43:44,100
this level is true even though this
3690
02:43:44,100 --> 02:43:47,880
below is also true because our alarm is
3691
02:43:47,880 --> 02:43:50,040
less than seven because this available
3692
02:43:50,040 --> 02:43:52,680
is too true it executes the code which
3693
02:43:52,680 --> 02:43:55,100
is on the right which is assigning this
3694
02:43:55,100 --> 02:43:57,180
expression to our text and then it
3695
02:43:57,180 --> 02:43:58,979
leaves the one expression it's not going
3696
02:43:58,979 --> 02:44:02,460
uh down to execute the next one so it
3697
02:44:02,460 --> 02:44:04,979
breaks uh like there and it stops it
3698
02:44:04,979 --> 02:44:06,420
actually stops right there and it's not
3699
02:44:06,420 --> 02:44:09,060
executing the next branch
3700
02:44:09,060 --> 02:44:12,540
so this is our discussion about the when
3701
02:44:12,540 --> 02:44:14,580
statement statement and the one
3702
02:44:14,580 --> 02:44:16,800
expression see you in the next video
3703
02:44:16,800 --> 02:44:18,300
now it's time to start a discussion
3704
02:44:18,300 --> 02:44:20,280
about null ability in kotlin but first
3705
02:44:20,280 --> 02:44:22,319
i'm going to create a new project I'm
3706
02:44:22,319 --> 02:44:25,279
going to call it null
3707
02:44:25,800 --> 02:44:27,859
make sure to have the language
3708
02:44:27,859 --> 02:44:30,180
culturally select it and for the build
3709
02:44:30,180 --> 02:44:33,120
system IntelliJ also make sure to have
3710
02:44:33,120 --> 02:44:34,979
the jdk selected here and I'm gonna
3711
02:44:34,979 --> 02:44:37,680
check this little box to generate the
3712
02:44:37,680 --> 02:44:39,840
main function for us and that code that
3713
02:44:39,840 --> 02:44:41,760
comes with the main function so I'm
3714
02:44:41,760 --> 02:44:43,319
going to click on create to create a new
3715
02:44:43,319 --> 02:44:44,340
project
3716
02:44:44,340 --> 02:44:46,260
now our project is going to be great in
3717
02:44:46,260 --> 02:44:48,859
a short moment
3718
02:44:49,859 --> 02:44:52,620
so we got our project created and we get
3719
02:44:52,620 --> 02:44:55,080
a log you get our main function Auto
3720
02:44:55,080 --> 02:44:56,819
generated here for us because we check
3721
02:44:56,819 --> 02:44:59,100
that box so I'm gonna delete this
3722
02:44:59,100 --> 02:45:02,479
because I don't need this code
3723
02:45:03,660 --> 02:45:05,580
and I'm going to hide the project pane
3724
02:45:05,580 --> 02:45:07,740
so what is a null value a null value is
3725
02:45:07,740 --> 02:45:09,180
basically when you assign to a variable
3726
02:45:09,180 --> 02:45:11,460
nothing synthetically speaking you
3727
02:45:11,460 --> 02:45:13,500
assign null to it but what that means is
3728
02:45:13,500 --> 02:45:15,300
that you have a variable which has no
3729
02:45:15,300 --> 02:45:18,600
value which has no memory reference and
3730
02:45:18,600 --> 02:45:20,220
if you try to do some other arithmetic
3731
02:45:20,220 --> 02:45:21,780
operation with that variable you're
3732
02:45:21,780 --> 02:45:24,720
getting an impossible scenario where you
3733
02:45:24,720 --> 02:45:26,939
have nothing try to do some work and
3734
02:45:26,939 --> 02:45:30,240
that will throw a null pointer exception
3735
02:45:30,240 --> 02:45:33,620
and this is particularly bad because the
3736
02:45:33,620 --> 02:45:37,020
null pointer exceptions are only thrown
3737
02:45:37,020 --> 02:45:39,899
at runtime and not at compile time so
3738
02:45:39,899 --> 02:45:43,080
you could have your program working well
3739
02:45:43,080 --> 02:45:46,620
and having no problems and at some point
3740
02:45:46,620 --> 02:45:49,500
you try to I don't know press a button
3741
02:45:49,500 --> 02:45:51,780
and somehow somehow that
3742
02:45:51,780 --> 02:45:55,500
pressing of the button is using that
3743
02:45:55,500 --> 02:45:57,720
variable which has another value and
3744
02:45:57,720 --> 02:45:59,580
then it's going to throw an old normal
3745
02:45:59,580 --> 02:46:02,460
pointer exception and your app will
3746
02:46:02,460 --> 02:46:03,479
crash
3747
02:46:03,479 --> 02:46:07,140
and kotlin aims to avoid the and to
3748
02:46:07,140 --> 02:46:09,000
eliminate the null pointer exceptions
3749
02:46:09,000 --> 02:46:11,340
and null values that is not to say that
3750
02:46:11,340 --> 02:46:14,160
we can't have a null value in a kotlin
3751
02:46:14,160 --> 02:46:17,939
but is very hard to have one so let's
3752
02:46:17,939 --> 02:46:20,700
see why so what cotton has done to make
3753
02:46:20,700 --> 02:46:23,040
null values and implicitlyn all
3754
02:46:23,040 --> 02:46:25,380
pointerizations very hard to get
3755
02:46:25,380 --> 02:46:28,260
it first made all its styles by default
3756
02:46:28,260 --> 02:46:29,939
non-nullable and that means that you
3757
02:46:29,939 --> 02:46:32,520
cannot assign null to it so if I declare
3758
02:46:32,520 --> 02:46:34,380
here a variable called text I'm going to
3759
02:46:34,380 --> 02:46:36,180
declare the type explicitly so it's
3760
02:46:36,180 --> 02:46:37,740
going to be a string
3761
02:46:37,740 --> 02:46:40,620
and if I assign null so to assign Alto
3762
02:46:40,620 --> 02:46:42,060
where we'll just type the null and we
3763
02:46:42,060 --> 02:46:44,100
have the null cured
3764
02:46:44,100 --> 02:46:46,200
we can't because this variable by
3765
02:46:46,200 --> 02:46:48,600
default and all the variables by default
3766
02:46:48,600 --> 02:46:50,700
are non-nullable and that means that you
3767
02:46:50,700 --> 02:46:52,260
cannot assign a value so if you hover
3768
02:46:52,260 --> 02:46:55,439
over this underline it says null cannot
3769
02:46:55,439 --> 02:46:58,260
be a value of a non null type string but
3770
02:46:58,260 --> 02:47:01,260
what if I want to assign null to it what
3771
02:47:01,260 --> 02:47:03,840
you if I want to have null for some
3772
02:47:03,840 --> 02:47:04,560
reason
3773
02:47:04,560 --> 02:47:07,620
in that case you go at the end of the
3774
02:47:07,620 --> 02:47:10,439
type and you put question mark and that
3775
02:47:10,439 --> 02:47:12,660
means that now you have a nullable type
3776
02:47:12,660 --> 02:47:14,580
that means that you can assign null to
3777
02:47:14,580 --> 02:47:17,399
this variable so if I put question mark
3778
02:47:17,399 --> 02:47:18,359
here
3779
02:47:18,359 --> 02:47:21,600
now I can assign null to this variable
3780
02:47:21,600 --> 02:47:23,280
so this is
3781
02:47:23,280 --> 02:47:25,920
how we can assign an altar variable by
3782
02:47:25,920 --> 02:47:27,479
putting the question mark at the end of
3783
02:47:27,479 --> 02:47:29,399
the type because that is saying to the
3784
02:47:29,399 --> 02:47:32,040
compiler hey I'm gonna let I'm gonna let
3785
02:47:32,040 --> 02:47:34,680
me to assign now to this variable so
3786
02:47:34,680 --> 02:47:39,000
to avoid that underwriting red now if I
3787
02:47:39,000 --> 02:47:41,160
try to print this so if I put here print
3788
02:47:41,160 --> 02:47:42,479
line
3789
02:47:42,479 --> 02:47:44,819
and I put here text
3790
02:47:44,819 --> 02:47:46,380
what do you think we're getting the
3791
02:47:46,380 --> 02:47:48,660
output
3792
02:47:48,660 --> 02:47:50,640
and output to get null you know you
3793
02:47:50,640 --> 02:47:53,220
don't get null pointer exception so now
3794
02:47:53,220 --> 02:47:56,460
if I hide this and let's say that I want
3795
02:47:56,460 --> 02:47:59,819
to get the length of let's say of the
3796
02:47:59,819 --> 02:48:04,200
text so let's say that I assign here
3797
02:48:04,200 --> 02:48:07,160
some text
3798
02:48:12,359 --> 02:48:14,040
well actually let's put the name here
3799
02:48:14,040 --> 02:48:16,340
name
3800
02:48:16,439 --> 02:48:19,680
unless and if I try to get the length
3801
02:48:19,680 --> 02:48:23,280
here so if I put here
3802
02:48:23,280 --> 02:48:26,399
dot length
3803
02:48:26,399 --> 02:48:28,200
I have an underline
3804
02:48:28,200 --> 02:48:30,960
which says only save and we have this
3805
02:48:30,960 --> 02:48:35,040
question mark that or not or asserted
3806
02:48:35,040 --> 02:48:38,220
and have this this operator Cults are
3807
02:48:38,220 --> 02:48:40,140
allowed on a nullable receiver of type
3808
02:48:40,140 --> 02:48:44,880
string so it's saying that we can
3809
02:48:44,880 --> 02:48:48,780
we can only get because you this this
3810
02:48:48,780 --> 02:48:51,720
variable can have a null value but we
3811
02:48:51,720 --> 02:48:54,899
know that it doesn't but the compiler is
3812
02:48:54,899 --> 02:48:57,359
saying to us because we said that this
3813
02:48:57,359 --> 02:49:00,840
can have null is saying to check first
3814
02:49:00,840 --> 02:49:03,960
that this variable is not equal to null
3815
02:49:03,960 --> 02:49:06,240
and then try to get its length and let's
3816
02:49:06,240 --> 02:49:08,100
do that in the Long Way first so let's
3817
02:49:08,100 --> 02:49:10,080
delete this
3818
02:49:10,080 --> 02:49:14,240
so Alex actually just copy this
3819
02:49:18,359 --> 02:49:21,240
and we type if
3820
02:49:21,240 --> 02:49:25,220
we put parenthesis if
3821
02:49:25,380 --> 02:49:30,438
text not equal to null
3822
02:49:33,960 --> 02:49:36,660
then I'm gonna output this one we're
3823
02:49:36,660 --> 02:49:38,100
then going to get its length so we're
3824
02:49:38,100 --> 02:49:40,859
gonna type here text
3825
02:49:40,859 --> 02:49:42,899
not the length so now that the compiler
3826
02:49:42,899 --> 02:49:46,080
is happy because it's it's uh it's
3827
02:49:46,080 --> 02:49:47,939
ensure that is not gonna have no because
3828
02:49:47,939 --> 02:49:49,560
we checked here especially that this
3829
02:49:49,560 --> 02:49:52,800
variable uh should have you should not
3830
02:49:52,800 --> 02:49:55,380
have null in order to get the length so
3831
02:49:55,380 --> 02:49:58,640
now if I run this
3832
02:49:59,120 --> 02:50:02,040
I get four so this is the length so we
3833
02:50:02,040 --> 02:50:04,319
have four letters here and I go I can
3834
02:50:04,319 --> 02:50:07,460
also add the else part
3835
02:50:07,500 --> 02:50:11,300
which is which is gonna say
3836
02:50:12,960 --> 02:50:15,180
the very
3837
02:50:15,180 --> 02:50:17,760
well it's not so if the variable is not
3838
02:50:17,760 --> 02:50:20,160
the else part is going to be called
3839
02:50:20,160 --> 02:50:23,300
so now if you run this
3840
02:50:25,380 --> 02:50:27,660
will get also four but I'm gonna assign
3841
02:50:27,660 --> 02:50:29,399
I'm gonna reassign this variable so I'm
3842
02:50:29,399 --> 02:50:31,439
gonna change it to VAR first
3843
02:50:31,439 --> 02:50:34,880
so we put here VAR
3844
02:50:36,540 --> 02:50:40,979
and put here text equals null so now our
3845
02:50:40,979 --> 02:50:43,260
variable is null and this check is going
3846
02:50:43,260 --> 02:50:45,780
to fail and it's gonna execute the else
3847
02:50:45,780 --> 02:50:49,100
part so if I run this code
3848
02:50:50,520 --> 02:50:51,960
so we get the variable is null because
3849
02:50:51,960 --> 02:50:54,300
now on the
3850
02:50:54,300 --> 02:50:56,760
on the third line we assign nullity so
3851
02:50:56,760 --> 02:50:59,040
this if fails and is executed there spot
3852
02:50:59,040 --> 02:51:00,720
but as you can see it's a lot of code
3853
02:51:00,720 --> 02:51:03,359
just to check to to check and imagine if
3854
02:51:03,359 --> 02:51:05,520
you have multiple variables to check to
3855
02:51:05,520 --> 02:51:08,880
always check for this that the variable
3856
02:51:08,880 --> 02:51:11,580
is not normal then print its length and
3857
02:51:11,580 --> 02:51:13,680
there is a shorter way in kotlin to do
3858
02:51:13,680 --> 02:51:16,859
that and we saw as a suggestion how to
3859
02:51:16,859 --> 02:51:21,120
do that previously so if I press Ctrl Z2
3860
02:51:21,120 --> 02:51:24,660
um undo our code so if I press still
3861
02:51:24,660 --> 02:51:27,300
pressing now if I hover over this error
3862
02:51:27,300 --> 02:51:30,060
that we had previously which says
3863
02:51:30,060 --> 02:51:33,800
only save call so and you have this
3864
02:51:33,800 --> 02:51:37,220
operator and this is called the
3865
02:51:37,220 --> 02:51:40,620
safe call operator and if I put here
3866
02:51:40,620 --> 02:51:43,500
just a question mark
3867
02:51:43,500 --> 02:51:46,979
this is equivalent to having if which is
3868
02:51:46,979 --> 02:51:49,380
checking to see if the length is null if
3869
02:51:49,380 --> 02:51:51,600
and if the length is null then it's
3870
02:51:51,600 --> 02:51:54,359
going to say null or and if it's not
3871
02:51:54,359 --> 02:51:58,020
null it's gonna just output four so now
3872
02:51:58,020 --> 02:52:00,359
if I run this so this is equivalent to
3873
02:52:00,359 --> 02:52:04,319
the if that if the the if statement that
3874
02:52:04,319 --> 02:52:07,620
you had previously so you have four
3875
02:52:07,620 --> 02:52:10,439
so what if I should and if I assign null
3876
02:52:10,439 --> 02:52:13,200
so if I type here let's change this to
3877
02:52:13,200 --> 02:52:15,720
vardy First
3878
02:52:15,720 --> 02:52:19,319
so if I assign here text equals
3879
02:52:19,319 --> 02:52:21,979
null
3880
02:52:23,040 --> 02:52:26,779
we'll simply get null
3881
02:52:29,340 --> 02:52:31,920
we get null but what if I want to get
3882
02:52:31,920 --> 02:52:33,779
that null pointer exception for whatever
3883
02:52:33,779 --> 02:52:36,420
reason what can I do well the compiler
3884
02:52:36,420 --> 02:52:39,960
or it helping is helping us also there
3885
02:52:39,960 --> 02:52:42,720
so because if I delete that save call
3886
02:52:42,720 --> 02:52:44,819
Operator which is just a question mark
3887
02:52:44,819 --> 02:52:48,060
that and that is basically saying
3888
02:52:48,060 --> 02:52:52,140
hey if this text is not null then output
3889
02:52:52,140 --> 02:52:55,140
in the console then get then get the
3890
02:52:55,140 --> 02:52:56,880
length and then output that in the
3891
02:52:56,880 --> 02:52:59,939
console if it's null just say no so this
3892
02:52:59,939 --> 02:53:02,340
is how the save call Operator works but
3893
02:53:02,340 --> 02:53:04,380
if I delete this as you can see there is
3894
02:53:04,380 --> 02:53:05,580
another
3895
02:53:05,580 --> 02:53:08,220
operator and is
3896
02:53:08,220 --> 02:53:11,399
and is this operator which is uh two
3897
02:53:11,399 --> 02:53:14,399
exclamation marks and this operator is
3898
02:53:14,399 --> 02:53:16,080
basically saying
3899
02:53:16,080 --> 02:53:20,340
hey if this variable has null then throw
3900
02:53:20,340 --> 02:53:23,939
that exception but if it's not null just
3901
02:53:23,939 --> 02:53:26,700
output don't just output the length so
3902
02:53:26,700 --> 02:53:30,359
if I put two exclamation marks here
3903
02:53:30,359 --> 02:53:34,020
but for whatever reason it's not the our
3904
02:53:34,020 --> 02:53:35,939
text that length is not getting the
3905
02:53:35,939 --> 02:53:38,399
length and I think this this is because
3906
02:53:38,399 --> 02:53:42,000
we assign to our text variable here null
3907
02:53:42,000 --> 02:53:45,899
it it can't it has a prep it has a pro a
3908
02:53:45,899 --> 02:53:48,060
problem with inferring what type this
3909
02:53:48,060 --> 02:53:50,760
variable is even though here is saying
3910
02:53:50,760 --> 02:53:53,160
that it's a string so we should
3911
02:53:53,160 --> 02:53:55,620
we should be able to call that length
3912
02:53:55,620 --> 02:53:58,560
and to avoid this you just put null at
3913
02:53:58,560 --> 02:54:00,960
uh when you declare the variable so we
3914
02:54:00,960 --> 02:54:02,220
assign here null
3915
02:54:02,220 --> 02:54:05,358
so we put here null
3916
02:54:07,080 --> 02:54:09,720
now if you run this because you have the
3917
02:54:09,720 --> 02:54:12,479
two inflammation Mark operator now this
3918
02:54:12,479 --> 02:54:14,640
will throw that null pointer accession
3919
02:54:14,640 --> 02:54:17,539
that I talked about
3920
02:54:17,660 --> 02:54:21,120
so we get exception thread main so it
3921
02:54:21,120 --> 02:54:23,100
doesn't matter what trademark is now I'm
3922
02:54:23,100 --> 02:54:25,020
gonna you get Javas like the null
3923
02:54:25,020 --> 02:54:27,540
pointer exception so this is the
3924
02:54:27,540 --> 02:54:31,020
the null pointer exception but if we
3925
02:54:31,020 --> 02:54:33,180
assign down here this variable text
3926
02:54:33,180 --> 02:54:35,220
equals
3927
02:54:35,220 --> 02:54:38,358
some text
3928
02:54:38,700 --> 02:54:42,380
and now if we run this code
3929
02:54:46,800 --> 02:54:49,560
you get nine because this is the length
3930
02:54:49,560 --> 02:54:51,240
of this so
3931
02:54:51,240 --> 02:54:54,000
this is how you can use this operator
3932
02:54:54,000 --> 02:54:55,800
the next operator that you can use is
3933
02:54:55,800 --> 02:54:58,859
the Elvis operator and to show you how
3934
02:54:58,859 --> 02:55:02,840
that works I'm going to delete this code
3935
02:55:04,859 --> 02:55:06,120
now I'm going to declare another
3936
02:55:06,120 --> 02:55:08,460
variable up here so I'm going to call it
3937
02:55:08,460 --> 02:55:10,620
text two
3938
02:55:10,620 --> 02:55:15,319
we put equals and we type text
3939
02:55:15,479 --> 02:55:18,000
now to use the Elvis operator we put a
3940
02:55:18,000 --> 02:55:21,899
question mark and the colon
3941
02:55:21,899 --> 02:55:24,899
and now we need to uh
3942
02:55:24,899 --> 02:55:26,880
type something on the right of the
3943
02:55:26,880 --> 02:55:29,279
office operator so we put here some text
3944
02:55:29,279 --> 02:55:30,600
I'm going to explain immediately how
3945
02:55:30,600 --> 02:55:34,340
this works so I'm gonna type here
3946
02:55:35,640 --> 02:55:38,420
some
3947
02:55:40,979 --> 02:55:42,840
text
3948
02:55:42,840 --> 02:55:46,140
now what the Elvis operator is doing is
3949
02:55:46,140 --> 02:55:47,100
saying
3950
02:55:47,100 --> 02:55:51,720
if this variable text is null then
3951
02:55:51,720 --> 02:55:54,660
assign what is on the right to this
3952
02:55:54,660 --> 02:55:56,220
variable text to
3953
02:55:56,220 --> 02:56:00,960
if this variable text is not null then
3954
02:56:00,960 --> 02:56:03,479
assign what is to this variable to our
3955
02:56:03,479 --> 02:56:07,020
variable text to our variable text to
3956
02:56:07,020 --> 02:56:10,080
and now let's uh
3957
02:56:10,080 --> 02:56:13,380
let's type here text too
3958
02:56:13,380 --> 02:56:17,599
and less the little length
3959
02:56:18,180 --> 02:56:21,859
so now if you run this code
3960
02:56:22,200 --> 02:56:23,939
let's actually put something more
3961
02:56:23,939 --> 02:56:26,460
discrete like
3962
02:56:26,460 --> 02:56:29,778
the variable
3963
02:56:30,060 --> 02:56:31,859
null
3964
02:56:31,859 --> 02:56:35,899
so if you run this code now
3965
02:56:36,720 --> 02:56:39,060
we get a variable is null because
3966
02:56:39,060 --> 02:56:42,479
it checked to see to to see if this
3967
02:56:42,479 --> 02:56:45,000
variable on the left is null and if it
3968
02:56:45,000 --> 02:56:48,420
is if it is now it's gonna it assigned
3969
02:56:48,420 --> 02:56:50,819
this to to a variable takes two and then
3970
02:56:50,819 --> 02:56:53,040
I'll put that in the console
3971
02:56:53,040 --> 02:56:55,080
but if this is not null then it's gonna
3972
02:56:55,080 --> 02:56:57,540
assign what is on this would what do
3973
02:56:57,540 --> 02:56:59,460
whatever value this variable has to or
3974
02:56:59,460 --> 02:57:02,279
variable takes two so if now I put here
3975
02:57:02,279 --> 02:57:05,340
text equals
3976
02:57:05,340 --> 02:57:11,240
this variable is not null
3977
02:57:11,340 --> 02:57:13,800
now if I run this
3978
02:57:13,800 --> 02:57:16,620
now this well this will evaluate to true
3979
02:57:16,620 --> 02:57:18,540
so it's gonna
3980
02:57:18,540 --> 02:57:21,540
assign whatever value is on the left or
3981
02:57:21,540 --> 02:57:23,520
variable text to a variable text to and
3982
02:57:23,520 --> 02:57:26,040
we're going to see in the output the
3983
02:57:26,040 --> 02:57:28,319
variable is not null so we want a
3984
02:57:28,319 --> 02:57:30,060
variable to X2 has now the value
3985
02:57:30,060 --> 02:57:32,640
assigned from our for each variable so
3986
02:57:32,640 --> 02:57:34,800
this is the shorter way of writing let
3987
02:57:34,800 --> 02:57:36,420
me show you how we will write this in
3988
02:57:36,420 --> 02:57:38,460
the longer way so you in the longer way
3989
02:57:38,460 --> 02:57:39,840
you will have something like this so
3990
02:57:39,840 --> 02:57:42,859
let's delete this quad
3991
02:57:43,920 --> 02:57:45,800
actually
3992
02:57:45,800 --> 02:57:50,000
yes let's delete this quad
3993
02:57:52,380 --> 02:57:55,500
will have a variable called
3994
02:57:55,500 --> 02:57:57,060
text
3995
02:57:57,060 --> 02:57:59,640
tool and
3996
02:57:59,640 --> 02:58:02,220
it's gonna have an empty string
3997
02:58:02,220 --> 02:58:04,380
so we would what we will have to do
3998
02:58:04,380 --> 02:58:07,200
without the Elvis operator we have to
3999
02:58:07,200 --> 02:58:10,859
first check if text
4000
02:58:10,859 --> 02:58:13,800
not equal to
4001
02:58:13,800 --> 02:58:17,179
is not equal to null
4002
02:58:17,939 --> 02:58:21,779
then I'm gonna call our variable a text
4003
02:58:21,779 --> 02:58:23,880
to
4004
02:58:23,880 --> 02:58:27,720
and we're gonna assign a text to it
4005
02:58:27,720 --> 02:58:30,199
else
4006
02:58:32,640 --> 02:58:35,160
we're going to assign to our text to
4007
02:58:35,160 --> 02:58:38,340
variable the text
4008
02:58:38,340 --> 02:58:42,899
this variable is null so this is the
4009
02:58:42,899 --> 02:58:45,180
longer where we're doing of what we did
4010
02:58:45,180 --> 02:58:47,520
previously so if I press Ctrl Delta
4011
02:58:47,520 --> 02:58:50,960
format of code now if you run this
4012
02:58:53,399 --> 02:58:56,220
you'll see the the same amount of the
4013
02:58:56,220 --> 02:58:58,620
variable is not null
4014
02:58:58,620 --> 02:59:00,540
so we get a variables not now and this
4015
02:59:00,540 --> 02:59:02,100
is the longer way of doing the same
4016
02:59:02,100 --> 02:59:06,060
thing so if I assign here
4017
02:59:06,060 --> 02:59:08,520
if I assign actually if I delete this
4018
02:59:08,520 --> 02:59:10,560
and the variable will have null
4019
02:59:10,560 --> 02:59:13,760
let's press Ctrl Z
4020
02:59:19,080 --> 02:59:22,039
if I run this
4021
02:59:22,260 --> 02:59:23,479
foreign
4022
02:59:23,479 --> 02:59:27,180
part is executed so our variable takes
4023
02:59:27,180 --> 02:59:29,819
two it will be it will have the thesis
4024
02:59:29,819 --> 02:59:31,740
variable this variable is null that
4025
02:59:31,740 --> 02:59:33,120
takes assigned to it and then it's going
4026
02:59:33,120 --> 02:59:36,120
to Output that in the console so this is
4027
02:59:36,120 --> 02:59:38,640
the longer I was doing what was of what
4028
02:59:38,640 --> 02:59:40,979
we had previously so if I press Ctrl Z
4029
02:59:40,979 --> 02:59:43,500
to have the Elvis operator back
4030
02:59:43,500 --> 02:59:46,080
so that was the longer way of doing what
4031
02:59:46,080 --> 02:59:48,540
of what you're doing here in one simple
4032
02:59:48,540 --> 02:59:49,859
line of code
4033
02:59:49,859 --> 02:59:53,160
so this is our discussion about nullable
4034
02:59:53,160 --> 02:59:56,700
types and uh I hope that you got a lot
4035
02:59:56,700 --> 02:59:58,979
from this video and see you in the next
4036
02:59:58,979 --> 02:59:59,760
video
4037
02:59:59,760 --> 03:00:01,500
right now it's time to start a
4038
03:00:01,500 --> 03:00:03,180
discussion about functions but first
4039
03:00:03,180 --> 03:00:04,979
let's create a new project and I'm going
4040
03:00:04,979 --> 03:00:06,479
to speed up the process now a little bit
4041
03:00:06,479 --> 03:00:08,760
because we already did this a few times
4042
03:00:08,760 --> 03:00:10,859
so I'm gonna call it functions should be
4043
03:00:10,859 --> 03:00:12,540
in Pascal case
4044
03:00:12,540 --> 03:00:14,819
select IntelliJ click on next and click
4045
03:00:14,819 --> 03:00:17,000
finish
4046
03:00:20,340 --> 03:00:21,660
all right let's delete the code inside
4047
03:00:21,660 --> 03:00:24,560
the curly braces
4048
03:00:24,660 --> 03:00:27,420
let's hide the project Pane and let's
4049
03:00:27,420 --> 03:00:29,279
start by saying that until now we have
4050
03:00:29,279 --> 03:00:31,140
written our code only inside the main
4051
03:00:31,140 --> 03:00:34,080
function and occasionally we have called
4052
03:00:34,080 --> 03:00:36,180
The Print line function to Output some
4053
03:00:36,180 --> 03:00:39,540
code to the console but as I said at the
4054
03:00:39,540 --> 03:00:42,240
beginning of our videos you can create
4055
03:00:42,240 --> 03:00:44,760
your own functions in kotlin so let's
4056
03:00:44,760 --> 03:00:47,460
see how we can do that so go down here
4057
03:00:47,460 --> 03:00:49,620
at the end of our main function at the
4058
03:00:49,620 --> 03:00:52,020
end of our enclosing curly brace
4059
03:00:52,020 --> 03:00:54,060
and to declare the function we type the
4060
03:00:54,060 --> 03:00:56,840
keyword fun
4061
03:00:57,960 --> 03:01:00,120
we put space next we need to give a name
4062
03:01:00,120 --> 03:01:01,800
to our function and the name of the
4063
03:01:01,800 --> 03:01:03,420
function should say what the function is
4064
03:01:03,420 --> 03:01:05,640
doing and generally it's a verb I'm
4065
03:01:05,640 --> 03:01:08,220
going to call this function say hello
4066
03:01:08,220 --> 03:01:10,680
and it should be in camera case next you
4067
03:01:10,680 --> 03:01:13,319
put parenthesis because functions can
4068
03:01:13,319 --> 03:01:15,060
take input to work with it
4069
03:01:15,060 --> 03:01:17,640
and next you put
4070
03:01:17,640 --> 03:01:20,279
curly braces because this is what we're
4071
03:01:20,279 --> 03:01:21,560
going to put our
4072
03:01:21,560 --> 03:01:23,819
instructions for our statements this is
4073
03:01:23,819 --> 03:01:26,819
going to put our logic now let's add the
4074
03:01:26,819 --> 03:01:31,160
print Elena statement here
4075
03:01:31,380 --> 03:01:33,560
foreign
4076
03:01:38,660 --> 03:01:42,619
and now let's run this code
4077
03:01:46,040 --> 03:01:47,640
now
4078
03:01:47,640 --> 03:01:50,100
as the input we don't see nothing here
4079
03:01:50,100 --> 03:01:52,020
because if you hover this say hello
4080
03:01:52,020 --> 03:01:54,000
function it says
4081
03:01:54,000 --> 03:01:56,640
function say hello is never used because
4082
03:01:56,640 --> 03:01:59,040
in order for that this in order for this
4083
03:01:59,040 --> 03:02:01,620
function to be used you need to call it
4084
03:02:01,620 --> 03:02:03,359
from main function and to call it from a
4085
03:02:03,359 --> 03:02:06,000
function we type here
4086
03:02:06,000 --> 03:02:08,960
the name of the function you can type it
4087
03:02:08,960 --> 03:02:12,420
entirely you or you can type start
4088
03:02:12,420 --> 03:02:13,859
typing you know IntelliJ is going to be
4089
03:02:13,859 --> 03:02:15,600
it's going to give you some suggestions
4090
03:02:15,600 --> 03:02:17,819
so press enter to fill and fill in that
4091
03:02:17,819 --> 03:02:22,319
video and also press Ctrl alt L to
4092
03:02:22,319 --> 03:02:24,720
format the code
4093
03:02:24,720 --> 03:02:28,099
now let's run our code
4094
03:02:32,580 --> 03:02:35,939
now you're gonna see that it says in the
4095
03:02:35,939 --> 03:02:38,700
output hello because the main function
4096
03:02:38,700 --> 03:02:40,859
executes in what's inside the curly
4097
03:02:40,859 --> 03:02:44,100
braces and uh on the line two it sees
4098
03:02:44,100 --> 03:02:46,260
that we have a function that we're
4099
03:02:46,260 --> 03:02:48,660
calling here a function executes it
4100
03:02:48,660 --> 03:02:51,180
executes the the say hello function the
4101
03:02:51,180 --> 03:02:53,460
code inside the say hello function in
4102
03:02:53,460 --> 03:02:55,500
this case it's a simple printl and a
4103
03:02:55,500 --> 03:02:58,380
statement and then it outputs that to
4104
03:02:58,380 --> 03:03:01,319
the console and it acts as the
4105
03:03:01,319 --> 03:03:05,340
the code it finishes the process because
4106
03:03:05,340 --> 03:03:07,500
you don't have anything else below for
4107
03:03:07,500 --> 03:03:09,720
say hello but what we can do is put
4108
03:03:09,720 --> 03:03:11,939
another say hello here to call our
4109
03:03:11,939 --> 03:03:13,979
function again and now we're gonna see
4110
03:03:13,979 --> 03:03:16,380
the output
4111
03:03:16,380 --> 03:03:18,899
hello two times because now we're
4112
03:03:18,899 --> 03:03:20,939
calling the say hello function
4113
03:03:20,939 --> 03:03:23,160
first on the line two and second the
4114
03:03:23,160 --> 03:03:25,680
line three so it will be executed two
4115
03:03:25,680 --> 03:03:28,439
times so you can see that in this wave
4116
03:03:28,439 --> 03:03:30,720
functions are reusable
4117
03:03:30,720 --> 03:03:33,240
and are separate chunks of code okay
4118
03:03:33,240 --> 03:03:36,060
which can be can be reused and you'll
4119
03:03:36,060 --> 03:03:38,040
see that they can be used in specific
4120
03:03:38,040 --> 03:03:40,979
cases now
4121
03:03:40,979 --> 03:03:43,800
as I said functions can take input and
4122
03:03:43,800 --> 03:03:46,380
to make a function to receive some input
4123
03:03:46,380 --> 03:03:48,180
we need to Define what is called the
4124
03:03:48,180 --> 03:03:50,520
parameter and to define a parameter we
4125
03:03:50,520 --> 03:03:52,920
type the name of the variable first I'm
4126
03:03:52,920 --> 03:03:54,540
going to call it name
4127
03:03:54,540 --> 03:03:57,120
and we need to explicitly provide the
4128
03:03:57,120 --> 03:03:58,680
type here
4129
03:03:58,680 --> 03:04:00,479
so for the type I'm going to choose
4130
03:04:00,479 --> 03:04:02,939
string and we need to explicitly provide
4131
03:04:02,939 --> 03:04:04,979
a type here because is there there is no
4132
03:04:04,979 --> 03:04:06,960
way for the type to be inferred here
4133
03:04:06,960 --> 03:04:09,420
because uh
4134
03:04:09,420 --> 03:04:11,939
the value that you're gonna pass to this
4135
03:04:11,939 --> 03:04:14,520
say hello function is going to be here
4136
03:04:14,520 --> 03:04:17,819
so it's impossible to know to what the
4137
03:04:17,819 --> 03:04:20,160
kind of value are we going to pass to it
4138
03:04:20,160 --> 03:04:22,680
so this is why you need to explicitly
4139
03:04:22,680 --> 03:04:26,100
say what data this parameter or this
4140
03:04:26,100 --> 03:04:27,720
variable is going to store so let's
4141
03:04:27,720 --> 03:04:30,720
delete this second say hello
4142
03:04:30,720 --> 03:04:32,279
now
4143
03:04:32,279 --> 03:04:34,620
let's change the text that I have here
4144
03:04:34,620 --> 03:04:37,439
in the println Let's uh
4145
03:04:37,439 --> 03:04:40,560
let's refer the parameter name so put a
4146
03:04:40,560 --> 03:04:43,439
dollar sign and start typing name
4147
03:04:43,439 --> 03:04:45,240
and you can see that has this P4
4148
03:04:45,240 --> 03:04:48,420
parameter and press enter now what is
4149
03:04:48,420 --> 03:04:51,060
going to happen now is that this say
4150
03:04:51,060 --> 03:04:53,580
hello function is going to be called
4151
03:04:53,580 --> 03:04:55,319
it's going to be executed from our main
4152
03:04:55,319 --> 03:04:56,520
function
4153
03:04:56,520 --> 03:05:00,000
and the code inside the say hello
4154
03:05:00,000 --> 03:05:01,740
functions is also going to be executed
4155
03:05:01,740 --> 03:05:03,840
and specifically it's going to execute
4156
03:05:03,840 --> 03:05:07,680
this uh this uh text that we type here
4157
03:05:07,680 --> 03:05:09,660
to be outputted it's going to execute
4158
03:05:09,660 --> 03:05:11,760
the hello and then it is going to
4159
03:05:11,760 --> 03:05:15,899
execute uh what what value if we have
4160
03:05:15,899 --> 03:05:17,220
passed
4161
03:05:17,220 --> 03:05:21,660
as an argument for this parameter and uh
4162
03:05:21,660 --> 03:05:24,060
if you don't know what an argument is
4163
03:05:24,060 --> 03:05:25,920
the argument is the value that you pass
4164
03:05:25,920 --> 03:05:27,479
to the
4165
03:05:27,479 --> 03:05:28,260
um
4166
03:05:28,260 --> 03:05:31,260
to the function so in this case up here
4167
03:05:31,260 --> 03:05:33,240
we need to pass
4168
03:05:33,240 --> 03:05:35,580
some text so I'm going to put the name
4169
03:05:35,580 --> 03:05:36,720
here
4170
03:05:36,720 --> 03:05:39,438
not here
4171
03:05:41,279 --> 03:05:44,399
it should be in a double rotation marks
4172
03:05:44,399 --> 03:05:46,020
and you see that we have a hint here
4173
03:05:46,020 --> 03:05:48,660
which says which is which says the name
4174
03:05:48,660 --> 03:05:50,580
of the very of the name of the parameter
4175
03:05:50,580 --> 03:05:52,979
which is name now what is going to
4176
03:05:52,979 --> 03:05:55,020
happen now is this this say hello
4177
03:05:55,020 --> 03:05:56,720
function is going to be called
4178
03:05:56,720 --> 03:06:00,660
and this value that we passed here is
4179
03:06:00,660 --> 03:06:03,479
going to be used in our println
4180
03:06:03,479 --> 03:06:06,740
statement where where
4181
03:06:06,740 --> 03:06:10,380
we're referring now which is uh here you
4182
03:06:10,380 --> 03:06:12,779
here we're using the name and it's going
4183
03:06:12,779 --> 03:06:14,040
to use that value and that value is
4184
03:06:14,040 --> 03:06:15,540
going to be outputted down here and
4185
03:06:15,540 --> 03:06:18,620
let's run the code to say
4186
03:06:21,960 --> 03:06:25,080
now get Hello Alex because now it's
4187
03:06:25,080 --> 03:06:27,540
using the value that you pass here as
4188
03:06:27,540 --> 03:06:30,180
the as an argument to the parameter name
4189
03:06:30,180 --> 03:06:34,380
and it's using that value down here
4190
03:06:34,380 --> 03:06:35,700
where a
4191
03:06:35,700 --> 03:06:38,760
we type the printerland statement or
4192
03:06:38,760 --> 03:06:42,000
function now functions can take multiple
4193
03:06:42,000 --> 03:06:45,540
arguments multiple arguments and to do
4194
03:06:45,540 --> 03:06:47,460
that we need to Define
4195
03:06:47,460 --> 03:06:50,220
I should say that you can Define more
4196
03:06:50,220 --> 03:06:52,859
parameters for the function and a
4197
03:06:52,859 --> 03:06:55,020
function contain multiple arguments and
4198
03:06:55,020 --> 03:06:57,479
to Define another parameter
4199
03:06:57,479 --> 03:07:00,560
we put the
4200
03:07:01,800 --> 03:07:03,420
comma here
4201
03:07:03,420 --> 03:07:07,500
and we type the name of our parameter is
4202
03:07:07,500 --> 03:07:09,960
going to be age and we need to provide
4203
03:07:09,960 --> 03:07:11,640
the type and I'm going to provide the
4204
03:07:11,640 --> 03:07:13,560
type as anything yet because as an
4205
03:07:13,560 --> 03:07:15,000
integer because it's going to store
4206
03:07:15,000 --> 03:07:16,620
whole numbers
4207
03:07:16,620 --> 03:07:17,819
and
4208
03:07:17,819 --> 03:07:20,399
you're gonna put an exclamation mark and
4209
03:07:20,399 --> 03:07:23,720
I'm going to type your
4210
03:07:25,800 --> 03:07:28,140
age is now we need to refer the
4211
03:07:28,140 --> 03:07:30,120
parameter age and
4212
03:07:30,120 --> 03:07:32,040
also need to provide the value for that
4213
03:07:32,040 --> 03:07:34,580
parameter
4214
03:07:35,580 --> 03:07:37,500
now if you go up here now it says that
4215
03:07:37,500 --> 03:07:40,319
you need to provide a value for the
4216
03:07:40,319 --> 03:07:42,779
parameter has age it says no value pass
4217
03:07:42,779 --> 03:07:45,000
from for the parameter a so here you
4218
03:07:45,000 --> 03:07:47,220
need to type on a value for our
4219
03:07:47,220 --> 03:07:49,260
parameter that we Define so let's say
4220
03:07:49,260 --> 03:07:54,240
22. all right now if you run this code
4221
03:07:54,240 --> 03:07:57,479
now those parameters that we Define here
4222
03:07:57,479 --> 03:08:00,060
are going to use the values the argument
4223
03:08:00,060 --> 03:08:04,740
that we type here inside the our
4224
03:08:04,740 --> 03:08:06,899
curly braces or
4225
03:08:06,899 --> 03:08:09,540
how it's often called the body of the
4226
03:08:09,540 --> 03:08:11,580
function or the code block so it's going
4227
03:08:11,580 --> 03:08:15,240
to use the values here and here
4228
03:08:15,240 --> 03:08:17,460
now another important thing to know is
4229
03:08:17,460 --> 03:08:20,040
that you can not change the value of a
4230
03:08:20,040 --> 03:08:22,560
parameter so if you type here age equals
4231
03:08:22,560 --> 03:08:25,160
let's say
4232
03:08:25,399 --> 03:08:27,000
30.
4233
03:08:27,000 --> 03:08:29,040
not gonna if you hover over here because
4234
03:08:29,040 --> 03:08:31,620
you have an underline it says Val cannot
4235
03:08:31,620 --> 03:08:34,439
be reassigned because those parameters
4236
03:08:34,439 --> 03:08:37,800
are are declared as valves as variables
4237
03:08:37,800 --> 03:08:39,359
false so that means that they are
4238
03:08:39,359 --> 03:08:41,279
immutable we cannot change their values
4239
03:08:41,279 --> 03:08:44,760
the only way to use the variable which
4240
03:08:44,760 --> 03:08:47,399
can have is value changes to declare on
4241
03:08:47,399 --> 03:08:49,859
so if you declare here a variable I'm
4242
03:08:49,859 --> 03:08:51,720
going to declare it as a VAR to change
4243
03:08:51,720 --> 03:08:54,300
this value I'm going to call it number
4244
03:08:54,300 --> 03:08:57,120
and if I
4245
03:08:57,120 --> 03:09:01,500
assigning healer value of 50 now I
4246
03:09:01,500 --> 03:09:03,359
as you know you can change now his value
4247
03:09:03,359 --> 03:09:06,479
to let's say 70.
4248
03:09:06,479 --> 03:09:09,660
and you can also assign the
4249
03:09:09,660 --> 03:09:11,460
parameter here
4250
03:09:11,460 --> 03:09:13,859
to our variable number and now we can
4251
03:09:13,859 --> 03:09:17,340
change its value because input number
4252
03:09:17,340 --> 03:09:19,500
and can put another value here
4253
03:09:19,500 --> 03:09:22,040
all right
4254
03:09:22,140 --> 03:09:25,500
another important thing to know is that
4255
03:09:25,500 --> 03:09:28,859
this variable that we Define here also
4256
03:09:28,859 --> 03:09:32,640
the parameters are scoped inside the
4257
03:09:32,640 --> 03:09:35,520
function say hello in other words they
4258
03:09:35,520 --> 03:09:38,460
cannot be referred or accessed outside
4259
03:09:38,460 --> 03:09:40,740
of the calibration of the say hello
4260
03:09:40,740 --> 03:09:44,160
function so if I try to access that
4261
03:09:44,160 --> 03:09:47,160
number variable here
4262
03:09:47,160 --> 03:09:49,140
is not working
4263
03:09:49,140 --> 03:09:51,960
if I try to access access here on the
4264
03:09:51,960 --> 03:09:54,240
main function let's say type number
4265
03:09:54,240 --> 03:09:57,359
I can because it's scoped inside the say
4266
03:09:57,359 --> 03:10:00,720
hello function it exists only inside the
4267
03:10:00,720 --> 03:10:02,760
say hello function the same thing with
4268
03:10:02,760 --> 03:10:05,460
the parameters age
4269
03:10:05,460 --> 03:10:09,319
or a name
4270
03:10:09,359 --> 03:10:10,740
so
4271
03:10:10,740 --> 03:10:12,660
in this way you can see that functions
4272
03:10:12,660 --> 03:10:16,800
can be very useful because they are
4273
03:10:16,800 --> 03:10:21,540
chunks of code which are reusable and
4274
03:10:21,540 --> 03:10:25,380
flexible and
4275
03:10:25,500 --> 03:10:27,899
to see an example with this let's
4276
03:10:27,899 --> 03:10:29,880
declare another function down here let's
4277
03:10:29,880 --> 03:10:32,640
call it the
4278
03:10:32,640 --> 03:10:34,979
let's type the fun key or let's call it
4279
03:10:34,979 --> 03:10:36,660
get data
4280
03:10:36,660 --> 03:10:39,779
and this function is going to mimic the
4281
03:10:39,779 --> 03:10:42,240
real function in a real app which can
4282
03:10:42,240 --> 03:10:43,680
get some data from the internet of
4283
03:10:43,680 --> 03:10:44,880
course that you're not going to get some
4284
03:10:44,880 --> 03:10:47,819
other internet but we're gonna uh mimic
4285
03:10:47,819 --> 03:10:50,220
that behavior so here I'm gonna Define a
4286
03:10:50,220 --> 03:10:53,279
parameter let's call it data and for the
4287
03:10:53,279 --> 03:10:55,800
type let's choose string right
4288
03:10:55,800 --> 03:10:58,920
let's put the curly braces and here
4289
03:10:58,920 --> 03:11:03,000
let's add the print Ln segments and here
4290
03:11:03,000 --> 03:11:05,479
you're gonna print
4291
03:11:06,120 --> 03:11:09,240
your data is
4292
03:11:09,240 --> 03:11:10,680
and
4293
03:11:10,680 --> 03:11:15,740
I'm gonna refer the parameter data
4294
03:11:16,439 --> 03:11:19,560
all right now what I can do up here is
4295
03:11:19,560 --> 03:11:22,439
to define a Boolean variable it's going
4296
03:11:22,439 --> 03:11:25,200
to be a Val I'm going to call it has
4297
03:11:25,200 --> 03:11:27,680
internet
4298
03:11:28,319 --> 03:11:30,180
connection I'm going to assign a value
4299
03:11:30,180 --> 03:11:32,460
of true to it
4300
03:11:32,460 --> 03:11:35,819
now I can put an if statement here now I
4301
03:11:35,819 --> 03:11:39,560
can check to see if that
4302
03:11:39,560 --> 03:11:42,840
expression is true so I can put TV
4303
03:11:42,840 --> 03:11:45,420
equals true or
4304
03:11:45,420 --> 03:11:48,540
use the shortcut because it's better
4305
03:11:48,540 --> 03:11:51,660
here I'm going to put the early braces
4306
03:11:51,660 --> 03:11:56,580
and now I'm gonna call the get data
4307
03:11:56,580 --> 03:12:00,660
function only if this condition is true
4308
03:12:00,660 --> 03:12:03,540
so in our case it's true the but in a
4309
03:12:03,540 --> 03:12:06,000
real lab that can be can be false so in
4310
03:12:06,000 --> 03:12:07,859
that case you can do something else as
4311
03:12:07,859 --> 03:12:09,359
you can assume we're gonna let's also
4312
03:12:09,359 --> 03:12:11,460
other else part here
4313
03:12:11,460 --> 03:12:13,200
and here we're gonna call another
4314
03:12:13,200 --> 03:12:15,060
function which can which is going to be
4315
03:12:15,060 --> 03:12:17,340
called in the case that there is no
4316
03:12:17,340 --> 03:12:20,460
internet connection all right so
4317
03:12:20,460 --> 03:12:22,439
above here you can personal logic to get
4318
03:12:22,439 --> 03:12:23,760
the data from the internet well because
4319
03:12:23,760 --> 03:12:25,800
this is a simple example gonna type here
4320
03:12:25,800 --> 03:12:28,699
directly
4321
03:12:30,260 --> 03:12:33,720
some text let's put some data
4322
03:12:33,720 --> 03:12:36,300
and uh for the else part in the case
4323
03:12:36,300 --> 03:12:37,800
that there is no internet connection
4324
03:12:37,800 --> 03:12:39,960
that variable is false we can declare
4325
03:12:39,960 --> 03:12:41,819
another function down here
4326
03:12:41,819 --> 03:12:46,519
to oh and I'm going to call it fun
4327
03:12:47,279 --> 03:12:49,880
show
4328
03:12:50,279 --> 03:12:52,859
message
4329
03:12:52,859 --> 03:12:55,319
and this is not gonna have any
4330
03:12:55,319 --> 03:12:57,479
parameters it's gonna have only simple
4331
03:12:57,479 --> 03:12:59,460
println statement
4332
03:12:59,460 --> 03:13:02,700
and it's going to say that uh
4333
03:13:02,700 --> 03:13:04,500
there is no
4334
03:13:04,500 --> 03:13:06,979
internet
4335
03:13:11,700 --> 03:13:13,920
connection
4336
03:13:13,920 --> 03:13:16,920
all right so now let's go up here now
4337
03:13:16,920 --> 03:13:19,439
and now we're gonna call
4338
03:13:19,439 --> 03:13:22,160
the
4339
03:13:23,460 --> 03:13:26,760
show message function
4340
03:13:26,760 --> 03:13:28,260
in the else part
4341
03:13:28,260 --> 03:13:31,640
now if you run this code
4342
03:13:38,399 --> 03:13:39,840
you're gonna see
4343
03:13:39,840 --> 03:13:42,960
Hello Alex which is our first function
4344
03:13:42,960 --> 03:13:46,080
up here on the line tool your age is 22
4345
03:13:46,080 --> 03:13:49,080
and next to see your data is some data
4346
03:13:49,080 --> 03:13:52,740
because it executes the say hello
4347
03:13:52,740 --> 03:13:54,840
function then it declares this variable
4348
03:13:54,840 --> 03:13:58,680
and it checks it checks to see if this
4349
03:13:58,680 --> 03:14:00,840
condition is true and this condition is
4350
03:14:00,840 --> 03:14:04,080
true and it executes this function get
4351
03:14:04,080 --> 03:14:07,620
data so this is why you get your data is
4352
03:14:07,620 --> 03:14:11,399
some data but if for whatever reason the
4353
03:14:11,399 --> 03:14:13,740
user has no internet connection let's
4354
03:14:13,740 --> 03:14:16,439
say you assign a value false to it
4355
03:14:16,439 --> 03:14:19,220
to this
4356
03:14:19,260 --> 03:14:21,420
now this function is going to be called
4357
03:14:21,420 --> 03:14:24,020
show message
4358
03:14:28,140 --> 03:14:30,720
so this function show message now is
4359
03:14:30,720 --> 03:14:32,340
called and the code inside the show
4360
03:14:32,340 --> 03:14:35,100
message function is called which is
4361
03:14:35,100 --> 03:14:36,840
printerland with this sprinterland
4362
03:14:36,840 --> 03:14:38,279
standard which says there is one
4363
03:14:38,279 --> 03:14:39,660
technical connection area lab you can
4364
03:14:39,660 --> 03:14:43,200
have some logic to show a dialogue that
4365
03:14:43,200 --> 03:14:45,540
there is no internet connection and you
4366
03:14:45,540 --> 03:14:47,460
can see that in this way with functions
4367
03:14:47,460 --> 03:14:49,260
you can divide your
4368
03:14:49,260 --> 03:14:52,979
code in different chunks of
4369
03:14:52,979 --> 03:14:55,620
different different chunks of code which
4370
03:14:55,620 --> 03:15:00,060
can be called in very specific cases so
4371
03:15:00,060 --> 03:15:01,460
in this case
4372
03:15:01,460 --> 03:15:05,819
we only call our functions only in in
4373
03:15:05,819 --> 03:15:06,899
the case that we have internet
4374
03:15:06,899 --> 03:15:09,000
connection or in the case that we don't
4375
03:15:09,000 --> 03:15:11,479
have internet connection so in this way
4376
03:15:11,479 --> 03:15:14,520
we have divided our code in different
4377
03:15:14,520 --> 03:15:17,340
chunks of code which can be called in
4378
03:15:17,340 --> 03:15:21,420
different uh specific scenarios and in
4379
03:15:21,420 --> 03:15:22,580
this way
4380
03:15:22,580 --> 03:15:25,800
we can write a program because if you
4381
03:15:25,800 --> 03:15:28,080
just put your code all of your code in
4382
03:15:28,080 --> 03:15:29,760
the main function and you execute that
4383
03:15:29,760 --> 03:15:33,180
code from top to bottom you you cannot
4384
03:15:33,180 --> 03:15:36,600
create a program like that so this was
4385
03:15:36,600 --> 03:15:39,000
an additional discussion with the get
4386
03:15:39,000 --> 03:15:41,520
data and show message and I hope that
4387
03:15:41,520 --> 03:15:44,520
I'm not confused you a lot so see in the
4388
03:15:44,520 --> 03:15:46,880
next video
4389
03:15:46,979 --> 03:15:49,620
all right so in the last video we've
4390
03:15:49,620 --> 03:15:52,020
seen how we can create functions how we
4391
03:15:52,020 --> 03:15:54,120
can call our functions how we can Define
4392
03:15:54,120 --> 03:15:58,439
parameters to our functions how can send
4393
03:15:58,439 --> 03:16:00,960
the data to our function as arguments
4394
03:16:00,960 --> 03:16:02,880
now it's time to see how we can return
4395
03:16:02,880 --> 03:16:04,740
some data from a function but let's
4396
03:16:04,740 --> 03:16:07,319
first read the code that we have here
4397
03:16:07,319 --> 03:16:10,380
to make things more clear
4398
03:16:10,380 --> 03:16:13,200
and the code inside the function the
4399
03:16:13,200 --> 03:16:15,620
main function
4400
03:16:18,000 --> 03:16:20,040
all right now let's
4401
03:16:20,040 --> 03:16:22,920
Define another function I'm going to
4402
03:16:22,920 --> 03:16:24,960
type the fan keyword space and it's
4403
03:16:24,960 --> 03:16:27,240
going to be called get Max
4404
03:16:27,240 --> 03:16:29,880
and this function is going to return the
4405
03:16:29,880 --> 03:16:34,080
maximum of uh two integers
4406
03:16:34,080 --> 03:16:37,439
so I'm gonna Define here a parameter I'm
4407
03:16:37,439 --> 03:16:39,000
going to call it a in a real aperture
4408
03:16:39,000 --> 03:16:40,920
those more descriptive names
4409
03:16:40,920 --> 03:16:43,560
is going to be of type int and the next
4410
03:16:43,560 --> 03:16:45,660
one is going to be called B and it's
4411
03:16:45,660 --> 03:16:49,580
going to be also of type int
4412
03:16:49,920 --> 03:16:52,020
I'm gonna put the
4413
03:16:52,020 --> 03:16:53,880
curly braces and I'm going to press
4414
03:16:53,880 --> 03:16:56,119
enter
4415
03:16:56,279 --> 03:17:00,120
now to get the maximum value between
4416
03:17:00,120 --> 03:17:02,939
these two numbers we type here we have
4417
03:17:02,939 --> 03:17:04,200
here Val
4418
03:17:04,200 --> 03:17:06,660
Max
4419
03:17:06,660 --> 03:17:09,300
and here we type an if expression so we
4420
03:17:09,300 --> 03:17:11,840
type if
4421
03:17:12,420 --> 03:17:13,979
a
4422
03:17:13,979 --> 03:17:16,680
greater than b
4423
03:17:16,680 --> 03:17:20,100
then we're going to assign a to our Max
4424
03:17:20,100 --> 03:17:22,620
variable else
4425
03:17:22,620 --> 03:17:24,960
I'm going to sign B and in the case that
4426
03:17:24,960 --> 03:17:27,479
the two numbers are equal B is going to
4427
03:17:27,479 --> 03:17:28,800
be assigned to our
4428
03:17:28,800 --> 03:17:30,720
Max
4429
03:17:30,720 --> 03:17:33,000
variable
4430
03:17:33,000 --> 03:17:36,779
now in order to return this uh to in
4431
03:17:36,779 --> 03:17:38,760
order to return this
4432
03:17:38,760 --> 03:17:43,399
value to our function we need first to
4433
03:17:43,399 --> 03:17:46,500
to say to the function what kind of data
4434
03:17:46,500 --> 03:17:49,200
is going to return and to do that go
4435
03:17:49,200 --> 03:17:52,800
here at the enclosing parenthesis and if
4436
03:17:52,800 --> 03:17:55,880
you don't have spaces here or you
4437
03:17:55,880 --> 03:17:58,859
wondering about how to
4438
03:17:58,859 --> 03:18:01,080
have the proper formatting you just
4439
03:18:01,080 --> 03:18:04,140
press Ctrl alt L and the code is going
4440
03:18:04,140 --> 03:18:06,240
to be formatted all right and here we
4441
03:18:06,240 --> 03:18:07,800
put a colon
4442
03:18:07,800 --> 03:18:11,580
and here we we Define why what type of
4443
03:18:11,580 --> 03:18:13,140
data this function is going to return
4444
03:18:13,140 --> 03:18:14,520
and in this case it's going to be an
4445
03:18:14,520 --> 03:18:17,100
integer so we put in here
4446
03:18:17,100 --> 03:18:19,020
and this is similar to The Way We
4447
03:18:19,020 --> 03:18:21,660
declare a type for a variable now
4448
03:18:21,660 --> 03:18:23,399
if you could if you go down here now you
4449
03:18:23,399 --> 03:18:24,720
see that it says
4450
03:18:24,720 --> 03:18:26,700
a return expression is required in a
4451
03:18:26,700 --> 03:18:28,859
function to block body so now we need to
4452
03:18:28,859 --> 03:18:31,200
return this uh
4453
03:18:31,200 --> 03:18:33,660
now you need to return some data that we
4454
03:18:33,660 --> 03:18:35,460
Define here in this case we need to
4455
03:18:35,460 --> 03:18:39,359
return an integer and to return the
4456
03:18:39,359 --> 03:18:41,460
maximum value to our function we type
4457
03:18:41,460 --> 03:18:44,100
here return the keyword return
4458
03:18:44,100 --> 03:18:47,040
and our value which is our variable Max
4459
03:18:47,040 --> 03:18:49,740
and press enter
4460
03:18:49,740 --> 03:18:52,020
now if you call this function in our
4461
03:18:52,020 --> 03:18:54,060
main function
4462
03:18:54,060 --> 03:18:57,540
get Max and now let's also press Ctrl
4463
03:18:57,540 --> 03:18:59,580
alt L to format the code
4464
03:18:59,580 --> 03:19:02,819
and here we let's say that we type two
4465
03:19:02,819 --> 03:19:05,279
arguments for our parameters let's say
4466
03:19:05,279 --> 03:19:07,080
that put five
4467
03:19:07,080 --> 03:19:11,479
and nine now if you run this code
4468
03:19:21,479 --> 03:19:22,979
now you're gonna see in the output
4469
03:19:22,979 --> 03:19:26,279
nothing because uh in order to
4470
03:19:26,279 --> 03:19:28,859
in order to get this value need to
4471
03:19:28,859 --> 03:19:30,779
retain this value to capture this value
4472
03:19:30,779 --> 03:19:32,700
in a variable and to do that we Define a
4473
03:19:32,700 --> 03:19:34,920
variable in front of our get Max
4474
03:19:34,920 --> 03:19:38,640
function it's also be a Val
4475
03:19:38,640 --> 03:19:40,439
I'm going to call it Max
4476
03:19:40,439 --> 03:19:45,740
and we're going to assign the get Max
4477
03:19:47,040 --> 03:19:49,020
function to our variable and that is
4478
03:19:49,020 --> 03:19:50,399
going to return
4479
03:19:50,399 --> 03:19:53,340
or a maximum number
4480
03:19:53,340 --> 03:19:56,100
now you now you may see that you have
4481
03:19:56,100 --> 03:19:59,819
two names we have the same name here in
4482
03:19:59,819 --> 03:20:01,020
the same and that is not a problem
4483
03:20:01,020 --> 03:20:03,779
because this variable marks that we have
4484
03:20:03,779 --> 03:20:04,920
inside the
4485
03:20:04,920 --> 03:20:07,020
the get Max function exists on inside
4486
03:20:07,020 --> 03:20:09,479
the the get Max function is scoped only
4487
03:20:09,479 --> 03:20:12,060
here this is why you can use the same
4488
03:20:12,060 --> 03:20:15,420
name here so if I try to type here also
4489
03:20:15,420 --> 03:20:18,540
Max to declare variable which is going
4490
03:20:18,540 --> 03:20:22,500
to be called Max I can't because
4491
03:20:22,500 --> 03:20:25,080
it says completed declaration because if
4492
03:20:25,080 --> 03:20:25,859
you
4493
03:20:25,859 --> 03:20:28,020
because the compiler doesn't know if you
4494
03:20:28,020 --> 03:20:30,960
assign a value let's say to this it will
4495
03:20:30,960 --> 03:20:33,840
not know what variable to to to call
4496
03:20:33,840 --> 03:20:37,380
because you have the same name but uh
4497
03:20:37,380 --> 03:20:39,960
up here is not a problem because this is
4498
03:20:39,960 --> 03:20:42,300
in the block of the main function so it
4499
03:20:42,300 --> 03:20:44,279
exists only here and this exists only
4500
03:20:44,279 --> 03:20:46,500
here now
4501
03:20:46,500 --> 03:20:48,899
we have called our get Max function and
4502
03:20:48,899 --> 03:20:49,979
this function is going to return the
4503
03:20:49,979 --> 03:20:52,020
maximum of these two values which is
4504
03:20:52,020 --> 03:20:54,720
nine now we need to print this value not
4505
03:20:54,720 --> 03:20:56,399
console and to print this value just
4506
03:20:56,399 --> 03:20:58,200
type here println
4507
03:20:58,200 --> 03:21:00,660
and we're referring our
4508
03:21:00,660 --> 03:21:02,220
Max variable
4509
03:21:02,220 --> 03:21:05,479
now if you run this code
4510
03:21:10,740 --> 03:21:14,520
you're gonna get nine because uh our get
4511
03:21:14,520 --> 03:21:17,399
Max function is called the two the two
4512
03:21:17,399 --> 03:21:19,620
arguments are passed to our function
4513
03:21:19,620 --> 03:21:22,260
then the code inside the auto get Max
4514
03:21:22,260 --> 03:21:25,200
function it's executed it Compares these
4515
03:21:25,200 --> 03:21:28,740
two two numbers and if a is greater than
4516
03:21:28,740 --> 03:21:31,920
b then it assigns eight or Max
4517
03:21:31,920 --> 03:21:35,160
a Max variable or it's gonna if that is
4518
03:21:35,160 --> 03:21:38,040
not true is going to assign the value of
4519
03:21:38,040 --> 03:21:41,399
B and then it returns that max value to
4520
03:21:41,399 --> 03:21:44,460
the to the function which is called so
4521
03:21:44,460 --> 03:21:46,380
if this function returns that value here
4522
03:21:46,380 --> 03:21:50,399
and and here we retain we capture that
4523
03:21:50,399 --> 03:21:52,319
value which is returned from the get Max
4524
03:21:52,319 --> 03:21:54,660
function our Max variable and then we
4525
03:21:54,660 --> 03:21:58,680
print that Max variable with our println
4526
03:21:58,680 --> 03:22:01,140
function or a statement and then we see
4527
03:22:01,140 --> 03:22:05,119
the output in our console 9 right
4528
03:22:05,220 --> 03:22:07,380
another important thing to know is that
4529
03:22:07,380 --> 03:22:10,620
after we type the return call or the
4530
03:22:10,620 --> 03:22:13,260
return keyword the
4531
03:22:13,260 --> 03:22:16,380
the function is lift the function is
4532
03:22:16,380 --> 03:22:19,319
stopped there so if we add the
4533
03:22:19,319 --> 03:22:21,840
other text here let's say we add here a
4534
03:22:21,840 --> 03:22:23,700
printerland
4535
03:22:23,700 --> 03:22:27,840
and we type here some text
4536
03:22:27,840 --> 03:22:29,520
we see that this is highlighted and it
4537
03:22:29,520 --> 03:22:31,080
says
4538
03:22:31,080 --> 03:22:32,939
that is this is a reachable code because
4539
03:22:32,939 --> 03:22:34,979
after we typed our return marks our
4540
03:22:34,979 --> 03:22:37,080
function is
4541
03:22:37,080 --> 03:22:41,520
stopped is lived and it's not executing
4542
03:22:41,520 --> 03:22:45,560
any any code below of of that return
4543
03:22:45,560 --> 03:22:52,039
color or of that return keyword right
4544
03:22:52,319 --> 03:22:54,359
another important thing that you can do
4545
03:22:54,359 --> 03:22:56,580
with the return keyword is that you can
4546
03:22:56,580 --> 03:23:00,060
return from a function without returning
4547
03:23:00,060 --> 03:23:01,979
any kind of value and in that case it
4548
03:23:01,979 --> 03:23:05,220
will have the same effect and that means
4549
03:23:05,220 --> 03:23:07,979
that it will leave the function right
4550
03:23:07,979 --> 03:23:09,899
there even though we don't return any
4551
03:23:09,899 --> 03:23:11,939
kind of value so we I can type here
4552
03:23:11,939 --> 03:23:14,040
return but let's uh
4553
03:23:14,040 --> 03:23:16,500
the list here the type
4554
03:23:16,500 --> 03:23:18,779
because now it's expecting a value to be
4555
03:23:18,779 --> 03:23:20,760
returned and now this will have the same
4556
03:23:20,760 --> 03:23:22,500
effect it will leave the function right
4557
03:23:22,500 --> 03:23:24,600
there and it will not execute any any
4558
03:23:24,600 --> 03:23:27,359
kind any line of code below of our
4559
03:23:27,359 --> 03:23:29,819
return keyword so if I type here
4560
03:23:29,819 --> 03:23:32,520
a println statement
4561
03:23:32,520 --> 03:23:36,140
and I put some text here
4562
03:23:40,560 --> 03:23:42,300
as you can see this has the same effect
4563
03:23:42,300 --> 03:23:45,120
if you type the return keyword in your
4564
03:23:45,120 --> 03:23:48,060
function it will stop the execution of
4565
03:23:48,060 --> 03:23:50,279
the function and every line of code
4566
03:23:50,279 --> 03:23:52,380
below of the return keyword is not going
4567
03:23:52,380 --> 03:23:56,580
to be executed so it's very good to know
4568
03:23:56,580 --> 03:23:58,859
this also
4569
03:23:58,859 --> 03:24:01,140
another important thing to know is that
4570
03:24:01,140 --> 03:24:03,300
you can we can only return a single
4571
03:24:03,300 --> 03:24:05,220
value with the return keyword so we
4572
03:24:05,220 --> 03:24:07,680
cannot return here to two integers let's
4573
03:24:07,680 --> 03:24:11,460
say and also we cannot have two returns
4574
03:24:11,460 --> 03:24:13,800
in the same function let's say if I put
4575
03:24:13,800 --> 03:24:17,040
here also return marks
4576
03:24:17,040 --> 03:24:19,260
I can't because this is also a reachable
4577
03:24:19,260 --> 03:24:23,279
code but I I can have two returns
4578
03:24:23,279 --> 03:24:25,380
two returns calls if they are rich in
4579
03:24:25,380 --> 03:24:28,279
different branches so if I change this
4580
03:24:28,279 --> 03:24:31,800
if expression to an if
4581
03:24:31,800 --> 03:24:34,380
statement so if I type something like
4582
03:24:34,380 --> 03:24:35,279
this
4583
03:24:35,279 --> 03:24:37,500
if let's say
4584
03:24:37,500 --> 03:24:40,439
a greater than b
4585
03:24:40,439 --> 03:24:42,779
let's add the curly brace now
4586
03:24:42,779 --> 03:24:44,340
we type here
4587
03:24:44,340 --> 03:24:45,660
return
4588
03:24:45,660 --> 03:24:48,140
a
4589
03:24:50,700 --> 03:24:53,060
else
4590
03:24:53,819 --> 03:24:56,520
return B
4591
03:24:56,520 --> 03:24:59,640
so now we have two returns but they are
4592
03:24:59,640 --> 03:25:01,460
never reached together
4593
03:25:01,460 --> 03:25:04,500
they are they are always reached
4594
03:25:04,500 --> 03:25:06,540
separately so this is why you can use
4595
03:25:06,540 --> 03:25:08,819
here two returns because they said they
4596
03:25:08,819 --> 03:25:11,399
are rich always separately so if you run
4597
03:25:11,399 --> 03:25:13,140
this code you're gonna see the same
4598
03:25:13,140 --> 03:25:15,500
output
4599
03:25:17,600 --> 03:25:21,960
so you C9 so this is another way to to
4600
03:25:21,960 --> 03:25:23,220
write the same thing that we had
4601
03:25:23,220 --> 03:25:24,960
previously that but it's recommended to
4602
03:25:24,960 --> 03:25:27,180
use a
4603
03:25:27,180 --> 03:25:30,000
to use uh what we had previously because
4604
03:25:30,000 --> 03:25:33,840
with uh specifically it's better to have
4605
03:25:33,840 --> 03:25:35,939
enough expression
4606
03:25:35,939 --> 03:25:39,779
another way to to write to the same
4607
03:25:39,779 --> 03:25:42,180
thing but in a more concise way and
4608
03:25:42,180 --> 03:25:44,760
let's undo the code that we have that
4609
03:25:44,760 --> 03:25:46,560
you had previously
4610
03:25:46,560 --> 03:25:50,539
so let's press Ctrl Z
4611
03:25:54,899 --> 03:25:57,000
so now I have the code that help briefly
4612
03:25:57,000 --> 03:25:58,979
now if you hover over over this Max it
4613
03:25:58,979 --> 03:26:00,899
says variable use only in formatting
4614
03:26:00,899 --> 03:26:02,880
return can be in line so if you click
4615
03:26:02,880 --> 03:26:04,859
here in line variable
4616
03:26:04,859 --> 03:26:07,859
now this thing does the same thing that
4617
03:26:07,859 --> 03:26:11,819
it it was doing previously but this is a
4618
03:26:11,819 --> 03:26:13,319
shorter way to write the same thing so
4619
03:26:13,319 --> 03:26:17,279
now it returns a if this condition is
4620
03:26:17,279 --> 03:26:20,520
true otherwise else part the else part
4621
03:26:20,520 --> 03:26:23,160
is called and it returns B so we have
4622
03:26:23,160 --> 03:26:24,739
the same thing but
4623
03:26:24,739 --> 03:26:27,239
written in a shorter way
4624
03:26:27,239 --> 03:26:29,399
all right another way to write the same
4625
03:26:29,399 --> 03:26:31,439
thing that we have here is by using what
4626
03:26:31,439 --> 03:26:33,239
is called the single expression function
4627
03:26:33,239 --> 03:26:37,140
and to do that we can remove the curly
4628
03:26:37,140 --> 03:26:39,060
braces
4629
03:26:39,060 --> 03:26:41,220
we can put our code up here
4630
03:26:41,220 --> 03:26:43,800
you can remove the first curly brace and
4631
03:26:43,800 --> 03:26:48,119
you can also remove the return keyword
4632
03:26:49,979 --> 03:26:52,560
and here you put equals
4633
03:26:52,560 --> 03:26:55,319
and this now does the same thing that
4634
03:26:55,319 --> 03:26:57,479
the code
4635
03:26:57,479 --> 03:26:59,939
the previous code that was doing but
4636
03:26:59,939 --> 03:27:02,580
this is a more concise way and it's
4637
03:27:02,580 --> 03:27:05,819
recommended to use this only if you know
4638
03:27:05,819 --> 03:27:07,800
that you know you have in your function
4639
03:27:07,800 --> 03:27:11,160
a single expression or in other words if
4640
03:27:11,160 --> 03:27:12,600
you know that you have a single line of
4641
03:27:12,600 --> 03:27:14,700
code and you can also remove the type
4642
03:27:14,700 --> 03:27:17,040
here because the type can be being
4643
03:27:17,040 --> 03:27:18,180
inferred
4644
03:27:18,180 --> 03:27:21,000
and again only use this if you know that
4645
03:27:21,000 --> 03:27:23,460
you have in your function a single line
4646
03:27:23,460 --> 03:27:27,540
of code and in that case you can you can
4647
03:27:27,540 --> 03:27:30,840
skip the return keyword but if you have
4648
03:27:30,840 --> 03:27:33,300
multiple lines of code you should always
4649
03:27:33,300 --> 03:27:35,540
use the curly braces and the return
4650
03:27:35,540 --> 03:27:38,399
color the return keyword
4651
03:27:38,399 --> 03:27:40,140
all right so this is our discussion
4652
03:27:40,140 --> 03:27:42,300
about how we can return a value from a
4653
03:27:42,300 --> 03:27:43,560
function
4654
03:27:43,560 --> 03:27:45,420
all right so now let's continue a
4655
03:27:45,420 --> 03:27:46,979
discussion about functions and now it's
4656
03:27:46,979 --> 03:27:49,080
time to look at what is called function
4657
03:27:49,080 --> 03:27:51,660
overloading and let's say that I want
4658
03:27:51,660 --> 03:27:54,479
here to pass not to integer types but
4659
03:27:54,479 --> 03:27:56,880
two doubles to be compared so let's say
4660
03:27:56,880 --> 03:28:00,899
that type here 6.5
4661
03:28:01,200 --> 03:28:04,380
and 9.6
4662
03:28:04,380 --> 03:28:06,600
now this will not work because it's
4663
03:28:06,600 --> 03:28:08,100
defined here explicit that the
4664
03:28:08,100 --> 03:28:11,520
parameters are of type int so we cannot
4665
03:28:11,520 --> 03:28:12,920
pass here double
4666
03:28:12,920 --> 03:28:16,200
and a solution to this it may be to
4667
03:28:16,200 --> 03:28:18,120
create another function the different
4668
03:28:18,120 --> 03:28:21,359
name and to define the parameters as
4669
03:28:21,359 --> 03:28:25,260
doubles but that does not make a lot of
4670
03:28:25,260 --> 03:28:26,420
sense because
4671
03:28:26,420 --> 03:28:29,760
we we are doing the exact same thing so
4672
03:28:29,760 --> 03:28:32,399
does not make sense to create a new to
4673
03:28:32,399 --> 03:28:33,720
create a function with a different name
4674
03:28:33,720 --> 03:28:34,680
so
4675
03:28:34,680 --> 03:28:37,260
what you can do instead is to
4676
03:28:37,260 --> 03:28:40,260
let's change it back to integers
4677
03:28:40,260 --> 03:28:42,540
is to duplicate this get Max function
4678
03:28:42,540 --> 03:28:44,460
and do that you can press Ctrl D or
4679
03:28:44,460 --> 03:28:46,859
command line on Mac some press I'm going
4680
03:28:46,859 --> 03:28:48,540
to press Ctrl D here
4681
03:28:48,540 --> 03:28:50,880
now what I can do is change the types of
4682
03:28:50,880 --> 03:28:53,040
the variables so I can put here instead
4683
03:28:53,040 --> 03:28:57,680
of integer I put double
4684
03:28:59,279 --> 03:29:03,200
and here I can put also double
4685
03:29:05,399 --> 03:29:07,500
let's add the space to make things more
4686
03:29:07,500 --> 03:29:10,140
clear now if I pass here
4687
03:29:10,140 --> 03:29:14,160
let's say your 5.6
4688
03:29:14,160 --> 03:29:17,939
and 9.7 now you see that we have an
4689
03:29:17,939 --> 03:29:18,960
error
4690
03:29:18,960 --> 03:29:22,220
so if you run this code
4691
03:29:25,220 --> 03:29:28,500
all right so we get in the output 9.7
4692
03:29:28,500 --> 03:29:30,840
and this is called this uh is bigger
4693
03:29:30,840 --> 03:29:33,420
than 0.5 or 6 but now it's using the
4694
03:29:33,420 --> 03:29:35,460
second function which is the function
4695
03:29:35,460 --> 03:29:37,620
which has the parameters as of type
4696
03:29:37,620 --> 03:29:40,020
double and you can see that this
4697
03:29:40,020 --> 03:29:41,939
function now which is using the
4698
03:29:41,939 --> 03:29:45,420
parameters as integers is gray out
4699
03:29:45,420 --> 03:29:48,660
because the compiler can figure out what
4700
03:29:48,660 --> 03:29:52,020
function to use by the argument types we
4701
03:29:52,020 --> 03:29:54,200
pass here so in this case we pass here
4702
03:29:54,200 --> 03:29:58,920
doubles and it knows that it has to use
4703
03:29:58,920 --> 03:30:01,260
this function which has the types which
4704
03:30:01,260 --> 03:30:04,620
has the parameters Define as type of
4705
03:30:04,620 --> 03:30:06,660
double right
4706
03:30:06,660 --> 03:30:10,800
another way to overload the function is
4707
03:30:10,800 --> 03:30:14,720
to best also press Ctrl d
4708
03:30:15,060 --> 03:30:17,580
is by changing the number of parameters
4709
03:30:17,580 --> 03:30:21,359
so I can put here let's say
4710
03:30:21,359 --> 03:30:24,500
also integer
4711
03:30:29,100 --> 03:30:31,560
also releases so let's pass another the
4712
03:30:31,560 --> 03:30:33,960
third parameter now and this is also
4713
03:30:33,960 --> 03:30:35,580
going to be an integer it's going to be
4714
03:30:35,580 --> 03:30:36,540
called C
4715
03:30:36,540 --> 03:30:39,800
gonna be of typing
4716
03:30:40,680 --> 03:30:43,859
all right now let's change this logic to
4717
03:30:43,859 --> 03:30:46,680
any statement so let's delete the if
4718
03:30:46,680 --> 03:30:50,779
expression let's put the curly braces
4719
03:30:59,460 --> 03:31:02,960
all right so let's change this now
4720
03:31:04,739 --> 03:31:07,439
so you can type it here now if a greater
4721
03:31:07,439 --> 03:31:08,819
than equal
4722
03:31:08,819 --> 03:31:10,800
to be
4723
03:31:10,800 --> 03:31:16,859
and a is greater than equal to to C then
4724
03:31:16,859 --> 03:31:19,700
I'm going to return a
4725
03:31:19,800 --> 03:31:22,920
so now we're using the return keyword
4726
03:31:22,920 --> 03:31:25,560
else if now
4727
03:31:25,560 --> 03:31:29,119
putting here an lcf
4728
03:31:29,640 --> 03:31:30,720
B
4729
03:31:30,720 --> 03:31:35,640
is greater than equal to a and
4730
03:31:35,640 --> 03:31:39,560
B is greater than equal
4731
03:31:39,960 --> 03:31:42,239
to C
4732
03:31:42,239 --> 03:31:44,939
then return B
4733
03:31:44,939 --> 03:31:48,420
else C is going to be the bigger value
4734
03:31:48,420 --> 03:31:53,040
so put here return C
4735
03:31:53,040 --> 03:31:56,220
all right let's press Ctrl
4736
03:31:56,220 --> 03:32:01,160
alt return B here
4737
03:32:03,000 --> 03:32:05,279
right so I need to put here the type
4738
03:32:05,279 --> 03:32:07,979
this will get that error right now
4739
03:32:07,979 --> 03:32:09,779
everything is okay
4740
03:32:09,779 --> 03:32:13,279
oh so from here is the problem
4741
03:32:15,180 --> 03:32:17,880
all right so now
4742
03:32:17,880 --> 03:32:19,920
if you run this code let's change this
4743
03:32:19,920 --> 03:32:22,260
to let's say
4744
03:32:22,260 --> 03:32:24,920
five
4745
03:32:27,359 --> 03:32:29,340
seven
4746
03:32:29,340 --> 03:32:31,739
and 10.
4747
03:32:31,739 --> 03:32:34,680
now if you run this code
4748
03:32:34,680 --> 03:32:36,479
you see that the two functions above our
4749
03:32:36,479 --> 03:32:37,620
big gray out
4750
03:32:37,620 --> 03:32:39,180
and I was going to use the third one
4751
03:32:39,180 --> 03:32:42,979
which has third parameters
4752
03:32:43,200 --> 03:32:44,760
all right so when you get to get in the
4753
03:32:44,760 --> 03:32:46,920
output 10 which is correct so if you
4754
03:32:46,920 --> 03:32:48,660
change this to test let's say if we put
4755
03:32:48,660 --> 03:32:49,920
here
4756
03:32:49,920 --> 03:32:51,359
100
4757
03:32:51,359 --> 03:32:53,700
let's put here
4758
03:32:53,700 --> 03:32:57,800
three and if you run this code
4759
03:33:05,540 --> 03:33:09,060
now we get 100 and this is correct
4760
03:33:09,060 --> 03:33:10,739
all right so this is our discussion
4761
03:33:10,739 --> 03:33:12,840
about function overloading and you can
4762
03:33:12,840 --> 03:33:14,580
use function overloading every time you
4763
03:33:14,580 --> 03:33:18,120
know that you will do the same task but
4764
03:33:18,120 --> 03:33:20,340
with different type of parameters or
4765
03:33:20,340 --> 03:33:22,800
with a different number of parameters
4766
03:33:22,800 --> 03:33:25,140
also you may have noticed that we have
4767
03:33:25,140 --> 03:33:28,859
another line over uh over if over the
4768
03:33:28,859 --> 03:33:31,200
statement so if you hover over here
4769
03:33:31,200 --> 03:33:32,939
says that the return can be lifted out
4770
03:33:32,939 --> 03:33:35,399
if so if you click this
4771
03:33:35,399 --> 03:33:38,100
you can write it in this way so or we
4772
03:33:38,100 --> 03:33:40,020
can keep it in the
4773
03:33:40,020 --> 03:33:43,140
previous way but because this is uh
4774
03:33:43,140 --> 03:33:45,239
underline that means that kotlin
4775
03:33:45,239 --> 03:33:48,920
recommends to use it in this way
4776
03:33:49,380 --> 03:33:51,180
in this way
4777
03:33:51,180 --> 03:33:52,800
so see you in the next video what we're
4778
03:33:52,800 --> 03:33:57,140
going to talk about default parameters
4779
03:33:57,660 --> 03:33:59,340
all right so now it's time to look at
4780
03:33:59,340 --> 03:34:01,560
another features that you can use with
4781
03:34:01,560 --> 03:34:03,180
functions and that is called default
4782
03:34:03,180 --> 03:34:06,359
parameters and let's first declare a
4783
03:34:06,359 --> 03:34:07,560
function
4784
03:34:07,560 --> 03:34:09,060
so I'm going to declare it down here at
4785
03:34:09,060 --> 03:34:11,520
the end of our enclosing curly brace for
4786
03:34:11,520 --> 03:34:14,040
main fun I'm going to call it send
4787
03:34:14,040 --> 03:34:16,399
message
4788
03:34:16,859 --> 03:34:18,720
let's put the parenthesis and inside the
4789
03:34:18,720 --> 03:34:20,160
parenthesis I'm going to Define two
4790
03:34:20,160 --> 03:34:21,779
parameters the first is going to be
4791
03:34:21,779 --> 03:34:23,520
called name it's going to be of type
4792
03:34:23,520 --> 03:34:25,140
string
4793
03:34:25,140 --> 03:34:26,939
and this second one is going to be
4794
03:34:26,939 --> 03:34:28,560
called the
4795
03:34:28,560 --> 03:34:31,319
message and it's going to be also of
4796
03:34:31,319 --> 03:34:33,359
type string
4797
03:34:33,359 --> 03:34:35,399
now let's uh
4798
03:34:35,399 --> 03:34:37,859
you take the cloud the curly braces
4799
03:34:37,859 --> 03:34:42,180
press enter and let's print those
4800
03:34:42,180 --> 03:34:45,239
parameters in our console so let's put
4801
03:34:45,239 --> 03:34:48,359
the printer line here and inside the
4802
03:34:48,359 --> 03:34:52,319
also uh while I'm typing while I'm
4803
03:34:52,319 --> 03:34:54,000
pumping this printable anime since that
4804
03:34:54,000 --> 03:34:56,160
this println function is a function
4805
03:34:56,160 --> 03:34:58,439
which is overload referring to our
4806
03:34:58,439 --> 03:35:00,479
previous video because you can see that
4807
03:35:00,479 --> 03:35:02,640
it can it has the same name but it can
4808
03:35:02,640 --> 03:35:05,100
take different types of parameters here
4809
03:35:05,100 --> 03:35:07,500
so it has in this thing which is any
4810
03:35:07,500 --> 03:35:10,140
bytes or Char long so the print element
4811
03:35:10,140 --> 03:35:13,260
5 function that you've used throughout
4812
03:35:13,260 --> 03:35:15,840
our videos is overloaded this is why you
4813
03:35:15,840 --> 03:35:17,819
can pass different uh
4814
03:35:17,819 --> 03:35:22,260
different types of of data to it now
4815
03:35:22,260 --> 03:35:24,720
let's type here
4816
03:35:24,720 --> 03:35:27,560
println
4817
03:35:28,739 --> 03:35:30,359
quotation marks inside the quotation
4818
03:35:30,359 --> 03:35:32,640
mark we're going to type name equals
4819
03:35:32,640 --> 03:35:35,660
dollar sign name
4820
03:35:39,000 --> 03:35:40,800
so now it's going to print the
4821
03:35:40,800 --> 03:35:42,779
argument values that are going to pass
4822
03:35:42,779 --> 03:35:45,960
to this function as parameters now let's
4823
03:35:45,960 --> 03:35:48,180
call this function here
4824
03:35:48,180 --> 03:35:50,939
so let's type send
4825
03:35:50,939 --> 03:35:53,760
message to
4826
03:35:53,760 --> 03:35:55,439
format our code and here I'm going to
4827
03:35:55,439 --> 03:35:58,580
pass a name let's say
4828
03:35:58,620 --> 03:36:01,200
Alexa and
4829
03:36:01,200 --> 03:36:04,260
the message let's say
4830
03:36:04,260 --> 03:36:05,460
hello
4831
03:36:05,460 --> 03:36:07,020
now if you run this code you're going to
4832
03:36:07,020 --> 03:36:08,939
see in the output
4833
03:36:08,939 --> 03:36:11,460
um the name here is going to be this
4834
03:36:11,460 --> 03:36:13,020
argument that we type here which is
4835
03:36:13,020 --> 03:36:16,620
Alexa and the message is gonna be hello
4836
03:36:16,620 --> 03:36:19,380
so we have this but let's say that for
4837
03:36:19,380 --> 03:36:22,520
whatever reason the user doesn't want to
4838
03:36:22,520 --> 03:36:26,520
to send any kind of message but we still
4839
03:36:26,520 --> 03:36:28,800
need to use this function what you can
4840
03:36:28,800 --> 03:36:29,779
do then
4841
03:36:29,779 --> 03:36:33,660
one way is to remove this parameter all
4842
03:36:33,660 --> 03:36:36,899
the way but uh that is not the
4843
03:36:36,899 --> 03:36:38,939
good because you still need to use this
4844
03:36:38,939 --> 03:36:41,040
function with those two parameters the
4845
03:36:41,040 --> 03:36:43,260
name and and the message so you know
4846
03:36:43,260 --> 03:36:45,239
let's say that if the user doesn't pass
4847
03:36:45,239 --> 03:36:47,580
any message we can Define here what is
4848
03:36:47,580 --> 03:36:49,739
called the default
4849
03:36:49,739 --> 03:36:52,800
param a default parameter so what you
4850
03:36:52,800 --> 03:36:55,380
can assign here a default value for our
4851
03:36:55,380 --> 03:36:57,180
parameter so I'm going to put here uh
4852
03:36:57,180 --> 03:37:02,540
just the double quotation marks or
4853
03:37:02,760 --> 03:37:04,560
or you can put some text here but let's
4854
03:37:04,560 --> 03:37:06,300
put just the double quotation marks
4855
03:37:06,300 --> 03:37:09,540
which is just an empty text so if now I
4856
03:37:09,540 --> 03:37:12,540
don't pass a message here
4857
03:37:12,540 --> 03:37:15,840
now we're still gonna work because as a
4858
03:37:15,840 --> 03:37:18,120
value for this parameter is going to use
4859
03:37:18,120 --> 03:37:21,060
the default value that we defined here
4860
03:37:21,060 --> 03:37:22,859
all right so let's run this code to see
4861
03:37:22,859 --> 03:37:23,700
now
4862
03:37:23,700 --> 03:37:26,540
how it works
4863
03:37:31,260 --> 03:37:33,660
so now get name Alexa and message equals
4864
03:37:33,660 --> 03:37:36,300
empty because m double quotation marks
4865
03:37:36,300 --> 03:37:39,779
without any text is an empty text now if
4866
03:37:39,779 --> 03:37:41,760
I want to pass here now
4867
03:37:41,760 --> 03:37:43,979
the message now you can pass here the
4868
03:37:43,979 --> 03:37:46,020
message
4869
03:37:46,020 --> 03:37:48,720
and now our default value is going to be
4870
03:37:48,720 --> 03:37:51,060
replaced with the argument value that we
4871
03:37:51,060 --> 03:37:52,439
typed here
4872
03:37:52,439 --> 03:37:54,060
so now I'm going to see name Alexa
4873
03:37:54,060 --> 03:37:55,560
message hello
4874
03:37:55,560 --> 03:37:57,840
all right you can do the same thing for
4875
03:37:57,840 --> 03:38:00,420
uh for the name so here you can Define
4876
03:38:00,420 --> 03:38:04,200
let's say that the user doesn't want to
4877
03:38:04,200 --> 03:38:06,359
to type a name and we can define a
4878
03:38:06,359 --> 03:38:09,180
default name let's say user
4879
03:38:09,180 --> 03:38:11,220
yes
4880
03:38:11,220 --> 03:38:13,819
now let's say that I delete both
4881
03:38:13,819 --> 03:38:17,000
arguments here
4882
03:38:18,300 --> 03:38:21,300
and I just have an empty
4883
03:38:21,300 --> 03:38:23,580
function if I run this now it's going to
4884
03:38:23,580 --> 03:38:25,020
use the default values that you type
4885
03:38:25,020 --> 03:38:27,420
here respectively user and this empty
4886
03:38:27,420 --> 03:38:30,600
string so now get name user and message
4887
03:38:30,600 --> 03:38:33,239
now let's put back what we have so let's
4888
03:38:33,239 --> 03:38:34,859
put here
4889
03:38:34,859 --> 03:38:36,600
Alexa
4890
03:38:36,600 --> 03:38:41,180
let's put our message here again
4891
03:38:44,279 --> 03:38:47,540
now let's run this code again
4892
03:38:50,939 --> 03:38:54,600
now what we can also do is use what is
4893
03:38:54,600 --> 03:38:57,180
called named parameters and let's say
4894
03:38:57,180 --> 03:38:58,739
that I want to
4895
03:38:58,739 --> 03:39:01,939
to pass a parameter only for the second
4896
03:39:01,939 --> 03:39:04,859
second let's let's say that I want to
4897
03:39:04,859 --> 03:39:07,920
pass the argument type for only for the
4898
03:39:07,920 --> 03:39:09,359
second parameter that we Define here
4899
03:39:09,359 --> 03:39:11,220
respectively for our message
4900
03:39:11,220 --> 03:39:14,160
so if I delete this now I'll say let's
4901
03:39:14,160 --> 03:39:15,720
say that I delete the
4902
03:39:15,720 --> 03:39:19,080
the argument for our name
4903
03:39:19,080 --> 03:39:21,239
and if I want to pass the value only for
4904
03:39:21,239 --> 03:39:23,220
the message
4905
03:39:23,220 --> 03:39:25,140
let's put in the quotation mark because
4906
03:39:25,140 --> 03:39:26,340
the string
4907
03:39:26,340 --> 03:39:29,160
let's say I put the message hello here
4908
03:39:29,160 --> 03:39:31,620
now you can see that the hint is named
4909
03:39:31,620 --> 03:39:34,739
so it's using the first parameter that
4910
03:39:34,739 --> 03:39:37,080
we Define here another second so what we
4911
03:39:37,080 --> 03:39:41,580
can do now to pass a value to only to
4912
03:39:41,580 --> 03:39:43,979
our second parameter because as you can
4913
03:39:43,979 --> 03:39:46,020
see it's using the it's assigning this
4914
03:39:46,020 --> 03:39:48,000
value that we type here for this
4915
03:39:48,000 --> 03:39:50,160
parameter and to solve that you can use
4916
03:39:50,160 --> 03:39:52,080
what is called the name parameters so
4917
03:39:52,080 --> 03:39:55,080
with name parameters you can type an
4918
03:39:55,080 --> 03:39:57,420
argument for our parameter irrespective
4919
03:39:57,420 --> 03:40:00,180
of the order in which they are defined
4920
03:40:00,180 --> 03:40:01,920
so in our case we have the name for
4921
03:40:01,920 --> 03:40:04,560
Define and then the message so to use
4922
03:40:04,560 --> 03:40:06,600
that you can
4923
03:40:06,600 --> 03:40:08,939
type the name of the parameter let's say
4924
03:40:08,939 --> 03:40:11,760
message now you see that you have this P
4925
03:40:11,760 --> 03:40:14,760
message and it's equals to A String so
4926
03:40:14,760 --> 03:40:16,140
that means that you need to pass some
4927
03:40:16,140 --> 03:40:17,100
text here
4928
03:40:17,100 --> 03:40:19,380
and now we can pass here
4929
03:40:19,380 --> 03:40:21,359
hello
4930
03:40:21,359 --> 03:40:23,819
and now this is gonna work because now
4931
03:40:23,819 --> 03:40:26,580
we can pass a value to our parameter
4932
03:40:26,580 --> 03:40:28,560
irrespective of the water in which they
4933
03:40:28,560 --> 03:40:31,319
are defined here
4934
03:40:31,319 --> 03:40:35,160
right so now I get name is this default
4935
03:40:35,160 --> 03:40:38,460
user and the message is hello and we can
4936
03:40:38,460 --> 03:40:40,200
do the same for uh
4937
03:40:40,200 --> 03:40:42,479
for the second one which is name so I
4938
03:40:42,479 --> 03:40:44,819
can put now here name
4939
03:40:44,819 --> 03:40:48,180
and I can type here
4940
03:40:48,180 --> 03:40:49,680
let's say Alexa
4941
03:40:49,680 --> 03:40:52,640
and if you run this
4942
03:40:55,319 --> 03:40:57,120
now get Alexa
4943
03:40:57,120 --> 03:40:59,100
is the name and the message is hello so
4944
03:40:59,100 --> 03:41:01,979
with the named parameters this is how
4945
03:41:01,979 --> 03:41:03,840
they are called a name arguments we can
4946
03:41:03,840 --> 03:41:05,760
pass the value
4947
03:41:05,760 --> 03:41:06,920
to our
4948
03:41:06,920 --> 03:41:09,600
parameters irrespective of the order in
4949
03:41:09,600 --> 03:41:11,880
which they are defined in the function
4950
03:41:11,880 --> 03:41:14,160
so in our case name is first defined and
4951
03:41:14,160 --> 03:41:17,279
you saw that that when I try to use a
4952
03:41:17,279 --> 03:41:19,800
the message to assign a value only for
4953
03:41:19,800 --> 03:41:22,020
the message it's what it was using the
4954
03:41:22,020 --> 03:41:23,760
first parameter because this was the
4955
03:41:23,760 --> 03:41:25,560
word in which they were defined
4956
03:41:25,560 --> 03:41:27,960
but with this named parameters you can
4957
03:41:27,960 --> 03:41:32,160
change you can you can type an argument
4958
03:41:32,160 --> 03:41:33,960
irrespective of the word in which they
4959
03:41:33,960 --> 03:41:35,960
are defined and you can put this in a
4960
03:41:35,960 --> 03:41:39,120
multiple lines to make it more clear
4961
03:41:39,120 --> 03:41:42,720
like this let's say
4962
03:41:42,720 --> 03:41:45,260
so this is our discussion about
4963
03:41:45,260 --> 03:41:48,420
named parameters and the default
4964
03:41:48,420 --> 03:41:51,120
parameters but before we win the video I
4965
03:41:51,120 --> 03:41:53,340
should say that to the default
4966
03:41:53,340 --> 03:41:55,680
parameters we cannot only type literals
4967
03:41:55,680 --> 03:41:57,899
values like we typed here some values
4968
03:41:57,899 --> 03:42:01,380
directly we can use variables or you can
4969
03:42:01,380 --> 03:42:04,200
use a function which can return a value
4970
03:42:04,200 --> 03:42:06,300
so I can Define here a function let's
4971
03:42:06,300 --> 03:42:07,680
say fun
4972
03:42:07,680 --> 03:42:09,479
send the
4973
03:42:09,479 --> 03:42:11,640
text it's not going to have any
4974
03:42:11,640 --> 03:42:15,180
parameters it's gonna return a string so
4975
03:42:15,180 --> 03:42:17,460
I'm going to Define here a string as the
4976
03:42:17,460 --> 03:42:19,979
type of return and here gonna type
4977
03:42:19,979 --> 03:42:23,760
return and let's type some
4978
03:42:23,760 --> 03:42:25,140
text
4979
03:42:25,140 --> 03:42:29,399
now we can call this function here send
4980
03:42:29,399 --> 03:42:30,960
text
4981
03:42:30,960 --> 03:42:33,600
and now this value which is returned for
4982
03:42:33,600 --> 03:42:35,939
this function send text is going to be
4983
03:42:35,939 --> 03:42:38,700
assigned to our parameter message so now
4984
03:42:38,700 --> 03:42:40,800
if you run this code
4985
03:42:40,800 --> 03:42:45,060
Let's uh increase this a little bit
4986
03:42:45,060 --> 03:42:47,220
so now if you run this code you're going
4987
03:42:47,220 --> 03:42:49,739
to see that the Now the default value
4988
03:42:49,739 --> 03:42:52,140
for the part method message is the
4989
03:42:52,140 --> 03:42:55,680
return value from our send text function
4990
03:42:55,680 --> 03:42:58,739
so you can see now that we have
4991
03:42:58,739 --> 03:43:03,260
okay so let's change it here to no value
4992
03:43:05,399 --> 03:43:08,180
for our message
4993
03:43:08,460 --> 03:43:10,800
so now if you run this code because
4994
03:43:10,800 --> 03:43:13,319
there we have an argument passed so
4995
03:43:13,319 --> 03:43:15,840
let's not passing on the northwest so
4996
03:43:15,840 --> 03:43:18,960
now it's using the value that we was was
4997
03:43:18,960 --> 03:43:20,939
passed as the default
4998
03:43:20,939 --> 03:43:23,880
value for our parameter which is this
4999
03:43:23,880 --> 03:43:25,800
return value from this function
5000
03:43:25,800 --> 03:43:27,720
now because here you have one single
5001
03:43:27,720 --> 03:43:29,640
line of code we have one single
5002
03:43:29,640 --> 03:43:32,220
expression we just return some text or
5003
03:43:32,220 --> 03:43:34,319
function we don't have multiple
5004
03:43:34,319 --> 03:43:37,319
statements in our function we can write
5005
03:43:37,319 --> 03:43:39,779
this as a single expression function
5006
03:43:39,779 --> 03:43:42,479
and you can remove the calibration you
5007
03:43:42,479 --> 03:43:44,819
can write this as a single expression
5008
03:43:44,819 --> 03:43:46,920
body like we did in a previous video so
5009
03:43:46,920 --> 03:43:48,840
you can put it just equals and you can
5010
03:43:48,840 --> 03:43:50,340
remove the
5011
03:43:50,340 --> 03:43:54,680
type here also so if you run this code
5012
03:43:54,840 --> 03:43:57,600
now I'm gonna see it to the output for
5013
03:43:57,600 --> 03:44:00,359
the parameter message some text which is
5014
03:44:00,359 --> 03:44:02,160
the value which is returned from this
5015
03:44:02,160 --> 03:44:04,859
function from the single body expression
5016
03:44:04,859 --> 03:44:06,960
function right
5017
03:44:06,960 --> 03:44:09,239
so see in the next video
5018
03:44:09,239 --> 03:44:10,979
all right now it's time to look at the
5019
03:44:10,979 --> 03:44:13,620
VAR Arc keyword and how we can use the
5020
03:44:13,620 --> 03:44:15,479
water keyword with functions but first
5021
03:44:15,479 --> 03:44:17,100
let's declare the function below of our
5022
03:44:17,100 --> 03:44:18,420
enclosing
5023
03:44:18,420 --> 03:44:21,000
calibrase of our main function so I'm
5024
03:44:21,000 --> 03:44:23,460
going to type here the
5025
03:44:23,460 --> 03:44:26,460
fun keyword to declare a function
5026
03:44:26,460 --> 03:44:28,500
I'm going to call it sum because it's
5027
03:44:28,500 --> 03:44:30,660
going to return the sum of all the
5028
03:44:30,660 --> 03:44:32,640
parameter that we're going to Define so
5029
03:44:32,640 --> 03:44:34,500
I'm going to Define here some parameters
5030
03:44:34,500 --> 03:44:36,479
for it's going to be called a it's going
5031
03:44:36,479 --> 03:44:38,580
to be of type int
5032
03:44:38,580 --> 03:44:41,220
a single second is going to be called B
5033
03:44:41,220 --> 03:44:43,938
also in
5034
03:44:44,460 --> 03:44:46,680
third is going to be called C
5035
03:44:46,680 --> 03:44:50,420
also an end
5036
03:44:50,640 --> 03:44:52,200
the final one is going to be called the
5037
03:44:52,200 --> 03:44:54,000
alternate
5038
03:44:54,000 --> 03:44:55,800
and this this function is going to
5039
03:44:55,800 --> 03:44:58,020
return the sum of all of those
5040
03:44:58,020 --> 03:45:00,720
parameters so we need to Define here the
5041
03:45:00,720 --> 03:45:01,859
return type
5042
03:45:01,859 --> 03:45:04,319
which is going to be an hint
5043
03:45:04,319 --> 03:45:07,080
a whole number and now we can put the
5044
03:45:07,080 --> 03:45:10,200
curly braces and here we type return
5045
03:45:10,200 --> 03:45:11,760
a
5046
03:45:11,760 --> 03:45:14,840
plus b
5047
03:45:14,880 --> 03:45:16,800
plus c
5048
03:45:16,800 --> 03:45:17,939
Plus
5049
03:45:17,939 --> 03:45:19,080
d
5050
03:45:19,080 --> 03:45:21,540
and this is going to return the sum of
5051
03:45:21,540 --> 03:45:23,580
all the parameters that we Define here
5052
03:45:23,580 --> 03:45:26,640
or the values that is going to be passed
5053
03:45:26,640 --> 03:45:29,640
as arguments to this function and you
5054
03:45:29,640 --> 03:45:31,920
can write this as a single expression
5055
03:45:31,920 --> 03:45:33,479
function if you want so you can write
5056
03:45:33,479 --> 03:45:37,020
this uh like this if you want
5057
03:45:37,020 --> 03:45:40,340
this will have the same effect
5058
03:45:41,520 --> 03:45:46,160
but I will I will write it with the
5059
03:45:46,160 --> 03:45:48,960
curly braces because it will make things
5060
03:45:48,960 --> 03:45:52,739
more clear so let's undo this
5061
03:45:52,739 --> 03:45:55,620
right now let's call this function here
5062
03:45:55,620 --> 03:45:57,720
sum
5063
03:45:57,720 --> 03:46:00,960
let's press Ctrl alt L to format the
5064
03:46:00,960 --> 03:46:03,779
code and let's pass some values here so
5065
03:46:03,779 --> 03:46:05,939
I'm going to type here five
5066
03:46:05,939 --> 03:46:09,840
six seven
5067
03:46:10,680 --> 03:46:12,359
and
5068
03:46:12,359 --> 03:46:14,160
10.
5069
03:46:14,160 --> 03:46:16,920
all right if you press shift Ctrl p on
5070
03:46:16,920 --> 03:46:19,140
this you're gonna see that the type of
5071
03:46:19,140 --> 03:46:22,140
this function let's press again
5072
03:46:22,140 --> 03:46:24,600
the type of this function is an ins so
5073
03:46:24,600 --> 03:46:29,520
this function returns an integer so it's
5074
03:46:29,520 --> 03:46:32,700
it's an expression that means that we
5075
03:46:32,700 --> 03:46:35,939
can put this instead of putting storing
5076
03:46:35,939 --> 03:46:36,479
um
5077
03:46:36,479 --> 03:46:40,200
uh the return value in a variable we can
5078
03:46:40,200 --> 03:46:42,540
put this direction a printerland
5079
03:46:42,540 --> 03:46:46,819
function so you can type here print
5080
03:46:46,920 --> 03:46:48,359
area
5081
03:46:48,359 --> 03:46:50,460
and we can put the function inside here
5082
03:46:50,460 --> 03:46:52,680
because that function is of type in so
5083
03:46:52,680 --> 03:46:54,300
it's going to return
5084
03:46:54,300 --> 03:46:57,000
the sum of all the arguments that we
5085
03:46:57,000 --> 03:46:58,739
type there
5086
03:46:58,739 --> 03:47:01,560
so now if you run this code
5087
03:47:01,560 --> 03:47:05,600
we got we get 28 because 5 plus 6 Plus
5088
03:47:05,600 --> 03:47:09,420
7.10 is 28. now if I want to pass
5089
03:47:09,420 --> 03:47:11,640
another parameter here let's say that I
5090
03:47:11,640 --> 03:47:14,160
want to pass another number here I can't
5091
03:47:14,160 --> 03:47:17,040
because I am limited by the number of
5092
03:47:17,040 --> 03:47:19,140
parameters that we Define here of course
5093
03:47:19,140 --> 03:47:22,260
that I can Define another parameter here
5094
03:47:22,260 --> 03:47:25,319
but what if I want to pass 10 numbers
5095
03:47:25,319 --> 03:47:29,279
for 15 numbers or 100 numbers in that
5096
03:47:29,279 --> 03:47:31,200
case it will be very tedious to write
5097
03:47:31,200 --> 03:47:34,020
all of those parameters and and to solve
5098
03:47:34,020 --> 03:47:35,700
that you need to use what is called a
5099
03:47:35,700 --> 03:47:39,239
varg varag keyword and Avada keyword
5100
03:47:39,239 --> 03:47:41,340
allows us to pass more than one
5101
03:47:41,340 --> 03:47:43,620
parameter actually more than one
5102
03:47:43,620 --> 03:47:45,720
argument to our
5103
03:47:45,720 --> 03:47:48,479
parameters so to declare a Vari keyword
5104
03:47:48,479 --> 03:47:51,380
we type here
5105
03:47:51,960 --> 03:47:55,319
we type here far ARG so this is the
5106
03:47:55,319 --> 03:47:58,319
keyword varag it stands for variable
5107
03:47:58,319 --> 03:47:59,880
arguments
5108
03:47:59,880 --> 03:48:02,700
now we need to define a name for this
5109
03:48:02,700 --> 03:48:06,720
bar Arc I'm going to call it numbers
5110
03:48:06,720 --> 03:48:10,439
and we also need to specify a type what
5111
03:48:10,439 --> 03:48:13,620
kind of values this varag is going to
5112
03:48:13,620 --> 03:48:14,340
store
5113
03:48:14,340 --> 03:48:18,319
now this return keyword is this
5114
03:48:18,319 --> 03:48:20,600
variables here are red because they
5115
03:48:20,600 --> 03:48:23,640
don't exist
5116
03:48:23,640 --> 03:48:27,180
now if I want to
5117
03:48:27,180 --> 03:48:29,700
but first let's say that I want to type
5118
03:48:29,700 --> 03:48:32,460
here another parameter now now you see
5119
03:48:32,460 --> 03:48:34,200
that we have no problem so I can type
5120
03:48:34,200 --> 03:48:36,420
here whatever
5121
03:48:36,420 --> 03:48:40,680
number of numbers I want and there is no
5122
03:48:40,680 --> 03:48:44,640
problem as you can see now
5123
03:48:44,640 --> 03:48:47,399
to to
5124
03:48:47,399 --> 03:48:51,000
sum the values which are stored in these
5125
03:48:51,000 --> 03:48:52,739
viral numbers we need to use something
5126
03:48:52,739 --> 03:48:55,680
which we didn't cover yet but we have a
5127
03:48:55,680 --> 03:48:58,979
separate section which I show you how to
5128
03:48:58,979 --> 03:49:01,200
we need to explain this thing that I'm
5129
03:49:01,200 --> 03:49:03,060
going to use now and that is Loops
5130
03:49:03,060 --> 03:49:07,200
because now we need to Loop through our
5131
03:49:07,200 --> 03:49:10,020
numbers and to add our numbers to a
5132
03:49:10,020 --> 03:49:12,300
variable so we need to use what is
5133
03:49:12,300 --> 03:49:15,239
called a for Loop so to do that first we
5134
03:49:15,239 --> 03:49:16,739
declare a variable
5135
03:49:16,739 --> 03:49:20,899
I'm gonna call it um
5136
03:49:21,180 --> 03:49:25,460
let's say uh result
5137
03:49:27,420 --> 03:49:30,840
I'm gonna assign a value of zero
5138
03:49:30,840 --> 03:49:35,220
now to use a for Loop we Type 4
5139
03:49:35,220 --> 03:49:37,979
and again I'm gonna go into more detail
5140
03:49:37,979 --> 03:49:40,260
about this in uh the separate section
5141
03:49:40,260 --> 03:49:42,060
but now we need to use this because
5142
03:49:42,060 --> 03:49:45,600
there's not another way to to sum up the
5143
03:49:45,600 --> 03:49:49,979
values inside the varag numbers
5144
03:49:49,979 --> 03:49:53,640
and here we need to define a name for
5145
03:49:53,640 --> 03:49:55,020
our variable so it's going to be called
5146
03:49:55,020 --> 03:49:56,220
number
5147
03:49:56,220 --> 03:50:00,479
and now we use the keyword in
5148
03:50:00,479 --> 03:50:02,939
and now we're referring the
5149
03:50:02,939 --> 03:50:06,720
parameter the varag parameter which is
5150
03:50:06,720 --> 03:50:09,960
defined up called numbers
5151
03:50:09,960 --> 03:50:11,520
all right
5152
03:50:11,520 --> 03:50:15,300
now we here you can put the curly braces
5153
03:50:15,300 --> 03:50:17,220
and inside the cutter blazes we're going
5154
03:50:17,220 --> 03:50:19,319
to type
5155
03:50:19,319 --> 03:50:22,319
numbers
5156
03:50:23,279 --> 03:50:25,200
result
5157
03:50:25,200 --> 03:50:26,520
and now we're going to use that
5158
03:50:26,520 --> 03:50:30,120
abbreviation which is plus n equals
5159
03:50:30,120 --> 03:50:33,200
plus equals
5160
03:50:33,899 --> 03:50:36,000
number n
5161
03:50:36,000 --> 03:50:38,760
now here gonna return
5162
03:50:38,760 --> 03:50:42,359
the result return
5163
03:50:42,420 --> 03:50:44,840
result
5164
03:50:45,779 --> 03:50:48,120
all right so let's explain what we have
5165
03:50:48,120 --> 03:50:50,640
here so first we declare a variable we
5166
03:50:50,640 --> 03:50:52,859
assign a value of 0 to it so it's just
5167
03:50:52,859 --> 03:50:54,840
type int
5168
03:50:54,840 --> 03:50:58,620
next what this for Loop is doing so this
5169
03:50:58,620 --> 03:51:01,020
for Loop is doing what is doing is
5170
03:51:01,020 --> 03:51:02,399
executing
5171
03:51:02,399 --> 03:51:06,300
um a line of code multiple times so it
5172
03:51:06,300 --> 03:51:10,260
executing is executing uh uh what's
5173
03:51:10,260 --> 03:51:14,340
inside the curly braces until the it
5174
03:51:14,340 --> 03:51:17,399
finishes to it finishes the to Loop
5175
03:51:17,399 --> 03:51:19,920
through all these numbers which are
5176
03:51:19,920 --> 03:51:24,979
stored in the varark number so it first
5177
03:51:24,979 --> 03:51:30,000
assigns uh the first the first number or
5178
03:51:30,000 --> 03:51:31,500
number here
5179
03:51:31,500 --> 03:51:35,640
and it loops and it has that it adds
5180
03:51:35,640 --> 03:51:37,859
that number to a result then it does
5181
03:51:37,859 --> 03:51:41,279
that again and again and again until it
5182
03:51:41,279 --> 03:51:43,920
finishes so then it reaches uh the final
5183
03:51:43,920 --> 03:51:46,080
number five and in that case also gonna
5184
03:51:46,080 --> 03:51:47,100
sign
5185
03:51:47,100 --> 03:51:50,700
from our numbers uh the number five to
5186
03:51:50,700 --> 03:51:54,720
our number and then that number is going
5187
03:51:54,720 --> 03:51:57,660
to be added to our result variable and
5188
03:51:57,660 --> 03:52:01,680
when it finishes uh that looping when it
5189
03:52:01,680 --> 03:52:05,700
finishes that thing is gonna return the
5190
03:52:05,700 --> 03:52:07,560
result it's gonna return all the values
5191
03:52:07,560 --> 03:52:11,160
which are which were been added to our
5192
03:52:11,160 --> 03:52:12,960
result variable
5193
03:52:12,960 --> 03:52:16,460
so now if you print this
5194
03:52:17,279 --> 03:52:20,580
now I get 85 and probably this is the
5195
03:52:20,580 --> 03:52:24,060
result of this calculation so this is
5196
03:52:24,060 --> 03:52:27,420
how you can use varag numbers what are
5197
03:52:27,420 --> 03:52:29,819
the keyword to store more than one
5198
03:52:29,819 --> 03:52:33,180
single more than a limited number of
5199
03:52:33,180 --> 03:52:35,220
parameters defined
5200
03:52:35,220 --> 03:52:40,020
bio and another short shortcut to to
5201
03:52:40,020 --> 03:52:42,779
Loop through this is using a for each
5202
03:52:42,779 --> 03:52:46,739
Loop so we can do something like this
5203
03:52:46,739 --> 03:52:49,260
but this time let's say that I want I
5204
03:52:49,260 --> 03:52:52,080
don't want to return something
5205
03:52:52,080 --> 03:52:55,640
so let's see that I delete this
5206
03:52:59,340 --> 03:53:04,140
and I I can type here actually numbers
5207
03:53:04,140 --> 03:53:05,819
four
5208
03:53:05,819 --> 03:53:08,340
dot here to put that
5209
03:53:08,340 --> 03:53:10,620
four
5210
03:53:10,620 --> 03:53:12,540
each
5211
03:53:12,540 --> 03:53:16,979
and here we type print Ln
5212
03:53:16,979 --> 03:53:20,399
again this is a more complex subject
5213
03:53:20,399 --> 03:53:22,140
which is going to be covered in the next
5214
03:53:22,140 --> 03:53:27,000
videos so here we type print Ln
5215
03:53:27,000 --> 03:53:30,180
and we type it
5216
03:53:30,180 --> 03:53:33,540
all right now let's uh
5217
03:53:33,540 --> 03:53:36,180
let's call our sum
5218
03:53:36,180 --> 03:53:38,160
let's define some numbers here let's say
5219
03:53:38,160 --> 03:53:40,859
that I type here
5220
03:53:40,859 --> 03:53:42,840
one
5221
03:53:42,840 --> 03:53:45,200
four
5222
03:53:50,880 --> 03:53:54,180
all right so if you run this code
5223
03:53:54,180 --> 03:53:56,880
now you can see that every number in
5224
03:53:56,880 --> 03:53:58,140
that
5225
03:53:58,140 --> 03:54:00,540
that we defined here is printed one by
5226
03:54:00,540 --> 03:54:03,779
one so this number this for each which
5227
03:54:03,779 --> 03:54:06,600
is also a loop it goes through all of
5228
03:54:06,600 --> 03:54:09,359
these numbers it and it prints them one
5229
03:54:09,359 --> 03:54:12,120
by one and again we're gonna look at uh
5230
03:54:12,120 --> 03:54:14,399
for the for Loop and the four each in
5231
03:54:14,399 --> 03:54:18,479
the next videos but I had no other way
5232
03:54:18,479 --> 03:54:21,060
to show you this example without using
5233
03:54:21,060 --> 03:54:23,819
them but if you feel confused by for the
5234
03:54:23,819 --> 03:54:26,220
for each or the for Loop don't worry we
5235
03:54:26,220 --> 03:54:27,840
have a separate
5236
03:54:27,840 --> 03:54:30,180
section which in which you talk about in
5237
03:54:30,180 --> 03:54:32,520
detail about this for now we need to
5238
03:54:32,520 --> 03:54:34,319
only the only thing you need to know is
5239
03:54:34,319 --> 03:54:36,540
that you can
5240
03:54:36,540 --> 03:54:40,140
pass an indefinite number of parameters
5241
03:54:40,140 --> 03:54:42,600
of actually you can pass it an
5242
03:54:42,600 --> 03:54:45,979
indefinite number of arguments to our
5243
03:54:45,979 --> 03:54:49,979
varag numbers because this allows us to
5244
03:54:49,979 --> 03:54:53,279
to power to to pass uh
5245
03:54:53,279 --> 03:54:57,479
an indefinite number of values so here
5246
03:54:57,479 --> 03:54:59,760
you see that we have one four five six
5247
03:54:59,760 --> 03:55:04,020
seven eight nine five seven and six
5248
03:55:04,020 --> 03:55:07,020
numbers that we have here so this for
5249
03:55:07,020 --> 03:55:09,060
each is going through all of them and it
5250
03:55:09,060 --> 03:55:10,319
prints
5251
03:55:10,319 --> 03:55:13,080
them one by one so
5252
03:55:13,080 --> 03:55:16,220
see you in the next video
5253
03:55:18,060 --> 03:55:19,680
all right now it's time to start our
5254
03:55:19,680 --> 03:55:21,840
discussion about loops but first let's
5255
03:55:21,840 --> 03:55:24,620
create a new project
5256
03:55:24,660 --> 03:55:27,920
I'm going to call it loops
5257
03:55:29,520 --> 03:55:34,460
select IntelliJ click on next and finish
5258
03:55:35,640 --> 03:55:37,680
right a project is created let's delete
5259
03:55:37,680 --> 03:55:40,319
the code inside the color braces
5260
03:55:40,319 --> 03:55:42,720
let's hide the project pane
5261
03:55:42,720 --> 03:55:46,800
and let's start by explaining what Loops
5262
03:55:46,800 --> 03:55:48,960
are and why you need to use them
5263
03:55:48,960 --> 03:55:53,040
so Loops allows us to execute a piece of
5264
03:55:53,040 --> 03:55:56,279
code multiple times without
5265
03:55:56,279 --> 03:56:00,979
you writing down every line of code
5266
03:56:00,979 --> 03:56:04,199
manually so
5267
03:56:04,199 --> 03:56:06,840
let's say that I want to print
5268
03:56:06,840 --> 03:56:10,680
10 numbers in the console or 20 or 50.
5269
03:56:10,680 --> 03:56:13,920
instead of printing instead of typing
5270
03:56:13,920 --> 03:56:16,199
that code manually move line by line
5271
03:56:16,199 --> 03:56:19,260
which will be very tedious you can use a
5272
03:56:19,260 --> 03:56:21,540
loop and you can put that code in a loop
5273
03:56:21,540 --> 03:56:23,720
and in that code in the loop it will be
5274
03:56:23,720 --> 03:56:28,560
executed repeatedly until
5275
03:56:29,040 --> 03:56:34,260
a certain number is reached so let's see
5276
03:56:34,260 --> 03:56:36,479
how we can define a loop to define a
5277
03:56:36,479 --> 03:56:38,640
loop we type here
5278
03:56:38,640 --> 03:56:41,520
four this is the first Loop that we're
5279
03:56:41,520 --> 03:56:43,140
gonna look at because there are other
5280
03:56:43,140 --> 03:56:45,560
loops
5281
03:56:45,600 --> 03:56:49,199
so this is the for Loop we press enter
5282
03:56:49,199 --> 03:56:51,540
inside the parenthesis we type I this is
5283
03:56:51,540 --> 03:56:53,520
convention you can put whatever name you
5284
03:56:53,520 --> 03:56:54,600
wonder
5285
03:56:54,600 --> 03:56:57,420
now we put here in this is the keyword
5286
03:56:57,420 --> 03:56:59,699
in and now let's say that I want to loop
5287
03:56:59,699 --> 03:57:00,720
from
5288
03:57:00,720 --> 03:57:02,220
one
5289
03:57:02,220 --> 03:57:05,340
and from 1 to 10 and to do that to type
5290
03:57:05,340 --> 03:57:10,160
1 dot dot this is a range
5291
03:57:10,620 --> 03:57:12,660
one to ten now
5292
03:57:12,660 --> 03:57:15,120
I put a curly braces
5293
03:57:15,120 --> 03:57:17,520
and inside the pre the curly braces and
5294
03:57:17,520 --> 03:57:19,319
I'm gonna put the code that I want to be
5295
03:57:19,319 --> 03:57:21,060
executed
5296
03:57:21,060 --> 03:57:24,180
10 times so I'm gonna type here println
5297
03:57:24,180 --> 03:57:26,760
and I'm gonna type the value of I at
5298
03:57:26,760 --> 03:57:30,779
each iteration and uh
5299
03:57:30,779 --> 03:57:33,479
we can put it like this you can type
5300
03:57:33,479 --> 03:57:35,279
here uh
5301
03:57:35,279 --> 03:57:37,140
you can put in the quotation marks and
5302
03:57:37,140 --> 03:57:40,460
you can do the placeholder
5303
03:57:41,520 --> 03:57:42,960
you can put I
5304
03:57:42,960 --> 03:57:46,278
so now if you run this
5305
03:57:47,939 --> 03:57:50,640
so now I get
5306
03:57:50,640 --> 03:57:52,680
is one two
5307
03:57:52,680 --> 03:57:57,199
until 10. so it what is doing is is
5308
03:57:57,199 --> 03:58:01,800
looping through this range until it
5309
03:58:01,800 --> 03:58:03,960
reaches 10 then it stops
5310
03:58:03,960 --> 03:58:07,680
now uh you can write this I like I did
5311
03:58:07,680 --> 03:58:09,660
here using a placeholder where you can
5312
03:58:09,660 --> 03:58:12,619
write it like this
5313
03:58:13,140 --> 03:58:15,359
I've run this code it will have the same
5314
03:58:15,359 --> 03:58:17,359
effect but will not have the taste
5315
03:58:17,359 --> 03:58:20,720
before here
5316
03:58:23,100 --> 03:58:24,840
so now you get
5317
03:58:24,840 --> 03:58:25,979
one
5318
03:58:25,979 --> 03:58:29,220
until 10.
5319
03:58:29,279 --> 03:58:31,920
now there are other other very
5320
03:58:31,920 --> 03:58:35,880
variations to use this range and one of
5321
03:58:35,880 --> 03:58:40,560
them is to use until so let's uh command
5322
03:58:40,560 --> 03:58:41,640
this code
5323
03:58:41,640 --> 03:58:45,500
so let's press Ctrl slash
5324
03:58:49,080 --> 03:58:52,620
right now let's type 4 again
5325
03:58:52,620 --> 03:58:55,260
and now I'm gonna
5326
03:58:55,260 --> 03:58:57,180
I'm gonna call it also I here you can
5327
03:58:57,180 --> 03:58:59,040
put whatever name you want
5328
03:58:59,040 --> 03:59:02,580
and instead of now I'm going to put also
5329
03:59:02,580 --> 03:59:05,640
again the in keyword and instead of
5330
03:59:05,640 --> 03:59:09,000
putting the dot dot to Loop to arrange
5331
03:59:09,000 --> 03:59:10,199
I'm gonna put
5332
03:59:10,199 --> 03:59:13,620
one until
5333
03:59:13,620 --> 03:59:15,479
10.
5334
03:59:15,479 --> 03:59:17,640
all right
5335
03:59:17,640 --> 03:59:20,939
now I'm gonna add the printerliner
5336
03:59:20,939 --> 03:59:23,040
statement
5337
03:59:23,040 --> 03:59:26,100
and here I'm going to type I
5338
03:59:26,100 --> 03:59:27,420
and
5339
03:59:27,420 --> 03:59:30,779
this will have the same effect with the
5340
03:59:30,779 --> 03:59:33,239
thing with a single difference that 1
5341
03:59:33,239 --> 03:59:37,979
until 10 is is gonna exclude ten and one
5342
03:59:37,979 --> 03:59:39,479
uh
5343
03:59:39,479 --> 03:59:43,380
using the range.10 it will include 10.
5344
03:59:43,380 --> 03:59:46,339
so if you run this code
5345
03:59:46,620 --> 03:59:49,140
we're gonna see that you will not have
5346
03:59:49,140 --> 03:59:52,100
10 here because 10 it is excluded when
5347
03:59:52,100 --> 03:59:54,899
you're using the until
5348
03:59:54,899 --> 03:59:57,180
so now as you can see we have one until
5349
03:59:57,180 --> 03:59:59,699
nine and then it's excluded because now
5350
03:59:59,699 --> 04:00:01,500
we're using the until
5351
04:00:01,500 --> 04:00:04,859
so if you if you're using the range
5352
04:00:04,859 --> 04:00:08,460
which is uh dot the last number is going
5353
04:00:08,460 --> 04:00:10,979
to be included but if you use the
5354
04:00:10,979 --> 04:00:13,439
until the last number is going to be
5355
04:00:13,439 --> 04:00:15,779
excluded so it's important to know that
5356
04:00:15,779 --> 04:00:20,220
this two respectively the until and the
5357
04:00:20,220 --> 04:00:24,560
range can only count upwards
5358
04:00:25,979 --> 04:00:29,460
so we cannot put here 10 until 1 because
5359
04:00:29,460 --> 04:00:32,640
the first number the number of on the
5360
04:00:32,640 --> 04:00:34,859
left needs to be smaller than the number
5361
04:00:34,859 --> 04:00:37,560
on the right so we cannot put 10 until
5362
04:00:37,560 --> 04:00:40,439
1. we can only count upwards you can
5363
04:00:40,439 --> 04:00:43,699
only put one until 10 or
5364
04:00:43,699 --> 04:00:46,140
1.10 so
5365
04:00:46,140 --> 04:00:49,739
to Loop backwards we need to use
5366
04:00:49,739 --> 04:00:52,439
another thing so let's comment this code
5367
04:00:52,439 --> 04:00:54,859
also
5368
04:00:56,580 --> 04:01:00,600
now to Loop backwards we Type 4
5369
04:01:00,600 --> 04:01:03,540
again in the paradise you put the I here
5370
04:01:03,540 --> 04:01:06,020
you can put
5371
04:01:07,739 --> 04:01:09,600
whatever if you want
5372
04:01:09,600 --> 04:01:10,979
in
5373
04:01:10,979 --> 04:01:13,260
so here you can put whatever name but
5374
04:01:13,260 --> 04:01:16,080
the convention is I issues now I'm going
5375
04:01:16,080 --> 04:01:19,920
to put in and now I'm going to put down
5376
04:01:19,920 --> 04:01:21,840
first let's put uh
5377
04:01:21,840 --> 04:01:23,160
10
5378
04:01:23,160 --> 04:01:25,260
down
5379
04:01:25,260 --> 04:01:28,220
down to
5380
04:01:29,640 --> 04:01:31,680
one
5381
04:01:31,680 --> 04:01:35,160
let's add the curly braces
5382
04:01:35,160 --> 04:01:37,800
the printerland
5383
04:01:37,800 --> 04:01:40,260
and let's put I here
5384
04:01:40,260 --> 04:01:42,680
now this is gonna this is gonna count
5385
04:01:42,680 --> 04:01:45,000
downwards now it's gonna it's gonna
5386
04:01:45,000 --> 04:01:47,880
count from ten to one so if you run this
5387
04:01:47,880 --> 04:01:50,119
code
5388
04:01:57,359 --> 04:02:00,540
now get 10 9 so it's counting it's it's
5389
04:02:00,540 --> 04:02:03,479
counting backwards right another thing
5390
04:02:03,479 --> 04:02:04,680
that you can do is that you can
5391
04:02:04,680 --> 04:02:09,300
introduce an arbitrary step so let's uh
5392
04:02:09,300 --> 04:02:12,979
let's comment this code also
5393
04:02:14,340 --> 04:02:17,520
let's add below another call
5394
04:02:17,520 --> 04:02:20,699
another for Loop so here I'm gonna type
5395
04:02:20,699 --> 04:02:21,660
four
5396
04:02:21,660 --> 04:02:23,399
parenthesis
5397
04:02:23,399 --> 04:02:25,920
let's put I
5398
04:02:25,920 --> 04:02:29,279
and let's use uh done TL
5399
04:02:29,279 --> 04:02:30,779
so in
5400
04:02:30,779 --> 04:02:32,699
one
5401
04:02:32,699 --> 04:02:35,300
until
5402
04:02:36,359 --> 04:02:38,939
10. so now I can put a
5403
04:02:38,939 --> 04:02:43,560
an arbitrary step so at uh at this
5404
04:02:43,560 --> 04:02:47,000
arbitrary step that number which
5405
04:02:47,000 --> 04:02:50,399
is that is that is at dotted arbitrary
5406
04:02:50,399 --> 04:02:52,500
step is going to be skip so I can put
5407
04:02:52,500 --> 04:02:54,660
here step
5408
04:02:54,660 --> 04:02:57,000
to let's say
5409
04:02:57,000 --> 04:02:59,160
so now I'm gonna put the quarter Blazers
5410
04:02:59,160 --> 04:03:02,220
to have the some code executed I'm going
5411
04:03:02,220 --> 04:03:03,899
to put println
5412
04:03:03,899 --> 04:03:06,300
and let's put here I
5413
04:03:06,300 --> 04:03:09,679
so now if you run the squad
5414
04:03:12,120 --> 04:03:15,300
I get one three five seven and nine so
5415
04:03:15,300 --> 04:03:17,760
you can see that uh
5416
04:03:17,760 --> 04:03:21,540
at the second iteration the number is
5417
04:03:21,540 --> 04:03:23,819
Skip so we have one two is skipped now
5418
04:03:23,819 --> 04:03:26,819
we have then we have three four three
5419
04:03:26,819 --> 04:03:29,939
and four skips and so on so you can put
5420
04:03:29,939 --> 04:03:31,739
this arbitrary step if you want so let's
5421
04:03:31,739 --> 04:03:34,880
comment this code also
5422
04:03:39,239 --> 04:03:41,460
and let's add the first code that we had
5423
04:03:41,460 --> 04:03:42,479
here
5424
04:03:42,479 --> 04:03:44,520
to
5425
04:03:44,520 --> 04:03:47,460
to talk a little bit more so let's put I
5426
04:03:47,460 --> 04:03:48,540
in
5427
04:03:48,540 --> 04:03:51,319
one
5428
04:03:51,479 --> 04:03:54,600
let's put the range Dot 10.
5429
04:03:54,600 --> 04:03:57,060
and curly braces
5430
04:03:57,060 --> 04:04:00,560
you're gonna type println
5431
04:04:04,080 --> 04:04:06,620
hi
5432
04:04:08,160 --> 04:04:12,080
now if run this code
5433
04:04:18,359 --> 04:04:21,359
we'll get the same output that as at the
5434
04:04:21,359 --> 04:04:25,500
beginning of the video so get one
5435
04:04:25,500 --> 04:04:29,760
until 10 inclusive now again what this
5436
04:04:29,760 --> 04:04:34,080
uh loop is doing is going through this
5437
04:04:34,080 --> 04:04:37,800
is going to this to this range from 1 to
5438
04:04:37,800 --> 04:04:41,040
10 and it's executing the code inside
5439
04:04:41,040 --> 04:04:44,460
the curly braces 10 times and when it
5440
04:04:44,460 --> 04:04:47,580
reaches 10 it stops and it executes the
5441
04:04:47,580 --> 04:04:49,140
next line of code below here that
5442
04:04:49,140 --> 04:04:50,699
because here we don't have any line of
5443
04:04:50,699 --> 04:04:53,279
code our
5444
04:04:53,279 --> 04:04:57,899
program X is exited and is finished so
5445
04:04:57,899 --> 04:04:59,600
this is our discussion about for Loop
5446
04:04:59,600 --> 04:05:03,439
there are other Loops which can Loop
5447
04:05:03,439 --> 04:05:05,939
which can loop as long as a certain
5448
04:05:05,939 --> 04:05:09,840
condition is true not as long as a
5449
04:05:09,840 --> 04:05:12,600
certain number is reached so we're going
5450
04:05:12,600 --> 04:05:15,359
to look at the next Loops in the next
5451
04:05:15,359 --> 04:05:16,140
video
5452
04:05:16,140 --> 04:05:18,540
see you next
5453
04:05:18,540 --> 04:05:20,160
all right now it's time to look at the
5454
04:05:20,160 --> 04:05:21,420
next two Loops that you can use in
5455
04:05:21,420 --> 04:05:23,279
quadrant and those are the while and the
5456
04:05:23,279 --> 04:05:24,479
Dual Loop
5457
04:05:24,479 --> 04:05:26,939
but first let's open our previous
5458
04:05:26,939 --> 04:05:28,560
project called Loops because I'm going
5459
04:05:28,560 --> 04:05:30,660
to type my code there so open your
5460
04:05:30,660 --> 04:05:33,120
previous project and here I already have
5461
04:05:33,120 --> 04:05:34,800
deleted the code that we had previously
5462
04:05:34,800 --> 04:05:38,160
here but if you have the code delete it
5463
04:05:38,160 --> 04:05:40,680
and let's start our discussion about the
5464
04:05:40,680 --> 04:05:41,760
while loop
5465
04:05:41,760 --> 04:05:44,279
so first I'm going to type
5466
04:05:44,279 --> 04:05:46,800
while
5467
04:05:46,800 --> 04:05:48,720
this is this is how you declare the
5468
04:05:48,720 --> 04:05:49,979
while loop
5469
04:05:49,979 --> 04:05:52,580
and here you can press enter to fill in
5470
04:05:52,580 --> 04:05:55,620
the the wallykid on the parenthesis for
5471
04:05:55,620 --> 04:05:57,300
you or you can type manually the while
5472
04:05:57,300 --> 04:05:58,680
and the parenthesis so I'm going to
5473
04:05:58,680 --> 04:06:00,660
press enter to fill that for you fill in
5474
04:06:00,660 --> 04:06:02,160
that for me
5475
04:06:02,160 --> 04:06:04,260
and next you put a space and you put
5476
04:06:04,260 --> 04:06:06,359
curly brace here so I'm going to put the
5477
04:06:06,359 --> 04:06:08,279
left curly brace and press enter and the
5478
04:06:08,279 --> 04:06:09,420
radical base is going to be added
5479
04:06:09,420 --> 04:06:11,100
automatically as you can see on the line
5480
04:06:11,100 --> 04:06:14,580
five now
5481
04:06:14,580 --> 04:06:17,640
a while loop Loops as long as a certain
5482
04:06:17,640 --> 04:06:20,760
condition is true in contrast to the for
5483
04:06:20,760 --> 04:06:23,040
Loop which Loops for a specific number
5484
04:06:23,040 --> 04:06:26,819
of time while a loop Loops as long as a
5485
04:06:26,819 --> 04:06:29,760
condition or an expression is true so
5486
04:06:29,760 --> 04:06:34,760
this gives us more flexibility in a
5487
04:06:34,819 --> 04:06:38,040
circumstances or you don't know for what
5488
04:06:38,040 --> 04:06:40,560
specific number of times we want our
5489
04:06:40,560 --> 04:06:43,620
code to be repeated so
5490
04:06:43,620 --> 04:06:45,720
we put our condition inside the
5491
04:06:45,720 --> 04:06:47,220
parenthesis here
5492
04:06:47,220 --> 04:06:49,920
and as long as this condition is true
5493
04:06:49,920 --> 04:06:51,720
the code inside the current base is
5494
04:06:51,720 --> 04:06:54,359
going to be executed repeatedly so let's
5495
04:06:54,359 --> 04:06:56,220
see how we can do that so first let's
5496
04:06:56,220 --> 04:06:58,020
discard a variable here
5497
04:06:58,020 --> 04:07:00,180
I'm going to call it number and I'm
5498
04:07:00,180 --> 04:07:02,160
going to sign a value of 0 to it
5499
04:07:02,160 --> 04:07:05,939
and here I'm gonna type while number
5500
04:07:05,939 --> 04:07:09,260
is less than 10
5501
04:07:09,479 --> 04:07:11,520
inside the calibration I'm going to type
5502
04:07:11,520 --> 04:07:14,279
the println function
5503
04:07:14,279 --> 04:07:17,279
and we're going to Output the value of
5504
04:07:17,279 --> 04:07:19,680
our number and then we're going to
5505
04:07:19,680 --> 04:07:21,540
increment the value of a number so I'm
5506
04:07:21,540 --> 04:07:23,040
going to put number I'm going to use the
5507
04:07:23,040 --> 04:07:26,460
increment operator plus plus
5508
04:07:26,460 --> 04:07:28,800
let's put a space here to have things
5509
04:07:28,800 --> 04:07:33,319
more clear now if you run this code
5510
04:07:37,080 --> 04:07:39,779
you're going to see the numbers from 0
5511
04:07:39,779 --> 04:07:41,279
to 9.
5512
04:07:41,279 --> 04:07:44,279
so what is happening how this works
5513
04:07:44,279 --> 04:07:45,600
first
5514
04:07:45,600 --> 04:07:47,220
with the color variable with a sine of
5515
04:07:47,220 --> 04:07:50,160
value of 0 to it next we Loop using the
5516
04:07:50,160 --> 04:07:53,160
while loop and first the while loop is
5517
04:07:53,160 --> 04:07:54,660
checking to see if this condition inside
5518
04:07:54,660 --> 04:07:57,000
the parenthesis is true so it checks to
5519
04:07:57,000 --> 04:07:59,160
see if the value of our variable number
5520
04:07:59,160 --> 04:08:01,439
is less than 10 and it's true because we
5521
04:08:01,439 --> 04:08:03,779
have zero then it executes the code
5522
04:08:03,779 --> 04:08:07,800
inside the curly braces and it outputs
5523
04:08:07,800 --> 04:08:09,600
the value of our number in the console
5524
04:08:09,600 --> 04:08:12,000
using the printerland function then it
5525
04:08:12,000 --> 04:08:14,520
increments the value of our number
5526
04:08:14,520 --> 04:08:16,979
and then it Loops again but now the
5527
04:08:16,979 --> 04:08:19,260
value of our number is one so now it
5528
04:08:19,260 --> 04:08:21,899
will compare again if
5529
04:08:21,899 --> 04:08:24,060
the value for number is less than 10
5530
04:08:24,060 --> 04:08:26,520
which is true it will loop again it will
5531
04:08:26,520 --> 04:08:28,260
output its value it will increment this
5532
04:08:28,260 --> 04:08:30,660
value so now we have two and so on until
5533
04:08:30,660 --> 04:08:33,359
it reaches nine when it reaches 9 it
5534
04:08:33,359 --> 04:08:36,000
will increment the value it will it will
5535
04:08:36,000 --> 04:08:37,680
print it will output the value in the
5536
04:08:37,680 --> 04:08:40,020
console it will increment this value
5537
04:08:40,020 --> 04:08:42,180
then it will check again to see if this
5538
04:08:42,180 --> 04:08:44,220
condition is true but now the value is
5539
04:08:44,220 --> 04:08:45,239
going to be
5540
04:08:45,239 --> 04:08:48,300
10 because not it was incremented when
5541
04:08:48,300 --> 04:08:49,680
it was nine
5542
04:08:49,680 --> 04:08:51,420
and this condition is going to be ever
5543
04:08:51,420 --> 04:08:53,520
it's going to evaluate to false and the
5544
04:08:53,520 --> 04:08:55,199
code inside the cutter base is not going
5545
04:08:55,199 --> 04:08:57,239
to be executed this way we don't have 10
5546
04:08:57,239 --> 04:09:00,060
here and it executes the code that comes
5547
04:09:00,060 --> 04:09:01,680
below of our while loop but because you
5548
04:09:01,680 --> 04:09:03,000
don't have any code
5549
04:09:03,000 --> 04:09:06,120
the program is terminated
5550
04:09:06,120 --> 04:09:08,699
and you can write this thing in one
5551
04:09:08,699 --> 04:09:11,580
single line but it's recommended to use
5552
04:09:11,580 --> 04:09:14,760
this form only if you know that you have
5553
04:09:14,760 --> 04:09:18,239
one single statement in our Loop so if I
5554
04:09:18,239 --> 04:09:20,899
can delete this
5555
04:09:21,000 --> 04:09:24,420
this tool
5556
04:09:24,420 --> 04:09:27,960
and I can put here directly number plus
5557
04:09:27,960 --> 04:09:31,020
plus and because this Returns the value
5558
04:09:31,020 --> 04:09:33,000
it increments the value and it Returns
5559
04:09:33,000 --> 04:09:36,359
the values and expression I can
5560
04:09:36,359 --> 04:09:38,399
I can run this code and it will have the
5561
04:09:38,399 --> 04:09:41,000
same output
5562
04:09:41,040 --> 04:09:43,680
but if I delete the increment operator
5563
04:09:43,680 --> 04:09:46,140
out after the
5564
04:09:46,140 --> 04:09:50,100
variable and if I put it before
5565
04:09:50,100 --> 04:09:52,439
now I'm gonna see a different output
5566
04:09:52,439 --> 04:09:54,540
here because this increments the value
5567
04:09:54,540 --> 04:09:55,800
of
5568
04:09:55,800 --> 04:09:58,560
of our number then it decrements the
5569
04:09:58,560 --> 04:10:00,540
value of our number and this is called
5570
04:10:00,540 --> 04:10:04,620
prefix in increment increment so this is
5571
04:10:04,620 --> 04:10:07,920
called prefix incrementing and the the
5572
04:10:07,920 --> 04:10:11,279
previous one let's press Ctrl G
5573
04:10:11,279 --> 04:10:14,939
this is called postfix increment so this
5574
04:10:14,939 --> 04:10:17,220
is postfix because it's the end of our
5575
04:10:17,220 --> 04:10:20,100
variable and uh if you put the
5576
04:10:20,100 --> 04:10:23,040
incremental operator at the beginning of
5577
04:10:23,040 --> 04:10:25,080
our variable is called prefix oh
5578
04:10:25,080 --> 04:10:26,640
increment anyway
5579
04:10:26,640 --> 04:10:29,460
let's press Ctrl Z to have the code that
5580
04:10:29,460 --> 04:10:33,438
I had previously and the curly braces
5581
04:10:34,140 --> 04:10:36,180
now look what happens if I delete the
5582
04:10:36,180 --> 04:10:37,680
increment
5583
04:10:37,680 --> 04:10:39,600
if I don't increment all variable now
5584
04:10:39,600 --> 04:10:43,560
now uh let's just talk what is happening
5585
04:10:43,560 --> 04:10:45,620
because we don't increment the variable
5586
04:10:45,620 --> 04:10:48,000
and the variable always has the value of
5587
04:10:48,000 --> 04:10:50,100
zero this will always be true and it
5588
04:10:50,100 --> 04:10:51,840
will execute the code inside the current
5589
04:10:51,840 --> 04:10:54,000
places forever so we'll have what is
5590
04:10:54,000 --> 04:10:55,560
called an infinite Loop so if you run
5591
04:10:55,560 --> 04:10:57,920
this code
5592
04:11:01,260 --> 04:11:04,920
now you can see that it prints 0 forever
5593
04:11:04,920 --> 04:11:07,680
because this condition never ever has to
5594
04:11:07,680 --> 04:11:10,560
to to false so let's stop this by
5595
04:11:10,560 --> 04:11:12,720
pressing on this uh
5596
04:11:12,720 --> 04:11:16,979
Red Square so now the process the
5597
04:11:16,979 --> 04:11:19,080
program is terminated so this is why you
5598
04:11:19,080 --> 04:11:20,340
need to increment the value of our
5599
04:11:20,340 --> 04:11:23,580
number because we want that condition to
5600
04:11:23,580 --> 04:11:27,180
be false at some point in the in the in
5601
04:11:27,180 --> 04:11:29,939
the in the future
5602
04:11:29,939 --> 04:11:31,140
next
5603
04:11:31,140 --> 04:11:35,340
let's say that I assign here 10.
5604
04:11:35,340 --> 04:11:37,439
so if I run this code now this condition
5605
04:11:37,439 --> 04:11:39,239
is going to be false because 10 is not
5606
04:11:39,239 --> 04:11:41,580
less than 10. and the code inside the
5607
04:11:41,580 --> 04:11:43,380
curly brace is not going to be executive
5608
04:11:43,380 --> 04:11:45,180
so now we have no output here because
5609
04:11:45,180 --> 04:11:47,460
this condition is evaluates to false and
5610
04:11:47,460 --> 04:11:49,380
is not executing the code inside the
5611
04:11:49,380 --> 04:11:50,760
calibration but there are certain
5612
04:11:50,760 --> 04:11:52,380
situations where you'll want to have
5613
04:11:52,380 --> 04:11:55,140
your code executed at least once even
5614
04:11:55,140 --> 04:11:57,120
though the initial condition the initial
5615
04:11:57,120 --> 04:11:59,699
uh condition or there is expression is
5616
04:11:59,699 --> 04:12:02,340
false so if I delete the curly braces
5617
04:12:02,340 --> 04:12:03,840
and the code inside the curly braces
5618
04:12:03,840 --> 04:12:05,819
here
5619
04:12:05,819 --> 04:12:10,439
now to to do what I said if to execute
5620
04:12:10,439 --> 04:12:12,359
the the code even though the condition
5621
04:12:12,359 --> 04:12:15,720
is initially set to false to execute the
5622
04:12:15,720 --> 04:12:17,640
code at least once you need to use a do
5623
04:12:17,640 --> 04:12:19,080
while loop and to use a do while loop
5624
04:12:19,080 --> 04:12:21,359
you put here at do press enter to add
5625
04:12:21,359 --> 04:12:24,779
the calibrases and we put our while at
5626
04:12:24,779 --> 04:12:27,600
the end of of our enclosing curly brace
5627
04:12:27,600 --> 04:12:30,600
of Urdu and here inside the calibration
5628
04:12:30,600 --> 04:12:32,399
or I'm going to put our printerland
5629
04:12:32,399 --> 04:12:34,199
function
5630
04:12:34,199 --> 04:12:37,920
and I'm gonna output the value of our
5631
04:12:37,920 --> 04:12:41,060
variable number
5632
04:12:42,300 --> 04:12:45,500
so now if you run this
5633
04:12:48,540 --> 04:12:52,080
now get 10 because this executes the
5634
04:12:52,080 --> 04:12:54,300
code inside the curly braces until at
5635
04:12:54,300 --> 04:12:58,680
least once and after that it comes down
5636
04:12:58,680 --> 04:13:00,060
here and it checks to see if this
5637
04:13:00,060 --> 04:13:02,520
condition is true or false and if this
5638
04:13:02,520 --> 04:13:05,460
condition is true it will repeat the
5639
04:13:05,460 --> 04:13:06,600
code
5640
04:13:06,600 --> 04:13:09,420
but if the condition is false it uh it
5641
04:13:09,420 --> 04:13:11,399
will not repeat the code but the
5642
04:13:11,399 --> 04:13:13,939
important aspect here is that it will
5643
04:13:13,939 --> 04:13:16,500
execute the code inside the curly braces
5644
04:13:16,500 --> 04:13:19,680
at least once irrespective of our
5645
04:13:19,680 --> 04:13:22,020
condition if the condition is true of
5646
04:13:22,020 --> 04:13:24,239
order false in our case is false
5647
04:13:24,239 --> 04:13:26,819
so these are the three Loops that you
5648
04:13:26,819 --> 04:13:29,100
can use in a kotlin the for Loop the
5649
04:13:29,100 --> 04:13:30,960
while loop and the do while loop now
5650
04:13:30,960 --> 04:13:33,840
it's time to see how we can use the
5651
04:13:33,840 --> 04:13:36,960
continue and the break keywords with
5652
04:13:36,960 --> 04:13:39,840
those Loops so let's press Ctrl Z to
5653
04:13:39,840 --> 04:13:42,239
undo the code have the while loop back
5654
04:13:42,239 --> 04:13:44,040
because I'm going to start the while
5655
04:13:44,040 --> 04:13:46,920
loop and to see how we can use the but
5656
04:13:46,920 --> 04:13:48,779
you can continue cured with the while
5657
04:13:48,779 --> 04:13:50,819
loop
5658
04:13:50,819 --> 04:13:53,819
let's bring this up a little bit
5659
04:13:53,819 --> 04:13:55,439
so let's first look at the continue
5660
04:13:55,439 --> 04:13:57,479
keyword and the continue keyword can be
5661
04:13:57,479 --> 04:14:00,660
used to bypass a section of code so
5662
04:14:00,660 --> 04:14:03,000
let's say that I want to skip the number
5663
04:14:03,000 --> 04:14:08,340
seven in our Loop so I can type here if
5664
04:14:08,340 --> 04:14:10,739
our variable number
5665
04:14:10,739 --> 04:14:14,520
equals 2 so 2 equals seven let's put
5666
04:14:14,520 --> 04:14:16,560
curly braces press enter toward the
5667
04:14:16,560 --> 04:14:19,620
right calibrase continue
5668
04:14:19,620 --> 04:14:22,380
so now what this is gonna do is when it
5669
04:14:22,380 --> 04:14:25,020
is gonna reach the number seven and this
5670
04:14:25,020 --> 04:14:26,100
condition is going to be true it's gonna
5671
04:14:26,100 --> 04:14:28,080
call continue and it's going to bypass
5672
04:14:28,080 --> 04:14:30,960
the code that comes below of our if
5673
04:14:30,960 --> 04:14:34,460
statement so if you run this code
5674
04:14:34,620 --> 04:14:37,880
look uh what happens
5675
04:14:39,720 --> 04:14:43,140
it happens nothing because uh here our
5676
04:14:43,140 --> 04:14:45,180
number has a value of 10 so this
5677
04:14:45,180 --> 04:14:46,680
condition is false so the code is not
5678
04:14:46,680 --> 04:14:49,500
executed so let's put here zero
5679
04:14:49,500 --> 04:14:52,699
now if you run this code
5680
04:14:56,399 --> 04:15:01,199
so we get 0 1 2 3 4 5 and 6 but uh
5681
04:15:01,199 --> 04:15:03,600
and you see that I save at seven it uh
5682
04:15:03,600 --> 04:15:05,760
it skips the code but why I don't have
5683
04:15:05,760 --> 04:15:08,580
the next numbers this is because uh
5684
04:15:08,580 --> 04:15:10,560
unintentionally we created here an
5685
04:15:10,560 --> 04:15:12,899
infinite look so let's stop this and uh
5686
04:15:12,899 --> 04:15:15,319
to explain
5687
04:15:15,359 --> 04:15:18,060
so what happens here is that because
5688
04:15:18,060 --> 04:15:20,880
um we said we say here explicit to
5689
04:15:20,880 --> 04:15:23,399
you to check here explicit we check here
5690
04:15:23,399 --> 04:15:25,080
to see if our number is equal to seven
5691
04:15:25,080 --> 04:15:26,939
then we're gonna continue and in other
5692
04:15:26,939 --> 04:15:28,859
words you're gonna skip the code which
5693
04:15:28,859 --> 04:15:30,779
comes below of our
5694
04:15:30,779 --> 04:15:34,620
of order is the then statement then this
5695
04:15:34,620 --> 04:15:37,380
code is never called so this code is
5696
04:15:37,380 --> 04:15:39,479
never called the printerland and more
5697
04:15:39,479 --> 04:15:42,239
importantly the increment operator is
5698
04:15:42,239 --> 04:15:45,359
now never called on our number variable
5699
04:15:45,359 --> 04:15:47,220
and because of that
5700
04:15:47,220 --> 04:15:49,500
because our variable is not incremented
5701
04:15:49,500 --> 04:15:51,840
here either when it's gonna loop again
5702
04:15:51,840 --> 04:15:56,340
it will have again the value 7. so this
5703
04:15:56,340 --> 04:15:59,520
condition or the in the if is going to
5704
04:15:59,520 --> 04:16:02,279
evaluate again to true and it's going to
5705
04:16:02,279 --> 04:16:04,859
call continue again and because this
5706
04:16:04,859 --> 04:16:07,859
code again is never reached Discord and
5707
04:16:07,859 --> 04:16:10,199
the values no not not incremented it
5708
04:16:10,199 --> 04:16:13,080
will still have the value seven so this
5709
04:16:13,080 --> 04:16:15,660
is gonna evaluate again to true so it
5710
04:16:15,660 --> 04:16:18,660
does this forever and to change that we
5711
04:16:18,660 --> 04:16:20,340
need to put our
5712
04:16:20,340 --> 04:16:22,140
number
5713
04:16:22,140 --> 04:16:25,859
and which is incremented before our reef
5714
04:16:25,859 --> 04:16:27,660
and that is going to solve the problem
5715
04:16:27,660 --> 04:16:29,939
so now if I
5716
04:16:29,939 --> 04:16:32,040
Let's uh
5717
04:16:32,040 --> 04:16:34,520
now
5718
04:16:37,859 --> 04:16:42,439
if I run the code let's increase this
5719
04:16:51,060 --> 04:16:53,939
now get one two three four five
5720
04:16:53,939 --> 04:16:58,020
and seven is skipped then 8 9 and 10.
5721
04:16:58,020 --> 04:17:00,600
because now when this condition level is
5722
04:17:00,600 --> 04:17:02,399
to true it will continue it's not going
5723
04:17:02,399 --> 04:17:04,680
to print the code it's not going to be
5724
04:17:04,680 --> 04:17:07,859
executed below of our if and it will
5725
04:17:07,859 --> 04:17:10,020
come back to Loop and now it's going to
5726
04:17:10,020 --> 04:17:11,340
increment the value and the value is
5727
04:17:11,340 --> 04:17:12,359
going to be
5728
04:17:12,359 --> 04:17:15,540
8 instead of 7 this is going to evaluate
5729
04:17:15,540 --> 04:17:17,340
to false and the continue is not going
5730
04:17:17,340 --> 04:17:19,739
to be called it will print the number
5731
04:17:19,739 --> 04:17:22,500
eight and it will do the same for it it
5732
04:17:22,500 --> 04:17:24,720
will increment the value again and then
5733
04:17:24,720 --> 04:17:27,779
we'll have 9 and so on so this is how
5734
04:17:27,779 --> 04:17:29,939
you can use the continue keyword and you
5735
04:17:29,939 --> 04:17:31,500
have to be careful where you put your in
5736
04:17:31,500 --> 04:17:34,199
uh your whatever incremental variable
5737
04:17:34,199 --> 04:17:36,720
because I you saw you can easily get
5738
04:17:36,720 --> 04:17:38,399
intro infinite Loop
5739
04:17:38,399 --> 04:17:40,859
now you can have a more complex
5740
04:17:40,859 --> 04:17:43,140
expression here so you can put here if
5741
04:17:43,140 --> 04:17:48,000
number is greater than let's say 2 and
5742
04:17:48,000 --> 04:17:49,920
number
5743
04:17:49,920 --> 04:17:52,500
is less than eight
5744
04:17:52,500 --> 04:17:54,239
now if you run this
5745
04:17:54,239 --> 04:17:57,020
look what happens
5746
04:17:57,120 --> 04:18:00,479
now get one two and then we get 8 9 and
5747
04:18:00,479 --> 04:18:03,120
10 because what this condition now
5748
04:18:03,120 --> 04:18:07,500
checks is is if our number is greater
5749
04:18:07,500 --> 04:18:10,620
than 10 and less than 8 then continue
5750
04:18:10,620 --> 04:18:13,380
then skip that number so this is why the
5751
04:18:13,380 --> 04:18:16,620
numbers between 2 and 8 are skipped
5752
04:18:16,620 --> 04:18:20,880
because we call continue on on them and
5753
04:18:20,880 --> 04:18:22,859
if you hover over here you see that it
5754
04:18:22,859 --> 04:18:24,840
says the two comparison should be
5755
04:18:24,840 --> 04:18:26,580
converted to a rain check so you can
5756
04:18:26,580 --> 04:18:28,680
write the same condition that have here
5757
04:18:28,680 --> 04:18:32,100
using a range but this is different from
5758
04:18:32,100 --> 04:18:35,699
a range that you you've used in a for
5759
04:18:35,699 --> 04:18:37,739
Loop in the sense that this is now
5760
04:18:37,739 --> 04:18:40,560
checking to see if our number is in the
5761
04:18:40,560 --> 04:18:42,720
range 3
5762
04:18:42,720 --> 04:18:47,060
from 3 to 7 so if you run the squad
5763
04:18:50,160 --> 04:18:52,699
now I have the exact same output but now
5764
04:18:52,699 --> 04:18:56,399
we are using the range to check to see
5765
04:18:56,399 --> 04:18:58,199
if our number is in the other engine if
5766
04:18:58,199 --> 04:19:01,199
this is the underage we call continue to
5767
04:19:01,199 --> 04:19:02,880
skip that
5768
04:19:02,880 --> 04:19:03,720
um
5769
04:19:03,720 --> 04:19:05,460
the next thing that you can do is you
5770
04:19:05,460 --> 04:19:06,779
can
5771
04:19:06,779 --> 04:19:09,840
break the loop you can stop the loop
5772
04:19:09,840 --> 04:19:12,840
using the break here so I can say here
5773
04:19:12,840 --> 04:19:14,460
again if number
5774
04:19:14,460 --> 04:19:16,800
equals equals let's say seven
5775
04:19:16,800 --> 04:19:18,420
call break
5776
04:19:18,420 --> 04:19:20,819
so I'm going to type here break
5777
04:19:20,819 --> 04:19:22,920
now when this condition is going to vary
5778
04:19:22,920 --> 04:19:25,020
to true it's gonna call break and the
5779
04:19:25,020 --> 04:19:26,520
loop is going to stop it's not going to
5780
04:19:26,520 --> 04:19:28,739
execute any line of code after that it's
5781
04:19:28,739 --> 04:19:30,300
gonna stop it's gonna leave and it's
5782
04:19:30,300 --> 04:19:31,979
going to execute the code which comes
5783
04:19:31,979 --> 04:19:33,540
below here but in our case we don't
5784
04:19:33,540 --> 04:19:35,340
don't have any code
5785
04:19:35,340 --> 04:19:37,560
so you see that we have one two three
5786
04:19:37,560 --> 04:19:40,340
four five and six and when it reaches
5787
04:19:40,340 --> 04:19:44,279
seven it breaks and it nuts is stopping
5788
04:19:44,279 --> 04:19:46,859
the loop it's not printing the value and
5789
04:19:46,859 --> 04:19:49,560
it executes the loop so it terminates so
5790
04:19:49,560 --> 04:19:51,739
foreign
5791
04:19:53,279 --> 04:19:55,140
and we can do the same thing with the
5792
04:19:55,140 --> 04:19:57,359
for Loop so we can use the four we can
5793
04:19:57,359 --> 04:19:59,640
use the continue and
5794
04:19:59,640 --> 04:20:01,319
and break with a for Loop so you can
5795
04:20:01,319 --> 04:20:03,120
type here four
5796
04:20:03,120 --> 04:20:07,739
let's say I in let's create the
5797
04:20:07,739 --> 04:20:09,779
let's say
5798
04:20:09,779 --> 04:20:12,359
zero
5799
04:20:12,359 --> 04:20:15,979
to 10 let's create this range
5800
04:20:16,020 --> 04:20:18,120
let's add the curly braces
5801
04:20:18,120 --> 04:20:22,460
now I can say here if
5802
04:20:23,939 --> 04:20:26,660
I
5803
04:20:28,260 --> 04:20:29,819
in
5804
04:20:29,819 --> 04:20:32,220
let's say three
5805
04:20:32,220 --> 04:20:34,819
to eight
5806
04:20:37,020 --> 04:20:39,800
then continue
5807
04:20:40,199 --> 04:20:42,300
let's put in the volume
5808
04:20:42,300 --> 04:20:45,619
let's type here println
5809
04:20:46,979 --> 04:20:50,660
let's print the value of I here
5810
04:20:51,180 --> 04:20:54,319
so now if you run this code
5811
04:21:01,739 --> 04:21:05,460
so we have our first Loop which Loops a
5812
04:21:05,460 --> 04:21:08,580
to six and then it breaks because you
5813
04:21:08,580 --> 04:21:11,220
call break there then it's using the for
5814
04:21:11,220 --> 04:21:14,279
Loop it Loops it loops
5815
04:21:14,279 --> 04:21:18,540
from 0 to 10 but because here we're
5816
04:21:18,540 --> 04:21:21,840
checking to see if our number is in the
5817
04:21:21,840 --> 04:21:24,660
range three to eight and skip it you get
5818
04:21:24,660 --> 04:21:29,640
0 1 2 and then the numbers uh the three
5819
04:21:29,640 --> 04:21:33,080
and uh the numbers between the three and
5820
04:21:33,080 --> 04:21:35,699
the numbers between 3 and 8 inclusive
5821
04:21:35,699 --> 04:21:40,080
are skipped here so we have 0 1 2 and
5822
04:21:40,080 --> 04:21:42,479
then the numbers between two and nine
5823
04:21:42,479 --> 04:21:45,660
are skipped and we have nine and ten
5824
04:21:45,660 --> 04:21:48,000
and we can use also the brake keyword
5825
04:21:48,000 --> 04:21:50,819
with the four I can put here if I equals
5826
04:21:50,819 --> 04:21:54,080
equals seven
5827
04:21:56,699 --> 04:21:57,460
break
5828
04:21:57,460 --> 04:21:59,460
[Music]
5829
04:21:59,460 --> 04:22:02,778
so now if you run this code
5830
04:22:06,600 --> 04:22:09,899
now you get 0 1 2 3 4 5 6 and when it
5831
04:22:09,899 --> 04:22:12,479
reaches seven it breaks similar to our
5832
04:22:12,479 --> 04:22:15,239
while loop so this is how you can use
5833
04:22:15,239 --> 04:22:17,939
the break and continue cured with the
5834
04:22:17,939 --> 04:22:21,359
while and for Loop and you can use the
5835
04:22:21,359 --> 04:22:23,699
brake and continue cured also with the
5836
04:22:23,699 --> 04:22:25,260
Dual Loop
5837
04:22:25,260 --> 04:22:27,180
next we're gonna look at how you can
5838
04:22:27,180 --> 04:22:29,460
Nest Loops within each others and we're
5839
04:22:29,460 --> 04:22:30,960
going to look at an example using the
5840
04:22:30,960 --> 04:22:32,340
while loop so I'm going to delete the
5841
04:22:32,340 --> 04:22:34,199
code of the for Loop and the printable
5842
04:22:34,199 --> 04:22:35,760
statement there
5843
04:22:35,760 --> 04:22:37,500
I'm going to delete the if then
5844
04:22:37,500 --> 04:22:39,540
statement the break yard and I'm going
5845
04:22:39,540 --> 04:22:41,880
to bring the printerland function up
5846
04:22:41,880 --> 04:22:43,140
there
5847
04:22:43,140 --> 04:22:44,580
down here I'm going to declare a
5848
04:22:44,580 --> 04:22:46,560
variable it's going to be a VAR I'm
5849
04:22:46,560 --> 04:22:48,840
going to call it I and I'm gonna assign
5850
04:22:48,840 --> 04:22:51,960
a value of 0 to it now in inside the
5851
04:22:51,960 --> 04:22:53,220
while loop I'm going to type another
5852
04:22:53,220 --> 04:22:55,140
while loop here I'm going to put a
5853
04:22:55,140 --> 04:22:59,300
conditional while I is less than 5
5854
04:22:59,699 --> 04:23:02,819
we're gonna increment I and we're gonna
5855
04:23:02,819 --> 04:23:04,979
print and output the value of I in the
5856
04:23:04,979 --> 04:23:07,460
console but let's put some text here to
5857
04:23:07,460 --> 04:23:11,160
make uh clear what Loops what Loop is
5858
04:23:11,160 --> 04:23:13,140
looping so I'm going to put three
5859
04:23:13,140 --> 04:23:15,000
asterisk signs here
5860
04:23:15,000 --> 04:23:19,399
I'm gonna type the dollar sign and I
5861
04:23:20,460 --> 04:23:23,160
so now if you run this code let's also
5862
04:23:23,160 --> 04:23:24,899
change this condition too
5863
04:23:24,899 --> 04:23:27,920
less than five
5864
04:23:28,920 --> 04:23:32,060
so if you run this code
5865
04:23:39,680 --> 04:23:42,960
now we get first you get one then we get
5866
04:23:42,960 --> 04:23:46,500
our code inside of our inner while loop
5867
04:23:46,500 --> 04:23:49,439
which is the three asterisks and get one
5868
04:23:49,439 --> 04:23:51,359
two three four five then it Loops again
5869
04:23:51,359 --> 04:23:54,060
but let's take this chord line by line
5870
04:23:54,060 --> 04:23:56,160
to explain what is happening here
5871
04:23:56,160 --> 04:23:58,080
so first decade is variable call number
5872
04:23:58,080 --> 04:24:00,120
assign a value of zero to it then we
5873
04:24:00,120 --> 04:24:02,880
Loop using the while loop the outer loop
5874
04:24:02,880 --> 04:24:04,380
and it checks to see this condition
5875
04:24:04,380 --> 04:24:06,840
issue and is true zero is less than five
5876
04:24:06,840 --> 04:24:09,840
so it executes the code inside the curly
5877
04:24:09,840 --> 04:24:12,300
braces and it increments the value of a
5878
04:24:12,300 --> 04:24:14,399
number by one then it outputs the value
5879
04:24:14,399 --> 04:24:16,260
of our number in the console so you get
5880
04:24:16,260 --> 04:24:17,340
here one
5881
04:24:17,340 --> 04:24:19,739
then it declared this variable called I
5882
04:24:19,739 --> 04:24:23,279
and then it starts looping inside our
5883
04:24:23,279 --> 04:24:25,739
outer loop while using the inner loop
5884
04:24:25,739 --> 04:24:28,680
while so it Loops here five times
5885
04:24:28,680 --> 04:24:31,859
so when it finishes it goes back
5886
04:24:31,859 --> 04:24:34,020
and now the value was incremented by one
5887
04:24:34,020 --> 04:24:35,880
it will check again if the condition is
5888
04:24:35,880 --> 04:24:39,180
true it will uh execute the code inside
5889
04:24:39,180 --> 04:24:40,880
the calibration to increment the value
5890
04:24:40,880 --> 04:24:44,399
now so the it will have two it will
5891
04:24:44,399 --> 04:24:46,020
output that value so now we have two
5892
04:24:46,020 --> 04:24:47,160
here
5893
04:24:47,160 --> 04:24:50,279
and then it will loop again five star
5894
04:24:50,279 --> 04:24:53,040
five times using the inner loop and it
5895
04:24:53,040 --> 04:24:55,500
will do the same thing for the next
5896
04:24:55,500 --> 04:24:57,060
round and the next round until you get
5897
04:24:57,060 --> 04:24:58,080
to
5898
04:24:58,080 --> 04:25:00,000
5. when
5899
04:25:00,000 --> 04:25:03,060
we'll get to five five will be not be
5900
04:25:03,060 --> 04:25:05,279
less than five and this condition will
5901
04:25:05,279 --> 04:25:07,439
evaluate to to false and the code inside
5902
04:25:07,439 --> 04:25:10,260
the caliber is not going to be executed
5903
04:25:10,260 --> 04:25:12,739
right
5904
04:25:12,840 --> 04:25:15,420
and you can use the break keyword inside
5905
04:25:15,420 --> 04:25:19,739
the inner loop so I can say here if
5906
04:25:19,739 --> 04:25:21,779
you can put it above here so you can say
5907
04:25:21,779 --> 04:25:23,760
if
5908
04:25:23,760 --> 04:25:28,140
I equals equals zero
5909
04:25:28,140 --> 04:25:30,420
then break and now you can put the break
5910
04:25:30,420 --> 04:25:32,939
and continue cures in one single line if
5911
04:25:32,939 --> 04:25:36,680
you want you can avoid the curly braces
5912
04:25:36,920 --> 04:25:40,819
so now if you run this code
5913
04:25:46,620 --> 04:25:49,920
now get one two three four five because
5914
04:25:49,920 --> 04:25:53,279
now uh when it reaches the inner loop it
5915
04:25:53,279 --> 04:25:56,520
breaks immediately what every at every
5916
04:25:56,520 --> 04:26:00,120
iteration in the outer loop so when it
5917
04:26:00,120 --> 04:26:02,520
comes down here it will check to see if
5918
04:26:02,520 --> 04:26:03,899
the conditions through is going to be
5919
04:26:03,899 --> 04:26:05,520
true but when it gets to the if
5920
04:26:05,520 --> 04:26:07,560
statement it will break it will stop the
5921
04:26:07,560 --> 04:26:11,100
loop so it will execute only the Outer
5922
04:26:11,100 --> 04:26:12,239
Loop
5923
04:26:12,239 --> 04:26:15,659
and with a break keyword you can
5924
04:26:15,659 --> 04:26:19,500
not not Target only the loop so with
5925
04:26:19,500 --> 04:26:21,960
with this break keyword we stop you stop
5926
04:26:21,960 --> 04:26:25,859
only this while loop that uh
5927
04:26:25,859 --> 04:26:29,159
that we are currently that is currently
5928
04:26:29,159 --> 04:26:31,500
the nearest Loop but there is something
5929
04:26:31,500 --> 04:26:33,359
which which is called labels and with
5930
04:26:33,359 --> 04:26:35,880
labels you can Target an outer loop so
5931
04:26:35,880 --> 04:26:38,340
it can break the outer loop here so you
5932
04:26:38,340 --> 04:26:40,800
can break the while loop which is here
5933
04:26:40,800 --> 04:26:43,739
and to do that we we give to our while
5934
04:26:43,739 --> 04:26:46,319
loop a label so we type here a name I'm
5935
04:26:46,319 --> 04:26:50,220
going to call it outer and we put at
5936
04:26:50,220 --> 04:26:54,000
now here you can put break and now I'm
5937
04:26:54,000 --> 04:26:55,859
gonna type
5938
04:26:55,859 --> 04:26:57,120
add
5939
04:26:57,120 --> 04:27:00,000
and you put outer and this this should
5940
04:27:00,000 --> 04:27:01,800
be a single word
5941
04:27:01,800 --> 04:27:06,319
now if you run this look what happens
5942
04:27:11,699 --> 04:27:14,399
now get one because it breaks the outer
5943
04:27:14,399 --> 04:27:16,620
loop so it first evolves this condition
5944
04:27:16,620 --> 04:27:19,020
it increments the value of our number it
5945
04:27:19,020 --> 04:27:22,380
prints the value of a number and then it
5946
04:27:22,380 --> 04:27:24,479
goes in the Inner Loop it checks to see
5947
04:27:24,479 --> 04:27:26,520
if this condition is true it's true then
5948
04:27:26,520 --> 04:27:28,620
it's checked to see if I is equal to 0
5949
04:27:28,620 --> 04:27:31,800
which is uh true then it breaks but now
5950
04:27:31,800 --> 04:27:34,199
it's not breaking this Loop this inner
5951
04:27:34,199 --> 04:27:35,100
loop
5952
04:27:35,100 --> 04:27:38,760
it's breaking the Outer Loop so it's
5953
04:27:38,760 --> 04:27:40,560
breaking this Loop so this is why you
5954
04:27:40,560 --> 04:27:42,120
get only one and the code is not
5955
04:27:42,120 --> 04:27:43,800
executed because it breaks the outer
5956
04:27:43,800 --> 04:27:47,460
loop and the code our program or code
5957
04:27:47,460 --> 04:27:50,279
it's terminated because about down here
5958
04:27:50,279 --> 04:27:52,979
we don't have any code so this is how
5959
04:27:52,979 --> 04:27:54,600
you can use labels with the break
5960
04:27:54,600 --> 04:27:57,359
keyboard to Target an outer loop and the
5961
04:27:57,359 --> 04:27:59,520
labels can be used also with the
5962
04:27:59,520 --> 04:28:03,000
continue keyword and in other Loops that
5963
04:28:03,000 --> 04:28:05,100
we've looked at so this is our
5964
04:28:05,100 --> 04:28:08,040
discussion about loops what you can do I
5965
04:28:08,040 --> 04:28:09,380
should say is that you cannot Target
5966
04:28:09,380 --> 04:28:12,479
with the labels and inner loop from an
5967
04:28:12,479 --> 04:28:15,180
outer loop so you cannot
5968
04:28:15,180 --> 04:28:17,460
we can give here a name to a label to
5969
04:28:17,460 --> 04:28:19,680
our inner loop but you cannot Target our
5970
04:28:19,680 --> 04:28:22,979
inner from our outer loop so this is our
5971
04:28:22,979 --> 04:28:25,020
discussion about loops and the next
5972
04:28:25,020 --> 04:28:27,300
video we're going to do a challenge
5973
04:28:27,300 --> 04:28:29,159
all right so now it's time to do a
5974
04:28:29,159 --> 04:28:31,020
challenge using all the knowledge that
5975
04:28:31,020 --> 04:28:33,060
we have accumulated so far and I'm gonna
5976
04:28:33,060 --> 04:28:35,340
use the previous project to put my code
5977
04:28:35,340 --> 04:28:37,319
there but if you don't have the previous
5978
04:28:37,319 --> 04:28:39,479
project you can create a new project and
5979
04:28:39,479 --> 04:28:41,640
give it whatever name you want and type
5980
04:28:41,640 --> 04:28:43,140
your code there so I'm going to open my
5981
04:28:43,140 --> 04:28:44,640
previous project
5982
04:28:44,640 --> 04:28:47,279
I'm going to delete the code inside the
5983
04:28:47,279 --> 04:28:49,979
main function
5984
04:28:49,979 --> 04:28:53,358
now what is the challenge
5985
04:28:53,640 --> 04:28:55,680
the challenge is to
5986
04:28:55,680 --> 04:28:57,720
create an arbitrary range of numbers
5987
04:28:57,720 --> 04:29:00,120
then we need to find a way to go to that
5988
04:29:00,120 --> 04:29:02,279
arbitrary range of numbers
5989
04:29:02,279 --> 04:29:06,779
and to determine if a number is an even
5990
04:29:06,779 --> 04:29:09,420
number or if a number is another number
5991
04:29:09,420 --> 04:29:12,180
and based on that if the number is an
5992
04:29:12,180 --> 04:29:14,520
odd number skip that number
5993
04:29:14,520 --> 04:29:16,859
but if the number is an even number then
5994
04:29:16,859 --> 04:29:18,960
output a number in the console
5995
04:29:18,960 --> 04:29:21,120
additionally should create a way to
5996
04:29:21,120 --> 04:29:24,239
count all the even numbers that you
5997
04:29:24,239 --> 04:29:27,000
found in your range and display the
5998
04:29:27,000 --> 04:29:30,420
total number so if neither numbers
5999
04:29:30,420 --> 04:29:32,100
um at the end of your program
6000
04:29:32,100 --> 04:29:35,880
so try to do this solution and after
6001
04:29:35,880 --> 04:29:39,000
that you can watch my solution to this
6002
04:29:39,000 --> 04:29:40,739
so first I'm going to decide I'm going
6003
04:29:40,739 --> 04:29:43,140
to declare a variable call number
6004
04:29:43,140 --> 04:29:46,439
I'm going to assign a value of 1 to it
6005
04:29:46,439 --> 04:29:47,760
next I'm going to declare another
6006
04:29:47,760 --> 04:29:49,319
variable but this is going to be a Val
6007
04:29:49,319 --> 04:29:51,960
it's going to be called last number
6008
04:29:51,960 --> 04:29:54,060
and it's going to have a value of 20.
6009
04:29:54,060 --> 04:29:57,600
now I'm gonna Loop using the while loop
6010
04:29:57,600 --> 04:29:59,340
and in the parantes you're going to type
6011
04:29:59,340 --> 04:30:00,899
while number
6012
04:30:00,899 --> 04:30:04,819
is less than equal to last number
6013
04:30:04,920 --> 04:30:06,479
then you're going to put curly braces
6014
04:30:06,479 --> 04:30:10,698
now I'm going to increment our number
6015
04:30:11,460 --> 04:30:13,319
now here you need to add the logic to
6016
04:30:13,319 --> 04:30:15,120
determine if a number is an even number
6017
04:30:15,120 --> 04:30:16,979
or not and to do that I'm going to add
6018
04:30:16,979 --> 04:30:19,500
the function at the end of our enclosing
6019
04:30:19,500 --> 04:30:21,239
curly brace of our many functions so I'm
6020
04:30:21,239 --> 04:30:24,060
going to type here the fun keyword
6021
04:30:24,060 --> 04:30:25,800
now you need to give a name to our
6022
04:30:25,800 --> 04:30:30,239
function it's going to be called is even
6023
04:30:30,239 --> 04:30:32,399
number
6024
04:30:32,399 --> 04:30:34,199
I'm gonna put parenthesis and it's going
6025
04:30:34,199 --> 04:30:36,180
to take a parameter called number of
6026
04:30:36,180 --> 04:30:37,739
type int
6027
04:30:37,739 --> 04:30:40,560
this function and it's gonna return a
6028
04:30:40,560 --> 04:30:42,779
Boolean value true or false this
6029
04:30:42,779 --> 04:30:45,180
function so you put colon and we type
6030
04:30:45,180 --> 04:30:48,239
Boolean then you put curly braces and
6031
04:30:48,239 --> 04:30:49,920
now we'll add the logic to determine if
6032
04:30:49,920 --> 04:30:51,899
any number is even or not so you type
6033
04:30:51,899 --> 04:30:54,560
here if
6034
04:30:54,899 --> 04:30:56,939
and now inside the parenthesis we put
6035
04:30:56,939 --> 04:30:59,340
another set of parentheses
6036
04:30:59,340 --> 04:31:01,800
and here we refer our parameter number
6037
04:31:01,800 --> 04:31:04,859
that we have defined above and here we
6038
04:31:04,859 --> 04:31:07,500
put the modulus operator because the
6039
04:31:07,500 --> 04:31:09,300
modulus operator gives us the reminder
6040
04:31:09,300 --> 04:31:11,399
of a division so we put a modulus
6041
04:31:11,399 --> 04:31:13,680
operator 2.
6042
04:31:13,680 --> 04:31:16,680
now outside our inner parenthesis we put
6043
04:31:16,680 --> 04:31:20,100
equals equals zero then we put curly
6044
04:31:20,100 --> 04:31:21,479
braces
6045
04:31:21,479 --> 04:31:24,679
and return true
6046
04:31:25,319 --> 04:31:29,239
else we're gonna return
6047
04:31:30,479 --> 04:31:33,300
false now how this logic works
6048
04:31:33,300 --> 04:31:34,560
so
6049
04:31:34,560 --> 04:31:38,760
our num our number parameter which is
6050
04:31:38,760 --> 04:31:39,899
defined here it's going to take an
6051
04:31:39,899 --> 04:31:41,880
argument let's say that it's going to
6052
04:31:41,880 --> 04:31:44,279
take an arbitrary number
6053
04:31:44,279 --> 04:31:46,319
then he's gonna check to see if that
6054
04:31:46,319 --> 04:31:49,500
number divided by 2 has no reminder if
6055
04:31:49,500 --> 04:31:51,720
it's and if it has no reminder then that
6056
04:31:51,720 --> 04:31:53,460
number is an even number because the
6057
04:31:53,460 --> 04:31:55,620
because even numbers when they are
6058
04:31:55,620 --> 04:31:58,620
divided by two they have no reminder
6059
04:31:58,620 --> 04:32:01,199
and if that is the case if the reminder
6060
04:32:01,199 --> 04:32:04,500
equals equals to zero our number is an
6061
04:32:04,500 --> 04:32:06,180
even number and it's gonna return true
6062
04:32:06,180 --> 04:32:07,680
and their spot is not going to be
6063
04:32:07,680 --> 04:32:08,819
executed
6064
04:32:08,819 --> 04:32:12,600
now let's add the logic here
6065
04:32:12,600 --> 04:32:15,060
list user function here because as you
6066
04:32:15,060 --> 04:32:17,220
can see now it's uh gray out it's not
6067
04:32:17,220 --> 04:32:20,040
used so let's type here if
6068
04:32:20,040 --> 04:32:22,020
and in the parenthesis you're gonna type
6069
04:32:22,020 --> 04:32:24,600
is even number our functions we're going
6070
04:32:24,600 --> 04:32:26,220
to call our function here and we're
6071
04:32:26,220 --> 04:32:29,040
going to pass a parameter to it called
6072
04:32:29,040 --> 04:32:32,840
now I'm going to pass the parameter
6073
04:32:33,600 --> 04:32:35,520
we're going to pass an argument and the
6074
04:32:35,520 --> 04:32:36,720
argument is going to be the variable
6075
04:32:36,720 --> 04:32:38,939
that we Define above as you can see you
6076
04:32:38,939 --> 04:32:41,460
have the V there so press enter to fill
6077
04:32:41,460 --> 04:32:43,859
on that for you let's press Ctrl alt L
6078
04:32:43,859 --> 04:32:45,899
to format the code
6079
04:32:45,899 --> 04:32:47,279
now
6080
04:32:47,279 --> 04:32:49,979
if we press shift Ctrl p on this you're
6081
04:32:49,979 --> 04:32:51,120
going to see that this is a Boolean
6082
04:32:51,120 --> 04:32:53,159
expression because this returns true or
6083
04:32:53,159 --> 04:32:55,020
false so we can put the logical not
6084
04:32:55,020 --> 04:32:57,359
operator in front of it to change its
6085
04:32:57,359 --> 04:32:59,159
value to switch his value from True to
6086
04:32:59,159 --> 04:33:02,760
false and from and from false to true
6087
04:33:02,760 --> 04:33:05,340
so let's put curly braces
6088
04:33:05,340 --> 04:33:07,500
and here we're gonna call our continue
6089
04:33:07,500 --> 04:33:11,039
quick keyword down here I'm gonna add
6090
04:33:11,039 --> 04:33:12,900
a printerland
6091
04:33:12,900 --> 04:33:15,840
which is going to Output the the number
6092
04:33:15,840 --> 04:33:18,419
in our console
6093
04:33:18,419 --> 04:33:21,539
next we need to add the logic to to
6094
04:33:21,539 --> 04:33:24,419
count the total uh the total even
6095
04:33:24,419 --> 04:33:26,520
numbers which are found so I'm gonna
6096
04:33:26,520 --> 04:33:29,160
declare a VAR it's going to be
6097
04:33:29,160 --> 04:33:32,938
even numbers
6098
04:33:34,561 --> 04:33:37,020
counter and I'm going to assign a value
6099
04:33:37,020 --> 04:33:39,539
0 to it and here we're gonna increment
6100
04:33:39,539 --> 04:33:43,219
our even numbers counter
6101
04:33:43,500 --> 04:33:47,118
so now if you run this code
6102
04:33:52,680 --> 04:33:57,599
we get 2 4 6 8 10 12 14 16 18 and 20. so
6103
04:33:57,599 --> 04:33:59,580
these are the even numbers that they are
6104
04:33:59,580 --> 04:34:01,680
that they found that the our program
6105
04:34:01,680 --> 04:34:03,778
found in is correct but let's add a
6106
04:34:03,778 --> 04:34:05,820
print line at the end of our Loop to
6107
04:34:05,820 --> 04:34:08,278
print the total number of numbers of
6108
04:34:08,278 --> 04:34:11,219
even numbers found so let's put println
6109
04:34:11,219 --> 04:34:13,859
here and let's put let's refer our even
6110
04:34:13,859 --> 04:34:17,480
numbers count here
6111
04:34:18,180 --> 04:34:20,400
you have a numbers counter so if you run
6112
04:34:20,400 --> 04:34:22,641
this
6113
04:34:26,160 --> 04:34:28,680
let's put some text actually let's put
6114
04:34:28,680 --> 04:34:31,320
this in curly braces so let's type here
6115
04:34:31,320 --> 04:34:32,879
total
6116
04:34:32,879 --> 04:34:36,439
number of even
6117
04:34:36,840 --> 04:34:39,480
numbers found
6118
04:34:39,480 --> 04:34:41,458
equals
6119
04:34:41,458 --> 04:34:44,239
dollar sign
6120
04:34:47,580 --> 04:34:50,719
now if you run this
6121
04:34:55,859 --> 04:34:57,719
now you get total numbers of you know
6122
04:34:57,719 --> 04:34:59,400
when even numbers found this 10 and this
6123
04:34:59,400 --> 04:35:03,000
is correct so in an output correctly or
6124
04:35:03,000 --> 04:35:05,879
even numbers and it show that the total
6125
04:35:05,879 --> 04:35:08,039
number two newer numbers found this 10.
6126
04:35:08,039 --> 04:35:11,520
so let's now think about how this logic
6127
04:35:11,520 --> 04:35:13,500
works here
6128
04:35:13,500 --> 04:35:16,320
so let's look at the if statement that
6129
04:35:16,320 --> 04:35:18,480
we have here so what is doing in this
6130
04:35:18,480 --> 04:35:21,660
thing here so let's take an um an um
6131
04:35:21,660 --> 04:35:24,419
specific example a specific number to
6132
04:35:24,419 --> 04:35:26,879
store to see how this works so let's see
6133
04:35:26,879 --> 04:35:29,219
that here we pass four and it's gonna
6134
04:35:29,219 --> 04:35:31,141
use the logic to determine if this
6135
04:35:31,141 --> 04:35:33,359
number is an even number and if that
6136
04:35:33,359 --> 04:35:34,980
number is any of a number is going to
6137
04:35:34,980 --> 04:35:38,660
return true so this
6138
04:35:38,958 --> 04:35:41,160
this function which is a Boolean
6139
04:35:41,160 --> 04:35:44,160
expression is going to return true and
6140
04:35:44,160 --> 04:35:46,379
then we're gonna negate this value gonna
6141
04:35:46,379 --> 04:35:48,958
change this value from True to false and
6142
04:35:48,958 --> 04:35:52,080
continuously not gonna be called and the
6143
04:35:52,080 --> 04:35:54,900
code which comes below which is uh our
6144
04:35:54,900 --> 04:35:58,799
variable which uh counts how many even
6145
04:35:58,799 --> 04:36:03,180
numbers uh we have then it it increments
6146
04:36:03,180 --> 04:36:04,980
then it outputs that number in the
6147
04:36:04,980 --> 04:36:07,379
console so we have our even number four
6148
04:36:07,379 --> 04:36:10,799
output output it in the console
6149
04:36:10,799 --> 04:36:14,699
but if you pass here 3 that the number
6150
04:36:14,699 --> 04:36:16,080
is not going to be an even number
6151
04:36:16,080 --> 04:36:18,599
because has a reminder and this is going
6152
04:36:18,599 --> 04:36:22,320
to return uh false and because of here
6153
04:36:22,320 --> 04:36:23,938
you put the logical not operator that is
6154
04:36:23,938 --> 04:36:25,799
going to be switched to true
6155
04:36:25,799 --> 04:36:27,778
and the continue that's going to
6156
04:36:27,778 --> 04:36:29,340
continue keyword is going to be called
6157
04:36:29,340 --> 04:36:32,340
and our code Channel which comes below
6158
04:36:32,340 --> 04:36:34,799
here which is uh our variable even
6159
04:36:34,799 --> 04:36:36,180
number counter is not going to be
6160
04:36:36,180 --> 04:36:37,500
incremented
6161
04:36:37,500 --> 04:36:40,561
and our number respect to our number
6162
04:36:40,561 --> 04:36:43,020
three now it's not going to be outputted
6163
04:36:43,020 --> 04:36:46,199
in the console so this is how this works
6164
04:36:46,199 --> 04:36:50,400
so this is our solution to our
6165
04:36:50,400 --> 04:36:52,320
to our challenge of course you can
6166
04:36:52,320 --> 04:36:54,599
simplify this so if you hover over here
6167
04:36:54,599 --> 04:36:56,520
because you have all of those underlines
6168
04:36:56,520 --> 04:36:58,680
you can see that says remove redundancy
6169
04:36:58,680 --> 04:37:00,000
if statement
6170
04:37:00,000 --> 04:37:02,458
so this can be simplified to this this
6171
04:37:02,458 --> 04:37:05,039
does the exact same thing it is going to
6172
04:37:05,039 --> 04:37:08,759
return true if this uh divided by 2 has
6173
04:37:08,759 --> 04:37:10,199
no reminder otherwise it's going to
6174
04:37:10,199 --> 04:37:12,480
return false and this can can be
6175
04:37:12,480 --> 04:37:15,118
simplified even further because if here
6176
04:37:15,118 --> 04:37:17,759
we have a single expression and we know
6177
04:37:17,759 --> 04:37:19,500
when we have a single expression we can
6178
04:37:19,500 --> 04:37:21,240
write our function as a single
6179
04:37:21,240 --> 04:37:23,699
expression function so I can remove the
6180
04:37:23,699 --> 04:37:25,680
curly braces here
6181
04:37:25,680 --> 04:37:27,958
you can remove
6182
04:37:27,958 --> 04:37:30,778
the bully return type you can remove the
6183
04:37:30,778 --> 04:37:32,340
return keyword
6184
04:37:32,340 --> 04:37:34,980
I'm gonna put the equal zero and this
6185
04:37:34,980 --> 04:37:37,438
now if you run this code
6186
04:37:37,438 --> 04:37:40,618
this will work
6187
04:37:40,618 --> 04:37:43,218
the same
6188
04:37:43,259 --> 04:37:46,199
so we have the same output here
6189
04:37:46,199 --> 04:37:48,539
now I use the function here because
6190
04:37:48,539 --> 04:37:51,118
we've talked about functions in our
6191
04:37:51,118 --> 04:37:53,699
previous videos but you can remove the
6192
04:37:53,699 --> 04:37:56,699
function completely and add the logic to
6193
04:37:56,699 --> 04:37:59,278
determine if a new number is even or not
6194
04:37:59,278 --> 04:38:01,740
directly in the if statement so you can
6195
04:38:01,740 --> 04:38:03,840
put here
6196
04:38:03,840 --> 04:38:06,660
if
6197
04:38:06,660 --> 04:38:10,799
let's put parenthesis our number
6198
04:38:10,799 --> 04:38:12,599
now we're going to use the modulus
6199
04:38:12,599 --> 04:38:15,180
operator tool
6200
04:38:15,180 --> 04:38:17,520
not equal
6201
04:38:17,520 --> 04:38:19,020
to zero
6202
04:38:19,020 --> 04:38:21,359
then continue and this will have the
6203
04:38:21,359 --> 04:38:23,641
same effect so if you run this code
6204
04:38:23,641 --> 04:38:27,560
this will work in the same way
6205
04:38:27,660 --> 04:38:30,539
so we have the same code here because
6206
04:38:30,539 --> 04:38:33,061
this does the exact that the function
6207
04:38:33,061 --> 04:38:34,740
did previously it will check to see if
6208
04:38:34,740 --> 04:38:37,199
this is a new number if this is any of a
6209
04:38:37,199 --> 04:38:38,099
number
6210
04:38:38,099 --> 04:38:39,778
the
6211
04:38:39,778 --> 04:38:41,820
this is not going to be true because
6212
04:38:41,820 --> 04:38:43,980
hero checking to see if this is not
6213
04:38:43,980 --> 04:38:47,458
equal to zero and our number is equal to
6214
04:38:47,458 --> 04:38:48,958
zero so this condition inside the
6215
04:38:48,958 --> 04:38:51,599
parenthesis is going to be false the
6216
04:38:51,599 --> 04:38:53,340
continue is not going to be called and
6217
04:38:53,340 --> 04:38:55,141
the our code below is going to be called
6218
04:38:55,141 --> 04:38:57,000
our even number counter is going to be
6219
04:38:57,000 --> 04:38:59,458
incremented and our number is going to
6220
04:38:59,458 --> 04:39:02,039
be output in the console so this does
6221
04:39:02,039 --> 04:39:04,259
the same thing as previously
6222
04:39:04,259 --> 04:39:06,719
and we can do this challenge also in the
6223
04:39:06,719 --> 04:39:10,438
first of the four Loop if you want
6224
04:39:10,438 --> 04:39:13,020
so you can type here four
6225
04:39:13,020 --> 04:39:16,259
I in one
6226
04:39:16,259 --> 04:39:19,561
to 20 then execute the code inside the
6227
04:39:19,561 --> 04:39:23,240
curly braces and here you can
6228
04:39:25,020 --> 04:39:28,219
check if
6229
04:39:30,958 --> 04:39:35,039
parenthesis again if I present
6230
04:39:35,039 --> 04:39:38,779
2 not equal to
6231
04:39:40,440 --> 04:39:42,480
zero
6232
04:39:42,480 --> 04:39:44,940
then call continue
6233
04:39:44,940 --> 04:39:46,378
then
6234
04:39:46,378 --> 04:39:49,260
else is gonna execute the code which
6235
04:39:49,260 --> 04:39:52,020
comes below here
6236
04:39:52,020 --> 04:39:55,760
which is let's first
6237
04:39:56,940 --> 04:39:59,580
assign a value for your to a even number
6238
04:39:59,580 --> 04:40:01,440
counter to zero
6239
04:40:01,440 --> 04:40:03,840
because where when we're gonna enter in
6240
04:40:03,840 --> 04:40:07,020
the for Loop we should have a zero to
6241
04:40:07,020 --> 04:40:09,000
not have the previous value which was
6242
04:40:09,000 --> 04:40:10,440
20.
6243
04:40:10,440 --> 04:40:12,540
so let's put 0 here and here you can
6244
04:40:12,540 --> 04:40:16,280
increment or even number counter
6245
04:40:16,378 --> 04:40:20,400
plus plus and we're gonna print our even
6246
04:40:20,400 --> 04:40:23,458
number so I'm going to print I here
6247
04:40:23,458 --> 04:40:25,860
so if you run this code
6248
04:40:25,860 --> 04:40:29,780
let's run it from here
6249
04:40:33,600 --> 04:40:35,580
but let's add the printerland to make
6250
04:40:35,580 --> 04:40:37,560
things more clear so I'm going to add
6251
04:40:37,560 --> 04:40:40,160
here print
6252
04:40:44,040 --> 04:40:46,020
let's put
6253
04:40:46,020 --> 04:40:50,360
quotation marks and here I'm gonna type
6254
04:40:50,640 --> 04:40:52,560
total
6255
04:40:52,560 --> 04:40:54,980
number
6256
04:40:57,958 --> 04:41:00,739
phone
6257
04:41:01,920 --> 04:41:04,140
using four
6258
04:41:04,140 --> 04:41:06,920
Loop
6259
04:41:07,798 --> 04:41:10,340
r
6260
04:41:12,718 --> 04:41:14,520
let's put the dollar sign now let's
6261
04:41:14,520 --> 04:41:16,980
refer our even number variable
6262
04:41:16,980 --> 04:41:20,180
now if you run this
6263
04:41:24,298 --> 04:41:27,000
now I have the exact same output but now
6264
04:41:27,000 --> 04:41:28,920
we're using the
6265
04:41:28,920 --> 04:41:31,260
first you're using the while loop then
6266
04:41:31,260 --> 04:41:33,540
you're using the photo clip so let's
6267
04:41:33,540 --> 04:41:34,920
let's add the printerline statement
6268
04:41:34,920 --> 04:41:37,020
between our
6269
04:41:37,020 --> 04:41:39,180
Loops to make clear which Loop is
6270
04:41:39,180 --> 04:41:40,920
looping so let's draw the print line
6271
04:41:40,920 --> 04:41:42,480
here
6272
04:41:42,480 --> 04:41:45,060
and let's type here
6273
04:41:45,060 --> 04:41:47,400
now
6274
04:41:47,400 --> 04:41:49,940
we
6275
04:41:52,020 --> 04:41:55,020
so we are
6276
04:41:55,020 --> 04:41:57,540
looping
6277
04:41:57,540 --> 04:42:00,798
using the
6278
04:42:01,378 --> 04:42:02,638
four
6279
04:42:02,638 --> 04:42:05,280
Loop so now if you run this
6280
04:42:05,280 --> 04:42:09,378
to make more clear which Loop is looping
6281
04:42:12,420 --> 04:42:14,878
so now as you can see first the while
6282
04:42:14,878 --> 04:42:18,120
loop Loops then we have our printerland
6283
04:42:18,120 --> 04:42:19,138
which
6284
04:42:19,138 --> 04:42:22,138
says that now our while loop our for
6285
04:42:22,138 --> 04:42:24,180
Loop starts looping then we loop with
6286
04:42:24,180 --> 04:42:25,740
the for Loop
6287
04:42:25,740 --> 04:42:28,940
then at the end
6288
04:42:29,160 --> 04:42:31,980
we output the total number total number
6289
04:42:31,980 --> 04:42:33,958
of even only fever numbers found with
6290
04:42:33,958 --> 04:42:37,320
the while loop we have 10 then we output
6291
04:42:37,320 --> 04:42:40,440
the total number found the total number
6292
04:42:40,440 --> 04:42:42,840
of even numbers found using the for Loop
6293
04:42:42,840 --> 04:42:46,440
and r10 so our code works perfectly well
6294
04:42:46,440 --> 04:42:48,718
and
6295
04:42:48,718 --> 04:42:51,958
uh this do not does not represent the
6296
04:42:51,958 --> 04:42:54,920
the best
6297
04:42:56,520 --> 04:42:58,920
the best way to solve this challenge of
6298
04:42:58,920 --> 04:43:00,298
course that you can
6299
04:43:00,298 --> 04:43:03,180
find a totally different way a better
6300
04:43:03,180 --> 04:43:05,340
way to solve this so
6301
04:43:05,340 --> 04:43:08,040
this was my this was just my way of
6302
04:43:08,040 --> 04:43:09,660
solving the problem
6303
04:43:09,660 --> 04:43:12,360
so see you in the next video and how I
6304
04:43:12,360 --> 04:43:14,340
hope that you enjoy this challenge so
6305
04:43:14,340 --> 04:43:15,600
now it's time to start a discussion
6306
04:43:15,600 --> 04:43:17,458
about the race but first let's create a
6307
04:43:17,458 --> 04:43:20,878
new project I'm going to call it a race
6308
04:43:20,878 --> 04:43:22,798
for the language select kotlin for the
6309
04:43:22,798 --> 04:43:24,780
build system IntelliJ make sure to have
6310
04:43:24,780 --> 04:43:26,940
the jdk selected and check this little
6311
04:43:26,940 --> 04:43:29,458
box at sample code to have the main
6312
04:43:29,458 --> 04:43:31,440
function Auto generator for us and click
6313
04:43:31,440 --> 04:43:33,739
on create
6314
04:43:34,680 --> 04:43:36,420
all right now let's delete the code
6315
04:43:36,420 --> 04:43:37,920
inside the calibrases of the main
6316
04:43:37,920 --> 04:43:40,280
function
6317
04:43:41,580 --> 04:43:43,378
let's hide the project Pane and let's
6318
04:43:43,378 --> 04:43:46,020
start our discussion about arrays and so
6319
04:43:46,020 --> 04:43:47,700
far in our videos we've only looked at
6320
04:43:47,700 --> 04:43:49,440
how we can store only one single value
6321
04:43:49,440 --> 04:43:52,200
in a variable and that is very useful
6322
04:43:52,200 --> 04:43:56,120
but what if you have a large amount of
6323
04:43:56,120 --> 04:43:58,138
values that you want to store in our
6324
04:43:58,138 --> 04:43:59,580
variables let's say that you want to
6325
04:43:59,580 --> 04:44:01,200
store
6326
04:44:01,200 --> 04:44:03,320
30 or 40 different
6327
04:44:03,320 --> 04:44:06,360
values in our variable of a certain type
6328
04:44:06,360 --> 04:44:08,400
of course that you can declare a
6329
04:44:08,400 --> 04:44:11,360
variable for each of one of those
6330
04:44:11,360 --> 04:44:14,458
values then and then assign it to the
6331
04:44:14,458 --> 04:44:16,878
variables but what happens if you have a
6332
04:44:16,878 --> 04:44:19,080
1000 or 10 20
6333
04:44:19,080 --> 04:44:19,940
000
6334
04:44:19,940 --> 04:44:22,920
values in that case uh it will not be
6335
04:44:22,920 --> 04:44:25,320
practically possible to write each one
6336
04:44:25,320 --> 04:44:26,760
of those variables and then assign a
6337
04:44:26,760 --> 04:44:30,420
value to it in a colon help us here
6338
04:44:30,420 --> 04:44:33,420
because it provides us arrays and an
6339
04:44:33,420 --> 04:44:36,540
array allows us to store more than one
6340
04:44:36,540 --> 04:44:39,958
single element in a variable name so it
6341
04:44:39,958 --> 04:44:42,840
allows us to store more more elements in
6342
04:44:42,840 --> 04:44:44,940
one single variable name
6343
04:44:44,940 --> 04:44:49,560
now to declare an array we type Val
6344
04:44:49,560 --> 04:44:51,060
hero type
6345
04:44:51,060 --> 04:44:52,920
names
6346
04:44:52,920 --> 04:44:54,958
next we need to provide the type and the
6347
04:44:54,958 --> 04:44:57,420
type is going to be an array
6348
04:44:57,420 --> 04:45:00,060
and IntelliJ is giving us some
6349
04:45:00,060 --> 04:45:01,920
suggestion there so press enter to fill
6350
04:45:01,920 --> 04:45:04,260
it in that for you and now we put angle
6351
04:45:04,260 --> 04:45:06,600
brackets and inside the angle brackets
6352
04:45:06,600 --> 04:45:07,860
we're going to put what kind of data
6353
04:45:07,860 --> 04:45:09,600
this array is going to store so it's
6354
04:45:09,600 --> 04:45:12,600
going to store string or text or
6355
04:45:12,600 --> 04:45:14,100
sequence of characters
6356
04:45:14,100 --> 04:45:17,400
then you put equals and here we don't
6357
04:45:17,400 --> 04:45:19,740
type political value here we have a hero
6358
04:45:19,740 --> 04:45:21,420
type of function call and here we type
6359
04:45:21,420 --> 04:45:24,138
array of
6360
04:45:24,240 --> 04:45:27,600
and now we put our elements inside the
6361
04:45:27,600 --> 04:45:31,798
the parenthesis of the function called
6362
04:45:31,798 --> 04:45:33,360
separated by comma so I'm going to put
6363
04:45:33,360 --> 04:45:35,760
three names here so I'm going to put
6364
04:45:35,760 --> 04:45:37,920
John
6365
04:45:37,920 --> 04:45:39,480
Steven
6366
04:45:39,480 --> 04:45:41,820
and Megan
6367
04:45:41,820 --> 04:45:44,580
now let's uh let's see what we have here
6368
04:45:44,580 --> 04:45:46,980
so first we have the Val keyword next we
6369
04:45:46,980 --> 04:45:49,320
have a name for our variable and the
6370
04:45:49,320 --> 04:45:52,080
name is names then we have the type and
6371
04:45:52,080 --> 04:45:55,080
the type is an array which means that
6372
04:45:55,080 --> 04:45:57,120
you can store multiple values in our
6373
04:45:57,120 --> 04:45:59,760
variable and next we have angle brackets
6374
04:45:59,760 --> 04:46:01,560
and string and this means that this area
6375
04:46:01,560 --> 04:46:05,100
is going to store text or a sequence of
6376
04:46:05,100 --> 04:46:07,680
characters it's gonna store only strings
6377
04:46:07,680 --> 04:46:09,900
and then you put equals we have the
6378
04:46:09,900 --> 04:46:11,700
function called array of and then we put
6379
04:46:11,700 --> 04:46:14,400
our elements of type string which is
6380
04:46:14,400 --> 04:46:17,700
those names separated by comma but
6381
04:46:17,700 --> 04:46:20,700
because cotton has Type inference here
6382
04:46:20,700 --> 04:46:22,920
you can delete the type
6383
04:46:22,920 --> 04:46:25,080
and the type is going to be inferred in
6384
04:46:25,080 --> 04:46:26,820
the same way it was it was inferred
6385
04:46:26,820 --> 04:46:29,100
previously when I when you just declare
6386
04:46:29,100 --> 04:46:31,138
a simple variable so now the type is
6387
04:46:31,138 --> 04:46:32,638
still inferred as an array of strings
6388
04:46:32,638 --> 04:46:36,240
and now it's a good moment to introduce
6389
04:46:36,240 --> 04:46:39,780
type hints but we need to activate the
6390
04:46:39,780 --> 04:46:42,660
typings from settings and to do that you
6391
04:46:42,660 --> 04:46:44,820
can go to file
6392
04:46:44,820 --> 04:46:46,980
and click on settings or you can press
6393
04:46:46,980 --> 04:46:49,080
the the shortcut that you have here for
6394
04:46:49,080 --> 04:46:52,080
our particular system and for Windows is
6395
04:46:52,080 --> 04:46:54,540
Ctrl alt s so I'm going to press Ctrl
6396
04:46:54,540 --> 04:46:56,900
this
6397
04:46:57,540 --> 04:47:00,480
and here we go to if you have something
6398
04:47:00,480 --> 04:47:02,340
like this you go to editor click on this
6399
04:47:02,340 --> 04:47:04,740
greater arrow in front of editor
6400
04:47:04,740 --> 04:47:07,620
and then you go down here where it says
6401
04:47:07,620 --> 04:47:10,500
what it says inline hints click on on
6402
04:47:10,500 --> 04:47:12,420
this Creator are in front of inline
6403
04:47:12,420 --> 04:47:14,160
hints and from
6404
04:47:14,160 --> 04:47:16,320
those options select kotlin
6405
04:47:16,320 --> 04:47:18,780
and here you go to types and select
6406
04:47:18,780 --> 04:47:22,260
select local variable types then click
6407
04:47:22,260 --> 04:47:24,679
apply
6408
04:47:25,020 --> 04:47:27,120
and okay
6409
04:47:27,120 --> 04:47:29,820
now what you have here on the right of
6410
04:47:29,820 --> 04:47:33,540
our variable name is a hint so it's not
6411
04:47:33,540 --> 04:47:35,520
real text this is just there to remind
6412
04:47:35,520 --> 04:47:38,638
us what kind of type this variable has
6413
04:47:38,638 --> 04:47:40,440
and in our case is an array of string
6414
04:47:40,440 --> 04:47:43,020
and my suggestion for this to keep those
6415
04:47:43,020 --> 04:47:44,878
scenes activated because in this way
6416
04:47:44,878 --> 04:47:47,458
you'll always see what type of variable
6417
04:47:47,458 --> 04:47:50,520
is without explicitly declaring the type
6418
04:47:50,520 --> 04:47:53,340
so this is not uh this is this is just
6419
04:47:53,340 --> 04:47:56,218
there to help us to remind us what kind
6420
04:47:56,218 --> 04:47:58,620
of type a variable is
6421
04:47:58,620 --> 04:48:01,080
now let's say that I want to Output a
6422
04:48:01,080 --> 04:48:02,820
certain element in the array in the
6423
04:48:02,820 --> 04:48:03,840
console
6424
04:48:03,840 --> 04:48:05,940
to do that we cannot just type here
6425
04:48:05,940 --> 04:48:08,638
println and pass names because you need
6426
04:48:08,638 --> 04:48:10,560
more to be more specific than that we
6427
04:48:10,560 --> 04:48:14,160
cannot pass the array here and to get a
6428
04:48:14,160 --> 04:48:16,500
specific element in the array you need
6429
04:48:16,500 --> 04:48:18,900
to know that the elements in the array
6430
04:48:18,900 --> 04:48:22,920
are stored at indexes and the index of
6431
04:48:22,920 --> 04:48:26,160
an array always starts at zero so the
6432
04:48:26,160 --> 04:48:28,260
first element is stored at the index 0
6433
04:48:28,260 --> 04:48:30,540
and to get the first element we tab we
6434
04:48:30,540 --> 04:48:31,560
type here
6435
04:48:31,560 --> 04:48:35,340
square brackets and you put 0 and that
6436
04:48:35,340 --> 04:48:37,378
if you press Ctrl
6437
04:48:37,378 --> 04:48:40,920
if you press shift Ctrl p on it
6438
04:48:40,920 --> 04:48:43,440
it's a string because that is going to
6439
04:48:43,440 --> 04:48:45,958
return as the first value in the array
6440
04:48:45,958 --> 04:48:49,680
which is uh John so if you run this code
6441
04:48:49,680 --> 04:48:52,200
we get John but let's put some text here
6442
04:48:52,200 --> 04:48:53,458
to make it more
6443
04:48:53,458 --> 04:48:55,500
clear that this is the first element so
6444
04:48:55,500 --> 04:48:57,240
let's type first
6445
04:48:57,240 --> 04:48:59,340
element now let's choose the dollar sign
6446
04:48:59,340 --> 04:49:03,000
because this now is a placeholder and
6447
04:49:03,000 --> 04:49:07,100
let's put the curly braces around our
6448
04:49:07,500 --> 04:49:10,320
our element
6449
04:49:10,320 --> 04:49:13,020
and the quotation mark here now if you
6450
04:49:13,020 --> 04:49:15,560
run this code
6451
04:49:18,480 --> 04:49:20,878
you get first element John let's put
6452
04:49:20,878 --> 04:49:23,240
here some
6453
04:49:25,200 --> 04:49:27,660
so get the first element John because uh
6454
04:49:27,660 --> 04:49:31,378
John is stored at the index 0. and in
6455
04:49:31,378 --> 04:49:32,940
the same way
6456
04:49:32,940 --> 04:49:36,180
John is stored at the index 0 Steven is
6457
04:49:36,180 --> 04:49:38,160
stored at the index one and Megan is
6458
04:49:38,160 --> 04:49:41,820
stored at the index two so our index
6459
04:49:41,820 --> 04:49:44,100
starts at zero the index of an array
6460
04:49:44,100 --> 04:49:47,040
always starts at zero and you can
6461
04:49:47,040 --> 04:49:48,120
replace
6462
04:49:48,120 --> 04:49:51,240
a specific in an element in the array
6463
04:49:51,240 --> 04:49:54,900
also using the index operator so you can
6464
04:49:54,900 --> 04:49:56,340
type here
6465
04:49:56,340 --> 04:49:58,680
names again
6466
04:49:58,680 --> 04:50:01,378
square brackets zero
6467
04:50:01,378 --> 04:50:03,360
equals so now I'm going to assign a new
6468
04:50:03,360 --> 04:50:06,060
value to our element stored at index 0
6469
04:50:06,060 --> 04:50:08,458
which is now put here
6470
04:50:08,458 --> 04:50:10,620
the value
6471
04:50:10,620 --> 04:50:15,200
Alex so now if you run this code
6472
04:50:18,420 --> 04:50:21,660
now get first elementalics because
6473
04:50:21,660 --> 04:50:25,798
John was John which is at index 0 was
6474
04:50:25,798 --> 04:50:28,680
replaced with the value Alex so now we
6475
04:50:28,680 --> 04:50:32,340
have Alex Steven and Megan but what you
6476
04:50:32,340 --> 04:50:35,760
can't do is you cannot get an uh element
6477
04:50:35,760 --> 04:50:38,218
at neither and an index that doesn't
6478
04:50:38,218 --> 04:50:42,060
exist so we cannot get here let's say an
6479
04:50:42,060 --> 04:50:44,878
element of the index 4. because if you
6480
04:50:44,878 --> 04:50:48,080
run this look what happens
6481
04:50:51,660 --> 04:50:53,040
we get
6482
04:50:53,040 --> 04:50:56,040
an error which says array index out of
6483
04:50:56,040 --> 04:50:59,040
bond extension index 4 out of bonds
6484
04:50:59,040 --> 04:51:01,920
bonds for length 3. that is because the
6485
04:51:01,920 --> 04:51:05,400
index 4 doesn't exist and here we try to
6486
04:51:05,400 --> 04:51:08,340
access that value and that value at that
6487
04:51:08,340 --> 04:51:10,798
value and that index doesn't exist and
6488
04:51:10,798 --> 04:51:13,320
this is called the runtime error because
6489
04:51:13,320 --> 04:51:18,480
this error is is called only one while
6490
04:51:18,480 --> 04:51:21,298
our app is still running so you don't
6491
04:51:21,298 --> 04:51:24,240
have here an error like we have for uh
6492
04:51:24,240 --> 04:51:27,600
here we don't have a compile error which
6493
04:51:27,600 --> 04:51:30,540
you saw in our previous videos that is
6494
04:51:30,540 --> 04:51:32,160
because
6495
04:51:32,160 --> 04:51:35,520
the size of an array is not known we
6496
04:51:35,520 --> 04:51:37,620
tell our compile time also it's very
6497
04:51:37,620 --> 04:51:40,260
important to know here that the size of
6498
04:51:40,260 --> 04:51:43,200
an array is fixed so this will always
6499
04:51:43,200 --> 04:51:47,700
have the size three and uh the only way
6500
04:51:47,700 --> 04:51:50,040
to add new elements to it is to put the
6501
04:51:50,040 --> 04:51:52,638
elements directly here but there is not
6502
04:51:52,638 --> 04:51:55,920
another way to let's say that I want I
6503
04:51:55,920 --> 04:51:57,780
can put another element in the array
6504
04:51:57,780 --> 04:52:00,180
down here we can only replace elements
6505
04:52:00,180 --> 04:52:03,000
and then in the next videos you're gonna
6506
04:52:03,000 --> 04:52:06,000
see that there are other arrays
6507
04:52:06,000 --> 04:52:08,820
which are more flexible in the sense
6508
04:52:08,820 --> 04:52:10,980
that they can grow and Shrink we can add
6509
04:52:10,980 --> 04:52:15,420
the or remove elements from those arrays
6510
04:52:15,420 --> 04:52:18,060
but for now uh we're gonna look just at
6511
04:52:18,060 --> 04:52:20,040
the for this array
6512
04:52:20,040 --> 04:52:21,840
so let's change this back to zero to
6513
04:52:21,840 --> 04:52:23,580
don't have that error
6514
04:52:23,580 --> 04:52:24,840
and um
6515
04:52:24,840 --> 04:52:27,480
to avoid this kind of Errors you can
6516
04:52:27,480 --> 04:52:29,878
check to see what is the size of the
6517
04:52:29,878 --> 04:52:31,560
array in other words to see what is the
6518
04:52:31,560 --> 04:52:34,500
number of elements stored in Array using
6519
04:52:34,500 --> 04:52:36,958
the size variable so let's do that so
6520
04:52:36,958 --> 04:52:38,040
I'm going to add the printer lens
6521
04:52:38,040 --> 04:52:39,900
statement below for first one so I'm
6522
04:52:39,900 --> 04:52:42,920
going to type here println
6523
04:52:45,000 --> 04:52:46,680
and here I'm going to put the text the
6524
04:52:46,680 --> 04:52:48,240
size of the array is
6525
04:52:48,240 --> 04:52:49,560
and here I'm going to put the dollar
6526
04:52:49,560 --> 04:52:52,080
sign curly braces now I'm going to use
6527
04:52:52,080 --> 04:52:54,600
our names array
6528
04:52:54,600 --> 04:52:57,180
and you're going to put that
6529
04:52:57,180 --> 04:52:58,440
size
6530
04:52:58,440 --> 04:53:00,900
and this is gonna give us the size of
6531
04:53:00,900 --> 04:53:03,120
the array or or it's going to give us
6532
04:53:03,120 --> 04:53:05,700
the another words the number of elements
6533
04:53:05,700 --> 04:53:07,798
which are stored in this particular
6534
04:53:07,798 --> 04:53:10,620
array so if you run this code
6535
04:53:10,620 --> 04:53:12,060
you're going to see first output the
6536
04:53:12,060 --> 04:53:13,920
first element is Alex
6537
04:53:13,920 --> 04:53:15,780
and then you're going to see the size of
6538
04:53:15,780 --> 04:53:18,980
the array is 3.
6539
04:53:19,920 --> 04:53:22,320
so we get first element Alex and the
6540
04:53:22,320 --> 04:53:24,180
size of the array is three and this is
6541
04:53:24,180 --> 04:53:26,100
very important to note that the size of
6542
04:53:26,100 --> 04:53:30,120
the array is always one times greater
6543
04:53:30,120 --> 04:53:32,458
than the last index in the array so we
6544
04:53:32,458 --> 04:53:35,280
have an array which starts as index 0
6545
04:53:35,280 --> 04:53:38,520
and and ends at index two
6546
04:53:38,520 --> 04:53:41,160
which has a size of three so very
6547
04:53:41,160 --> 04:53:44,040
important to remember this to avoid the
6548
04:53:44,040 --> 04:53:47,580
crashes and your programming and of
6549
04:53:47,580 --> 04:53:49,378
course that you can put other data types
6550
04:53:49,378 --> 04:53:51,120
in our array so I can declare another
6551
04:53:51,120 --> 04:53:53,820
array here also Val
6552
04:53:53,820 --> 04:53:55,860
called
6553
04:53:55,860 --> 04:53:58,400
numbers
6554
04:53:58,500 --> 04:54:01,980
and we can use the array of again
6555
04:54:01,980 --> 04:54:05,480
and here I can pass some numbers
6556
04:54:06,060 --> 04:54:08,160
and now you see the type is an array of
6557
04:54:08,160 --> 04:54:11,400
Fint or an array of integers or whole
6558
04:54:11,400 --> 04:54:14,638
numbers and you can also mix types here
6559
04:54:14,638 --> 04:54:16,560
so you can put here
6560
04:54:16,560 --> 04:54:20,060
numbers and strings
6561
04:54:20,580 --> 04:54:23,160
so now we have another and this which is
6562
04:54:23,160 --> 04:54:25,020
between our angle brackets you don't
6563
04:54:25,020 --> 04:54:26,340
know you don't need to understand this
6564
04:54:26,340 --> 04:54:29,580
for now but this allows us to have array
6565
04:54:29,580 --> 04:54:32,458
of two types respectively of type int
6566
04:54:32,458 --> 04:54:35,280
and of type string and you can post also
6567
04:54:35,280 --> 04:54:38,780
a chart here if you want
6568
04:54:39,180 --> 04:54:42,780
let's say a so we can create an array of
6569
04:54:42,780 --> 04:54:44,760
any type if you want
6570
04:54:44,760 --> 04:54:47,400
so this allows us to
6571
04:54:47,400 --> 04:54:52,260
to put to mix our types in in the array
6572
04:54:52,260 --> 04:54:54,900
and because we've looked at loops and
6573
04:54:54,900 --> 04:54:56,820
how you can use loops
6574
04:54:56,820 --> 04:54:59,340
you can use Loops to Loop to this array
6575
04:54:59,340 --> 04:55:01,200
so I can put here
6576
04:55:01,200 --> 04:55:03,000
let's say that I want to use the first
6577
04:55:03,000 --> 04:55:06,920
array so I can put here four
6578
04:55:07,620 --> 04:55:09,000
name
6579
04:55:09,000 --> 04:55:11,580
in names now I'm going to refer our
6580
04:55:11,580 --> 04:55:15,200
array of strings
6581
04:55:16,260 --> 04:55:18,920
then
6582
04:55:19,080 --> 04:55:20,580
print
6583
04:55:20,580 --> 04:55:24,180
print A lender name
6584
04:55:24,180 --> 04:55:26,040
so now you see that we have this type
6585
04:55:26,040 --> 04:55:29,160
hint for our name variable that we have
6586
04:55:29,160 --> 04:55:31,020
here of course this can have whatever
6587
04:55:31,020 --> 04:55:33,900
name you want now if you run this you're
6588
04:55:33,900 --> 04:55:36,540
gonna see our three names outputted down
6589
04:55:36,540 --> 04:55:39,500
here in the console
6590
04:55:41,638 --> 04:55:44,940
so now we have Alex Stefan Steven and
6591
04:55:44,940 --> 04:55:48,660
Megan because uh now you're we're using
6592
04:55:48,660 --> 04:55:53,100
the for Loop to Loop through our array
6593
04:55:53,100 --> 04:55:54,600
names
6594
04:55:54,600 --> 04:55:56,218
and uh
6595
04:55:56,218 --> 04:55:58,200
this is interesting but what if you want
6596
04:55:58,200 --> 04:56:00,600
to Loop through our array numbers which
6597
04:56:00,600 --> 04:56:03,420
is a mixed array we have numbers and
6598
04:56:03,420 --> 04:56:08,179
strings and charts we can refer it here
6599
04:56:11,480 --> 04:56:14,218
let's call it
6600
04:56:14,218 --> 04:56:15,420
I
6601
04:56:15,420 --> 04:56:17,899
in
6602
04:56:21,540 --> 04:56:24,540
and now you see the type that was
6603
04:56:24,540 --> 04:56:26,638
inferred for the eye is this comparable
6604
04:56:26,638 --> 04:56:28,200
that we have here
6605
04:56:28,200 --> 04:56:30,060
so this
6606
04:56:30,060 --> 04:56:32,160
is going to Loop through our mixed array
6607
04:56:32,160 --> 04:56:33,900
so we put here I
6608
04:56:33,900 --> 04:56:35,580
now if you run this
6609
04:56:35,580 --> 04:56:38,520
you're gonna see our
6610
04:56:38,520 --> 04:56:40,740
numbers and in our name one and our
6611
04:56:40,740 --> 04:56:44,060
chart output down here
6612
04:56:45,298 --> 04:56:48,060
so now we get
6613
04:56:48,060 --> 04:56:49,560
first element Alex which is a
6614
04:56:49,560 --> 04:56:51,600
printerland statement then the next
6615
04:56:51,600 --> 04:56:53,700
print line statement the size of the red
6616
04:56:53,700 --> 04:56:55,080
then you have our
6617
04:56:55,080 --> 04:56:58,138
numbers in our numbers array
6618
04:56:58,138 --> 04:57:00,660
and then we have our name one and our
6619
04:57:00,660 --> 04:57:02,700
and then we have our chart
6620
04:57:02,700 --> 04:57:04,980
so this allows us to to Loop through our
6621
04:57:04,980 --> 04:57:07,560
mix arrays but what if I want to
6622
04:57:07,560 --> 04:57:09,480
to Output
6623
04:57:09,480 --> 04:57:12,900
only if I want to Output in the console
6624
04:57:12,900 --> 04:57:15,480
only the integers in this mixed are
6625
04:57:15,480 --> 04:57:18,420
equal numbers to to do that and this is
6626
04:57:18,420 --> 04:57:19,520
a
6627
04:57:19,520 --> 04:57:21,260
good good
6628
04:57:21,260 --> 04:57:25,440
way to introduce this keyword and with
6629
04:57:25,440 --> 04:57:28,860
this keyword we can check to see if
6630
04:57:28,860 --> 04:57:31,500
a literal or a variable is supposed
6631
04:57:31,500 --> 04:57:36,718
certain type so I can put here if
6632
04:57:36,718 --> 04:57:38,040
I
6633
04:57:38,040 --> 04:57:41,218
is let's say an INT
6634
04:57:41,218 --> 04:57:43,680
then and only then I'm gonna output this
6635
04:57:43,680 --> 04:57:45,060
value in the console then I'm going to
6636
04:57:45,060 --> 04:57:47,520
call our printerland and I'm gonna post
6637
04:57:47,520 --> 04:57:50,218
our password I variable to our println
6638
04:57:50,218 --> 04:57:51,298
function
6639
04:57:51,298 --> 04:57:54,180
now if you run this you gonna you
6640
04:57:54,180 --> 04:57:57,680
gonna see in our Loop only
6641
04:57:57,680 --> 04:58:00,660
the numbers here only the integers
6642
04:58:00,660 --> 04:58:01,580
because
6643
04:58:01,580 --> 04:58:05,280
this is keyword is checking to see if
6644
04:58:05,280 --> 04:58:07,740
our right is of type integer and this is
6645
04:58:07,740 --> 04:58:10,020
going to return true or false in our
6646
04:58:10,020 --> 04:58:11,760
case it's going to return true for our
6647
04:58:11,760 --> 04:58:13,620
for all of our numbers that we have here
6648
04:58:13,620 --> 04:58:15,958
so we have four five six seven and four
6649
04:58:15,958 --> 04:58:16,860
again
6650
04:58:16,860 --> 04:58:20,218
and you can check to see if this is a HR
6651
04:58:20,218 --> 04:58:25,080
I'm gonna see in the console only our
6652
04:58:25,080 --> 04:58:30,138
single element a I'll put it down here
6653
04:58:32,040 --> 04:58:34,500
so we have a or you can check to see if
6654
04:58:34,500 --> 04:58:37,400
this is a string
6655
04:58:37,400 --> 04:58:40,980
this element that We're looping at this
6656
04:58:40,980 --> 04:58:42,540
particular iteration let's say so if you
6657
04:58:42,540 --> 04:58:44,400
run this you're gonna see in the console
6658
04:58:44,400 --> 04:58:47,458
only name one because only name one is
6659
04:58:47,458 --> 04:58:50,420
of type string
6660
04:58:50,638 --> 04:58:53,298
so I get name one so this is very
6661
04:58:53,298 --> 04:58:55,620
important to know that with the is
6662
04:58:55,620 --> 04:58:57,718
keyword you can check to see if a
6663
04:58:57,718 --> 04:58:59,878
variable or a literally is of a certain
6664
04:58:59,878 --> 04:59:02,340
type or if it's not and based on that
6665
04:59:02,340 --> 04:59:06,260
too to just to do some
6666
04:59:06,740 --> 04:59:09,660
in our case you output that value in the
6667
04:59:09,660 --> 04:59:12,120
console if that condition is true
6668
04:59:12,120 --> 04:59:14,280
so this is how you can use the is
6669
04:59:14,280 --> 04:59:17,340
keyword and the for Loop to
6670
04:59:17,340 --> 04:59:19,798
to the for Loop to Loop through our
6671
04:59:19,798 --> 04:59:22,440
array and these keyword to check to see
6672
04:59:22,440 --> 04:59:25,260
if an element in our arrays of a certain
6673
04:59:25,260 --> 04:59:29,120
type or if it's not and let's change our
6674
04:59:29,120 --> 04:59:31,560
array numbers to a different name
6675
04:59:31,560 --> 04:59:33,958
because numbers is very specific it
6676
04:59:33,958 --> 04:59:35,820
implies that we only have numbers in
6677
04:59:35,820 --> 04:59:36,958
this array
6678
04:59:36,958 --> 04:59:41,400
but we have a mixed type so we have
6679
04:59:41,400 --> 04:59:43,798
multiple types in uh our array don't
6680
04:59:43,798 --> 04:59:45,240
have only numbers
6681
04:59:45,240 --> 04:59:47,940
and to do that instead of changing our
6682
04:59:47,940 --> 04:59:50,580
numbers array and everywhere in our code
6683
04:59:50,580 --> 04:59:53,340
where you're using the this variable we
6684
04:59:53,340 --> 04:59:55,138
can right click on it
6685
04:59:55,138 --> 04:59:58,560
go to refactor and click on rename or
6686
04:59:58,560 --> 05:00:02,240
you can click shift F6
6687
05:00:03,298 --> 05:00:06,120
now if I change the name of our array
6688
05:00:06,120 --> 05:00:08,700
numbers it will be change everywhere
6689
05:00:08,700 --> 05:00:11,700
where you call this variable so look if
6690
05:00:11,700 --> 05:00:14,280
I delete now the name the name is also
6691
05:00:14,280 --> 05:00:17,760
changed in our line of Code 9 we're
6692
05:00:17,760 --> 05:00:20,458
using in the loop so now if I change
6693
05:00:20,458 --> 05:00:25,500
this to let's call it mixed
6694
05:00:27,958 --> 05:00:30,958
elements
6695
05:00:31,920 --> 05:00:34,740
if I press enter now this will be also
6696
05:00:34,740 --> 05:00:37,798
changed everywhere or we call our our
6697
05:00:37,798 --> 05:00:41,160
variable respectively on the line 9 here
6698
05:00:41,160 --> 05:00:44,760
so now we have our our
6699
05:00:44,760 --> 05:00:47,040
array numbers change to mixed Elements
6700
05:00:47,040 --> 05:00:50,100
which is more uh descriptive because it
6701
05:00:50,100 --> 05:00:52,440
implies that we have an array of mixed
6702
05:00:52,440 --> 05:00:53,638
elements
6703
05:00:53,638 --> 05:00:55,680
so now it's time to do a challenge using
6704
05:00:55,680 --> 05:00:57,180
the knowledge that we have about arrays
6705
05:00:57,180 --> 05:00:58,740
but first of all I'm going to delete the
6706
05:00:58,740 --> 05:01:00,298
code inside the curry base of the main
6707
05:01:00,298 --> 05:01:02,878
function because I have the previous
6708
05:01:02,878 --> 05:01:04,860
project opened
6709
05:01:04,860 --> 05:01:07,080
now the challenge is to create a
6710
05:01:07,080 --> 05:01:08,878
function which is going to take as an
6711
05:01:08,878 --> 05:01:12,180
argument an array of integers then you
6712
05:01:12,180 --> 05:01:15,298
need to figure out a way to determine
6713
05:01:15,298 --> 05:01:17,820
which number is the biggest number in
6714
05:01:17,820 --> 05:01:20,760
that array then we need to return that
6715
05:01:20,760 --> 05:01:23,100
number to the function
6716
05:01:23,100 --> 05:01:25,260
and you need to create another function
6717
05:01:25,260 --> 05:01:27,600
which is going to do the same thing but
6718
05:01:27,600 --> 05:01:29,340
it's gonna figure out what is the
6719
05:01:29,340 --> 05:01:32,160
minimum number stored in that array and
6720
05:01:32,160 --> 05:01:34,138
then it's going to return that value
6721
05:01:34,138 --> 05:01:36,480
and third you need to find a way to
6722
05:01:36,480 --> 05:01:39,000
combine those two function in just one
6723
05:01:39,000 --> 05:01:40,860
function which is gonna
6724
05:01:40,860 --> 05:01:43,860
return the maximum value stored in that
6725
05:01:43,860 --> 05:01:46,260
array if you want or if you don't want
6726
05:01:46,260 --> 05:01:50,580
it to return the minimum value so try to
6727
05:01:50,580 --> 05:01:53,160
solve this Challenge and then watch my
6728
05:01:53,160 --> 05:01:54,120
solution
6729
05:01:54,120 --> 05:01:57,360
now let's solve this challenge
6730
05:01:57,360 --> 05:01:59,160
so first I'm going to create a function
6731
05:01:59,160 --> 05:02:01,378
which is going to be called the find
6732
05:02:01,378 --> 05:02:03,660
marks this function is going to return
6733
05:02:03,660 --> 05:02:05,580
the maximum value
6734
05:02:05,580 --> 05:02:07,920
and it's going to take us and I'm going
6735
05:02:07,920 --> 05:02:10,560
to Define here parameter and an array so
6736
05:02:10,560 --> 05:02:13,260
here I'm going to type a name for our
6737
05:02:13,260 --> 05:02:15,958
array is going to be called numbers
6738
05:02:15,958 --> 05:02:20,520
n is going to have the type array
6739
05:02:20,520 --> 05:02:24,060
of int of integers and it's going to
6740
05:02:24,060 --> 05:02:26,958
return an integer
6741
05:02:36,120 --> 05:02:38,400
now here I'm going to declare a variable
6742
05:02:38,400 --> 05:02:40,378
it's going to be a VAR and it's going to
6743
05:02:40,378 --> 05:02:43,700
be called Max
6744
05:02:44,580 --> 05:02:46,620
and in this Max variable I'm going to
6745
05:02:46,620 --> 05:02:50,160
store the first value in the which is uh
6746
05:02:50,160 --> 05:02:52,920
which is past which is in the which is
6747
05:02:52,920 --> 05:02:54,900
in the array so I'm gonna type here
6748
05:02:54,900 --> 05:02:56,820
numbers
6749
05:02:56,820 --> 05:02:58,620
square brackets
6750
05:02:58,620 --> 05:03:00,780
and I'm going to use the index 0 to get
6751
05:03:00,780 --> 05:03:04,138
the first value in the array then
6752
05:03:04,138 --> 05:03:06,600
I'm gonna Loop through this array
6753
05:03:06,600 --> 05:03:08,400
so
6754
05:03:08,400 --> 05:03:13,100
for number in numbers
6755
05:03:19,080 --> 05:03:20,820
then here you're going to check to see
6756
05:03:20,820 --> 05:03:23,420
if our
6757
05:03:23,940 --> 05:03:26,660
number
6758
05:03:26,940 --> 05:03:29,340
is greater than our first value which
6759
05:03:29,340 --> 05:03:31,500
was stored in the array or max value and
6760
05:03:31,500 --> 05:03:34,080
if that is true then I'm gonna store
6761
05:03:34,080 --> 05:03:36,120
this value which is now
6762
05:03:36,120 --> 05:03:39,600
greater than our than our maximum value
6763
05:03:39,600 --> 05:03:42,298
in our Max variable so here I'm going to
6764
05:03:42,298 --> 05:03:44,280
put Max now it's going to be equal to
6765
05:03:44,280 --> 05:03:46,200
this number which is found to be greater
6766
05:03:46,200 --> 05:03:49,560
than our previous max value
6767
05:03:49,560 --> 05:03:51,298
and at the end of this loop we're gonna
6768
05:03:51,298 --> 05:03:52,920
return
6769
05:03:52,920 --> 05:03:55,877
the maximum value
6770
05:03:56,100 --> 05:03:58,440
and we're going to do the same for the
6771
05:03:58,440 --> 05:04:00,480
finding the minimum value so I'm going
6772
05:04:00,480 --> 05:04:03,138
to copy this code
6773
05:04:05,580 --> 05:04:09,320
and this is going to be called find mean
6774
05:04:13,080 --> 05:04:15,298
but uh here gonna change a little bit
6775
05:04:15,298 --> 05:04:19,458
the logic let's call this variable mean
6776
05:04:24,540 --> 05:04:25,860
and you're going to change the condition
6777
05:04:25,860 --> 05:04:28,100
to less than min
6778
05:04:28,100 --> 05:04:31,168
[Music]
6779
05:04:44,280 --> 05:04:46,500
now let's call those function in our
6780
05:04:46,500 --> 05:04:48,718
main function and let's pass summaries
6781
05:04:48,718 --> 05:04:51,420
to see how it works
6782
05:04:51,420 --> 05:04:54,240
so first I'm going to declare a variable
6783
05:04:54,240 --> 05:04:55,860
it's going to be a Val and it's going to
6784
05:04:55,860 --> 05:04:58,320
be called Max
6785
05:04:58,320 --> 05:05:01,100
and now I'm going to call our find Max
6786
05:05:01,100 --> 05:05:03,240
function and here I'm going to pass an
6787
05:05:03,240 --> 05:05:05,040
array and I'm gonna I'm gonna pass the
6788
05:05:05,040 --> 05:05:07,680
array directly here so I'm going to type
6789
05:05:07,680 --> 05:05:09,420
array off and I'm going to pass some
6790
05:05:09,420 --> 05:05:12,980
arbitrary numbers like
6791
05:05:18,500 --> 05:05:20,638
then I'm going to create another valve
6792
05:05:20,638 --> 05:05:23,580
this is going to be called mean
6793
05:05:23,580 --> 05:05:25,920
now I'm going to call find
6794
05:05:25,920 --> 05:05:29,218
Min and here I'm going to also
6795
05:05:29,218 --> 05:05:31,980
call our array of function and here I'm
6796
05:05:31,980 --> 05:05:34,020
going to Define some
6797
05:05:34,020 --> 05:05:37,520
arbitrary numbers again
6798
05:05:44,400 --> 05:05:46,138
now I'm going to Output those value in
6799
05:05:46,138 --> 05:05:48,298
the consoles so I'm going to add
6800
05:05:48,298 --> 05:05:51,199
printerland here
6801
05:05:55,440 --> 05:05:57,298
I'm going to type max value is dollar
6802
05:05:57,298 --> 05:05:58,740
sign
6803
05:05:58,740 --> 05:06:00,900
and I'm going to refer our Max variable
6804
05:06:00,900 --> 05:06:03,560
defined above
6805
05:06:06,900 --> 05:06:10,520
another println
6806
05:06:19,138 --> 05:06:22,760
now let's run this code it's
6807
05:06:32,878 --> 05:06:36,298
get max value 7 which is correct this is
6808
05:06:36,298 --> 05:06:38,760
the biggest number then we get the
6809
05:06:38,760 --> 05:06:40,680
minimum value is 4 and this is also
6810
05:06:40,680 --> 05:06:44,580
correct so our code Works uh perfectly
6811
05:06:44,580 --> 05:06:46,860
well now the next thing is to combine
6812
05:06:46,860 --> 05:06:49,798
those two functions that we have here
6813
05:06:49,798 --> 05:06:51,600
let's
6814
05:06:51,600 --> 05:06:54,298
increase this now we need to find a way
6815
05:06:54,298 --> 05:06:56,400
to combine those function in one single
6816
05:06:56,400 --> 05:06:58,080
function which is going to also return
6817
05:06:58,080 --> 05:07:00,600
the maximum and minimum value but only
6818
05:07:00,600 --> 05:07:04,020
if I want to return to be returned so I
6819
05:07:04,020 --> 05:07:06,540
can determine if I want the minimum
6820
05:07:06,540 --> 05:07:09,420
value to be returned by that function or
6821
05:07:09,420 --> 05:07:11,760
if I want the maximum value to be
6822
05:07:11,760 --> 05:07:13,378
returned by that function so I need to
6823
05:07:13,378 --> 05:07:17,660
find a way to combine those two
6824
05:07:18,360 --> 05:07:20,700
and to do that I'm going to delete our
6825
05:07:20,700 --> 05:07:24,200
second function
6826
05:07:27,480 --> 05:07:29,520
I'm gonna delete the code inside the
6827
05:07:29,520 --> 05:07:32,480
this function
6828
05:07:32,820 --> 05:07:34,260
and I'm going to change the name of this
6829
05:07:34,260 --> 05:07:36,120
function to
6830
05:07:36,120 --> 05:07:41,638
find mean and Max and now here I'm going
6831
05:07:41,638 --> 05:07:43,440
to define a parameter which is going to
6832
05:07:43,440 --> 05:07:46,740
be a Boolean uh which is going to have a
6833
05:07:46,740 --> 05:07:49,620
Boolean type so I'm gonna call it
6834
05:07:49,620 --> 05:07:53,420
I'm going to call it search
6835
05:07:54,000 --> 05:07:56,540
Max
6836
05:07:56,580 --> 05:07:59,540
and it's going to be
6837
05:08:01,798 --> 05:08:03,718
a Boolean
6838
05:08:03,718 --> 05:08:06,180
all right
6839
05:08:06,180 --> 05:08:08,340
now
6840
05:08:08,340 --> 05:08:11,280
based on that Boolean value if is true
6841
05:08:11,280 --> 05:08:14,280
if I want to search the maximum value
6842
05:08:14,280 --> 05:08:16,680
here I'm going to type an if statement
6843
05:08:16,680 --> 05:08:19,620
so I'm going to put if search marks then
6844
05:08:19,620 --> 05:08:21,718
I'm gonna
6845
05:08:21,718 --> 05:08:24,000
add here the code to search for the
6846
05:08:24,000 --> 05:08:26,760
maximum value the first let's define um
6847
05:08:26,760 --> 05:08:29,160
a variable here is going to be also VAR
6848
05:08:29,160 --> 05:08:31,980
let's be called gonna be called
6849
05:08:31,980 --> 05:08:36,240
Max and uh it's also going to have the
6850
05:08:36,240 --> 05:08:39,420
first value in the array
6851
05:08:39,420 --> 05:08:42,298
which is also adding the zero now if our
6852
05:08:42,298 --> 05:08:45,780
search marks is true then we want our
6853
05:08:45,780 --> 05:08:48,840
maximum value to be search and return so
6854
05:08:48,840 --> 05:08:51,120
I'm gonna add here to the code to to do
6855
05:08:51,120 --> 05:08:52,860
that so I'm going to type here a for
6856
05:08:52,860 --> 05:08:54,780
Loop
6857
05:08:54,780 --> 05:08:56,878
which is going to go through our numbers
6858
05:08:56,878 --> 05:08:59,420
array
6859
05:09:05,280 --> 05:09:07,320
and here I'm gonna add the
6860
05:09:07,320 --> 05:09:09,420
the if statement which I'm gonna check
6861
05:09:09,420 --> 05:09:11,700
to see if our number
6862
05:09:11,700 --> 05:09:13,920
is greater than our Max then I'm gonna
6863
05:09:13,920 --> 05:09:16,699
assign our
6864
05:09:17,100 --> 05:09:18,480
our
6865
05:09:18,480 --> 05:09:22,340
number Tower Max
6866
05:09:22,920 --> 05:09:26,040
now I'm gonna go down here and and here
6867
05:09:26,040 --> 05:09:27,840
I'm going to add the else part so if
6868
05:09:27,840 --> 05:09:29,458
this uh
6869
05:09:29,458 --> 05:09:31,378
argument which is passed to our
6870
05:09:31,378 --> 05:09:34,440
parameter search Max is false so I don't
6871
05:09:34,440 --> 05:09:36,600
want to search for the maximum value
6872
05:09:36,600 --> 05:09:38,280
then that means that I want to search
6873
05:09:38,280 --> 05:09:39,718
for the minimum value so I'm going to
6874
05:09:39,718 --> 05:09:42,060
put here else now I'm going to add the
6875
05:09:42,060 --> 05:09:44,520
code to search for the minimum value and
6876
05:09:44,520 --> 05:09:46,378
for this you need to Define also
6877
05:09:46,378 --> 05:09:47,638
variable here
6878
05:09:47,638 --> 05:09:49,980
it's going to be called mean and it's
6879
05:09:49,980 --> 05:09:52,320
going to have the value of Max I'm going
6880
05:09:52,320 --> 05:09:54,840
to explain a minute how this works
6881
05:09:54,840 --> 05:09:56,218
now
6882
05:09:56,218 --> 05:09:59,820
here we're going to gonna also Loop
6883
05:09:59,820 --> 05:10:02,660
in our
6884
05:10:03,180 --> 05:10:07,200
numbers draw numbers array
6885
05:10:07,200 --> 05:10:09,660
and you're going to check if we
6886
05:10:09,660 --> 05:10:11,520
essentially we have the same code that
6887
05:10:11,520 --> 05:10:14,580
we have in our find the mean function so
6888
05:10:14,580 --> 05:10:17,700
here we're going to search if our number
6889
05:10:17,700 --> 05:10:20,100
is less than our mean
6890
05:10:20,100 --> 05:10:23,180
then gonna sign
6891
05:10:23,700 --> 05:10:27,260
our number to our mean
6892
05:10:33,360 --> 05:10:36,900
also I need to return this value
6893
05:10:36,900 --> 05:10:39,000
at the end of our Loop so here we need
6894
05:10:39,000 --> 05:10:40,378
to return
6895
05:10:40,378 --> 05:10:42,540
Max
6896
05:10:42,540 --> 05:10:46,160
and here you need to return mean
6897
05:10:48,718 --> 05:10:51,958
now let's press Ctrl alt L to format the
6898
05:10:51,958 --> 05:10:53,638
code
6899
05:10:53,638 --> 05:10:56,040
now let's delete the
6900
05:10:56,040 --> 05:10:59,580
those the code that you have here
6901
05:10:59,580 --> 05:11:02,638
let's delete also this code now
6902
05:11:02,638 --> 05:11:05,160
here now I'm going to put our fine Min
6903
05:11:05,160 --> 05:11:07,378
and Max function directly in the
6904
05:11:07,378 --> 05:11:09,180
printerland statement so I'm gonna type
6905
05:11:09,180 --> 05:11:12,420
here uh or to put in a variable for so
6906
05:11:12,420 --> 05:11:14,160
let's put in a variable first
6907
05:11:14,160 --> 05:11:16,440
so I'm gonna
6908
05:11:16,440 --> 05:11:18,780
Define a variable he's going to be also
6909
05:11:18,780 --> 05:11:21,000
a map it's going to be Auto Max and here
6910
05:11:21,000 --> 05:11:23,820
you're gonna type find
6911
05:11:23,820 --> 05:11:26,218
Min and Max
6912
05:11:26,218 --> 05:11:28,500
now here you need to pass an array first
6913
05:11:28,500 --> 05:11:30,900
so I need to pass
6914
05:11:30,900 --> 05:11:33,660
we need to call our array off we need to
6915
05:11:33,660 --> 05:11:37,798
Define some numbers here let's say uh
6916
05:11:37,798 --> 05:11:40,399
20
6917
05:11:41,718 --> 05:11:43,620
40.
6918
05:11:43,620 --> 05:11:46,579
50.
6919
05:11:48,480 --> 05:11:50,580
now we need to pass
6920
05:11:50,580 --> 05:11:53,638
an argument to our parameter search maps
6921
05:11:53,638 --> 05:11:55,620
which is going to be either true or
6922
05:11:55,620 --> 05:11:57,420
false so first I'm going to pass through
6923
05:11:57,420 --> 05:11:59,780
here
6924
05:12:00,360 --> 05:12:04,320
now if we print this value if you output
6925
05:12:04,320 --> 05:12:07,340
this value in the console
6926
05:12:07,620 --> 05:12:10,820
let's say that we type here
6927
05:12:11,040 --> 05:12:14,000
the marks
6928
05:12:14,700 --> 05:12:16,798
equals to dollar sign I'm going to refer
6929
05:12:16,798 --> 05:12:19,340
our variable
6930
05:12:22,980 --> 05:12:26,360
now if you're around the squad
6931
05:12:28,620 --> 05:12:31,138
we get the maximum value is 100 which is
6932
05:12:31,138 --> 05:12:32,940
correct now let's duplicate this code
6933
05:12:32,940 --> 05:12:34,920
that we have here and let's try it for
6934
05:12:34,920 --> 05:12:37,579
Dominion value
6935
05:12:40,500 --> 05:12:43,458
let's call it mean
6936
05:12:45,958 --> 05:12:48,480
and let's add the printerland uh
6937
05:12:48,480 --> 05:12:51,980
statement down here
6938
05:12:55,798 --> 05:12:58,520
let's type
6939
05:12:58,620 --> 05:13:00,060
domain
6940
05:13:00,060 --> 05:13:02,780
values
6941
05:13:05,280 --> 05:13:09,120
dollar sign mean
6942
05:13:10,920 --> 05:13:13,080
now if you run this code
6943
05:13:13,080 --> 05:13:16,580
and let's pass here to false
6944
05:13:19,138 --> 05:13:22,160
let's run this code again
6945
05:13:29,580 --> 05:13:32,040
now get the maximum value is 100 which
6946
05:13:32,040 --> 05:13:33,120
is correct
6947
05:13:33,120 --> 05:13:36,240
and the minimum value is 20 and we did
6948
05:13:36,240 --> 05:13:39,120
this with our find Min and Max function
6949
05:13:39,120 --> 05:13:42,958
by combining our two previous functions
6950
05:13:42,958 --> 05:13:47,400
and the our code on the networks okay
6951
05:13:47,400 --> 05:13:49,740
now it's time to
6952
05:13:49,740 --> 05:13:52,138
have a discussion about how this code
6953
05:13:52,138 --> 05:13:55,500
how this code how this solution to my
6954
05:13:55,500 --> 05:13:58,200
challenge works
6955
05:13:58,200 --> 05:14:00,958
so now let's see how our logic inside
6956
05:14:00,958 --> 05:14:03,780
our find Min and Max function works so
6957
05:14:03,780 --> 05:14:05,520
firstly we declare our function find the
6958
05:14:05,520 --> 05:14:07,320
minion marks then we declare two
6959
05:14:07,320 --> 05:14:09,540
parameters which is one is numbers and
6960
05:14:09,540 --> 05:14:12,120
is of type array of integers so here
6961
05:14:12,120 --> 05:14:13,440
we're gonna pass only an array of
6962
05:14:13,440 --> 05:14:14,760
integers
6963
05:14:14,760 --> 05:14:17,280
and the second parameter is called
6964
05:14:17,280 --> 05:14:19,080
search marks and it's a Boolean so you
6965
05:14:19,080 --> 05:14:21,780
can pass here only true or false then
6966
05:14:21,780 --> 05:14:23,218
we're going to say to the then we say
6967
05:14:23,218 --> 05:14:24,540
here that this function is going to
6968
05:14:24,540 --> 05:14:25,980
return a value
6969
05:14:25,980 --> 05:14:29,940
next on the line 9 we declare a variable
6970
05:14:29,940 --> 05:14:31,680
which is called Max
6971
05:14:31,680 --> 05:14:34,740
and we assign using the index operator
6972
05:14:34,740 --> 05:14:36,900
the first
6973
05:14:36,900 --> 05:14:39,540
element in the array in our Max so in
6974
05:14:39,540 --> 05:14:42,298
this case we're sine 20 in our Max
6975
05:14:42,298 --> 05:14:44,638
variable and then we declare another
6976
05:14:44,638 --> 05:14:47,160
variable called mean and we assign our
6977
05:14:47,160 --> 05:14:50,580
marks in our mean but our Max has the
6978
05:14:50,580 --> 05:14:52,798
value of 20 because we assigned it here
6979
05:14:52,798 --> 05:14:56,280
so this variable mean is also is also
6980
05:14:56,280 --> 05:14:59,580
also having the value 20. so both have
6981
05:14:59,580 --> 05:15:03,298
have the have the value 20 at the start
6982
05:15:03,298 --> 05:15:06,138
then we check to see if our search marks
6983
05:15:06,138 --> 05:15:10,620
parameter is true or false and if it's
6984
05:15:10,620 --> 05:15:13,378
true then I'm gonna execute the If part
6985
05:15:13,378 --> 05:15:17,580
and if part works in the following way
6986
05:15:17,580 --> 05:15:20,160
first we Loop using the for Loop through
6987
05:15:20,160 --> 05:15:22,320
our numbers array
6988
05:15:22,320 --> 05:15:26,040
then we check at each iteration if our
6989
05:15:26,040 --> 05:15:29,458
number let's say if 20 is greater than
6990
05:15:29,458 --> 05:15:31,560
our Max so if 20 is greater than 20
6991
05:15:31,560 --> 05:15:33,780
which is going to be false so the code
6992
05:15:33,780 --> 05:15:35,700
inside the calibration if it's not going
6993
05:15:35,700 --> 05:15:37,260
to be executed
6994
05:15:37,260 --> 05:15:40,260
then go for the next iteration so here
6995
05:15:40,260 --> 05:15:42,060
now we're gonna see it gonna check to
6996
05:15:42,060 --> 05:15:45,718
see if our number respectively 40 is
6997
05:15:45,718 --> 05:15:48,540
greater than our Max which is 20. and
6998
05:15:48,540 --> 05:15:50,638
this is going to be true and because of
6999
05:15:50,638 --> 05:15:52,378
that the code inside the calibration is
7000
05:15:52,378 --> 05:15:54,240
going to be executed so now we're going
7001
05:15:54,240 --> 05:15:56,100
to assign to our marks which is now is
7002
05:15:56,100 --> 05:15:59,700
20. our number which is 40. so now our
7003
05:15:59,700 --> 05:16:02,400
Max variable has a value of 40. and
7004
05:16:02,400 --> 05:16:03,480
we're going to we're going to do the
7005
05:16:03,480 --> 05:16:05,400
setting for the next for the 50. so this
7006
05:16:05,400 --> 05:16:06,840
condition is also going to be true so
7007
05:16:06,840 --> 05:16:08,160
we're going to assign it to our Max 50
7008
05:16:08,160 --> 05:16:09,660
and so on and so on
7009
05:16:09,660 --> 05:16:13,138
until it reaches 100 which is uh which
7010
05:16:13,138 --> 05:16:15,240
is going to be also true this condition
7011
05:16:15,240 --> 05:16:16,860
is going to repeat true and I'm going to
7012
05:16:16,860 --> 05:16:18,298
be assigning
7013
05:16:18,298 --> 05:16:20,520
100 to our Max
7014
05:16:20,520 --> 05:16:25,500
value of course that if we had another
7015
05:16:25,500 --> 05:16:27,840
number here let's say two is going to
7016
05:16:27,840 --> 05:16:29,760
Loop for that number also and this
7017
05:16:29,760 --> 05:16:30,958
condition is going to be false for that
7018
05:16:30,958 --> 05:16:32,520
because our number two is not going to
7019
05:16:32,520 --> 05:16:34,440
be greater than Max and it's going to
7020
05:16:34,440 --> 05:16:36,420
exit it's going to exit the loop it's
7021
05:16:36,420 --> 05:16:38,100
going to tell it's terminate and it's
7022
05:16:38,100 --> 05:16:40,500
gonna return the maximum value which is
7023
05:16:40,500 --> 05:16:43,260
100 like we saw in our
7024
05:16:43,260 --> 05:16:46,980
output in our console and at this point
7025
05:16:46,980 --> 05:16:49,320
when we return our max value the
7026
05:16:49,320 --> 05:16:52,080
function is left so it's not executing
7027
05:16:52,080 --> 05:16:54,920
any line of code
7028
05:16:55,980 --> 05:16:57,958
besides our
7029
05:16:57,958 --> 05:17:01,080
return Max is not going to execute the
7030
05:17:01,080 --> 05:17:04,320
subsequent code which comes below but if
7031
05:17:04,320 --> 05:17:06,298
our search marks is false it's going to
7032
05:17:06,298 --> 05:17:08,160
execute the else part so in this case
7033
05:17:08,160 --> 05:17:09,840
it's gonna search
7034
05:17:09,840 --> 05:17:12,120
for the minimum value so here we have
7035
05:17:12,120 --> 05:17:14,638
the same for Loop we search you go
7036
05:17:14,638 --> 05:17:15,958
through the
7037
05:17:15,958 --> 05:17:18,540
through all the numbers but here not
7038
05:17:18,540 --> 05:17:20,878
checking to see if our number is greater
7039
05:17:20,878 --> 05:17:23,700
then marks you're checking to see if our
7040
05:17:23,700 --> 05:17:26,700
number is less than our minimum number
7041
05:17:26,700 --> 05:17:29,760
so you check to see if our let's say uh
7042
05:17:29,760 --> 05:17:32,120
let's say let's take 20 30 40
7043
05:17:32,120 --> 05:17:34,620
is less than our minimum value which is
7044
05:17:34,620 --> 05:17:36,958
20. and that is going to be false so
7045
05:17:36,958 --> 05:17:38,520
this code is not going to be going to be
7046
05:17:38,520 --> 05:17:40,138
executed inside the calibration of the
7047
05:17:40,138 --> 05:17:42,780
if and it's gonna loop again and now
7048
05:17:42,780 --> 05:17:44,400
it's gonna check for the fourth and
7049
05:17:44,400 --> 05:17:47,160
that's also going to be gonna be
7050
05:17:47,160 --> 05:17:48,600
um
7051
05:17:48,600 --> 05:17:51,600
false so and so on so and so on until it
7052
05:17:51,600 --> 05:17:53,100
reaches 100 which is also going to be
7053
05:17:53,100 --> 05:17:55,378
false the loop is going to execute and
7054
05:17:55,378 --> 05:17:57,480
it's going to return 20. of course that
7055
05:17:57,480 --> 05:18:01,500
if we put here two
7056
05:18:01,500 --> 05:18:03,958
let's say now the minimum value is going
7057
05:18:03,958 --> 05:18:06,120
to be 2 because it's going to Loop for
7058
05:18:06,120 --> 05:18:08,160
another iteration and now one is going
7059
05:18:08,160 --> 05:18:10,260
to compare C4 number
7060
05:18:10,260 --> 05:18:12,060
respectively two
7061
05:18:12,060 --> 05:18:14,400
is less than our mean which our mean
7062
05:18:14,400 --> 05:18:16,980
previously was 20 that is going to be
7063
05:18:16,980 --> 05:18:19,440
true and it's going to assign our 2 to
7064
05:18:19,440 --> 05:18:22,020
our mean so if you run this code now
7065
05:18:22,020 --> 05:18:23,760
now you're going to see that the minimum
7066
05:18:23,760 --> 05:18:26,160
value is not 20 but it's but it's two
7067
05:18:26,160 --> 05:18:28,740
and after that we return this value to
7068
05:18:28,740 --> 05:18:32,160
the function which is called
7069
05:18:32,160 --> 05:18:34,080
so now get the minion values 2 which is
7070
05:18:34,080 --> 05:18:36,660
correct so this is how this uh code
7071
05:18:36,660 --> 05:18:38,820
works
7072
05:18:38,820 --> 05:18:42,000
and see you in the next video so now
7073
05:18:42,000 --> 05:18:43,378
it's time to start a discussion about
7074
05:18:43,378 --> 05:18:45,298
object orientated programming and
7075
05:18:45,298 --> 05:18:46,680
particularly in this video we're gonna
7076
05:18:46,680 --> 05:18:49,020
look at classes and objects but first
7077
05:18:49,020 --> 05:18:51,000
i'm going to create a new project
7078
05:18:51,000 --> 05:18:53,280
I'm going to call it op
7079
05:18:53,280 --> 05:18:55,620
for the language select Catalin and for
7080
05:18:55,620 --> 05:18:57,420
the build system IntelliJ make sure to
7081
05:18:57,420 --> 05:18:59,878
have the jdk selected and also I'm gonna
7082
05:18:59,878 --> 05:19:03,120
check this little box because this ad
7083
05:19:03,120 --> 05:19:04,740
sample code is going to add the main
7084
05:19:04,740 --> 05:19:07,200
function and that code for us
7085
05:19:07,200 --> 05:19:08,580
so I'm going to click on create to
7086
05:19:08,580 --> 05:19:11,180
create the project
7087
05:19:13,980 --> 05:19:17,239
now I'm going to delete this code
7088
05:19:18,958 --> 05:19:21,240
and I'm going to hide the project pane
7089
05:19:21,240 --> 05:19:23,700
now what is object oriented programming
7090
05:19:23,700 --> 05:19:26,580
object orientated programming is a way
7091
05:19:26,580 --> 05:19:30,120
of writing code in a sense in the way
7092
05:19:30,120 --> 05:19:32,638
you think about the real world so in the
7093
05:19:32,638 --> 05:19:35,820
real world we have objects
7094
05:19:35,820 --> 05:19:38,340
which can interact with it with each
7095
05:19:38,340 --> 05:19:40,860
other and which can do stuff but the
7096
05:19:40,860 --> 05:19:43,260
objects in the real world
7097
05:19:43,260 --> 05:19:46,860
have two major characteristics first is
7098
05:19:46,860 --> 05:19:49,680
the state so another the state of an
7099
05:19:49,680 --> 05:19:52,260
object in real life is represented let's
7100
05:19:52,260 --> 05:19:55,378
say that we think about of a car
7101
05:19:55,378 --> 05:19:57,420
the state is represented by the color of
7102
05:19:57,420 --> 05:19:59,240
that car by the number
7103
05:19:59,240 --> 05:20:03,240
of Gears that car has by uh by the brand
7104
05:20:03,240 --> 05:20:06,180
name of that car so this is the state of
7105
05:20:06,180 --> 05:20:08,458
a car the next thing that real life
7106
05:20:08,458 --> 05:20:10,860
objects have is functionality
7107
05:20:10,860 --> 05:20:13,560
so objects can do stuff so let's take
7108
05:20:13,560 --> 05:20:15,480
again the example of a car
7109
05:20:15,480 --> 05:20:18,660
a car can move a car can stop a car can
7110
05:20:18,660 --> 05:20:21,180
switch gears so
7111
05:20:21,180 --> 05:20:24,360
objects in real life have this uh though
7112
05:20:24,360 --> 05:20:27,180
this those two characteristics is those
7113
05:20:27,180 --> 05:20:28,860
two major character characteristics
7114
05:20:28,860 --> 05:20:32,280
represented by the state and the
7115
05:20:32,280 --> 05:20:35,458
functionality and the aim of object
7116
05:20:35,458 --> 05:20:38,600
orientative programming is to construct
7117
05:20:38,600 --> 05:20:43,040
objects using code in the way they are
7118
05:20:43,040 --> 05:20:45,420
they in the way they are in the real
7119
05:20:45,420 --> 05:20:46,700
life
7120
05:20:46,700 --> 05:20:49,440
but uh to do that let's say that you
7121
05:20:49,440 --> 05:20:51,360
want to build an object an actual object
7122
05:20:51,360 --> 05:20:53,580
in real life what you first need to do
7123
05:20:53,580 --> 05:20:55,260
the first thing that you need to do you
7124
05:20:55,260 --> 05:20:57,718
need to create a template or a
7125
05:20:57,718 --> 05:21:00,120
construction plan for creating that
7126
05:21:00,120 --> 05:21:02,820
object in which you're gonna Define the
7127
05:21:02,820 --> 05:21:05,940
generic things that a car has
7128
05:21:05,940 --> 05:21:09,000
and in programming you will do the same
7129
05:21:09,000 --> 05:21:11,878
thing but instead of using a piece of
7130
05:21:11,878 --> 05:21:16,280
paper or uh or a something
7131
05:21:16,280 --> 05:21:20,580
on on your PC to draw that that template
7132
05:21:20,580 --> 05:21:22,980
in programming you're going to use a
7133
05:21:22,980 --> 05:21:25,440
class so the class is used to create the
7134
05:21:25,440 --> 05:21:28,138
template which then is going to be used
7135
05:21:28,138 --> 05:21:30,840
to create an actual object an object
7136
05:21:30,840 --> 05:21:33,718
which is going to have a specific State
7137
05:21:33,718 --> 05:21:36,060
and a specific functionality
7138
05:21:36,060 --> 05:21:37,500
so um
7139
05:21:37,500 --> 05:21:41,218
in a way you can think that
7140
05:21:41,218 --> 05:21:44,218
the real world serves here as a model
7141
05:21:44,218 --> 05:21:46,560
for us to write code
7142
05:21:46,560 --> 05:21:51,000
and in this way in by combining uh the
7143
05:21:51,000 --> 05:21:52,860
state and the functionality in one
7144
05:21:52,860 --> 05:21:56,218
entity and have an object and as you can
7145
05:21:56,218 --> 05:21:58,200
see you can also have
7146
05:21:58,200 --> 05:22:00,240
like in the real life objects
7147
05:22:00,240 --> 05:22:02,280
communicating with each other you're
7148
05:22:02,280 --> 05:22:03,958
gonna see that this is really powerful
7149
05:22:03,958 --> 05:22:08,040
because you can you can model your code
7150
05:22:08,040 --> 05:22:11,520
in a more dynamic in a more you know
7151
05:22:11,520 --> 05:22:14,940
more create creative way so this is what
7152
05:22:14,940 --> 05:22:17,760
we're gonna do next so first let's say
7153
05:22:17,760 --> 05:22:20,340
that I want to create an object of type
7154
05:22:20,340 --> 05:22:21,480
car
7155
05:22:21,480 --> 05:22:25,260
and to do that we go down here and we
7156
05:22:25,260 --> 05:22:27,298
need to Define as I said the
7157
05:22:27,298 --> 05:22:29,458
construction plan the template for
7158
05:22:29,458 --> 05:22:31,138
building an object and in this case
7159
05:22:31,138 --> 05:22:33,480
we're going to build the car object so
7160
05:22:33,480 --> 05:22:35,638
we're going to Define what is called a
7161
05:22:35,638 --> 05:22:39,120
class and type the keyword class
7162
05:22:39,120 --> 05:22:41,280
now we need to name our class I'm going
7163
05:22:41,280 --> 05:22:43,500
to call it car and the name of the of
7164
05:22:43,500 --> 05:22:44,700
the
7165
05:22:44,700 --> 05:22:46,020
class should start with the capital
7166
05:22:46,020 --> 05:22:47,878
letter and if it has multiple words it
7167
05:22:47,878 --> 05:22:49,520
should be in Pascal case
7168
05:22:49,520 --> 05:22:51,660
curly braces
7169
05:22:51,660 --> 05:22:53,878
and press enter now inside the color
7170
05:22:53,878 --> 05:22:56,040
braces we need to define the state and
7171
05:22:56,040 --> 05:22:59,400
the functionality as I said for the
7172
05:22:59,400 --> 05:23:01,440
objects that are going to be built with
7173
05:23:01,440 --> 05:23:02,700
this template
7174
05:23:02,700 --> 05:23:06,060
and to do that we need to use variables
7175
05:23:06,060 --> 05:23:09,298
first for the state or
7176
05:23:09,298 --> 05:23:11,160
or in the context of object orientated
7177
05:23:11,160 --> 05:23:12,900
programming and more specifically in the
7178
05:23:12,900 --> 05:23:14,458
context of
7179
05:23:14,458 --> 05:23:17,280
classes they are called the classes and
7180
05:23:17,280 --> 05:23:19,378
objects they are called properties
7181
05:23:19,378 --> 05:23:22,080
and we Define them like we Define a
7182
05:23:22,080 --> 05:23:24,840
simple variable we Type R and the next
7183
05:23:24,840 --> 05:23:28,260
the first thing that that a car has is a
7184
05:23:28,260 --> 05:23:29,940
name or brand name so we're gonna type
7185
05:23:29,940 --> 05:23:30,840
name
7186
05:23:30,840 --> 05:23:32,600
but we cannot keep this variable
7187
05:23:32,600 --> 05:23:34,440
uninitialized we need to provide the
7188
05:23:34,440 --> 05:23:35,760
value to it and I'm going to put an
7189
05:23:35,760 --> 05:23:37,560
empty string
7190
05:23:37,560 --> 05:23:39,660
for uh for now so it's gonna have the
7191
05:23:39,660 --> 05:23:42,120
default value an empty string
7192
05:23:42,120 --> 05:23:44,638
the next thing is the model what kind of
7193
05:23:44,638 --> 05:23:47,218
model this car is so it's going to be
7194
05:23:47,218 --> 05:23:49,320
also a string I'm gonna assign an empty
7195
05:23:49,320 --> 05:23:50,878
string
7196
05:23:50,878 --> 05:23:52,620
the next property is going to be called
7197
05:23:52,620 --> 05:23:55,080
the color it's going to be also an empty
7198
05:23:55,080 --> 05:23:57,120
string
7199
05:23:57,120 --> 05:23:58,500
and the next property is going to be
7200
05:23:58,500 --> 05:23:59,940
called doors so this is going to store
7201
05:23:59,940 --> 05:24:02,218
the number of doors that a specific
7202
05:24:02,218 --> 05:24:03,780
object which is going to be built with
7203
05:24:03,780 --> 05:24:05,760
this template is going to have and we're
7204
05:24:05,760 --> 05:24:08,718
gonna put zero I'm gonna put zero now
7205
05:24:08,718 --> 05:24:11,280
we need to Define some functionality for
7206
05:24:11,280 --> 05:24:14,040
this class and for the objects for an
7207
05:24:14,040 --> 05:24:15,840
implicit for the objects that are going
7208
05:24:15,840 --> 05:24:17,580
to be built with this class
7209
05:24:17,580 --> 05:24:21,718
and um I'm gonna add two functions first
7210
05:24:21,718 --> 05:24:24,980
one is going to be called move
7211
05:24:27,240 --> 05:24:29,160
it's not going to take any parameters
7212
05:24:29,160 --> 05:24:31,860
it's gonna just output some text to the
7213
05:24:31,860 --> 05:24:33,420
console which is going to say that the
7214
05:24:33,420 --> 05:24:36,660
car is moving so we put quotation marks
7215
05:24:36,660 --> 05:24:39,298
the card is moving
7216
05:24:39,298 --> 05:24:42,780
now I'm gonna Define another function
7217
05:24:42,780 --> 05:24:45,500
call stop
7218
05:24:47,280 --> 05:24:49,620
also it's not going to take any
7219
05:24:49,620 --> 05:24:52,080
parameters it's gonna just outpost on
7220
05:24:52,080 --> 05:24:53,940
this the console it's gonna it's gonna
7221
05:24:53,940 --> 05:24:57,377
say that the car has stopped
7222
05:25:00,900 --> 05:25:02,160
now
7223
05:25:02,160 --> 05:25:06,180
we have the template we defined what our
7224
05:25:06,180 --> 05:25:08,340
car is gonna
7225
05:25:08,340 --> 05:25:13,560
have now we need to create an actual or
7226
05:25:13,560 --> 05:25:16,860
object an actual and specific object
7227
05:25:16,860 --> 05:25:18,660
using this template and to do that to
7228
05:25:18,660 --> 05:25:20,638
firstly declare a variable
7229
05:25:20,638 --> 05:25:23,280
and it's going to be a Val and I'm going
7230
05:25:23,280 --> 05:25:25,980
to call it car one so so our object is
7231
05:25:25,980 --> 05:25:27,840
going to be stored in this variable car
7232
05:25:27,840 --> 05:25:28,740
one
7233
05:25:28,740 --> 05:25:32,760
then you put equals and we type car and
7234
05:25:32,760 --> 05:25:34,620
now we have some suggestions and we have
7235
05:25:34,620 --> 05:25:38,160
our car that we Define below our car
7236
05:25:38,160 --> 05:25:40,620
class so we press enter then we put
7237
05:25:40,620 --> 05:25:42,180
parenthesis
7238
05:25:42,180 --> 05:25:46,560
so now we have created an actual object
7239
05:25:46,560 --> 05:25:48,780
a real object real in the programming
7240
05:25:48,780 --> 05:25:49,620
sense
7241
05:25:49,620 --> 05:25:52,399
and
7242
05:25:52,620 --> 05:25:55,980
which but now this actual object was
7243
05:25:55,980 --> 05:25:58,440
also this is called the instantiation
7244
05:25:58,440 --> 05:26:01,200
because we created an actual instance
7245
05:26:01,200 --> 05:26:03,480
but we don't need to bother with these
7246
05:26:03,480 --> 05:26:05,100
names for now
7247
05:26:05,100 --> 05:26:08,218
so we created an actual object using
7248
05:26:08,218 --> 05:26:11,780
this template that we have below but the
7249
05:26:11,780 --> 05:26:14,940
the properties of these specific objects
7250
05:26:14,940 --> 05:26:17,520
are those properties that we Define here
7251
05:26:17,520 --> 05:26:20,580
those default properties and to Define
7252
05:26:20,580 --> 05:26:24,298
some specific profit some specific
7253
05:26:24,298 --> 05:26:28,080
values to these properties that this car
7254
05:26:28,080 --> 05:26:31,080
object that created has we need to type
7255
05:26:31,080 --> 05:26:32,540
car one
7256
05:26:32,540 --> 05:26:35,820
dot so this is called informally dot
7257
05:26:35,820 --> 05:26:37,080
notation
7258
05:26:37,080 --> 05:26:38,878
and now we have some suggestions so we
7259
05:26:38,878 --> 05:26:40,920
have our properties that we defined for
7260
05:26:40,920 --> 05:26:42,060
a car
7261
05:26:42,060 --> 05:26:44,400
and now we type car one dot and we
7262
05:26:44,400 --> 05:26:45,660
choose the name
7263
05:26:45,660 --> 05:26:47,160
and now I'm going to assign a value to
7264
05:26:47,160 --> 05:26:49,980
this name I'm going to be called Tesla
7265
05:26:49,980 --> 05:26:53,458
so now our car one
7266
05:26:53,458 --> 05:26:57,900
object that was created has the property
7267
05:26:57,900 --> 05:26:58,920
name
7268
05:26:58,920 --> 05:27:01,980
Tesla now we do the same thing for the
7269
05:27:01,980 --> 05:27:04,560
next property for the model car one dot
7270
05:27:04,560 --> 05:27:06,860
model
7271
05:27:08,940 --> 05:27:11,700
press enter
7272
05:27:11,700 --> 05:27:15,000
and you put we put here s blade so the
7273
05:27:15,000 --> 05:27:16,740
model
7274
05:27:16,740 --> 05:27:19,680
next we also type car one
7275
05:27:19,680 --> 05:27:23,218
Dot now we're gonna assign a value to
7276
05:27:23,218 --> 05:27:24,780
the property caller
7277
05:27:24,780 --> 05:27:27,900
it's going to be red
7278
05:27:27,900 --> 05:27:31,378
next we type also car one and we Define
7279
05:27:31,378 --> 05:27:36,440
for the property names for the property
7280
05:27:36,480 --> 05:27:39,540
doors also value so we're gonna put here
7281
05:27:39,540 --> 05:27:41,340
four
7282
05:27:41,340 --> 05:27:43,680
so now our object R1
7283
05:27:43,680 --> 05:27:47,040
has for its property for for its
7284
05:27:47,040 --> 05:27:49,080
properties some specific values
7285
05:27:49,080 --> 05:27:51,420
respectively have for the name Tesla for
7286
05:27:51,420 --> 05:27:53,878
the model as played for the car for the
7287
05:27:53,878 --> 05:27:56,820
color red and for the doors 4.
7288
05:27:56,820 --> 05:27:59,100
so now let's output the property values
7289
05:27:59,100 --> 05:28:01,320
of this object that we created in the
7290
05:28:01,320 --> 05:28:03,780
console and to do that we put our
7291
05:28:03,780 --> 05:28:05,878
println function down here we put
7292
05:28:05,878 --> 05:28:07,620
quotation marks inside the parenthesis
7293
05:28:07,620 --> 05:28:09,900
because I'm gonna add some text here so
7294
05:28:09,900 --> 05:28:12,440
I'm going to type name
7295
05:28:12,440 --> 05:28:16,020
equals we put dollar sign then input
7296
05:28:16,020 --> 05:28:17,820
curly braces and inside the calibration
7297
05:28:17,820 --> 05:28:18,718
type
7298
05:28:18,718 --> 05:28:21,480
car one dot
7299
05:28:21,480 --> 05:28:22,798
name
7300
05:28:22,798 --> 05:28:25,260
and this is going to return the
7301
05:28:25,260 --> 05:28:28,560
the value of the property name for the
7302
05:28:28,560 --> 05:28:30,060
car one object that we created
7303
05:28:30,060 --> 05:28:32,638
respectively is going to return Tesla
7304
05:28:32,638 --> 05:28:35,040
let's press Ctrl d three times to do the
7305
05:28:35,040 --> 05:28:36,958
same thing for the next properties and
7306
05:28:36,958 --> 05:28:40,080
here we put car one dot model so this is
7307
05:28:40,080 --> 05:28:41,340
gonna return the
7308
05:28:41,340 --> 05:28:45,240
value of the model property
7309
05:28:45,240 --> 05:28:47,878
you're gonna put color
7310
05:28:47,878 --> 05:28:49,740
and here
7311
05:28:49,740 --> 05:28:52,560
I'm gonna type doors
7312
05:28:52,560 --> 05:28:55,878
now if you run this code
7313
05:28:58,520 --> 05:29:01,620
we get name Tesla
7314
05:29:01,620 --> 05:29:05,280
actually let's change the text here to
7315
05:29:05,280 --> 05:29:09,120
model to match the property name
7316
05:29:09,120 --> 05:29:11,660
model
7317
05:29:13,080 --> 05:29:15,560
color
7318
05:29:16,860 --> 05:29:19,760
endorse
7319
05:29:22,218 --> 05:29:26,060
so now if you're on this
7320
05:29:26,820 --> 05:29:28,920
we get the name Tesla Model is played
7321
05:29:28,920 --> 05:29:32,420
color red and doors 4 and those are the
7322
05:29:32,420 --> 05:29:36,060
values of the properties of this object
7323
05:29:36,060 --> 05:29:38,700
that we created here with our
7324
05:29:38,700 --> 05:29:41,100
template car
7325
05:29:41,100 --> 05:29:43,920
next what you can also do because in the
7326
05:29:43,920 --> 05:29:46,200
class we Define the two function we can
7327
05:29:46,200 --> 05:29:49,620
call those functions on this object that
7328
05:29:49,620 --> 05:29:50,878
we created
7329
05:29:50,878 --> 05:29:53,638
and to do that we also use the dot
7330
05:29:53,638 --> 05:29:55,560
notation we type car one
7331
05:29:55,560 --> 05:29:58,798
dot move and this is going to call the
7332
05:29:58,798 --> 05:30:00,600
move function
7333
05:30:00,600 --> 05:30:03,420
which is inside the car one
7334
05:30:03,420 --> 05:30:04,860
object
7335
05:30:04,860 --> 05:30:06,420
let's also
7336
05:30:06,420 --> 05:30:09,180
type car one dot stop to call the store
7337
05:30:09,180 --> 05:30:12,660
function so now if you run this code
7338
05:30:12,660 --> 05:30:14,400
we get uh
7339
05:30:14,400 --> 05:30:16,920
the values of our to our properties and
7340
05:30:16,920 --> 05:30:19,260
then we get the car is moving and the
7341
05:30:19,260 --> 05:30:21,958
car has stopped so the car one dot move
7342
05:30:21,958 --> 05:30:23,878
and car one the stop is calling the
7343
05:30:23,878 --> 05:30:25,378
functions
7344
05:30:25,378 --> 05:30:29,520
on this object that we created here now
7345
05:30:29,520 --> 05:30:31,320
let's uh
7346
05:30:31,320 --> 05:30:34,500
also add inside the functions
7347
05:30:34,500 --> 05:30:38,100
the name of the card that is moving so
7348
05:30:38,100 --> 05:30:40,138
he to do that with here dollar sign
7349
05:30:40,138 --> 05:30:42,120
inside the class
7350
05:30:42,120 --> 05:30:44,458
and we type name
7351
05:30:44,458 --> 05:30:47,400
and this is gonna reference this
7352
05:30:47,400 --> 05:30:51,000
property name that is defined here
7353
05:30:51,000 --> 05:30:53,760
let's also do the same thing for the
7354
05:30:53,760 --> 05:30:56,760
stop function let's type name
7355
05:30:56,760 --> 05:30:58,860
and now what this is gonna do because
7356
05:30:58,860 --> 05:31:02,218
those functions are in they are they're
7357
05:31:02,218 --> 05:31:04,080
living inside this object that we
7358
05:31:04,080 --> 05:31:07,680
created here car one and this object has
7359
05:31:07,680 --> 05:31:11,340
the name Tesla when those functions are
7360
05:31:11,340 --> 05:31:12,840
going to be called
7361
05:31:12,840 --> 05:31:15,480
they they gonna they're gonna say that
7362
05:31:15,480 --> 05:31:17,458
the car
7363
05:31:17,458 --> 05:31:20,100
and the name of the car which is in our
7364
05:31:20,100 --> 05:31:23,280
case for our objectives Tesla is moving
7365
05:31:23,280 --> 05:31:25,020
and then it's gonna say
7366
05:31:25,020 --> 05:31:28,260
the car again Tesla has stopped because
7367
05:31:28,260 --> 05:31:31,680
they are calling this uh specific object
7368
05:31:31,680 --> 05:31:35,820
that we created which has uh those
7369
05:31:35,820 --> 05:31:38,700
specific values for its properties
7370
05:31:38,700 --> 05:31:42,680
all right so now if you run this quad
7371
05:31:43,620 --> 05:31:46,440
we get as expected the car Tesla is
7372
05:31:46,440 --> 05:31:48,840
moving and the car Tesla has stopped so
7373
05:31:48,840 --> 05:31:51,958
the functions are called on this object
7374
05:31:51,958 --> 05:31:54,180
that we Define here and it's using the
7375
05:31:54,180 --> 05:31:55,980
property values that we Define for this
7376
05:31:55,980 --> 05:31:56,940
object
7377
05:31:56,940 --> 05:32:00,120
but as I said with uh you can create as
7378
05:32:00,120 --> 05:32:03,000
many objects as you want using the same
7379
05:32:03,000 --> 05:32:05,040
template and let's do that let's create
7380
05:32:05,040 --> 05:32:07,080
another object so I'm going to type here
7381
05:32:07,080 --> 05:32:08,400
Val
7382
05:32:08,400 --> 05:32:10,020
Car Tool
7383
05:32:10,020 --> 05:32:13,260
let's put equals we type car and then we
7384
05:32:13,260 --> 05:32:15,000
put parenthesis
7385
05:32:15,000 --> 05:32:17,580
and this create is now another object
7386
05:32:17,580 --> 05:32:19,680
but we need to Define some values to its
7387
05:32:19,680 --> 05:32:21,180
properties because now the values for
7388
05:32:21,180 --> 05:32:23,280
the properties are the default values
7389
05:32:23,280 --> 05:32:24,540
that we Define inside the class
7390
05:32:24,540 --> 05:32:26,958
respectively the empty string and the
7391
05:32:26,958 --> 05:32:28,680
zero
7392
05:32:28,680 --> 05:32:31,378
and to do that we type here car2
7393
05:32:31,378 --> 05:32:33,660
dot name
7394
05:32:33,660 --> 05:32:36,718
so now we are calling the properties
7395
05:32:36,718 --> 05:32:39,660
for the car 2 object and now we Define
7396
05:32:39,660 --> 05:32:41,520
some values to the cut to the properties
7397
05:32:41,520 --> 05:32:44,100
of the car 2 object so they are
7398
05:32:44,100 --> 05:32:46,620
this here is not overending
7399
05:32:46,620 --> 05:32:48,840
the value that we have here because
7400
05:32:48,840 --> 05:32:50,638
this
7401
05:32:50,638 --> 05:32:52,680
thing that we have here it's a distinct
7402
05:32:52,680 --> 05:32:55,440
object in memory uh from this object
7403
05:32:55,440 --> 05:32:57,420
they are this thing distinct objects and
7404
05:32:57,420 --> 05:32:59,218
they have their own properties with
7405
05:32:59,218 --> 05:33:01,798
their own values and their own functions
7406
05:33:01,798 --> 05:33:06,138
and now let's put here Ford
7407
05:33:07,920 --> 05:33:13,080
let's put the car to that model
7408
05:33:13,080 --> 05:33:16,100
let's put Mustang
7409
05:33:17,400 --> 05:33:19,798
let's put
7410
05:33:19,798 --> 05:33:22,620
car to that
7411
05:33:22,620 --> 05:33:25,218
color
7412
05:33:25,378 --> 05:33:27,240
blue
7413
05:33:27,240 --> 05:33:30,060
and for the doors let's put two
7414
05:33:30,060 --> 05:33:32,700
car two that doors
7415
05:33:32,700 --> 05:33:36,780
equals to two and let's output those
7416
05:33:36,780 --> 05:33:39,000
values in the console so let's output
7417
05:33:39,000 --> 05:33:41,940
the values of the properties for this
7418
05:33:41,940 --> 05:33:43,740
second object that we created in the
7419
05:33:43,740 --> 05:33:46,080
console and to that to do that I'm going
7420
05:33:46,080 --> 05:33:48,420
to copy this code
7421
05:33:48,420 --> 05:33:51,718
I'm gonna paste the paste it below here
7422
05:33:51,718 --> 05:33:53,820
and now I'm just gonna change
7423
05:33:53,820 --> 05:33:56,400
the
7424
05:33:56,400 --> 05:34:00,420
the variable to car two to to call to
7425
05:34:00,420 --> 05:34:01,680
call the properties on the cartoon
7426
05:34:01,680 --> 05:34:05,840
object so we put here car2
7427
05:34:09,718 --> 05:34:11,400
so now we're calling the properties
7428
05:34:11,400 --> 05:34:15,240
values on our car to object
7429
05:34:15,240 --> 05:34:16,920
and now if
7430
05:34:16,920 --> 05:34:19,877
I run this code
7431
05:34:20,060 --> 05:34:24,000
now we get name Ford Model Mustang color
7432
05:34:24,000 --> 05:34:26,760
blue endorseful and the first part is
7433
05:34:26,760 --> 05:34:28,680
from the our SEC for from our first
7434
05:34:28,680 --> 05:34:30,540
object which is named Tesla Model let's
7435
05:34:30,540 --> 05:34:32,820
play The Red Doors 4 and the Carter
7436
05:34:32,820 --> 05:34:34,138
slice movie and the cards has stopped
7437
05:34:34,138 --> 05:34:37,080
but let's add a space here
7438
05:34:37,080 --> 05:34:40,940
to make a clear uh to make a clear which
7439
05:34:40,940 --> 05:34:44,878
to make uh clear that this is the output
7440
05:34:44,878 --> 05:34:46,740
for the second object to make things
7441
05:34:46,740 --> 05:34:48,840
more clear and to add the space we put
7442
05:34:48,840 --> 05:34:52,080
here a printerland
7443
05:34:52,080 --> 05:34:55,440
statement we put quotation marks and we
7444
05:34:55,440 --> 05:34:58,458
put a backslash
7445
05:34:59,100 --> 05:35:00,780
n and this is going to add a space
7446
05:35:00,780 --> 05:35:03,718
between our println statements so it's
7447
05:35:03,718 --> 05:35:05,520
going to add a space between those
7448
05:35:05,520 --> 05:35:09,840
statements and we uh and those so now if
7449
05:35:09,840 --> 05:35:12,500
you run this code
7450
05:35:14,820 --> 05:35:17,218
let's increase this
7451
05:35:17,218 --> 05:35:19,440
we get the name Tesla so this is our
7452
05:35:19,440 --> 05:35:20,820
first object model let's play it called
7453
05:35:20,820 --> 05:35:22,980
Red Doors 4 The Carter size moving
7454
05:35:22,980 --> 05:35:24,958
because we're calling the functions on
7455
05:35:24,958 --> 05:35:27,600
the first object and the character has
7456
05:35:27,600 --> 05:35:30,480
stopped and then we get name Ford Model
7457
05:35:30,480 --> 05:35:32,878
Mustang color blue Doors 2 and we have
7458
05:35:32,878 --> 05:35:34,920
our space because you put here this
7459
05:35:34,920 --> 05:35:37,860
backflash and which adds that space for
7460
05:35:37,860 --> 05:35:38,820
us
7461
05:35:38,820 --> 05:35:42,360
let's um decrease this
7462
05:35:42,360 --> 05:35:45,780
and let's also call the functions the
7463
05:35:45,780 --> 05:35:48,060
move install functions on the second
7464
05:35:48,060 --> 05:35:49,980
object so I'm going to type here car2
7465
05:35:49,980 --> 05:35:52,500
that move
7466
05:35:52,500 --> 05:35:56,160
and card two dot stop
7467
05:35:56,160 --> 05:35:58,020
so now if you run this
7468
05:35:58,020 --> 05:35:59,280
let's
7469
05:35:59,280 --> 05:36:02,400
right click and just click on run
7470
05:36:02,400 --> 05:36:05,400
main.kt
7471
05:36:07,860 --> 05:36:09,420
now get
7472
05:36:09,420 --> 05:36:13,320
our first object which is with this uh
7473
05:36:13,320 --> 05:36:16,440
value for the value of the value for its
7474
05:36:16,440 --> 05:36:18,180
properties and then we get the two
7475
05:36:18,180 --> 05:36:21,000
functions called on our first object and
7476
05:36:21,000 --> 05:36:21,718
um
7477
05:36:21,718 --> 05:36:24,298
we get in our second object which is
7478
05:36:24,298 --> 05:36:25,980
values for its properties and then we
7479
05:36:25,980 --> 05:36:28,620
get the card for this moving and the car
7480
05:36:28,620 --> 05:36:31,260
Ford has stopped because now
7481
05:36:31,260 --> 05:36:32,160
um
7482
05:36:32,160 --> 05:36:34,500
we're calling the functions on the this
7483
05:36:34,500 --> 05:36:37,500
second object and this second object has
7484
05:36:37,500 --> 05:36:39,958
the value for the properties has has
7485
05:36:39,958 --> 05:36:42,360
different values for its properties so
7486
05:36:42,360 --> 05:36:44,638
this is why you get the card for this
7487
05:36:44,638 --> 05:36:46,740
movie and the car four has stopped again
7488
05:36:46,740 --> 05:36:48,440
they are different
7489
05:36:48,440 --> 05:36:51,240
objects they are distinct objects
7490
05:36:51,240 --> 05:36:54,540
created using the same template the same
7491
05:36:54,540 --> 05:36:56,458
construction plan
7492
05:36:56,458 --> 05:36:58,260
and
7493
05:36:58,260 --> 05:37:00,718
I'm gonna end this video now and see you
7494
05:37:00,718 --> 05:37:01,860
in the next video
7495
05:37:01,860 --> 05:37:03,718
so in the last video we saw how we can
7496
05:37:03,718 --> 05:37:05,638
create a class and then how we can
7497
05:37:05,638 --> 05:37:08,040
Define some properties and some
7498
05:37:08,040 --> 05:37:10,320
functionality to that class then we use
7499
05:37:10,320 --> 05:37:13,080
that class to create two objects and
7500
05:37:13,080 --> 05:37:15,780
then we defined for those objects for
7501
05:37:15,780 --> 05:37:17,638
the property of those objects some
7502
05:37:17,638 --> 05:37:21,360
values but the way we we Define values
7503
05:37:21,360 --> 05:37:23,340
to the properties of the object is to
7504
05:37:23,340 --> 05:37:26,218
call the the object and then the
7505
05:37:26,218 --> 05:37:27,780
property of logic and then we assign a
7506
05:37:27,780 --> 05:37:30,660
value and this is okay but if you have
7507
05:37:30,660 --> 05:37:34,200
more than than four properties let's say
7508
05:37:34,200 --> 05:37:36,240
you have 20 or 100 properties that will
7509
05:37:36,240 --> 05:37:38,820
be very tedious to write each one of
7510
05:37:38,820 --> 05:37:40,560
them and then assign a value
7511
05:37:40,560 --> 05:37:43,200
and a better way is to use what is
7512
05:37:43,200 --> 05:37:45,240
called a Constructor and as the name
7513
05:37:45,240 --> 05:37:46,920
implied The Constructor is used to
7514
05:37:46,920 --> 05:37:48,900
construct the object and the Constructor
7515
05:37:48,900 --> 05:37:51,000
is called at the time when we create a
7516
05:37:51,000 --> 05:37:53,458
new object so at the time of line we we
7517
05:37:53,458 --> 05:37:55,920
type this line of code when you finish
7518
05:37:55,920 --> 05:37:57,958
to type this line of code The
7519
05:37:57,958 --> 05:38:00,298
Constructor is called immediately so
7520
05:38:00,298 --> 05:38:02,218
that is the
7521
05:38:02,218 --> 05:38:05,580
the time where we need to pass the
7522
05:38:05,580 --> 05:38:07,440
values to our properties and to do that
7523
05:38:07,440 --> 05:38:09,540
we go down here where we Define our
7524
05:38:09,540 --> 05:38:11,218
class
7525
05:38:11,218 --> 05:38:12,718
we put
7526
05:38:12,718 --> 05:38:16,740
at the at the end of our
7527
05:38:16,740 --> 05:38:18,600
end of the name of the class we put
7528
05:38:18,600 --> 05:38:20,100
parenthesis and here we Define some
7529
05:38:20,100 --> 05:38:21,900
parameters the way we Define parameters
7530
05:38:21,900 --> 05:38:23,218
for functions
7531
05:38:23,218 --> 05:38:24,958
and the first one is going to be called
7532
05:38:24,958 --> 05:38:27,718
name it's going to be a string
7533
05:38:27,718 --> 05:38:30,420
the second one is going to be model
7534
05:38:30,420 --> 05:38:33,500
also string
7535
05:38:35,760 --> 05:38:37,200
the next one
7536
05:38:37,200 --> 05:38:40,020
color also string
7537
05:38:40,020 --> 05:38:42,420
and the last one is going to be doors
7538
05:38:42,420 --> 05:38:45,060
and this is going to be an integer
7539
05:38:45,060 --> 05:38:47,878
no we need to
7540
05:38:47,878 --> 05:38:50,458
assign whatever value values pass to
7541
05:38:50,458 --> 05:38:52,860
those parameters to the properties of
7542
05:38:52,860 --> 05:38:54,958
the class here and to do that just
7543
05:38:54,958 --> 05:38:57,000
delete the empty string here and type
7544
05:38:57,000 --> 05:38:59,218
name
7545
05:38:59,218 --> 05:39:01,200
and as I'm typing you see that we have
7546
05:39:01,200 --> 05:39:04,680
name and it has this p and this P stands
7547
05:39:04,680 --> 05:39:07,200
for prop for a parameter so you're
7548
05:39:07,200 --> 05:39:09,718
saying do you mean this parameter that
7549
05:39:09,718 --> 05:39:12,540
we Define up here and then we press
7550
05:39:12,540 --> 05:39:15,298
enter to fill that for us
7551
05:39:15,298 --> 05:39:19,638
then put we type here model
7552
05:39:20,638 --> 05:39:22,860
color
7553
05:39:22,860 --> 05:39:24,540
and then
7554
05:39:24,540 --> 05:39:26,160
doors
7555
05:39:26,160 --> 05:39:27,780
now if you go up here we have some
7556
05:39:27,780 --> 05:39:31,680
errors because now the the
7557
05:39:31,680 --> 05:39:33,718
when you create the object the objects
7558
05:39:33,718 --> 05:39:37,080
is expecting for its Constructor some
7559
05:39:37,080 --> 05:39:39,420
values so if you hover over here inside
7560
05:39:39,420 --> 05:39:42,900
the parenthesis it says no value fast
7561
05:39:42,900 --> 05:39:45,360
for parameter color Doors Model and name
7562
05:39:45,360 --> 05:39:47,700
so if you and if you look down here you
7563
05:39:47,700 --> 05:39:50,340
see that it has this public Constructor
7564
05:39:50,340 --> 05:39:52,440
so now what we need to do is to pass the
7565
05:39:52,440 --> 05:39:54,718
value that you passed down here to to
7566
05:39:54,718 --> 05:39:57,360
the properties by calling the
7567
05:39:57,360 --> 05:39:59,340
calling them on the object is to pass
7568
05:39:59,340 --> 05:40:01,680
them directly in the Constructor and
7569
05:40:01,680 --> 05:40:04,080
then the values the arguments if you
7570
05:40:04,080 --> 05:40:05,878
want are going to be assigned to the
7571
05:40:05,878 --> 05:40:09,420
properties so I'm going to put here
7572
05:40:09,420 --> 05:40:13,860
and you have this hint name Tesla
7573
05:40:13,860 --> 05:40:17,100
then we put a comma
7574
05:40:17,100 --> 05:40:20,100
so we have the again the hint which is
7575
05:40:20,100 --> 05:40:23,340
very helpful so for the model we put a
7576
05:40:23,340 --> 05:40:25,820
splade
7577
05:40:27,060 --> 05:40:30,120
and then for the color
7578
05:40:30,120 --> 05:40:32,700
we put red
7579
05:40:32,700 --> 05:40:36,440
and for the number of doors four
7580
05:40:36,480 --> 05:40:39,480
all right so now this is more concise
7581
05:40:39,480 --> 05:40:42,420
and now this is doing the exact thing
7582
05:40:42,420 --> 05:40:44,280
that we did previously but this is more
7583
05:40:44,280 --> 05:40:47,280
concise and we're using the Constructor
7584
05:40:47,280 --> 05:40:51,958
to pass the those values to the
7585
05:40:51,958 --> 05:40:54,298
parameters that we Define here and then
7586
05:40:54,298 --> 05:40:55,740
those parameters going to be assigned to
7587
05:40:55,740 --> 05:40:57,480
the properties respectively with those
7588
05:40:57,480 --> 05:40:59,218
properties name model core and doors
7589
05:40:59,218 --> 05:41:01,260
you're gonna see what is the difference
7590
05:41:01,260 --> 05:41:05,280
between the properties and parameters in
7591
05:41:05,280 --> 05:41:09,060
uh in the next video now we have also an
7592
05:41:09,060 --> 05:41:10,520
error here because
7593
05:41:10,520 --> 05:41:13,138
this also express some values to its
7594
05:41:13,138 --> 05:41:15,718
Constructor so instead of putting those
7595
05:41:15,718 --> 05:41:18,420
values here we put up here
7596
05:41:18,420 --> 05:41:21,138
Ford
7597
05:41:24,000 --> 05:41:26,900
Mustang
7598
05:41:29,760 --> 05:41:32,480
blue
7599
05:41:32,700 --> 05:41:34,200
and
7600
05:41:34,200 --> 05:41:35,520
two
7601
05:41:35,520 --> 05:41:37,860
now we can delete this
7602
05:41:37,860 --> 05:41:40,500
because we already passed the values for
7603
05:41:40,500 --> 05:41:43,320
this second object for its properties
7604
05:41:43,320 --> 05:41:45,480
now if you look at the codes the code is
7605
05:41:45,480 --> 05:41:50,040
more concise and more and easier to read
7606
05:41:50,040 --> 05:41:52,260
now if you run this code
7607
05:41:52,260 --> 05:41:55,260
you will have the same output
7608
05:41:55,260 --> 05:41:57,480
because the code
7609
05:41:57,480 --> 05:42:00,378
works well
7610
05:42:02,480 --> 05:42:07,200
so now if you look at the console let's
7611
05:42:07,200 --> 05:42:09,120
increase this a little bit
7612
05:42:09,120 --> 05:42:11,218
let's scroll up you have the name Tesla
7613
05:42:11,218 --> 05:42:13,440
Model let's Place color red doors4 the
7614
05:42:13,440 --> 05:42:15,000
cartridge lights moving the cut Isla has
7615
05:42:15,000 --> 05:42:17,040
stopped so you have the same output as
7616
05:42:17,040 --> 05:42:19,798
previously and down for the second
7617
05:42:19,798 --> 05:42:22,699
object we have
7618
05:42:22,798 --> 05:42:24,540
we have a
7619
05:42:24,540 --> 05:42:26,638
the name Ford the model Mustang the
7620
05:42:26,638 --> 05:42:28,500
color blue Doors 2 and the Carphone is
7621
05:42:28,500 --> 05:42:30,180
moving in a car 4 has stopped so our
7622
05:42:30,180 --> 05:42:34,378
code works well now let's close the
7623
05:42:34,378 --> 05:42:37,020
console but if you go down here you'll
7624
05:42:37,020 --> 05:42:39,000
see that you have this um
7625
05:42:39,000 --> 05:42:41,638
this uh the properties colored so if you
7626
05:42:41,638 --> 05:42:43,200
hover over them it says the property is
7627
05:42:43,200 --> 05:42:45,540
excretely assigned to parameter name so
7628
05:42:45,540 --> 05:42:47,040
it can be declared directly in
7629
05:42:47,040 --> 05:42:48,298
Constructor
7630
05:42:48,298 --> 05:42:51,660
now what is saying here in other words
7631
05:42:51,660 --> 05:42:54,000
is that we can put the properties
7632
05:42:54,000 --> 05:42:56,100
directly in the Constructor so you don't
7633
05:42:56,100 --> 05:42:58,620
need to define the properties down here
7634
05:42:58,620 --> 05:43:01,260
and then assign the value that is passed
7635
05:43:01,260 --> 05:43:05,218
to the parameter to the property and
7636
05:43:05,218 --> 05:43:07,740
the difference between the properties
7637
05:43:07,740 --> 05:43:10,620
and parameters parameters is that the
7638
05:43:10,620 --> 05:43:12,360
properties respectively those that we
7639
05:43:12,360 --> 05:43:14,760
Define in the in the header of the class
7640
05:43:14,760 --> 05:43:16,940
inside the parentheses 0
7641
05:43:16,940 --> 05:43:19,320
in the Constructor
7642
05:43:19,320 --> 05:43:20,940
is that
7643
05:43:20,940 --> 05:43:24,958
the properties are not uh storing the
7644
05:43:24,958 --> 05:43:28,080
state of of the class they are mastering
7645
05:43:28,080 --> 05:43:29,760
the state of the object there they are
7646
05:43:29,760 --> 05:43:32,700
only here to to get whatever value we
7647
05:43:32,700 --> 05:43:34,138
pass here
7648
05:43:34,138 --> 05:43:36,840
so whatever value you pass here
7649
05:43:36,840 --> 05:43:39,060
and his job is to only get that value
7650
05:43:39,060 --> 05:43:41,638
and assign it to the property name so
7651
05:43:41,638 --> 05:43:44,100
only the the variables which are
7652
05:43:44,100 --> 05:43:47,540
declared declared inside the class are
7653
05:43:47,540 --> 05:43:51,240
storing ultimately the the state of that
7654
05:43:51,240 --> 05:43:54,660
object so uh if I delete let's say if I
7655
05:43:54,660 --> 05:43:57,620
delete this here
7656
05:43:57,780 --> 05:44:01,320
now we have an error here because the
7657
05:44:01,320 --> 05:44:03,900
property name um it says on our result
7658
05:44:03,900 --> 05:44:06,360
reference name create member property
7659
05:44:06,360 --> 05:44:09,058
card.name and that is because the
7660
05:44:09,058 --> 05:44:12,780
property name is not declared it's not
7661
05:44:12,780 --> 05:44:16,820
exit it's not it's not a it's not uh
7662
05:44:16,820 --> 05:44:20,280
it's not a characteristic of a car we
7663
05:44:20,280 --> 05:44:23,160
didn't Define a property name because
7664
05:44:23,160 --> 05:44:26,120
here the parameters the parameter name
7665
05:44:26,120 --> 05:44:29,340
is only there to get the value and then
7666
05:44:29,340 --> 05:44:31,860
pass the value to the property
7667
05:44:31,860 --> 05:44:36,540
but we can let's press Ctrl Z to put the
7668
05:44:36,540 --> 05:44:37,680
code
7669
05:44:37,680 --> 05:44:39,120
back
7670
05:44:39,120 --> 05:44:40,920
now the error disappeared because we
7671
05:44:40,920 --> 05:44:43,558
have the property declared but because
7672
05:44:43,558 --> 05:44:46,680
the only thing that we do here is to to
7673
05:44:46,680 --> 05:44:48,900
to get the value that is passed to the
7674
05:44:48,900 --> 05:44:51,718
property as an argument here and for the
7675
05:44:51,718 --> 05:44:54,240
next ones and then assign it to the
7676
05:44:54,240 --> 05:44:57,540
property we can put those directly in
7677
05:44:57,540 --> 05:45:00,780
the Constructor so to do that we just we
7678
05:45:00,780 --> 05:45:04,378
can hover over here and
7679
05:45:04,378 --> 05:45:06,840
let's hover over
7680
05:45:06,840 --> 05:45:09,360
and click on this light bulb and click
7681
05:45:09,360 --> 05:45:11,760
move to Constructor so now you have this
7682
05:45:11,760 --> 05:45:13,440
you have the same
7683
05:45:13,440 --> 05:45:16,440
the same thing the same thing but the
7684
05:45:16,440 --> 05:45:18,420
only difference is that we have this VAR
7685
05:45:18,420 --> 05:45:20,940
keyword in front and that is responsible
7686
05:45:20,940 --> 05:45:23,458
for changing the
7687
05:45:23,458 --> 05:45:26,520
changing the parameter into a property
7688
05:45:26,520 --> 05:45:27,718
so
7689
05:45:27,718 --> 05:45:29,280
let's do the same thing for the next
7690
05:45:29,280 --> 05:45:30,540
ones
7691
05:45:30,540 --> 05:45:33,620
move to Constructor
7692
05:45:33,718 --> 05:45:35,700
and now we don't have that error that we
7693
05:45:35,700 --> 05:45:37,740
don't have a property for our class
7694
05:45:37,740 --> 05:45:40,440
because never because they have the VAR
7695
05:45:40,440 --> 05:45:42,900
so if you put the VAR or the VAR keyword
7696
05:45:42,900 --> 05:45:46,138
before declaring the the the parameter
7697
05:45:46,138 --> 05:45:48,420
it is going to be converted into a
7698
05:45:48,420 --> 05:45:49,320
property
7699
05:45:49,320 --> 05:45:53,160
so now those are properties of our class
7700
05:45:53,160 --> 05:45:55,620
car so they are storing actually they
7701
05:45:55,620 --> 05:45:57,718
are storing the the value that you pass
7702
05:45:57,718 --> 05:46:00,240
to to them
7703
05:46:00,240 --> 05:46:03,660
for the project that we create so uh
7704
05:46:03,660 --> 05:46:05,520
it's not like previously where you have
7705
05:46:05,520 --> 05:46:08,520
the property the parameter and then the
7706
05:46:08,520 --> 05:46:10,378
value is passed to the property inside
7707
05:46:10,378 --> 05:46:11,700
the class
7708
05:46:11,700 --> 05:46:15,020
so now if you run this code
7709
05:46:24,298 --> 05:46:25,440
foreign
7710
05:46:25,440 --> 05:46:27,900
we have the exact same output
7711
05:46:27,900 --> 05:46:30,360
the only difference now this that this
7712
05:46:30,360 --> 05:46:32,218
is uh more concise we have the
7713
05:46:32,218 --> 05:46:33,660
properties declared the right directly
7714
05:46:33,660 --> 05:46:35,040
in the Constructor so we don't have
7715
05:46:35,040 --> 05:46:37,620
declare the parameters and then we
7716
05:46:37,620 --> 05:46:39,120
assign the value that is passed to those
7717
05:46:39,120 --> 05:46:41,520
parameters to properties so you can
7718
05:46:41,520 --> 05:46:44,160
declare the The Constructor like this if
7719
05:46:44,160 --> 05:46:46,200
you want so the question may arise when
7720
05:46:46,200 --> 05:46:49,080
to use properties and when to use
7721
05:46:49,080 --> 05:46:53,580
parameters and to to
7722
05:46:53,580 --> 05:46:55,500
to answer that question
7723
05:46:55,500 --> 05:46:56,900
um you have you need to have in mind
7724
05:46:56,900 --> 05:47:00,298
that if you want to do some if you want
7725
05:47:00,298 --> 05:47:01,860
to do some work before you assign the
7726
05:47:01,860 --> 05:47:03,540
value to the property
7727
05:47:03,540 --> 05:47:06,600
because in our case uh the value that is
7728
05:47:06,600 --> 05:47:09,540
passed here to the to the
7729
05:47:09,540 --> 05:47:12,180
property in this case is directly stored
7730
05:47:12,180 --> 05:47:14,878
in the property so we cannot add some
7731
05:47:14,878 --> 05:47:18,240
validation to check to see if uh I don't
7732
05:47:18,240 --> 05:47:21,600
know if this name is if it starts to a
7733
05:47:21,600 --> 05:47:24,298
specific letter or if it's if it's an
7734
05:47:24,298 --> 05:47:26,760
uppercase or because the value that is
7735
05:47:26,760 --> 05:47:27,900
passed
7736
05:47:27,900 --> 05:47:30,360
that is passed as an argument to the
7737
05:47:30,360 --> 05:47:32,638
property there I say property because
7738
05:47:32,638 --> 05:47:34,558
they are there there are no declared as
7739
05:47:34,558 --> 05:47:38,760
properties it is directly stored as uh
7740
05:47:38,760 --> 05:47:42,900
as a the state of the object so
7741
05:47:42,900 --> 05:47:46,138
if you want to to do some work before
7742
05:47:46,138 --> 05:47:47,760
you assign the value of the property you
7743
05:47:47,760 --> 05:47:50,160
need to declare it let's say let's say
7744
05:47:50,160 --> 05:47:52,260
that you want to Let's actually delete
7745
05:47:52,260 --> 05:47:56,340
this and I'm going to declare it as a
7746
05:47:56,340 --> 05:48:00,000
parameter so let's say that you want to
7747
05:48:00,000 --> 05:48:02,878
to to trim the space that is passed so
7748
05:48:02,878 --> 05:48:06,320
let's say that I put some spaces here
7749
05:48:09,840 --> 05:48:12,660
let's actually press Ctrl G to see what
7750
05:48:12,660 --> 05:48:16,400
is happening if I don't do that
7751
05:48:19,740 --> 05:48:22,200
all right
7752
05:48:22,200 --> 05:48:26,700
I press Ctrl Z too many times so
7753
05:48:26,700 --> 05:48:29,340
let's let's again Paul let's put some
7754
05:48:29,340 --> 05:48:33,680
spaces here so now if you run this code
7755
05:48:37,500 --> 05:48:40,820
let's run it again
7756
05:48:44,800 --> 05:48:46,980
[Music]
7757
05:48:46,980 --> 05:48:51,000
now because the value that is passed
7758
05:48:51,000 --> 05:48:53,760
as an argument here to the object car
7759
05:48:53,760 --> 05:48:55,260
one
7760
05:48:55,260 --> 05:48:58,080
is passed directly to the property so
7761
05:48:58,080 --> 05:49:00,058
it's stored in the property and we we
7762
05:49:00,058 --> 05:49:03,298
don't do nothing with it we have also
7763
05:49:03,298 --> 05:49:05,878
the space but if you want to to delete
7764
05:49:05,878 --> 05:49:08,520
that space currently we can do that
7765
05:49:08,520 --> 05:49:10,798
because the values pass directly to the
7766
05:49:10,798 --> 05:49:12,900
property and there is nothing that we
7767
05:49:12,900 --> 05:49:14,820
can do from that point
7768
05:49:14,820 --> 05:49:18,600
and to to change that we can declare the
7769
05:49:18,600 --> 05:49:21,180
the property that is defined here the
7770
05:49:21,180 --> 05:49:24,000
property name as a parameter first
7771
05:49:24,000 --> 05:49:27,780
and we can declare it down here
7772
05:49:27,780 --> 05:49:29,580
we can declare down here now the
7773
05:49:29,580 --> 05:49:31,138
property
7774
05:49:31,138 --> 05:49:35,218
and we can put equals and now we we pass
7775
05:49:35,218 --> 05:49:38,000
the the
7776
05:49:38,000 --> 05:49:41,100
parameter to the property so it's a sign
7777
05:49:41,100 --> 05:49:42,360
name here
7778
05:49:42,360 --> 05:49:45,718
but before before that value is assigned
7779
05:49:45,718 --> 05:49:48,958
to the property name we put that
7780
05:49:48,958 --> 05:49:50,878
stream
7781
05:49:50,878 --> 05:49:52,740
and that is going to remove the spaces
7782
05:49:52,740 --> 05:49:54,958
so that Rim is a function which is part
7783
05:49:54,958 --> 05:49:59,100
of the string class because the the data
7784
05:49:59,100 --> 05:50:01,440
the all the data size that we looked at
7785
05:50:01,440 --> 05:50:04,798
are also classes so you can call
7786
05:50:04,798 --> 05:50:06,600
functions on them the way we called
7787
05:50:06,600 --> 05:50:10,020
functions so not our on our objects
7788
05:50:10,020 --> 05:50:13,798
so name that trim removes the space so
7789
05:50:13,798 --> 05:50:16,378
in this case it removes the space that
7790
05:50:16,378 --> 05:50:17,520
we have here
7791
05:50:17,520 --> 05:50:21,058
and after that it's assigning the value
7792
05:50:21,058 --> 05:50:24,298
to the name property so now if you run
7793
05:50:24,298 --> 05:50:26,520
this code now now we have the space but
7794
05:50:26,520 --> 05:50:27,958
if you run the code
7795
05:50:27,958 --> 05:50:30,420
so let's go up
7796
05:50:30,420 --> 05:50:33,260
run the code
7797
05:50:39,420 --> 05:50:41,340
now as you can see the space is removed
7798
05:50:41,340 --> 05:50:42,980
because
7799
05:50:42,980 --> 05:50:46,080
we pass the value to the
7800
05:50:46,080 --> 05:50:49,138
parameter now name then the parameter
7801
05:50:49,138 --> 05:50:52,080
name trims.space and after that it's
7802
05:50:52,080 --> 05:50:54,958
assign the value to the property name so
7803
05:50:54,958 --> 05:50:57,600
in this case so in this can be a
7804
05:50:57,600 --> 05:51:00,000
scenario where you can uh where you can
7805
05:51:00,000 --> 05:51:02,040
declare the instead of declaring
7806
05:51:02,040 --> 05:51:03,480
directly here the property can declare
7807
05:51:03,480 --> 05:51:07,260
as a parameter but what do what what you
7808
05:51:07,260 --> 05:51:09,840
can do if you want to add more code than
7809
05:51:09,840 --> 05:51:11,820
just one line of code in that case we'll
7810
05:51:11,820 --> 05:51:13,260
need to use what is called the
7811
05:51:13,260 --> 05:51:16,320
initializer blocks but
7812
05:51:16,320 --> 05:51:17,400
um we're going to look at the
7813
05:51:17,400 --> 05:51:20,760
initializer blocks and uh other things
7814
05:51:20,760 --> 05:51:22,680
in the next video because this video is
7815
05:51:22,680 --> 05:51:25,740
long enough so see you in the next video
7816
05:51:25,740 --> 05:51:28,160
so in the last video we looked at
7817
05:51:28,160 --> 05:51:30,660
Constructors and how you can use
7818
05:51:30,660 --> 05:51:33,480
Constructors to construct your objects
7819
05:51:33,480 --> 05:51:35,940
and it was an alternative to defining
7820
05:51:35,940 --> 05:51:39,360
the values to the properties of the for
7821
05:51:39,360 --> 05:51:40,920
particular object
7822
05:51:40,920 --> 05:51:43,320
and we've also looked at how we can
7823
05:51:43,320 --> 05:51:46,378
Define the this Source I should say it's
7824
05:51:46,378 --> 05:51:48,360
also called the primary Constructor that
7825
05:51:48,360 --> 05:51:50,218
we have here the parenthesis and then
7826
05:51:50,218 --> 05:51:52,500
the parameters or the properties
7827
05:51:52,500 --> 05:51:55,558
and we've also talked about how we can
7828
05:51:55,558 --> 05:51:57,780
Define parameters or Constructor and
7829
05:51:57,780 --> 05:52:00,120
then how we can convert those parameters
7830
05:52:00,120 --> 05:52:03,840
directly into properties and I've also
7831
05:52:03,840 --> 05:52:06,540
talked a little bit about how and when
7832
05:52:06,540 --> 05:52:07,700
to use
7833
05:52:07,700 --> 05:52:11,280
parameters and properties but in this
7834
05:52:11,280 --> 05:52:12,420
video we'll continue our discussion
7835
05:52:12,420 --> 05:52:14,400
about parameters and properties and
7836
05:52:14,400 --> 05:52:17,340
specifically going to look at how we can
7837
05:52:17,340 --> 05:52:20,520
execute more than one line of code when
7838
05:52:20,520 --> 05:52:23,400
the object is created because
7839
05:52:23,400 --> 05:52:28,260
now when the object is created and we in
7840
05:52:28,260 --> 05:52:29,760
the last video we trimmed this space
7841
05:52:29,760 --> 05:52:31,580
that we have here
7842
05:52:31,580 --> 05:52:34,200
you can execute only this line of course
7843
05:52:34,200 --> 05:52:35,878
so if you want to add more line of
7844
05:52:35,878 --> 05:52:38,280
course here to do some
7845
05:52:38,280 --> 05:52:42,660
um to do a more complex validation we
7846
05:52:42,660 --> 05:52:44,400
can't because here we can put just one
7847
05:52:44,400 --> 05:52:45,718
line of code
7848
05:52:45,718 --> 05:52:48,718
and to add more than one line of code of
7849
05:52:48,718 --> 05:52:50,458
code they need to use what is called
7850
05:52:50,458 --> 05:52:53,700
initializer blocks and the initializer
7851
05:52:53,700 --> 05:52:56,580
blocks are as you'll see some blocks
7852
05:52:56,580 --> 05:53:00,718
where we can put our code and you can
7853
05:53:00,718 --> 05:53:02,218
put more than online so forth you can
7854
05:53:02,218 --> 05:53:04,080
put as many lines of code you want and
7855
05:53:04,080 --> 05:53:06,298
that code is going to be executed when
7856
05:53:06,298 --> 05:53:08,760
an object is created on an instance of
7857
05:53:08,760 --> 05:53:12,660
our car of our of your class is going to
7858
05:53:12,660 --> 05:53:13,798
be created
7859
05:53:13,798 --> 05:53:16,080
so um this is what we're gonna do in
7860
05:53:16,080 --> 05:53:18,718
this video also I should say that you
7861
05:53:18,718 --> 05:53:20,820
can declare the class in another file
7862
05:53:20,820 --> 05:53:23,100
not just in this file like we did here
7863
05:53:23,100 --> 05:53:26,218
so we can open now the project pane
7864
05:53:26,218 --> 05:53:29,180
you can go to SRC go domain
7865
05:53:29,180 --> 05:53:31,500
go to kotlin
7866
05:53:31,500 --> 05:53:36,378
and here on kotlin you can right click
7867
05:53:36,900 --> 05:53:39,120
select kotlin class file
7868
05:53:39,120 --> 05:53:41,700
and from here make sure to select file
7869
05:53:41,700 --> 05:53:43,558
and I'm going to call this file classes
7870
05:53:43,558 --> 05:53:45,298
because you're going to put our classes
7871
05:53:45,298 --> 05:53:47,340
respect we're going to put our card
7872
05:53:47,340 --> 05:53:50,340
class but you can put more classes there
7873
05:53:50,340 --> 05:53:52,200
and uh
7874
05:53:52,200 --> 05:53:54,180
we're just going to copy this class that
7875
05:53:54,180 --> 05:53:56,340
we defined here
7876
05:53:56,340 --> 05:53:57,900
Ctrl C
7877
05:53:57,900 --> 05:53:59,458
and paste it here
7878
05:53:59,458 --> 05:54:01,620
now I have an error because this is
7879
05:54:01,620 --> 05:54:05,700
declared two times here and here and we
7880
05:54:05,700 --> 05:54:07,440
just delete it from here
7881
05:54:07,440 --> 05:54:10,320
and now the arrow disappears
7882
05:54:10,320 --> 05:54:13,520
and if you run this code
7883
05:54:18,480 --> 05:54:21,480
we get the same output because our code
7884
05:54:21,480 --> 05:54:23,040
works
7885
05:54:23,040 --> 05:54:25,740
perfectly well now the only difference
7886
05:54:25,740 --> 05:54:28,020
is that now we will put we put our class
7887
05:54:28,020 --> 05:54:30,540
in a different file and my suggestion is
7888
05:54:30,540 --> 05:54:32,940
for you is to put the class if you want
7889
05:54:32,940 --> 05:54:35,100
in a different fly file
7890
05:54:35,100 --> 05:54:37,620
because in this way you have the code in
7891
05:54:37,620 --> 05:54:39,780
when I'm going to organize the way
7892
05:54:39,780 --> 05:54:42,840
because here you have your class here we
7893
05:54:42,840 --> 05:54:45,420
have the code the objects that we
7894
05:54:45,420 --> 05:54:46,860
created with that class
7895
05:54:46,860 --> 05:54:49,558
and so on
7896
05:54:49,558 --> 05:54:50,638
no
7897
05:54:50,638 --> 05:54:52,680
for this video I'm gonna use I'm gonna
7898
05:54:52,680 --> 05:54:55,500
create a different class and
7899
05:54:55,500 --> 05:54:57,378
to do to do that I'm gonna declare
7900
05:54:57,378 --> 05:54:59,638
another class but I'm going to declare
7901
05:54:59,638 --> 05:55:02,340
it down here and I'm going to delete the
7902
05:55:02,340 --> 05:55:04,138
code that we have here
7903
05:55:04,138 --> 05:55:05,700
because we're going to use a different
7904
05:55:05,700 --> 05:55:08,058
class
7905
05:55:08,280 --> 05:55:10,558
and the class is going to be called user
7906
05:55:10,558 --> 05:55:14,400
so it's gonna represent the
7907
05:55:14,400 --> 05:55:17,340
the use and end user object so we type
7908
05:55:17,340 --> 05:55:18,958
here class
7909
05:55:18,958 --> 05:55:22,320
user now we create the Constructor the
7910
05:55:22,320 --> 05:55:24,120
primary Constructor we put parenthesis
7911
05:55:24,120 --> 05:55:26,760
we Define the parameters or the
7912
05:55:26,760 --> 05:55:29,520
properties for uh now I'm going to
7913
05:55:29,520 --> 05:55:31,500
Define directly the properties here so
7914
05:55:31,500 --> 05:55:33,740
we type here
7915
05:55:33,740 --> 05:55:36,660
keyword to have the property declared
7916
05:55:36,660 --> 05:55:39,480
VAR name
7917
05:55:39,480 --> 05:55:41,878
string so it's going to be we're going
7918
05:55:41,878 --> 05:55:43,680
to be store stacks
7919
05:55:43,680 --> 05:55:46,400
VAR
7920
05:55:47,940 --> 05:55:51,557
last name also string
7921
05:55:52,500 --> 05:55:54,480
and uh
7922
05:55:54,480 --> 05:55:58,378
VAR age this is going to be an integer
7923
05:55:58,378 --> 05:56:00,958
so this with this class we're going to
7924
05:56:00,958 --> 05:56:03,058
create some user objects I'm going to
7925
05:56:03,058 --> 05:56:05,638
put curly braces
7926
05:56:05,638 --> 05:56:08,400
and for now we're not gonna put any code
7927
05:56:08,400 --> 05:56:10,558
here so let's create a user so let's
7928
05:56:10,558 --> 05:56:13,218
type here Val
7929
05:56:13,558 --> 05:56:15,780
or let's call it user you can give it
7930
05:56:15,780 --> 05:56:17,520
whatever you want
7931
05:56:17,520 --> 05:56:20,100
so let's create a new object now so we
7932
05:56:20,100 --> 05:56:21,480
put
7933
05:56:21,480 --> 05:56:23,400
you type your user and then for the
7934
05:56:23,400 --> 05:56:25,798
parenthesis because I'm going to put
7935
05:56:25,798 --> 05:56:27,120
we're going to pass the values to the
7936
05:56:27,120 --> 05:56:29,218
Constructor here Sony to pass a name I'm
7937
05:56:29,218 --> 05:56:31,680
gonna type Alex
7938
05:56:31,680 --> 05:56:33,660
last name
7939
05:56:33,660 --> 05:56:36,500
no binka
7940
05:56:36,540 --> 05:56:40,280
and for the age 23
7941
05:56:40,860 --> 05:56:43,980
so now we created a new instance a new
7942
05:56:43,980 --> 05:56:46,620
object using the using the cluster to
7943
05:56:46,620 --> 05:56:48,360
Define below
7944
05:56:48,360 --> 05:56:53,458
but what if I want to to pass to to the
7945
05:56:53,458 --> 05:56:55,138
to the name
7946
05:56:55,138 --> 05:56:57,920
to the name name property only the the
7947
05:56:57,920 --> 05:57:02,340
names which starts with the letter A
7948
05:57:02,340 --> 05:57:04,580
in that case we need to declare this
7949
05:57:04,580 --> 05:57:08,400
property as a parameter first we need to
7950
05:57:08,400 --> 05:57:09,840
declare here the parameter and then to
7951
05:57:09,840 --> 05:57:13,920
declare the property below and before
7952
05:57:13,920 --> 05:57:16,320
before we assign the value that is
7953
05:57:16,320 --> 05:57:18,660
passed to to the property we need to do
7954
05:57:18,660 --> 05:57:22,440
some validation and if that argument
7955
05:57:22,440 --> 05:57:24,298
that is passed to the
7956
05:57:24,298 --> 05:57:28,080
parameter is starting with the ale
7957
05:57:28,080 --> 05:57:30,000
letter then you're going to assign the
7958
05:57:30,000 --> 05:57:32,280
value to the property name otherwise we
7959
05:57:32,280 --> 05:57:34,980
don't do that so to do that we need to
7960
05:57:34,980 --> 05:57:37,740
use initializer blocks because we're
7961
05:57:37,740 --> 05:57:40,320
gonna type more than one line of code
7962
05:57:40,320 --> 05:57:42,718
and the initializer blocks are used in
7963
05:57:42,718 --> 05:57:44,638
conjunction with the primary Constructor
7964
05:57:44,638 --> 05:57:47,160
so when we create a new object like we
7965
05:57:47,160 --> 05:57:49,798
did here the initializer blocks or the
7966
05:57:49,798 --> 05:57:51,600
initializer block is going to be called
7967
05:57:51,600 --> 05:57:54,180
immediately when the object is created
7968
05:57:54,180 --> 05:57:57,718
and to use an initial initializer block
7969
05:57:57,718 --> 05:58:00,420
we just but first let's declare let's
7970
05:58:00,420 --> 05:58:03,298
change this to to a parameter so let's
7971
05:58:03,298 --> 05:58:06,620
delete the VAR keyword in front of
7972
05:58:06,620 --> 05:58:09,058
the variable so now we have only a
7973
05:58:09,058 --> 05:58:11,040
parameter here now let's declare the
7974
05:58:11,040 --> 05:58:13,440
property down here
7975
05:58:13,440 --> 05:58:15,360
and now here we need to Define an
7976
05:58:15,360 --> 05:58:17,520
explicit type declaration so we need to
7977
05:58:17,520 --> 05:58:20,520
put here the type string because as I
7978
05:58:20,520 --> 05:58:21,180
said
7979
05:58:21,180 --> 05:58:23,580
um we're not going to assign the value
7980
05:58:23,580 --> 05:58:26,820
to the property directly here in one
7981
05:58:26,820 --> 05:58:29,580
line of code we're going to assign the
7982
05:58:29,580 --> 05:58:31,920
value to the property inside the
7983
05:58:31,920 --> 05:58:33,958
initializer blocked after we do the
7984
05:58:33,958 --> 05:58:36,718
validation respectively after we check
7985
05:58:36,718 --> 05:58:40,260
to see if the name that is passed to the
7986
05:58:40,260 --> 05:58:42,120
objects that we're going to create with
7987
05:58:42,120 --> 05:58:43,138
the class
7988
05:58:43,138 --> 05:58:46,500
starts with the letter a and if the the
7989
05:58:46,500 --> 05:58:49,080
name if the argument doesn't start with
7990
05:58:49,080 --> 05:58:50,218
the letter A then we're going to assign
7991
05:58:50,218 --> 05:58:52,080
an empty string and we're gonna output
7992
05:58:52,080 --> 05:58:53,878
something to the console so this is why
7993
05:58:53,878 --> 05:58:55,860
we need to provide here the type because
7994
05:58:55,860 --> 05:58:57,120
I'm not gonna
7995
05:58:57,120 --> 05:58:59,280
assign the value directly here so it
7996
05:58:59,280 --> 05:59:02,040
cannot infer what kind of data is going
7997
05:59:02,040 --> 05:59:02,940
to store
7998
05:59:02,940 --> 05:59:04,558
then you press enter
7999
05:59:04,558 --> 05:59:06,600
we type init
8000
05:59:06,600 --> 05:59:08,760
and here we have a suggestion we have
8001
05:59:08,760 --> 05:59:10,620
the unique keyword and then we have the
8002
05:59:10,620 --> 05:59:12,900
curly braces press enter
8003
05:59:12,900 --> 05:59:15,058
and now here we're gonna add our
8004
05:59:15,058 --> 05:59:18,120
validation and here we type if
8005
05:59:18,120 --> 05:59:20,160
parenthesis
8006
05:59:20,160 --> 05:59:23,638
now type name so now we're referring the
8007
05:59:23,638 --> 05:59:26,940
name parameter defined above here so if
8008
05:59:26,940 --> 05:59:29,940
name or the value that is passed to the
8009
05:59:29,940 --> 05:59:31,878
name parameter
8010
05:59:31,878 --> 05:59:34,020
first we're gonna
8011
05:59:34,020 --> 05:59:34,740
um
8012
05:59:34,740 --> 05:59:37,500
lowercase dot name
8013
05:59:37,500 --> 05:59:40,260
and to do that we type here name dot
8014
05:59:40,260 --> 05:59:43,620
lowercase because I want to have both
8015
05:59:43,620 --> 05:59:45,600
cases so if
8016
05:59:45,600 --> 05:59:48,958
if the user passes let's say a name of
8017
05:59:48,958 --> 05:59:52,138
the lowercase letter then we're gonna
8018
05:59:52,138 --> 05:59:54,780
check to see if it starts with OTA but
8019
05:59:54,780 --> 05:59:57,900
if the user passes the the name of the
8020
05:59:57,900 --> 06:00:00,058
an uppercase later we also want to check
8021
06:00:00,058 --> 06:00:03,180
to see if if that letter starts with a
8022
06:00:03,180 --> 06:00:05,820
and to cover both cases we lowercase the
8023
06:00:05,820 --> 06:00:08,820
input we lowercase the name so all the
8024
06:00:08,820 --> 06:00:11,940
lowercase all the letters then we put
8025
06:00:11,940 --> 06:00:13,920
here another Dot
8026
06:00:13,920 --> 06:00:17,218
starts with an input here
8027
06:00:17,218 --> 06:00:18,958
a
8028
06:00:18,958 --> 06:00:21,058
and if that is true
8029
06:00:21,058 --> 06:00:24,000
then we're gonna assign whatever value
8030
06:00:24,000 --> 06:00:25,980
was passed to the name parameter to the
8031
06:00:25,980 --> 06:00:27,298
name property
8032
06:00:27,298 --> 06:00:31,020
and we cannot type here name equals name
8033
06:00:31,020 --> 06:00:33,480
because now it's referring the
8034
06:00:33,480 --> 06:00:36,420
parameter here and to refer the property
8035
06:00:36,420 --> 06:00:38,160
that we Define here we need to use what
8036
06:00:38,160 --> 06:00:40,378
is called the disk keyword so the disk
8037
06:00:40,378 --> 06:00:42,600
keyword is used to refer them
8038
06:00:42,600 --> 06:00:45,780
properties of the class so it are here
8039
06:00:45,780 --> 06:00:47,458
this
8040
06:00:47,458 --> 06:00:50,700
so we're saying this name so now we have
8041
06:00:50,700 --> 06:00:52,320
this name and we have a different we
8042
06:00:52,320 --> 06:00:54,540
have this V variable here
8043
06:00:54,540 --> 06:00:56,760
then put equals
8044
06:00:56,760 --> 06:00:59,760
name so now we assign the value that is
8045
06:00:59,760 --> 06:01:02,520
passed to parameter only if that
8046
06:01:02,520 --> 06:01:05,400
valuable only if that value pass to the
8047
06:01:05,400 --> 06:01:07,378
parameter starts with the letter A then
8048
06:01:07,378 --> 06:01:09,840
we're gonna assign the name to the
8049
06:01:09,840 --> 06:01:11,218
property name
8050
06:01:11,218 --> 06:01:13,740
else if that is not true
8051
06:01:13,740 --> 06:01:15,718
put curly braces
8052
06:01:15,718 --> 06:01:17,760
and now we need to cover the else part
8053
06:01:17,760 --> 06:01:20,040
because if this is not true we also need
8054
06:01:20,040 --> 06:01:22,500
to pass a value to to the name proper
8055
06:01:22,500 --> 06:01:25,020
the name property cannot stay without a
8056
06:01:25,020 --> 06:01:26,878
value so we need to pass those over here
8057
06:01:26,878 --> 06:01:28,798
else we're going to put this
8058
06:01:28,798 --> 06:01:30,718
dot name
8059
06:01:30,718 --> 06:01:33,660
and I'm going to put
8060
06:01:33,660 --> 06:01:37,138
user so I'm going to put the user
8061
06:01:37,138 --> 06:01:41,100
text if the if the the name doesn't
8062
06:01:41,100 --> 06:01:43,320
start with the with the letter A
8063
06:01:43,320 --> 06:01:45,660
and we can also output some text.com so
8064
06:01:45,660 --> 06:01:48,620
we put println here
8065
06:01:48,780 --> 06:01:51,180
and we'll type the name
8066
06:01:51,180 --> 06:01:53,840
doesn't
8067
06:01:53,940 --> 06:01:55,500
start
8068
06:01:55,500 --> 06:01:57,540
with
8069
06:01:57,540 --> 06:02:00,200
the letter
8070
06:02:01,320 --> 06:02:04,320
a or
8071
06:02:04,320 --> 06:02:07,340
capital a
8072
06:02:07,680 --> 06:02:10,500
so now if you create a new object here
8073
06:02:10,500 --> 06:02:15,600
so we type here Val let's say for friend
8074
06:02:15,600 --> 06:02:17,580
we create a new object so we type user
8075
06:02:17,580 --> 06:02:19,680
now we need to pass the values to the
8076
06:02:19,680 --> 06:02:22,558
Constructor so you put here a let's say
8077
06:02:22,558 --> 06:02:24,360
uh
8078
06:02:24,360 --> 06:02:26,900
John
8079
06:02:29,100 --> 06:02:30,958
let's put
8080
06:02:30,958 --> 06:02:32,458
Smith
8081
06:02:32,458 --> 06:02:34,080
let's put
8082
06:02:34,080 --> 06:02:37,740
30. now because the the value that you
8083
06:02:37,740 --> 06:02:39,840
pass here to the parameter name doesn't
8084
06:02:39,840 --> 06:02:43,320
start with a letter a and start the
8085
06:02:43,320 --> 06:02:45,120
letter j
8086
06:02:45,120 --> 06:02:47,580
this value validation inside the
8087
06:02:47,580 --> 06:02:50,100
initializer block is going to be false
8088
06:02:50,100 --> 06:02:52,020
and then it's gonna it's gonna execute
8089
06:02:52,020 --> 06:02:53,458
the else part and it's going to assign
8090
06:02:53,458 --> 06:02:57,420
to the property name the user text and
8091
06:02:57,420 --> 06:02:58,500
it's going to Output the name doesn't
8092
06:02:58,500 --> 06:03:01,020
start with the letter A or a but if that
8093
06:03:01,020 --> 06:03:03,840
is not uh if but if it's true if it
8094
06:03:03,840 --> 06:03:05,340
starts with the letter A then this line
8095
06:03:05,340 --> 06:03:06,840
of code is going to basically the base
8096
06:03:06,840 --> 06:03:10,080
part n is going to be a sign it's going
8097
06:03:10,080 --> 06:03:12,718
to assign the value to the property
8098
06:03:12,718 --> 06:03:16,218
so now if you run the squad
8099
06:03:21,420 --> 06:03:22,980
we get the name doesn't start with the
8100
06:03:22,980 --> 06:03:25,320
letter A or a and it's correct our name
8101
06:03:25,320 --> 06:03:26,940
starts with the letter j
8102
06:03:26,940 --> 06:03:31,039
so if I put here let's say
8103
06:03:31,500 --> 06:03:34,160
Andhra
8104
06:03:35,280 --> 06:03:38,420
let's change this to
8105
06:03:40,920 --> 06:03:43,138
oh let's see here also
8106
06:03:43,138 --> 06:03:45,798
Smith
8107
06:03:46,680 --> 06:03:50,000
now if I run this
8108
06:03:50,100 --> 06:03:53,638
now we have no output because the this
8109
06:03:53,638 --> 06:03:56,700
the initializer block is is called
8110
06:03:56,700 --> 06:03:59,820
and the If part is evalu is to true and
8111
06:03:59,820 --> 06:04:02,940
it's assigning the the name
8112
06:04:02,940 --> 06:04:06,058
Andra to our name property so let's add
8113
06:04:06,058 --> 06:04:08,040
the print and then down here to see that
8114
06:04:08,040 --> 06:04:10,020
that is correct so print
8115
06:04:10,020 --> 06:04:12,120
let's print the value of
8116
06:04:12,120 --> 06:04:14,700
of the property name for our friend
8117
06:04:14,700 --> 06:04:18,020
object and we put here
8118
06:04:18,378 --> 06:04:21,180
and here we type
8119
06:04:21,180 --> 06:04:24,138
dollar sign
8120
06:04:25,020 --> 06:04:30,138
you put curly braces and you type friend
8121
06:04:33,360 --> 06:04:35,218
Dot
8122
06:04:35,218 --> 06:04:37,200
name
8123
06:04:37,200 --> 06:04:41,298
so now if you run this let's close
8124
06:04:45,240 --> 06:04:48,240
get name under let's put comma here
8125
06:04:48,240 --> 06:04:49,920
let's put
8126
06:04:49,920 --> 06:04:52,820
colon here
8127
06:04:53,878 --> 06:04:57,120
so our initializer blocked is called the
8128
06:04:57,120 --> 06:04:59,520
code inside the initializer block this
8129
06:04:59,520 --> 06:05:02,940
execute it and then this if check
8130
06:05:02,940 --> 06:05:04,680
average to true and then and then it
8131
06:05:04,680 --> 06:05:07,020
assigns the name Andra to our property
8132
06:05:07,020 --> 06:05:11,298
name but if we I put here John
8133
06:05:17,400 --> 06:05:19,680
let's type it like this let's say to see
8134
06:05:19,680 --> 06:05:21,240
that it's working because we lowercase
8135
06:05:21,240 --> 06:05:23,840
the letters
8136
06:05:28,440 --> 06:05:30,000
okay the name doesn't start with the
8137
06:05:30,000 --> 06:05:32,878
letter A or a name user so the
8138
06:05:32,878 --> 06:05:34,378
initializer block is executed
8139
06:05:34,378 --> 06:05:36,298
immediately when the object is created
8140
06:05:36,298 --> 06:05:38,280
and
8141
06:05:38,280 --> 06:05:42,058
the code incidential log is called it
8142
06:05:42,058 --> 06:05:43,680
checks to see if it starts at the lower
8143
06:05:43,680 --> 06:05:46,920
it starts with the with the letter A
8144
06:05:46,920 --> 06:05:49,138
and if not then it is assigned to the
8145
06:05:49,138 --> 06:05:51,058
property name user so we have this we
8146
06:05:51,058 --> 06:05:52,260
have user here
8147
06:05:52,260 --> 06:05:54,420
and then it outputs uh
8148
06:05:54,420 --> 06:05:56,458
this
8149
06:05:56,458 --> 06:05:58,558
text to the console so this is how we
8150
06:05:58,558 --> 06:06:00,240
can use the initializer blocks in
8151
06:06:00,240 --> 06:06:02,000
conjunction with the primary Constructor
8152
06:06:02,000 --> 06:06:05,100
and you can also put more initializer
8153
06:06:05,100 --> 06:06:07,740
blocks and the order in which you put
8154
06:06:07,740 --> 06:06:10,558
the initializer blocks matters in the
8155
06:06:10,558 --> 06:06:13,260
sense that the word in which you define
8156
06:06:13,260 --> 06:06:15,360
the initializer blocked is is going to
8157
06:06:15,360 --> 06:06:17,400
be the word in which they are executed
8158
06:06:17,400 --> 06:06:19,680
so the word in which you define them
8159
06:06:19,680 --> 06:06:22,920
it's important but most of the time I
8160
06:06:22,920 --> 06:06:24,780
think you'll you use only one
8161
06:06:24,780 --> 06:06:28,080
initializer block to put your code so
8162
06:06:28,080 --> 06:06:30,120
this is our discussion about initializer
8163
06:06:30,120 --> 06:06:32,100
blocks and in the next video we're going
8164
06:06:32,100 --> 06:06:33,480
to look at um
8165
06:06:33,480 --> 06:06:34,740
because
8166
06:06:34,740 --> 06:06:36,920
we talked about the primary Constructors
8167
06:06:36,920 --> 06:06:40,440
there is a another type of Constructor
8168
06:06:40,440 --> 06:06:42,058
which is called the secondary
8169
06:06:42,058 --> 06:06:44,040
Constructor of this secondary
8170
06:06:44,040 --> 06:06:45,780
Constructors
8171
06:06:45,780 --> 06:06:48,540
and we can also look at the default
8172
06:06:48,540 --> 06:06:51,080
parameters because you can put you can
8173
06:06:51,080 --> 06:06:55,020
we can Define default values to the
8174
06:06:55,020 --> 06:06:57,360
properties also the parameters of the
8175
06:06:57,360 --> 06:06:59,400
Constructor so we're gonna do that we're
8176
06:06:59,400 --> 06:07:02,040
going to do that in the next video
8177
06:07:02,040 --> 06:07:04,378
I'm gonna end this video a little now
8178
06:07:04,378 --> 06:07:06,240
so now it's time to start a discussion
8179
06:07:06,240 --> 06:07:08,638
about secondary Constructors because
8180
06:07:08,638 --> 06:07:11,638
besides our primary Constructor that we
8181
06:07:11,638 --> 06:07:13,920
Define and that we talked about in
8182
06:07:13,920 --> 06:07:15,840
Cotton you can Define multiple
8183
06:07:15,840 --> 06:07:18,000
Constructors and those are called the
8184
06:07:18,000 --> 06:07:19,798
secondary Constructors
8185
06:07:19,798 --> 06:07:21,540
now you may be wondering why you will
8186
06:07:21,540 --> 06:07:23,400
need secondary Constructors
8187
06:07:23,400 --> 06:07:26,878
and to answer that question we need to
8188
06:07:26,878 --> 06:07:30,240
consider the user class and those user
8189
06:07:30,240 --> 06:07:32,340
objects that you can create so let's say
8190
06:07:32,340 --> 06:07:33,958
that an user
8191
06:07:33,958 --> 06:07:37,260
only provides its first name and for the
8192
06:07:37,260 --> 06:07:39,718
property is last name and the age
8193
06:07:39,718 --> 06:07:42,120
doesn't provide any value in that case
8194
06:07:42,120 --> 06:07:44,760
you will need the you need a way to to
8195
06:07:44,760 --> 06:07:46,940
Define some default values for those
8196
06:07:46,940 --> 06:07:49,620
properties in case the user only
8197
06:07:49,620 --> 06:07:51,298
provides the first name
8198
06:07:51,298 --> 06:07:53,458
and uh
8199
06:07:53,458 --> 06:07:57,540
if let's say the next user does provide
8200
06:07:57,540 --> 06:07:59,638
the the first name and the last name and
8201
06:07:59,638 --> 06:08:02,520
the age in that case you're gonna use uh
8202
06:08:02,520 --> 06:08:05,100
the the primary Constructor but if it
8203
06:08:05,100 --> 06:08:07,558
doesn't provide those uh those values
8204
06:08:07,558 --> 06:08:08,940
then you're gonna use the secondary
8205
06:08:08,940 --> 06:08:11,520
Constructor but to make this more clear
8206
06:08:11,520 --> 06:08:14,400
lists uh let's add the code and we talk
8207
06:08:14,400 --> 06:08:17,340
we talk about was while I am writing the
8208
06:08:17,340 --> 06:08:19,200
code so let's delete the initializer
8209
06:08:19,200 --> 06:08:21,360
blocks that we have here because you
8210
06:08:21,360 --> 06:08:23,580
don't need this code
8211
06:08:23,580 --> 06:08:25,740
and let's also put the property now
8212
06:08:25,740 --> 06:08:27,480
let's define the property in the primary
8213
06:08:27,480 --> 06:08:30,240
Constructor so let's just put here a
8214
06:08:30,240 --> 06:08:32,760
VAR the work here in front of the
8215
06:08:32,760 --> 06:08:34,020
variable
8216
06:08:34,020 --> 06:08:38,120
let's also delete this printalian
8217
06:08:39,360 --> 06:08:41,458
and um
8218
06:08:41,458 --> 06:08:44,899
let's delete the values here
8219
06:08:47,040 --> 06:08:49,740
for the last name and the age so now as
8220
06:08:49,740 --> 06:08:51,120
you can see we have an error because
8221
06:08:51,120 --> 06:08:53,940
it's expecting here the
8222
06:08:53,940 --> 06:08:55,798
um it's expecting an argument a value
8223
06:08:55,798 --> 06:08:59,340
for the last name and for the age and to
8224
06:08:59,340 --> 06:09:01,620
solve that problem we just go down here
8225
06:09:01,620 --> 06:09:05,458
and we type Constructor so this is the
8226
06:09:05,458 --> 06:09:07,740
keyword for declaring the secondary
8227
06:09:07,740 --> 06:09:10,280
Constructor
8228
06:09:10,860 --> 06:09:12,958
and we press enter to fill in that for
8229
06:09:12,958 --> 06:09:15,958
us and we put parentheses and now here
8230
06:09:15,958 --> 06:09:17,878
in the parenthesis we Define the
8231
06:09:17,878 --> 06:09:21,298
parameters and I am specifically saying
8232
06:09:21,298 --> 06:09:23,940
parameters because as you will see the
8233
06:09:23,940 --> 06:09:26,820
secondary Constructors cannot declare
8234
06:09:26,820 --> 06:09:28,798
properties they can only declare
8235
06:09:28,798 --> 06:09:30,900
parameters because
8236
06:09:30,900 --> 06:09:33,540
anyway Let's uh so let's define here the
8237
06:09:33,540 --> 06:09:37,860
parameter so I'm gonna type here name
8238
06:09:37,860 --> 06:09:40,200
let's put here the type of this variable
8239
06:09:40,200 --> 06:09:42,298
is going to be a string
8240
06:09:42,298 --> 06:09:45,900
now if you hover over here
8241
06:09:45,900 --> 06:09:47,760
it says primary Constructor called
8242
06:09:47,760 --> 06:09:52,680
aspect insert this call so what all the
8243
06:09:52,680 --> 06:09:55,740
secondary Constructors need to do is
8244
06:09:55,740 --> 06:09:57,780
they need to call the primary
8245
06:09:57,780 --> 06:10:00,600
Constructor because ultimately the
8246
06:10:00,600 --> 06:10:02,520
primary Constructor is the one which
8247
06:10:02,520 --> 06:10:04,860
declares the property the properties for
8248
06:10:04,860 --> 06:10:09,840
the class and stores the values for uh
8249
06:10:09,840 --> 06:10:13,260
of that Pro of that property and to do
8250
06:10:13,260 --> 06:10:15,120
that to put here
8251
06:10:15,120 --> 06:10:17,520
colon
8252
06:10:17,520 --> 06:10:20,520
we type this
8253
06:10:20,520 --> 06:10:23,878
so we put colon this type that this
8254
06:10:23,878 --> 06:10:26,280
keyword then you put parenthesis
8255
06:10:26,280 --> 06:10:27,420
and
8256
06:10:27,420 --> 06:10:31,798
to the disc call we pass the name so
8257
06:10:31,798 --> 06:10:34,878
this name that we defined here
8258
06:10:34,878 --> 06:10:37,860
and for the next two values we're going
8259
06:10:37,860 --> 06:10:39,420
to pass some default values so here I'm
8260
06:10:39,420 --> 06:10:42,360
going to put user because let's say that
8261
06:10:42,360 --> 06:10:45,900
because as I said in the beginning this
8262
06:10:45,900 --> 06:10:48,680
user is gonna not it's not gonna provide
8263
06:10:48,680 --> 06:10:50,940
the last name so let's put your last
8264
06:10:50,940 --> 06:10:53,760
name not user last
8265
06:10:53,760 --> 06:10:55,200
name
8266
06:10:55,200 --> 06:10:59,340
and for the age let's put
8267
06:10:59,340 --> 06:11:01,620
let's put let's say zero
8268
06:11:01,620 --> 06:11:04,080
all right now
8269
06:11:04,080 --> 06:11:06,900
what this is going to do is now if you
8270
06:11:06,900 --> 06:11:09,180
look up here don't have an error so
8271
06:11:09,180 --> 06:11:11,820
let's uh so now we don't have that error
8272
06:11:11,820 --> 06:11:14,400
because now it's using the secondary
8273
06:11:14,400 --> 06:11:17,218
Constructor is passing the value here
8274
06:11:17,218 --> 06:11:20,520
Alex to the secondary Constructor that
8275
06:11:20,520 --> 06:11:22,680
we Define here and here we Define only
8276
06:11:22,680 --> 06:11:24,900
the parameter and the value that is
8277
06:11:24,900 --> 06:11:27,718
passed as parameter then
8278
06:11:27,718 --> 06:11:30,180
is passed to this to the primary
8279
06:11:30,180 --> 06:11:33,420
Constructor because by putting colon and
8280
06:11:33,420 --> 06:11:35,700
these and parenthesis we're calling now
8281
06:11:35,700 --> 06:11:37,500
the primary Constructor
8282
06:11:37,500 --> 06:11:40,020
and is the primary Constructor the one
8283
06:11:40,020 --> 06:11:41,040
which
8284
06:11:41,040 --> 06:11:43,920
ultimately declares the property and
8285
06:11:43,920 --> 06:11:47,100
stores whatever value we send to him in
8286
06:11:47,100 --> 06:11:48,900
this case we send
8287
06:11:48,900 --> 06:11:52,798
the value Alex and for the last name we
8288
06:11:52,798 --> 06:11:56,040
uh we just send the last the text last
8289
06:11:56,040 --> 06:11:58,740
name and for the age you put 0 because
8290
06:11:58,740 --> 06:12:02,100
this is our way to saying that this user
8291
06:12:02,100 --> 06:12:05,580
doesn't want to provide its age so this
8292
06:12:05,580 --> 06:12:08,400
is why you need to call the the primary
8293
06:12:08,400 --> 06:12:10,378
Constructor from the secondary
8294
06:12:10,378 --> 06:12:14,040
Constructor because again the secondary
8295
06:12:14,040 --> 06:12:17,400
the primary Constructor is the one which
8296
06:12:17,400 --> 06:12:19,320
ultimately declares the properties of
8297
06:12:19,320 --> 06:12:22,740
the classes the one which stores the the
8298
06:12:22,740 --> 06:12:25,680
the properties of the class the
8299
06:12:25,680 --> 06:12:28,200
secondary Constructor only
8300
06:12:28,200 --> 06:12:30,958
only takes parameters and then calls the
8301
06:12:30,958 --> 06:12:33,420
primary Constructor so in fact if you
8302
06:12:33,420 --> 06:12:35,400
put here the VAR keyword
8303
06:12:35,400 --> 06:12:37,798
you have an arrow which says
8304
06:12:37,798 --> 06:12:40,080
Varon secondary Constructor parameter is
8305
06:12:40,080 --> 06:12:41,520
not allowed that is because the
8306
06:12:41,520 --> 06:12:43,320
secondary Constructor cannot call it
8307
06:12:43,320 --> 06:12:45,298
cannot declare properties
8308
06:12:45,298 --> 06:12:47,760
only the primary Constructor
8309
06:12:47,760 --> 06:12:50,340
and because as I said you can declare
8310
06:12:50,340 --> 06:12:52,500
multiple second Constructors we can type
8311
06:12:52,500 --> 06:12:53,400
here
8312
06:12:53,400 --> 06:12:55,920
Constructor so we can declare another
8313
06:12:55,920 --> 06:12:58,020
secondary Constructor and here you can
8314
06:12:58,020 --> 06:12:58,860
put
8315
06:12:58,860 --> 06:13:01,580
name
8316
06:13:02,100 --> 06:13:05,218
here we put string
8317
06:13:05,218 --> 06:13:07,980
then we type last name also string so
8318
06:13:07,980 --> 06:13:09,780
this secondary Constructor is only going
8319
06:13:09,780 --> 06:13:10,680
to take
8320
06:13:10,680 --> 06:13:12,240
the
8321
06:13:12,240 --> 06:13:16,378
name and the last name as arguments the
8322
06:13:16,378 --> 06:13:20,280
age is not gonna this uh is gonna not
8323
06:13:20,280 --> 06:13:22,980
gonna take any value for the age now we
8324
06:13:22,980 --> 06:13:25,680
need to put colon
8325
06:13:25,680 --> 06:13:28,500
we type this parenthesis because now
8326
06:13:28,500 --> 06:13:30,180
we're calling the
8327
06:13:30,180 --> 06:13:33,840
primary Constructor and here we we pass
8328
06:13:33,840 --> 06:13:35,520
the name that is passed to this
8329
06:13:35,520 --> 06:13:37,558
secondary Constructor this that we
8330
06:13:37,558 --> 06:13:39,600
Define here
8331
06:13:39,600 --> 06:13:42,557
the last name
8332
06:13:43,920 --> 06:13:45,840
and for the age we're going to put 0
8333
06:13:45,840 --> 06:13:48,480
because this uh
8334
06:13:48,480 --> 06:13:50,780
this second secondary Constructor
8335
06:13:50,780 --> 06:13:52,520
doesn't
8336
06:13:52,520 --> 06:13:56,100
take any value any arguments for its uh
8337
06:13:56,100 --> 06:13:58,200
for its age
8338
06:13:58,200 --> 06:14:01,378
but because as I said ultimately the
8339
06:14:01,378 --> 06:14:03,500
primary Constructor is the one which
8340
06:14:03,500 --> 06:14:07,500
defines the properties we cannot we
8341
06:14:07,500 --> 06:14:08,878
cannot call the prime Constructor and
8342
06:14:08,878 --> 06:14:10,500
not pass any value so we pass this
8343
06:14:10,500 --> 06:14:12,120
default value here
8344
06:14:12,120 --> 06:14:14,100
and uh
8345
06:14:14,100 --> 06:14:16,680
so now if you go up here and let's say
8346
06:14:16,680 --> 06:14:19,638
that I delete the age
8347
06:14:20,218 --> 06:14:23,940
now our code works fine because now what
8348
06:14:23,940 --> 06:14:26,638
is happening here is this the the value
8349
06:14:26,638 --> 06:14:28,378
for the
8350
06:14:28,378 --> 06:14:31,740
for the name and the last name is passed
8351
06:14:31,740 --> 06:14:34,020
to this secondary Constructor this one
8352
06:14:34,020 --> 06:14:35,638
here
8353
06:14:35,638 --> 06:14:39,120
and the secondary Constructor then calls
8354
06:14:39,120 --> 06:14:41,458
this primary Constructor and it's
8355
06:14:41,458 --> 06:14:43,260
passing the two values to the Primal
8356
06:14:43,260 --> 06:14:45,718
structure name respectively this name
8357
06:14:45,718 --> 06:14:50,458
John and last name Smith and for the age
8358
06:14:50,458 --> 06:14:54,540
it passes the default volume zero so
8359
06:14:54,540 --> 06:14:56,520
this is how you can this is why we can
8360
06:14:56,520 --> 06:14:59,218
pass here uh
8361
06:14:59,218 --> 06:15:01,798
this this is why you can avoid here to
8362
06:15:01,798 --> 06:15:04,620
pass let's say uh you can avoid to pass
8363
06:15:04,620 --> 06:15:07,200
the last name and the age for the first
8364
06:15:07,200 --> 06:15:09,780
because it is using the first secondary
8365
06:15:09,780 --> 06:15:11,218
Constructor
8366
06:15:11,218 --> 06:15:13,320
and this object that we created here
8367
06:15:13,320 --> 06:15:16,500
it's using this secondary Constructor
8368
06:15:16,500 --> 06:15:18,840
because here we're passing only the
8369
06:15:18,840 --> 06:15:21,840
first name for the value for the first
8370
06:15:21,840 --> 06:15:23,878
name property and the value for the last
8371
06:15:23,878 --> 06:15:26,218
name property so it's passing them to
8372
06:15:26,218 --> 06:15:28,920
this secondary Constructor which then in
8373
06:15:28,920 --> 06:15:32,218
turn calls the primary Constructor it
8374
06:15:32,218 --> 06:15:33,900
passes them to the primary Constructor
8375
06:15:33,900 --> 06:15:35,780
so it passes the
8376
06:15:35,780 --> 06:15:39,780
the values for the two properties and
8377
06:15:39,780 --> 06:15:42,298
the last one is defaulted because
8378
06:15:42,298 --> 06:15:44,100
okay because you don't provide the value
8379
06:15:44,100 --> 06:15:45,240
for the age
8380
06:15:45,240 --> 06:15:47,280
with Pro with secondary Constructors you
8381
06:15:47,280 --> 06:15:49,558
can also put you can at the end of the
8382
06:15:49,558 --> 06:15:51,298
call of the primary consultor you can
8383
06:15:51,298 --> 06:15:54,180
put curly braces
8384
06:15:54,180 --> 06:15:56,280
to have some code executed when you
8385
06:15:56,280 --> 06:15:58,920
create an object with that specific uh
8386
06:15:58,920 --> 06:16:01,500
secondary Constructor so you can put
8387
06:16:01,500 --> 06:16:03,660
here curly braces and inside the color
8388
06:16:03,660 --> 06:16:06,000
blades you can have some code which is
8389
06:16:06,000 --> 06:16:07,378
going to be executed you want to create
8390
06:16:07,378 --> 06:16:10,020
an object with this let's say secondary
8391
06:16:10,020 --> 06:16:12,020
Constructor
8392
06:16:12,020 --> 06:16:13,920
an object with this secondary
8393
06:16:13,920 --> 06:16:14,940
Constructor
8394
06:16:14,940 --> 06:16:17,100
so it's up to you what the code you put
8395
06:16:17,100 --> 06:16:19,980
there but to illustrate this let's put a
8396
06:16:19,980 --> 06:16:24,180
println statement here in the
8397
06:16:24,180 --> 06:16:25,920
in this secondary Constructor so let's
8398
06:16:25,920 --> 06:16:27,600
type here println and here I'm going to
8399
06:16:27,600 --> 06:16:28,378
type
8400
06:16:28,378 --> 06:16:31,378
second and I'm going to type second
8401
06:16:31,378 --> 06:16:34,138
because uh
8402
06:16:34,138 --> 06:16:36,780
this is the first Constructor this is
8403
06:16:36,780 --> 06:16:38,458
the primary Constructor and this is the
8404
06:16:38,458 --> 06:16:41,160
one which ultimately does all the work
8405
06:16:41,160 --> 06:16:43,200
is the one which stores the value for uh
8406
06:16:43,200 --> 06:16:45,240
the properties
8407
06:16:45,240 --> 06:16:48,058
and this is the third
8408
06:16:48,058 --> 06:16:50,878
Constructor so let's copy this
8409
06:16:50,878 --> 06:16:54,138
let's put here to Third
8410
06:16:55,500 --> 06:16:58,378
just for illustration
8411
06:16:58,378 --> 06:17:01,500
Also let's output in the console the
8412
06:17:01,500 --> 06:17:06,058
value of the the property values for
8413
06:17:06,058 --> 06:17:08,520
these objects so let's type here
8414
06:17:08,520 --> 06:17:10,860
printerland
8415
06:17:10,860 --> 06:17:14,280
and just type here name
8416
06:17:14,280 --> 06:17:19,160
equals let's put dollar sign
8417
06:17:19,860 --> 06:17:22,378
let's put curly braces
8418
06:17:22,378 --> 06:17:25,920
let's use our user
8419
06:17:25,920 --> 06:17:29,100
variable now let's call the properties
8420
06:17:29,100 --> 06:17:32,638
on this public so let's type name
8421
06:17:32,638 --> 06:17:34,920
now let's press Ctrl d
8422
06:17:34,920 --> 06:17:39,679
two times and let's put here now
8423
06:17:40,080 --> 06:17:42,540
last name
8424
06:17:42,540 --> 06:17:45,540
and the age
8425
06:17:45,540 --> 06:17:50,160
and let's also other space between
8426
06:17:50,160 --> 06:17:52,798
the information about the two objects to
8427
06:17:52,798 --> 06:17:54,540
make things more clear so let's put the
8428
06:17:54,540 --> 06:17:56,458
printer and heroes and put a backslash
8429
06:17:56,458 --> 06:17:58,877
in here
8430
06:17:59,940 --> 06:18:02,218
and let's copy the code that we have
8431
06:18:02,218 --> 06:18:05,400
here let's paste it below
8432
06:18:05,400 --> 06:18:08,218
let's change this
8433
06:18:08,218 --> 06:18:11,520
object to our friend object that we
8434
06:18:11,520 --> 06:18:14,280
created just like your friend
8435
06:18:14,280 --> 06:18:16,440
and I'm gonna speed this now a little
8436
06:18:16,440 --> 06:18:18,180
bit because
8437
06:18:18,180 --> 06:18:20,958
it will take some
8438
06:18:22,080 --> 06:18:24,718
now let's output this information in the
8439
06:18:24,718 --> 06:18:27,080
console
8440
06:18:29,340 --> 06:18:32,638
so now what we have is we have
8441
06:18:32,638 --> 06:18:34,080
we have
8442
06:18:34,080 --> 06:18:37,440
second and then third because this first
8443
06:18:37,440 --> 06:18:40,940
object is using the
8444
06:18:41,218 --> 06:18:43,320
the second Constructor
8445
06:18:43,320 --> 06:18:45,718
so this is why we have here a
8446
06:18:45,718 --> 06:18:47,520
second
8447
06:18:47,520 --> 06:18:49,798
and then the next line of code is called
8448
06:18:49,798 --> 06:18:52,740
and we have third because this one this
8449
06:18:52,740 --> 06:18:54,600
object that you have here the front
8450
06:18:54,600 --> 06:18:58,320
object it's using the third Constructor
8451
06:18:58,320 --> 06:19:00,360
this secondary Constructor so this I
8452
06:19:00,360 --> 06:19:02,280
have here third
8453
06:19:02,280 --> 06:19:04,440
then we have the values for the
8454
06:19:04,440 --> 06:19:06,120
properties for the I didn't have the
8455
06:19:06,120 --> 06:19:08,160
values for the properties of the user
8456
06:19:08,160 --> 06:19:10,020
object so you have
8457
06:19:10,020 --> 06:19:12,000
here the output
8458
06:19:12,000 --> 06:19:13,920
down here
8459
06:19:13,920 --> 06:19:15,840
and
8460
06:19:15,840 --> 06:19:18,660
then we scroll down and have name John
8461
06:19:18,660 --> 06:19:20,458
last name Smith age
8462
06:19:20,458 --> 06:19:23,520
zero because we have a
8463
06:19:23,520 --> 06:19:25,260
the property values for the second
8464
06:19:25,260 --> 06:19:27,660
object for this object
8465
06:19:27,660 --> 06:19:30,540
and this is okay this works completely
8466
06:19:30,540 --> 06:19:35,160
fine the thing to have in mind is that
8467
06:19:35,160 --> 06:19:36,480
now we're using the secondary
8468
06:19:36,480 --> 06:19:38,100
Constructors and the secondary
8469
06:19:38,100 --> 06:19:40,500
Constructor then calls the primary
8470
06:19:40,500 --> 06:19:42,120
Constructor and the primary Constructor
8471
06:19:42,120 --> 06:19:44,458
is the one which ultimately stores the
8472
06:19:44,458 --> 06:19:46,798
value source which ultimately declares
8473
06:19:46,798 --> 06:19:49,740
the properties and stored the values
8474
06:19:49,740 --> 06:19:53,100
but one thing to notice in the output is
8475
06:19:53,100 --> 06:19:56,760
that the first object has the name
8476
06:19:56,760 --> 06:20:00,000
Alex and the last name last name and age
8477
06:20:00,000 --> 06:20:03,360
zero that is because the first object is
8478
06:20:03,360 --> 06:20:04,558
only
8479
06:20:04,558 --> 06:20:07,378
taking a value for the first name
8480
06:20:07,378 --> 06:20:09,660
property or for the name property
8481
06:20:09,660 --> 06:20:14,540
and for the last name and the age
8482
06:20:14,580 --> 06:20:16,440
because it's using this secondary
8483
06:20:16,440 --> 06:20:19,378
Constructor is only passing in the first
8484
06:20:19,378 --> 06:20:21,840
name which is Alex that we have here
8485
06:20:21,840 --> 06:20:23,878
and then it's using the default values
8486
06:20:23,878 --> 06:20:26,580
last name and a zero it's passing those
8487
06:20:26,580 --> 06:20:28,440
default default values to the primary
8488
06:20:28,440 --> 06:20:30,120
Constructor so it's calling the primary
8489
06:20:30,120 --> 06:20:32,638
Constructor is passing the value Alex
8490
06:20:32,638 --> 06:20:35,160
and then the default value last name and
8491
06:20:35,160 --> 06:20:37,260
zero this is why I have last name and 0
8492
06:20:37,260 --> 06:20:40,680
here and for the next one we have
8493
06:20:40,680 --> 06:20:44,400
name John and last name Smith this is
8494
06:20:44,400 --> 06:20:47,580
because now this is using um the next
8495
06:20:47,580 --> 06:20:51,680
secondary Constructor and that takes as
8496
06:20:51,680 --> 06:20:54,120
arguments the name and the last name and
8497
06:20:54,120 --> 06:20:55,138
then it's calling the primary
8498
06:20:55,138 --> 06:20:57,240
Constructor it passes the
8499
06:20:57,240 --> 06:20:59,218
first name and the last name and the
8500
06:20:59,218 --> 06:21:01,260
aegi is defaulty so you have zero so
8501
06:21:01,260 --> 06:21:03,240
this way you have here John and Smith
8502
06:21:03,240 --> 06:21:06,058
and then zero so it's important to
8503
06:21:06,058 --> 06:21:09,680
to notice this and to think about how
8504
06:21:09,680 --> 06:21:13,080
this uh to think about this thing
8505
06:21:13,080 --> 06:21:15,600
so this is our discussion about multiple
8506
06:21:15,600 --> 06:21:17,218
Constructors respectively about
8507
06:21:17,218 --> 06:21:20,040
secondary Constructors in this video you
8508
06:21:20,040 --> 06:21:21,480
saw how you can
8509
06:21:21,480 --> 06:21:27,420
pass a different number of values to our
8510
06:21:27,420 --> 06:21:30,420
properties ultimately you pass all the
8511
06:21:30,420 --> 06:21:32,878
values to the properties by calling the
8512
06:21:32,878 --> 06:21:34,320
primary Constructor and providing
8513
06:21:34,320 --> 06:21:37,320
default values because in this way you
8514
06:21:37,320 --> 06:21:42,180
can satisfy all the possible cases so if
8515
06:21:42,180 --> 06:21:45,180
in our case one user only provides the
8516
06:21:45,180 --> 06:21:46,740
name and the last name is not provided
8517
06:21:46,740 --> 06:21:49,260
and the age is going to use the this
8518
06:21:49,260 --> 06:21:50,878
secondary Constructor and it's going to
8519
06:21:50,878 --> 06:21:52,620
pass the last name as default and the
8520
06:21:52,620 --> 06:21:57,000
zero and this is uh in a way
8521
06:21:57,000 --> 06:22:00,320
similar to overloading functions but is
8522
06:22:00,320 --> 06:22:03,480
not the same because what you're doing
8523
06:22:03,480 --> 06:22:07,558
here is overloading the constructors and
8524
06:22:07,558 --> 06:22:10,320
this in this way you can you can think
8525
06:22:10,320 --> 06:22:11,580
about
8526
06:22:11,580 --> 06:22:14,580
why why we're overloading the
8527
06:22:14,580 --> 06:22:17,400
constructors and that is because
8528
06:22:17,400 --> 06:22:20,580
if we didn't have secondary Constructors
8529
06:22:20,580 --> 06:22:24,240
we would have to create another class
8530
06:22:24,240 --> 06:22:27,660
with a different number of parameters
8531
06:22:27,660 --> 06:22:30,138
or the different number of
8532
06:22:30,138 --> 06:22:33,958
properties and we will need to to create
8533
06:22:33,958 --> 06:22:37,860
each object with that specific class
8534
06:22:37,860 --> 06:22:41,280
let's say that we create this object
8535
06:22:41,280 --> 06:22:43,500
which only takes a value for its
8536
06:22:43,500 --> 06:22:45,660
parameter name then we have to create a
8537
06:22:45,660 --> 06:22:47,700
class which we're going to Define only
8538
06:22:47,700 --> 06:22:50,160
the name property and it's gonna then
8539
06:22:50,160 --> 06:22:52,020
we're gonna create that obviously with
8540
06:22:52,020 --> 06:22:53,218
that class
8541
06:22:53,218 --> 06:22:58,260
and if an object if an object
8542
06:22:58,260 --> 06:23:01,920
let's say if an user creates uh if your
8543
06:23:01,920 --> 06:23:03,020
user doesn't
8544
06:23:03,020 --> 06:23:06,540
providing the the name and the last name
8545
06:23:06,540 --> 06:23:08,160
but now the age you have to create
8546
06:23:08,160 --> 06:23:10,860
another class with only the name and the
8547
06:23:10,860 --> 06:23:14,280
last name defined as its properties and
8548
06:23:14,280 --> 06:23:17,280
you can see that this this uh is uh this
8549
06:23:17,280 --> 06:23:20,160
will generate uh duplicated code and so
8550
06:23:20,160 --> 06:23:22,860
on and we can solve all of this by using
8551
06:23:22,860 --> 06:23:25,440
secondary Constructors and
8552
06:23:25,440 --> 06:23:27,240
calling the
8553
06:23:27,240 --> 06:23:30,600
primary Constructor so see you on the
8554
06:23:30,600 --> 06:23:33,260
next video and because quoting is about
8555
06:23:33,260 --> 06:23:35,700
conciseness in the next video we're
8556
06:23:35,700 --> 06:23:37,200
going to look at how we can do the same
8557
06:23:37,200 --> 06:23:39,058
thing that we have here with default
8558
06:23:39,058 --> 06:23:41,280
parameters because
8559
06:23:41,280 --> 06:23:42,780
um
8560
06:23:42,780 --> 06:23:45,840
kotlin supports for its properties for
8561
06:23:45,840 --> 06:23:47,400
its parameters in the constructed
8562
06:23:47,400 --> 06:23:49,200
default values so the same thing that we
8563
06:23:49,200 --> 06:23:51,240
have here can be achieved by providing
8564
06:23:51,240 --> 06:23:54,240
default values to the properties so see
8565
06:23:54,240 --> 06:23:56,160
in the next video
8566
06:23:56,160 --> 06:23:57,600
so now it's time to look at default
8567
06:23:57,600 --> 06:24:00,540
values for our properties and first
8568
06:24:00,540 --> 06:24:04,638
let's delete the secondary Constructors
8569
06:24:07,138 --> 06:24:09,540
and now I have a
8570
06:24:09,540 --> 06:24:12,480
error series because it expects the
8571
06:24:12,480 --> 06:24:13,520
value
8572
06:24:13,520 --> 06:24:15,298
expecting the values for the next
8573
06:24:15,298 --> 06:24:16,620
properties here
8574
06:24:16,620 --> 06:24:18,360
and um
8575
06:24:18,360 --> 06:24:21,000
let's also change this to
8576
06:24:21,000 --> 06:24:23,878
to first name because I refer to it to
8577
06:24:23,878 --> 06:24:26,638
the first name but it was called This is
8578
06:24:26,638 --> 06:24:28,620
it is called name so let's call it first
8579
06:24:28,620 --> 06:24:31,260
name so go right click on it
8580
06:24:31,260 --> 06:24:34,378
go to rename and then to refactor
8581
06:24:34,378 --> 06:24:37,440
to change it in all the places and let's
8582
06:24:37,440 --> 06:24:39,058
call it
8583
06:24:39,058 --> 06:24:41,400
first name and as you can see now let's
8584
06:24:41,400 --> 06:24:42,840
change it in all the place then press
8585
06:24:42,840 --> 06:24:45,080
enter
8586
06:24:45,120 --> 06:24:47,580
so now we have the first name and the
8587
06:24:47,580 --> 06:24:51,420
default properties are default values to
8588
06:24:51,420 --> 06:24:54,180
our properties are basically they are
8589
06:24:54,180 --> 06:24:55,920
similar to default values that we
8590
06:24:55,920 --> 06:24:59,458
provide to our parameters when we talked
8591
06:24:59,458 --> 06:25:01,680
about functions
8592
06:25:01,680 --> 06:25:04,440
then what we do is uh
8593
06:25:04,440 --> 06:25:06,718
let's say that I wanted to provide the
8594
06:25:06,718 --> 06:25:08,400
default value for the last name we just
8595
06:25:08,400 --> 06:25:11,760
put equals so inside the primary
8596
06:25:11,760 --> 06:25:13,860
Constructor we put equals and here we
8597
06:25:13,860 --> 06:25:16,020
put let's say
8598
06:25:16,020 --> 06:25:20,580
last name so now this has the last name
8599
06:25:20,580 --> 06:25:22,980
as a default value if a value is not
8600
06:25:22,980 --> 06:25:25,320
provided for the last name and here you
8601
06:25:25,320 --> 06:25:28,458
can put also equals
8602
06:25:29,700 --> 06:25:32,760
n equals zero and now the error as you
8603
06:25:32,760 --> 06:25:35,160
can see disappeared from both of our
8604
06:25:35,160 --> 06:25:37,740
objects because now
8605
06:25:37,740 --> 06:25:39,900
what is doing is when we don't provide
8606
06:25:39,900 --> 06:25:41,340
the value for the
8607
06:25:41,340 --> 06:25:44,700
last name and for the age then the
8608
06:25:44,700 --> 06:25:46,558
default values that we provide here is
8609
06:25:46,558 --> 06:25:48,000
going to be are going to be used as the
8610
06:25:48,000 --> 06:25:51,180
values for the properties so this is a
8611
06:25:51,180 --> 06:25:54,480
way of achieving the same thing that we
8612
06:25:54,480 --> 06:25:56,340
achieved previously with the secondary
8613
06:25:56,340 --> 06:25:59,040
Constructor but this is more concise
8614
06:25:59,040 --> 06:26:01,740
and the question is when to use one on
8615
06:26:01,740 --> 06:26:04,020
one or the other is
8616
06:26:04,020 --> 06:26:06,420
the um the answer may be if you want to
8617
06:26:06,420 --> 06:26:09,718
execute some code when an object is
8618
06:26:09,718 --> 06:26:11,940
created let's say
8619
06:26:11,940 --> 06:26:14,458
with one of the
8620
06:26:14,458 --> 06:26:16,378
let's say if you wanted to if you want
8621
06:26:16,378 --> 06:26:18,900
to exit some code when you create an
8622
06:26:18,900 --> 06:26:20,280
object then you can use secondary
8623
06:26:20,280 --> 06:26:22,200
Constructor because as I said secondary
8624
06:26:22,200 --> 06:26:24,200
Constructors can have a block of code
8625
06:26:24,200 --> 06:26:28,320
when uh which can be executed but the
8626
06:26:28,320 --> 06:26:31,378
this uh the default because primary
8627
06:26:31,378 --> 06:26:33,718
Constructor does not don't don't have a
8628
06:26:33,718 --> 06:26:36,540
don't have a block of code they you
8629
06:26:36,540 --> 06:26:38,780
cannot do that maybe you could use a
8630
06:26:38,780 --> 06:26:40,798
initializer blocks
8631
06:26:40,798 --> 06:26:46,020
but that is uh open to discussion
8632
06:26:46,020 --> 06:26:48,298
so this is how you can provide the four
8633
06:26:48,298 --> 06:26:49,860
values to
8634
06:26:49,860 --> 06:26:52,138
to the primary Constructor
8635
06:26:52,138 --> 06:26:54,900
in our code was fine it's
8636
06:26:54,900 --> 06:26:58,798
In This Very concise very beautiful and
8637
06:26:58,798 --> 06:27:00,660
the next thing that you can do is that
8638
06:27:00,660 --> 06:27:04,320
you can also use named arguments so you
8639
06:27:04,320 --> 06:27:06,660
can use the because here you use the
8640
06:27:06,660 --> 06:27:09,480
first name and the last name and if I um
8641
06:27:09,480 --> 06:27:13,740
because those are those are both strings
8642
06:27:13,740 --> 06:27:16,260
here you cannot see the distinction but
8643
06:27:16,260 --> 06:27:20,638
if I put here uh now I can use the name
8644
06:27:20,638 --> 06:27:24,557
argument so I can put your first name
8645
06:27:27,540 --> 06:27:30,540
equals so it's similar to the way we
8646
06:27:30,540 --> 06:27:32,638
have name arguments in our function and
8647
06:27:32,638 --> 06:27:35,218
here you can put the text
8648
06:27:35,218 --> 06:27:36,900
John
8649
06:27:36,900 --> 06:27:40,020
also here and this uh
8650
06:27:40,020 --> 06:27:42,480
with this thing you can you can change
8651
06:27:42,480 --> 06:27:44,520
the order in which you need to define
8652
06:27:44,520 --> 06:27:46,558
the values for the parameter so let's
8653
06:27:46,558 --> 06:27:49,458
put your last name
8654
06:27:49,620 --> 06:27:51,240
but we'll say this better let's create
8655
06:27:51,240 --> 06:27:53,040
another object
8656
06:27:53,040 --> 06:27:54,540
is
8657
06:27:54,540 --> 06:27:56,878
user 2
8658
06:27:56,878 --> 06:28:00,378
let's type user
8659
06:28:00,420 --> 06:28:03,859
and let's put here you wanna
8660
06:28:05,520 --> 06:28:08,940
but let's use the name arrowman so let's
8661
06:28:08,940 --> 06:28:11,100
put first the age so we're going to
8662
06:28:11,100 --> 06:28:13,378
provide the value for the age let's say
8663
06:28:13,378 --> 06:28:14,280
um
8664
06:28:14,280 --> 06:28:16,260
19.
8665
06:28:16,260 --> 06:28:18,540
and um
8666
06:28:18,540 --> 06:28:21,958
Let's uh let's type first name so here
8667
06:28:21,958 --> 06:28:24,718
let's put um
8668
06:28:24,718 --> 06:28:28,200
IWANNA because it's a string
8669
06:28:28,200 --> 06:28:31,340
quotation marks
8670
06:28:32,820 --> 06:28:36,920
and for the last name let's say
8671
06:28:39,540 --> 06:28:41,638
let's type also the name argument last
8672
06:28:41,638 --> 06:28:43,500
name and for the last name let's put
8673
06:28:43,500 --> 06:28:45,840
here um
8674
06:28:45,840 --> 06:28:47,520
Dobby
8675
06:28:47,520 --> 06:28:50,180
let's say
8676
06:28:50,820 --> 06:28:54,620
right so what we did here is that
8677
06:28:54,620 --> 06:28:58,440
in the primary Constructor the order for
8678
06:28:58,440 --> 06:29:01,620
for the properties very specific we have
8679
06:29:01,620 --> 06:29:03,058
the first name and the last name and
8680
06:29:03,058 --> 06:29:04,980
then we have the age but with the name
8681
06:29:04,980 --> 06:29:07,680
arguments we first provided the age
8682
06:29:07,680 --> 06:29:11,040
so we provided a value for the age
8683
06:29:11,040 --> 06:29:13,558
product which is the last one so and
8684
06:29:13,558 --> 06:29:15,718
then you provide the value for the first
8685
06:29:15,718 --> 06:29:19,138
name and with the name arguments similar
8686
06:29:19,138 --> 06:29:23,340
to the way used in our functions you can
8687
06:29:23,340 --> 06:29:25,740
pass the values to the properties in
8688
06:29:25,740 --> 06:29:27,480
which order you want irrespective of the
8689
06:29:27,480 --> 06:29:30,180
word in which they defined here so this
8690
06:29:30,180 --> 06:29:33,540
is very powerful if you
8691
06:29:33,540 --> 06:29:36,058
if you ask me it's very interesting
8692
06:29:36,058 --> 06:29:39,360
because you can pass the values to the
8693
06:29:39,360 --> 06:29:41,218
properties irrespective of the world in
8694
06:29:41,218 --> 06:29:43,440
which they are defined
8695
06:29:43,440 --> 06:29:46,218
so this is our discussion about default
8696
06:29:46,218 --> 06:29:51,240
parameters and the name argument in
8697
06:29:51,240 --> 06:29:53,940
with the primary Constructor and you
8698
06:29:53,940 --> 06:29:55,740
cannot use default parameters name
8699
06:29:55,740 --> 06:29:57,058
arguments with the secondary
8700
06:29:57,058 --> 06:29:59,520
Constructors they can only be used the
8701
06:29:59,520 --> 06:30:00,958
primary Constructor
8702
06:30:00,958 --> 06:30:05,057
so see you in the next video
8703
06:30:05,638 --> 06:30:08,400
but uh before I end the video Let's
8704
06:30:08,400 --> 06:30:10,500
actually run this code to see that it
8705
06:30:10,500 --> 06:30:14,520
works in the same way it was working in
8706
06:30:14,520 --> 06:30:17,100
previously when we use the secondary
8707
06:30:17,100 --> 06:30:18,660
Constructors
8708
06:30:18,660 --> 06:30:21,798
so let's run this code
8709
06:30:23,940 --> 06:30:26,340
so you have name Alex last name last
8710
06:30:26,340 --> 06:30:28,740
name because it's using this default
8711
06:30:28,740 --> 06:30:31,138
value that defined here then we have h0
8712
06:30:31,138 --> 06:30:33,360
because it's using this value of defined
8713
06:30:33,360 --> 06:30:34,860
here so this is
8714
06:30:34,860 --> 06:30:36,718
this object
8715
06:30:36,718 --> 06:30:40,020
and then we have name John last name
8716
06:30:40,020 --> 06:30:42,660
Smith a0 because now it's using this
8717
06:30:42,660 --> 06:30:44,458
default value so the same thing can be
8718
06:30:44,458 --> 06:30:46,200
achieved using um
8719
06:30:46,200 --> 06:30:49,920
default parameters and we didn't
8720
06:30:49,920 --> 06:30:52,680
output the values for the
8721
06:30:52,680 --> 06:30:55,378
for the second for the third object that
8722
06:30:55,378 --> 06:30:58,020
you created here in user 2 but you can
8723
06:30:58,020 --> 06:30:59,940
do that as a
8724
06:30:59,940 --> 06:31:03,240
as a challenge for if you want you can
8725
06:31:03,240 --> 06:31:07,500
output the values for this object so see
8726
06:31:07,500 --> 06:31:08,878
you in the next video
8727
06:31:08,878 --> 06:31:10,440
so now it's time to start a discussion
8728
06:31:10,440 --> 06:31:14,160
about Getters and Setters because so far
8729
06:31:14,160 --> 06:31:16,920
enough videos even though we didn't see
8730
06:31:16,920 --> 06:31:20,218
explicitly the Getters and Setters here
8731
06:31:20,218 --> 06:31:23,160
we've used the Getters and Setters every
8732
06:31:23,160 --> 06:31:25,500
time we got the value of a property and
8733
06:31:25,500 --> 06:31:28,138
every time we assign a new value to a
8734
06:31:28,138 --> 06:31:29,458
property
8735
06:31:29,458 --> 06:31:31,020
and um
8736
06:31:31,020 --> 06:31:33,360
the getter and Setter in kotlin are
8737
06:31:33,360 --> 06:31:35,638
implicit so they they are declared they
8738
06:31:35,638 --> 06:31:38,580
are Auto auto generated by default by
8739
06:31:38,580 --> 06:31:41,878
color by cutling for you so every time
8740
06:31:41,878 --> 06:31:44,638
you and let's say you get the value of
8741
06:31:44,638 --> 06:31:46,500
an object let's say let's get the value
8742
06:31:46,500 --> 06:31:48,180
of the user that's first name what
8743
06:31:48,180 --> 06:31:51,718
actually happens here is not that we're
8744
06:31:51,718 --> 06:31:53,700
getting the value of the first name
8745
06:31:53,700 --> 06:31:55,860
directly even though if you look at the
8746
06:31:55,860 --> 06:31:58,440
code this what it seems to be what has
8747
06:31:58,440 --> 06:32:00,600
happened is that every time we get the
8748
06:32:00,600 --> 06:32:04,440
value of of a property that value is
8749
06:32:04,440 --> 06:32:06,480
returned by the getter so you never
8750
06:32:06,480 --> 06:32:09,058
access the property directly and you
8751
06:32:09,058 --> 06:32:13,860
never never does change the the property
8752
06:32:13,860 --> 06:32:16,200
of uh the value of a property directly
8753
06:32:16,200 --> 06:32:18,480
you every you every time you you are
8754
06:32:18,480 --> 06:32:20,520
using the Getters and setters
8755
06:32:20,520 --> 06:32:22,620
and now this discussion is touching on
8756
06:32:22,620 --> 06:32:25,680
on a very important Concept in quadrant
8757
06:32:25,680 --> 06:32:27,900
programming and that is encapsulation so
8758
06:32:27,900 --> 06:32:31,558
encapsulation basically means to
8759
06:32:31,558 --> 06:32:35,820
to protect to hide the inner workings of
8760
06:32:35,820 --> 06:32:39,058
the class from being accessed outside
8761
06:32:39,058 --> 06:32:41,520
and if you look at this syntax that we
8762
06:32:41,520 --> 06:32:43,798
have here it seems that we actually
8763
06:32:43,798 --> 06:32:47,760
accessing the values the properties
8764
06:32:47,760 --> 06:32:49,920
directly but as I said that is not the
8765
06:32:49,920 --> 06:32:51,718
case because we're always using the
8766
06:32:51,718 --> 06:32:55,620
getter and the setter and because we
8767
06:32:55,620 --> 06:32:58,500
always use use the Getters and the
8768
06:32:58,500 --> 06:33:00,420
Setters the getter to get the value and
8769
06:33:00,420 --> 06:33:02,520
the set to to change the value of
8770
06:33:02,520 --> 06:33:03,600
property
8771
06:33:03,600 --> 06:33:07,740
that means that your data your
8772
06:33:07,740 --> 06:33:10,280
properties are never accessed directly
8773
06:33:10,280 --> 06:33:15,900
thus the laws of encapsulation are not
8774
06:33:15,900 --> 06:33:19,260
broken the the in other words you you
8775
06:33:19,260 --> 06:33:21,958
never allow somebody outside of the
8776
06:33:21,958 --> 06:33:24,000
class to access directly that property
8777
06:33:24,000 --> 06:33:26,520
so this is why uh
8778
06:33:26,520 --> 06:33:29,520
this is why we have Getters and Setters
8779
06:33:29,520 --> 06:33:32,040
and this is why the Getters and Setters
8780
06:33:32,040 --> 06:33:35,638
in Kathleen are implicit so they are not
8781
06:33:35,638 --> 06:33:37,680
even declared explicitly here we don't
8782
06:33:37,680 --> 06:33:38,700
see any
8783
06:33:38,700 --> 06:33:41,458
code which it will imply that we have a
8784
06:33:41,458 --> 06:33:44,458
get a Setter here because they are
8785
06:33:44,458 --> 06:33:46,980
automatically and by default generated
8786
06:33:46,980 --> 06:33:48,900
every time you define a property so
8787
06:33:48,900 --> 06:33:51,120
every time you define a property either
8788
06:33:51,120 --> 06:33:53,400
in the primary Constructor or
8789
06:33:53,400 --> 06:33:55,860
inside the class the
8790
06:33:55,860 --> 06:33:58,378
Getters and the get the getter and
8791
06:33:58,378 --> 06:34:01,980
Setters automatically generated for you
8792
06:34:01,980 --> 06:34:04,798
but uh let's say that you want to
8793
06:34:04,798 --> 06:34:07,260
override the getter and Setter to
8794
06:34:07,260 --> 06:34:10,080
provide some additional
8795
06:34:10,080 --> 06:34:12,660
additional code to the code that is by
8796
06:34:12,660 --> 06:34:14,638
the by default provided you can do that
8797
06:34:14,638 --> 06:34:16,860
by changing let's say
8798
06:34:16,860 --> 06:34:20,100
this property to a parameter
8799
06:34:20,100 --> 06:34:22,860
and to override the getter in Setter and
8800
06:34:22,860 --> 06:34:25,200
to see how the Getters and Setters
8801
06:34:25,200 --> 06:34:27,900
actually look under the covers
8802
06:34:27,900 --> 06:34:30,000
just type here VAR
8803
06:34:30,000 --> 06:34:33,480
we Define the property first name
8804
06:34:33,480 --> 06:34:35,280
we assign the first name parameter
8805
06:34:35,280 --> 06:34:38,520
property now the Getters and Setter need
8806
06:34:38,520 --> 06:34:41,120
to need to be overrated
8807
06:34:41,120 --> 06:34:44,280
immediately after the with after we
8808
06:34:44,280 --> 06:34:46,320
Define the property so we cannot have
8809
06:34:46,320 --> 06:34:48,718
some code here and then to override you
8810
06:34:48,718 --> 06:34:51,240
can to override the getter and Setter so
8811
06:34:51,240 --> 06:34:53,160
we need to
8812
06:34:53,160 --> 06:34:54,718
we need we need to Define them
8813
06:34:54,718 --> 06:34:57,000
immediately after we we Define the
8814
06:34:57,000 --> 06:35:00,360
property so we type here get
8815
06:35:00,360 --> 06:35:03,000
and as an attempting you see that I have
8816
06:35:03,000 --> 06:35:04,620
some suggestions then you go down here
8817
06:35:04,620 --> 06:35:06,540
you and you can choose you choose this
8818
06:35:06,540 --> 06:35:07,920
one
8819
06:35:07,920 --> 06:35:10,138
so you have parentheses curl braces and
8820
06:35:10,138 --> 06:35:14,600
inside the curly braces you press enter
8821
06:35:14,700 --> 06:35:17,580
here you type return
8822
06:35:17,580 --> 06:35:19,080
field I'm going to explain immediately
8823
06:35:19,080 --> 06:35:20,638
what is this
8824
06:35:20,638 --> 06:35:23,340
and now we need to override the setter
8825
06:35:23,340 --> 06:35:25,620
and to do that to just type set and I
8826
06:35:25,620 --> 06:35:27,120
have some suggestions
8827
06:35:27,120 --> 06:35:30,900
we also go down here we choose this one
8828
06:35:30,900 --> 06:35:33,540
press enter and hero type
8829
06:35:33,540 --> 06:35:36,420
field again this
8830
06:35:36,420 --> 06:35:39,298
under this identifier and input equals
8831
06:35:39,298 --> 06:35:40,378
to value
8832
06:35:40,378 --> 06:35:44,340
so this is the implicit code that is
8833
06:35:44,340 --> 06:35:46,740
generated that is auto-generated by
8834
06:35:46,740 --> 06:35:49,558
kotlin for you every time you you you
8835
06:35:49,558 --> 06:35:51,718
define a property in your class either
8836
06:35:51,718 --> 06:35:53,520
in the as I said in the primary sector
8837
06:35:53,520 --> 06:35:55,500
or inside the class
8838
06:35:55,500 --> 06:35:59,040
so because in fact if you look here we
8839
06:35:59,040 --> 06:36:00,660
have some underlines and if you hover
8840
06:36:00,660 --> 06:36:02,458
over here it says redundant getter
8841
06:36:02,458 --> 06:36:06,058
remove redundant getter because uh and
8842
06:36:06,058 --> 06:36:08,058
also for the set it under setup because
8843
06:36:08,058 --> 06:36:10,458
they are redundant because they are
8844
06:36:10,458 --> 06:36:13,440
implicitly again Auto generated by
8845
06:36:13,440 --> 06:36:16,500
cotton for you so this quad is redundant
8846
06:36:16,500 --> 06:36:17,700
here you don't need this code because
8847
06:36:17,700 --> 06:36:19,378
Scotland already provides this code
8848
06:36:19,378 --> 06:36:21,660
under the covers for you but if you're
8849
06:36:21,660 --> 06:36:24,540
not such satisfied with this quad with
8850
06:36:24,540 --> 06:36:26,700
this implicit code that is uh Auto
8851
06:36:26,700 --> 06:36:29,638
generated by cotton can you can um you
8852
06:36:29,638 --> 06:36:31,680
can override them like we did here and
8853
06:36:31,680 --> 06:36:33,660
provide some additional code so we can
8854
06:36:33,660 --> 06:36:36,180
put here let's say some text
8855
06:36:36,180 --> 06:36:38,580
so so that every time we get the value
8856
06:36:38,580 --> 06:36:40,980
of our
8857
06:36:40,980 --> 06:36:43,740
we get the value of our first name
8858
06:36:43,740 --> 06:36:45,480
property you're gonna have a pretty
8859
06:36:45,480 --> 06:36:47,340
pretty prefix and let's put here a first
8860
06:36:47,340 --> 06:36:48,958
name
8861
06:36:48,958 --> 06:36:51,958
let's put the colon dollar sign
8862
06:36:51,958 --> 06:36:53,760
and field we're gonna play I'm gonna
8863
06:36:53,760 --> 06:36:55,378
explain immediately what this field is
8864
06:36:55,378 --> 06:36:57,958
but let's say first however
8865
06:36:57,958 --> 06:37:00,780
how this works and here let's put a
8866
06:37:00,780 --> 06:37:02,760
printerland to print a value that is
8867
06:37:02,760 --> 06:37:04,620
passed to the setter and then assigned
8868
06:37:04,620 --> 06:37:06,240
to the first name property so let's put
8869
06:37:06,240 --> 06:37:09,798
here dollar sign
8870
06:37:11,458 --> 06:37:13,860
value was
8871
06:37:13,860 --> 06:37:15,900
assigned
8872
06:37:15,900 --> 06:37:19,260
to First
8873
06:37:21,120 --> 06:37:23,298
foreign
8874
06:37:30,480 --> 06:37:33,900
now I have name so we have our text that
8875
06:37:33,900 --> 06:37:36,480
we have here name and then have first
8876
06:37:36,480 --> 06:37:38,340
name Alex let's actually delete this
8877
06:37:38,340 --> 06:37:40,680
text to make things more clear
8878
06:37:40,680 --> 06:37:43,700
let's delete this text
8879
06:37:43,740 --> 06:37:46,700
let's delete this
8880
06:37:50,340 --> 06:37:53,600
now let's run this code again
8881
06:37:56,280 --> 06:37:59,218
so what we have now is that every time
8882
06:37:59,218 --> 06:38:01,680
we get the value of our property name we
8883
06:38:01,680 --> 06:38:03,958
have we have that prefix first name that
8884
06:38:03,958 --> 06:38:07,020
we have uh overwritten here in the in
8885
06:38:07,020 --> 06:38:09,840
the getter so the getter is called as I
8886
06:38:09,840 --> 06:38:12,360
said the value is returned by the getter
8887
06:38:12,360 --> 06:38:14,700
so together is called it Returns the
8888
06:38:14,700 --> 06:38:16,558
value of the first name property so this
8889
06:38:16,558 --> 06:38:19,500
field is is the first name property but
8890
06:38:19,500 --> 06:38:22,080
is uh it has this identifier field
8891
06:38:22,080 --> 06:38:24,840
because of uh of an important reason
8892
06:38:24,840 --> 06:38:28,020
which I'm gonna explain immediately so
8893
06:38:28,020 --> 06:38:30,660
it Returns the value of the first name
8894
06:38:30,660 --> 06:38:31,740
property
8895
06:38:31,740 --> 06:38:34,558
with the prefix first name and we have
8896
06:38:34,558 --> 06:38:37,320
first name the text that we have here
8897
06:38:37,320 --> 06:38:39,958
then I have the value of the first name
8898
06:38:39,958 --> 06:38:42,660
property we have Alex in our case
8899
06:38:42,660 --> 06:38:45,840
because our object has the name Alex as
8900
06:38:45,840 --> 06:38:48,240
the property name Alex
8901
06:38:48,240 --> 06:38:50,580
then for the
8902
06:38:50,580 --> 06:38:52,620
for the setter we don't have any code
8903
06:38:52,620 --> 06:38:55,500
here also we have the first name prefix
8904
06:38:55,500 --> 06:38:58,138
for John so our our every time we get uh
8905
06:38:58,138 --> 06:39:02,900
we get the first name property by
8906
06:39:02,900 --> 06:39:06,420
of our objects created by by using the
8907
06:39:06,420 --> 06:39:09,540
user class it gets that value it returns
8908
06:39:09,540 --> 06:39:11,700
that value through the getter and the
8909
06:39:11,700 --> 06:39:14,400
setter we don't have value was assigned
8910
06:39:14,400 --> 06:39:15,420
to first name property because the
8911
06:39:15,420 --> 06:39:19,080
setter is called only one we change the
8912
06:39:19,080 --> 06:39:22,798
value of uh of uh of our properties and
8913
06:39:22,798 --> 06:39:25,378
to do that we type user like we did in
8914
06:39:25,378 --> 06:39:27,180
our first video about objects or another
8915
06:39:27,180 --> 06:39:28,700
programming
8916
06:39:28,700 --> 06:39:31,138
DOT first name
8917
06:39:31,138 --> 06:39:34,020
equals let's say
8918
06:39:34,020 --> 06:39:36,558
oh
8919
06:39:36,780 --> 06:39:39,540
let's put here
8920
06:39:39,540 --> 06:39:42,138
Vlad
8921
06:39:42,420 --> 06:39:44,638
so now if I run this code
8922
06:39:44,638 --> 06:39:46,200
now the setter is going to be called
8923
06:39:46,200 --> 06:39:47,760
because the setter is called as I said
8924
06:39:47,760 --> 06:39:50,400
only when it we change the value of our
8925
06:39:50,400 --> 06:39:54,000
of our of of our property
8926
06:39:54,000 --> 06:39:56,878
so we get uh
8927
06:39:56,878 --> 06:39:59,160
get Vlad was assigned to first name
8928
06:39:59,160 --> 06:40:01,440
property so the code inside the setter
8929
06:40:01,440 --> 06:40:02,940
was called
8930
06:40:02,940 --> 06:40:07,378
because this code that we type here is
8931
06:40:07,378 --> 06:40:10,200
calling the setter so our code only only
8932
06:40:10,200 --> 06:40:12,900
always goes to the setter and the code
8933
06:40:12,900 --> 06:40:15,240
that we have here user that first name
8934
06:40:15,240 --> 06:40:18,058
Vlad what it's actually doing is
8935
06:40:18,058 --> 06:40:21,298
passing that value Vlad to
8936
06:40:21,298 --> 06:40:25,500
here so this for this uh this parameter
8937
06:40:25,500 --> 06:40:28,320
it that we have here is receiving the
8938
06:40:28,320 --> 06:40:31,558
value of Vlad and then that value is
8939
06:40:31,558 --> 06:40:34,320
assigned to first name property again we
8940
06:40:34,320 --> 06:40:36,360
have this field the identifier for a
8941
06:40:36,360 --> 06:40:37,920
reason and the reason is going to be
8942
06:40:37,920 --> 06:40:39,718
explained immediately
8943
06:40:39,718 --> 06:40:42,180
so uh
8944
06:40:42,180 --> 06:40:46,080
the Getters and Setters are working well
8945
06:40:46,080 --> 06:40:49,440
and they are as they said again the use
8946
06:40:49,440 --> 06:40:52,260
always when we get or
8947
06:40:52,260 --> 06:40:55,860
assign a new value to our properties
8948
06:40:55,860 --> 06:40:58,920
now what is this field identifier why I
8949
06:40:58,920 --> 06:41:00,298
didn't
8950
06:41:00,298 --> 06:41:02,218
type here uh
8951
06:41:02,218 --> 06:41:07,200
first name and here I didn't put uh
8952
06:41:07,200 --> 06:41:09,718
first name equals to Value so whatever
8953
06:41:09,718 --> 06:41:11,820
value is passed to the setter then that
8954
06:41:11,820 --> 06:41:13,740
value is assigned to the first name
8955
06:41:13,740 --> 06:41:15,900
property so why
8956
06:41:15,900 --> 06:41:18,298
why don't we don't do that
8957
06:41:18,298 --> 06:41:22,320
and um the reason uh for that is that
8958
06:41:22,320 --> 06:41:25,280
because
8959
06:41:26,580 --> 06:41:30,360
this syntax user.first name user first
8960
06:41:30,360 --> 06:41:31,500
name
8961
06:41:31,500 --> 06:41:33,900
user.first name equals Vlad is calling
8962
06:41:33,900 --> 06:41:35,280
the setter
8963
06:41:35,280 --> 06:41:38,820
if we put here first name equals value
8964
06:41:38,820 --> 06:41:41,520
so first name equals Alex it will be
8965
06:41:41,520 --> 06:41:45,240
equivalent to to call the setter again
8966
06:41:45,240 --> 06:41:47,700
so this line of code that we have here
8967
06:41:47,700 --> 06:41:49,680
it will call the setter again it will
8968
06:41:49,680 --> 06:41:51,718
pass the value to the center and then it
8969
06:41:51,718 --> 06:41:53,940
will assign the value again to the first
8970
06:41:53,940 --> 06:41:56,760
name so it will call the Center inside
8971
06:41:56,760 --> 06:41:59,160
the setter forever it will be it will
8972
06:41:59,160 --> 06:42:01,920
generate an error and to avoid the that
8973
06:42:01,920 --> 06:42:04,500
recursive call that infinite call we
8974
06:42:04,500 --> 06:42:07,500
need to use this field identifier so the
8975
06:42:07,500 --> 06:42:09,420
field in an identifier is the first name
8976
06:42:09,420 --> 06:42:12,440
property but it has this
8977
06:42:12,440 --> 06:42:15,378
special
8978
06:42:15,378 --> 06:42:19,680
use that it bypasses that error that it
8979
06:42:19,680 --> 06:42:21,540
will be generated if you use the first
8980
06:42:21,540 --> 06:42:22,860
name here
8981
06:42:22,860 --> 06:42:27,360
so in fact let's um
8982
06:42:27,360 --> 06:42:31,020
let's put here a first name to see
8983
06:42:31,020 --> 06:42:34,020
so if we put here first name
8984
06:42:34,020 --> 06:42:36,298
instead of field
8985
06:42:36,298 --> 06:42:38,878
and we have an error here
8986
06:42:38,878 --> 06:42:41,040
let's put here a
8987
06:42:41,040 --> 06:42:42,180
field
8988
06:42:42,180 --> 06:42:44,280
because
8989
06:42:44,280 --> 06:42:47,280
I want uh I want the error from the
8990
06:42:47,280 --> 06:42:49,138
setter to to get in the console not
8991
06:42:49,138 --> 06:42:51,540
there or from this thing here so now if
8992
06:42:51,540 --> 06:42:52,860
you run the squad
8993
06:42:52,860 --> 06:42:55,080
as I said that will generate a recursive
8994
06:42:55,080 --> 06:42:57,480
call it will call the getter over and
8995
06:42:57,480 --> 06:42:59,660
over
8996
06:43:00,540 --> 06:43:03,980
so get the error
8997
06:43:04,320 --> 06:43:07,320
so you see we have Vlad was assigned to
8998
06:43:07,320 --> 06:43:08,820
first name property Vlad was assigned to
8999
06:43:08,820 --> 06:43:11,160
first name property vad and so on that
9000
06:43:11,160 --> 06:43:12,080
is because
9001
06:43:12,080 --> 06:43:14,280
the setter
9002
06:43:14,280 --> 06:43:18,058
it's calling the setter forever
9003
06:43:18,058 --> 06:43:20,878
it's a recourse of call because this
9004
06:43:20,878 --> 06:43:22,740
line of code here it's calling the
9005
06:43:22,740 --> 06:43:25,860
setter it's it's a assigning the value
9006
06:43:25,860 --> 06:43:28,740
to the sending the value to the seta
9007
06:43:28,740 --> 06:43:30,958
then it's that values assigned again to
9008
06:43:30,958 --> 06:43:33,058
the first name property which in turn
9009
06:43:33,058 --> 06:43:35,638
close again the setter and so on so this
9010
06:43:35,638 --> 06:43:38,400
field the identifier is used because it
9011
06:43:38,400 --> 06:43:40,700
has this characteristic of
9012
06:43:40,700 --> 06:43:43,138
bypassing that error this is why we're
9013
06:43:43,138 --> 06:43:45,240
using the field instead of the first
9014
06:43:45,240 --> 06:43:46,500
name
9015
06:43:46,500 --> 06:43:50,280
property here and it's basically the
9016
06:43:50,280 --> 06:43:52,680
first name property but it has this
9017
06:43:52,680 --> 06:43:54,798
specific characteristics which
9018
06:43:54,798 --> 06:43:57,718
characteristics which bypasses the
9019
06:43:57,718 --> 06:44:01,558
recursive call which we got when we've
9020
06:44:01,558 --> 06:44:03,780
used the first name property
9021
06:44:03,780 --> 06:44:07,100
now if you run the code
9022
06:44:10,200 --> 06:44:12,780
now I have no error because the recourse
9023
06:44:12,780 --> 06:44:14,040
of call is not happening because you're
9024
06:44:14,040 --> 06:44:15,660
using the field which bypasses that
9025
06:44:15,660 --> 06:44:16,500
error
9026
06:44:16,500 --> 06:44:18,660
so this is why we're using uh and we're
9027
06:44:18,660 --> 06:44:21,180
having an encodone has this thing filled
9028
06:44:21,180 --> 06:44:23,040
and this the field can only be used
9029
06:44:23,040 --> 06:44:26,520
inside the getter or the setter not uh
9030
06:44:26,520 --> 06:44:29,400
not anywhere else also either getter or
9031
06:44:29,400 --> 06:44:30,718
the setter
9032
06:44:30,718 --> 06:44:33,058
and the same is true about the getter
9033
06:44:33,058 --> 06:44:34,200
this is why you need to use the field
9034
06:44:34,200 --> 06:44:36,360
identifier inside the getter also
9035
06:44:36,360 --> 06:44:39,360
because it will generate the recursive
9036
06:44:39,360 --> 06:44:41,280
call again
9037
06:44:41,280 --> 06:44:45,840
now to to illustrate better because you
9038
06:44:45,840 --> 06:44:47,340
maybe find the
9039
06:44:47,340 --> 06:44:49,558
confusing this discussion about Getters
9040
06:44:49,558 --> 06:44:52,860
and Setters what they really
9041
06:44:52,860 --> 06:44:56,280
are they are equivalent to having two
9042
06:44:56,280 --> 06:44:58,260
functions which
9043
06:44:58,260 --> 06:45:01,138
update the first name property and
9044
06:45:01,138 --> 06:45:02,760
return the first name property so they
9045
06:45:02,760 --> 06:45:04,378
they are equivalent to having
9046
06:45:04,378 --> 06:45:07,200
a function called Set uh
9047
06:45:07,200 --> 06:45:09,298
first name
9048
06:45:09,298 --> 06:45:11,638
and this function defines a parameter
9049
06:45:11,638 --> 06:45:14,580
called let's call it new value
9050
06:45:14,580 --> 06:45:18,200
is going to be of type string
9051
06:45:18,718 --> 06:45:21,180
let's put curly braces and here we type
9052
06:45:21,180 --> 06:45:24,900
this DOT first name
9053
06:45:24,900 --> 06:45:26,638
equals
9054
06:45:26,638 --> 06:45:28,798
new value
9055
06:45:28,798 --> 06:45:32,218
and the geta equivalent to having a
9056
06:45:32,218 --> 06:45:33,718
function
9057
06:45:33,718 --> 06:45:34,860
called
9058
06:45:34,860 --> 06:45:36,120
get
9059
06:45:36,120 --> 06:45:38,520
first name
9060
06:45:38,520 --> 06:45:39,900
and this is not going to take any
9061
06:45:39,900 --> 06:45:42,600
parameter is going is we're going to say
9062
06:45:42,600 --> 06:45:44,458
say explicit here that it's going to
9063
06:45:44,458 --> 06:45:46,558
return a string because it's gonna only
9064
06:45:46,558 --> 06:45:48,840
return a value
9065
06:45:48,840 --> 06:45:51,240
and this is going to return the first
9066
06:45:51,240 --> 06:45:53,958
name property
9067
06:45:54,480 --> 06:45:57,480
so we put here this
9068
06:45:57,480 --> 06:46:02,100
that first name so the getter and Setter
9069
06:46:02,100 --> 06:46:05,660
that we have here are similar this is uh
9070
06:46:05,660 --> 06:46:09,000
illustrative for you to have in mind to
9071
06:46:09,000 --> 06:46:12,240
having two functions which are setting
9072
06:46:12,240 --> 06:46:14,040
the first name property or updating the
9073
06:46:14,040 --> 06:46:16,680
first name property and are returning
9074
06:46:16,680 --> 06:46:20,240
let's change this to First
9075
06:46:20,280 --> 06:46:23,280
and are returning the first uh the value
9076
06:46:23,280 --> 06:46:25,740
of the first name property but because
9077
06:46:25,740 --> 06:46:28,920
constantly and it's about conciseness it
9078
06:46:28,920 --> 06:46:32,520
has this special syntax and they are
9079
06:46:32,520 --> 06:46:35,340
automatically generated for you because
9080
06:46:35,340 --> 06:46:37,700
uh as I said accordingly it's about
9081
06:46:37,700 --> 06:46:40,620
conciseness so you don't need to declare
9082
06:46:40,620 --> 06:46:42,900
those things every time you create a
9083
06:46:42,900 --> 06:46:46,138
class so let's delete this so have in
9084
06:46:46,138 --> 06:46:49,620
mind that under the covers is we have
9085
06:46:49,620 --> 06:46:52,200
something like this so let's delete this
9086
06:46:52,200 --> 06:46:54,660
now the arrow disappears
9087
06:46:54,660 --> 06:46:57,240
also you can have a shorter way of
9088
06:46:57,240 --> 06:46:58,620
writing the centering letter if you
9089
06:46:58,620 --> 06:47:01,200
don't provide some additional code so if
9090
06:47:01,200 --> 06:47:03,540
I delete this
9091
06:47:03,540 --> 06:47:06,200
and this
9092
06:47:06,298 --> 06:47:11,600
I can put here get equals field
9093
06:47:11,638 --> 06:47:14,660
and um
9094
06:47:16,580 --> 06:47:19,978
yes this is this okay because this is
9095
06:47:19,978 --> 06:47:22,920
like having a single line a single
9096
06:47:22,920 --> 06:47:24,600
function a single line expression
9097
06:47:24,600 --> 06:47:26,700
function because we're returning the
9098
06:47:26,700 --> 06:47:30,600
value to the function get so you can
9099
06:47:30,600 --> 06:47:33,478
write it like this because or I I show
9100
06:47:33,478 --> 06:47:35,340
you this photo because maybe you'll see
9101
06:47:35,340 --> 06:47:38,160
a written in this way and you may be
9102
06:47:38,160 --> 06:47:40,920
wondering why that's because this is
9103
06:47:40,920 --> 06:47:44,760
like having a a single body expression
9104
06:47:44,760 --> 06:47:46,080
function
9105
06:47:46,080 --> 06:47:47,820
so you can write it like this if you
9106
06:47:47,820 --> 06:47:50,340
want but again if you hover over this is
9107
06:47:50,340 --> 06:47:51,540
still already done let's so let's press
9108
06:47:51,540 --> 06:47:54,660
Ctrl G to have the previous code
9109
06:47:54,660 --> 06:47:56,878
so this is our discussion about Getters
9110
06:47:56,878 --> 06:47:59,760
and Setters and see you on the next
9111
06:47:59,760 --> 06:48:00,840
video
9112
06:48:00,840 --> 06:48:02,400
so now it's time to start a discussion
9113
06:48:02,400 --> 06:48:04,920
about the late init keyword and how we
9114
06:48:04,920 --> 06:48:07,740
can use the latent keyword with the our
9115
06:48:07,740 --> 06:48:10,020
properties but first let's delete the
9116
06:48:10,020 --> 06:48:11,100
code that we have from the previous
9117
06:48:11,100 --> 06:48:13,260
video because don't need this code let's
9118
06:48:13,260 --> 06:48:15,180
also delete this code
9119
06:48:15,180 --> 06:48:18,420
let's delete those two objects
9120
06:48:18,420 --> 06:48:19,860
that's
9121
06:48:19,860 --> 06:48:22,798
and lazil is property let's declare it
9122
06:48:22,798 --> 06:48:25,620
here so let's put the VAR keyword
9123
06:48:25,620 --> 06:48:27,780
and let's also delete the default values
9124
06:48:27,780 --> 06:48:30,020
here
9125
06:48:34,200 --> 06:48:36,298
and let's press Ctrl alt form on the
9126
06:48:36,298 --> 06:48:37,620
code
9127
06:48:37,620 --> 06:48:39,240
now we need to provide the values here
9128
06:48:39,240 --> 06:48:41,160
because we don't have default values for
9129
06:48:41,160 --> 06:48:43,558
the last name property and the age so
9130
06:48:43,558 --> 06:48:45,958
I'm going to type here
9131
06:48:45,958 --> 06:48:48,420
dobbyn let's say
9132
06:48:48,420 --> 06:48:50,940
age 23.
9133
06:48:50,940 --> 06:48:52,440
now
9134
06:48:52,440 --> 06:48:54,600
as you already know if you declare
9135
06:48:54,600 --> 06:48:57,000
inside the class
9136
06:48:57,000 --> 06:48:59,400
a property let's say that that property
9137
06:48:59,400 --> 06:49:01,740
is called favorite movie and it's going
9138
06:49:01,740 --> 06:49:05,520
to store the favorite movie of the user
9139
06:49:05,520 --> 06:49:07,458
you cannot let this variable
9140
06:49:07,458 --> 06:49:09,958
uninitialize so let's say that is going
9141
06:49:09,958 --> 06:49:11,520
to be of type string
9142
06:49:11,520 --> 06:49:12,958
so you cannot let this variable
9143
06:49:12,958 --> 06:49:14,878
uninitialize you need to provide a value
9144
06:49:14,878 --> 06:49:17,940
either by assigning directly
9145
06:49:17,940 --> 06:49:21,240
a literal value here or by receiving the
9146
06:49:21,240 --> 06:49:25,020
value through through a parameter
9147
06:49:25,020 --> 06:49:26,700
defined in the Constructor
9148
06:49:26,700 --> 06:49:29,940
but um that is not completely true
9149
06:49:29,940 --> 06:49:31,920
because
9150
06:49:31,920 --> 06:49:35,160
sometimes you'll want to to have a
9151
06:49:35,160 --> 06:49:37,558
property declared inside the class but
9152
06:49:37,558 --> 06:49:40,020
you don't have to assign a Twitter
9153
06:49:40,020 --> 06:49:42,840
valued uh right away so you want to
9154
06:49:42,840 --> 06:49:45,200
assign the value to the property later
9155
06:49:45,200 --> 06:49:47,580
and to be able to do that you need to
9156
06:49:47,580 --> 06:49:50,580
use the late init keyword and to use the
9157
06:49:50,580 --> 06:49:52,620
late neat keyword you just type here
9158
06:49:52,620 --> 06:49:54,718
late it and you have a suggestion later
9159
06:49:54,718 --> 06:49:55,620
need
9160
06:49:55,620 --> 06:49:58,680
and later need search for initialize
9161
06:49:58,680 --> 06:50:01,500
letter later so what we're saying here
9162
06:50:01,500 --> 06:50:02,540
is
9163
06:50:02,540 --> 06:50:06,660
initialize let later this property
9164
06:50:06,660 --> 06:50:09,058
favorite movies so you say in the to the
9165
06:50:09,058 --> 06:50:11,400
compiler hey I'm gonna
9166
06:50:11,400 --> 06:50:13,798
initialize this uh property favorite
9167
06:50:13,798 --> 06:50:16,138
movie but I'm gonna initialize it later
9168
06:50:16,138 --> 06:50:19,740
so believe me and uh
9169
06:50:19,740 --> 06:50:21,718
to do that you just
9170
06:50:21,718 --> 06:50:23,940
type user
9171
06:50:23,940 --> 06:50:27,120
that so you whatever object you create
9172
06:50:27,120 --> 06:50:29,100
with this class and you have a defined
9173
06:50:29,100 --> 06:50:31,620
latent property
9174
06:50:31,620 --> 06:50:33,360
favorite movie
9175
06:50:33,360 --> 06:50:35,400
and you put equals and then you put
9176
06:50:35,400 --> 06:50:39,558
let's say Interstellar here
9177
06:50:42,120 --> 06:50:45,120
so now we've initialized our favorite
9178
06:50:45,120 --> 06:50:47,760
movie property but we initialize the its
9179
06:50:47,760 --> 06:50:49,558
value later so we didn't provide the
9180
06:50:49,558 --> 06:50:51,900
value here directly inside the class
9181
06:50:51,900 --> 06:50:54,840
either by as I said by providing a
9182
06:50:54,840 --> 06:50:56,340
literal value or by
9183
06:50:56,340 --> 06:50:59,280
providing a value through the parameter
9184
06:50:59,280 --> 06:51:02,218
defined in the primary Constructor so
9185
06:51:02,218 --> 06:51:04,320
this is how you can use the
9186
06:51:04,320 --> 06:51:08,478
latent keyword if you want to
9187
06:51:08,700 --> 06:51:11,040
assign a value to a property later so
9188
06:51:11,040 --> 06:51:12,180
you don't want to assign the value
9189
06:51:12,180 --> 06:51:14,638
directly or or you don't want to cite
9190
06:51:14,638 --> 06:51:17,420
the the value more correctly said
9191
06:51:17,420 --> 06:51:19,680
right away so you want to assign the
9192
06:51:19,680 --> 06:51:22,020
value later you just put the latest
9193
06:51:22,020 --> 06:51:23,760
keyword in front of the property and
9194
06:51:23,760 --> 06:51:26,218
then you declare the property as
9195
06:51:26,218 --> 06:51:28,680
you the way you declare a simple
9196
06:51:28,680 --> 06:51:29,760
variable
9197
06:51:29,760 --> 06:51:32,718
right and if you don't initialize this
9198
06:51:32,718 --> 06:51:36,660
this property and you want a new output
9199
06:51:36,660 --> 06:51:38,638
the value in the console so let's say
9200
06:51:38,638 --> 06:51:41,040
that I put here a printerland
9201
06:51:41,040 --> 06:51:43,558
and I type user that
9202
06:51:43,558 --> 06:51:45,000
favorite movie
9203
06:51:45,000 --> 06:51:48,680
and I don't initialize it
9204
06:51:52,020 --> 06:51:55,740
and if I run this code now this
9205
06:51:55,740 --> 06:51:58,138
favorite movie property is not
9206
06:51:58,138 --> 06:52:00,840
initialized doesn't have a value
9207
06:52:00,840 --> 06:52:04,080
we're gonna have an error
9208
06:52:04,080 --> 06:52:06,958
and the error is uh
9209
06:52:06,958 --> 06:52:10,100
it's very specific
9210
06:52:12,000 --> 06:52:14,180
so you have exception thread main
9211
06:52:14,180 --> 06:52:16,978
kotlin unofficialized projects access
9212
06:52:16,978 --> 06:52:19,680
exception we have and then it says later
9213
06:52:19,680 --> 06:52:21,540
need property favorite movie has not
9214
06:52:21,540 --> 06:52:23,218
been initialized so it's very specific
9215
06:52:23,218 --> 06:52:25,500
in saying that hey you didn't you said
9216
06:52:25,500 --> 06:52:27,478
that you're going to initialize that
9217
06:52:27,478 --> 06:52:30,420
property but you didn't and uh down here
9218
06:52:30,420 --> 06:52:33,600
you try to to Output in the console the
9219
06:52:33,600 --> 06:52:35,760
value of the favorite movie but we don't
9220
06:52:35,760 --> 06:52:38,458
have provided a value and it's better to
9221
06:52:38,458 --> 06:52:41,160
have this uh this is called an exception
9222
06:52:41,160 --> 06:52:43,378
so it's better to have this energy Pro
9223
06:52:43,378 --> 06:52:46,500
and initialize property access exception
9224
06:52:46,500 --> 06:52:49,680
then having a null pointer pointer
9225
06:52:49,680 --> 06:52:52,080
exception because
9226
06:52:52,080 --> 06:52:54,840
this is why kotlin actually enforces
9227
06:52:54,840 --> 06:52:58,218
this behavior of uh
9228
06:52:58,218 --> 06:53:02,040
assigning values to the variable to the
9229
06:53:02,040 --> 06:53:04,558
variables so it Cotillion forces to
9230
06:53:04,558 --> 06:53:07,680
assign values to the variables and not
9231
06:53:07,680 --> 06:53:10,558
let them uninitialize because if you
9232
06:53:10,558 --> 06:53:12,600
don't initialize
9233
06:53:12,600 --> 06:53:16,080
uh you don't provide the value to a bear
9234
06:53:16,080 --> 06:53:18,958
to a variable or two property then when
9235
06:53:18,958 --> 06:53:21,840
you try to access that variable or that
9236
06:53:21,840 --> 06:53:23,878
property and let's say that you have an
9237
06:53:23,878 --> 06:53:24,798
app
9238
06:53:24,798 --> 06:53:28,740
that is gonna throw which is going to
9239
06:53:28,740 --> 06:53:30,120
throw what is called the null pointer
9240
06:53:30,120 --> 06:53:32,940
exception and that is very bad because
9241
06:53:32,940 --> 06:53:35,240
those kind of excursions are called only
9242
06:53:35,240 --> 06:53:38,400
at runtime and not at compile time so
9243
06:53:38,400 --> 06:53:42,718
your app can work let's say completely
9244
06:53:42,718 --> 06:53:46,620
well and when you press a button I don't
9245
06:53:46,620 --> 06:53:52,440
know to to see the the favorite movie of
9246
06:53:52,440 --> 06:53:55,860
a user then you get an error and adopt
9247
06:53:55,860 --> 06:53:58,860
crashes so this is why you need to avoid
9248
06:53:58,860 --> 06:54:02,400
those null pointer exceptions and kotlin
9249
06:54:02,400 --> 06:54:04,200
as I said enforces this behavior of
9250
06:54:04,200 --> 06:54:05,820
providing um
9251
06:54:05,820 --> 06:54:08,280
values always to the variables to the
9252
06:54:08,280 --> 06:54:09,718
end to the properties
9253
06:54:09,718 --> 06:54:12,420
and if you don't want to provide the
9254
06:54:12,420 --> 06:54:14,580
value to the
9255
06:54:14,580 --> 06:54:16,920
property then you can use this late on
9256
06:54:16,920 --> 06:54:20,280
it and it will show this uh different uh
9257
06:54:20,280 --> 06:54:22,320
exception called latent property
9258
06:54:22,320 --> 06:54:24,120
favorite movie has not finished so it's
9259
06:54:24,120 --> 06:54:28,020
it's a different exception than the null
9260
06:54:28,020 --> 06:54:30,540
pointer exception but with the latent
9261
06:54:30,540 --> 06:54:33,120
keyword we can still have as I said that
9262
06:54:33,120 --> 06:54:34,740
behavior of not initializing the
9263
06:54:34,740 --> 06:54:37,080
variable but you still need to
9264
06:54:37,080 --> 06:54:39,600
initialize it later because this one is
9265
06:54:39,600 --> 06:54:41,580
called late init
9266
06:54:41,580 --> 06:54:45,120
but this is not uh saying that the
9267
06:54:45,120 --> 06:54:46,920
latent
9268
06:54:46,920 --> 06:54:51,000
error that is shown here is not also uh
9269
06:54:51,000 --> 06:54:53,458
having the same behavior like the null
9270
06:54:53,458 --> 06:54:54,718
pointer exception that I talked about
9271
06:54:54,718 --> 06:54:58,620
because the late init error that is that
9272
06:54:58,620 --> 06:54:59,820
we have here
9273
06:54:59,820 --> 06:55:02,700
if you have an app it will uh also
9274
06:55:02,700 --> 06:55:05,340
Crusher up but the later neat keyword
9275
06:55:05,340 --> 06:55:08,160
allows us to not provide as I said the
9276
06:55:08,160 --> 06:55:11,160
value literally a little value here or a
9277
06:55:11,160 --> 06:55:12,740
value through the
9278
06:55:12,740 --> 06:55:15,298
constructed by the by assigning the
9279
06:55:15,298 --> 06:55:16,798
parameter here with the value pass of
9280
06:55:16,798 --> 06:55:18,000
the parameter
9281
06:55:18,000 --> 06:55:20,000
and the
9282
06:55:20,000 --> 06:55:24,020
is uh is our
9283
06:55:24,020 --> 06:55:26,580
responsibility to initialize that
9284
06:55:26,580 --> 06:55:28,920
variable later so that you don't get
9285
06:55:28,920 --> 06:55:31,680
into the same problems that we talked
9286
06:55:31,680 --> 06:55:34,978
about so we don't get this error so it's
9287
06:55:34,978 --> 06:55:36,718
up to you to
9288
06:55:36,718 --> 06:55:40,378
to declare a a property as late and
9289
06:55:40,378 --> 06:55:44,820
eaten then provide a value later because
9290
06:55:44,820 --> 06:55:47,580
if you don't it will also Crush you up
9291
06:55:47,580 --> 06:55:51,660
when you try to access uh by uh I don't
9292
06:55:51,660 --> 06:55:54,298
know in Europe somehow you want to
9293
06:55:54,298 --> 06:55:57,058
access the value of that property
9294
06:55:57,058 --> 06:55:59,940
so I always have a have a decent mind
9295
06:55:59,940 --> 06:56:01,260
that you need to initialize that
9296
06:56:01,260 --> 06:56:04,080
variable later
9297
06:56:04,080 --> 06:56:06,540
also I should say that the latent
9298
06:56:06,540 --> 06:56:08,400
keyword only works with
9299
06:56:08,400 --> 06:56:09,958
classes
9300
06:56:09,958 --> 06:56:12,900
and it does not work with primitive type
9301
06:56:12,900 --> 06:56:15,180
so even though the as I said the
9302
06:56:15,180 --> 06:56:17,340
integers and all the data types that we
9303
06:56:17,340 --> 06:56:19,680
looked at and also the
9304
06:56:19,680 --> 06:56:22,978
Boolean and Char are classes
9305
06:56:22,978 --> 06:56:25,440
we cannot use latent adhered with them
9306
06:56:25,440 --> 06:56:27,718
because if I put here in you can see
9307
06:56:27,718 --> 06:56:29,580
that I have an error
9308
06:56:29,580 --> 06:56:31,798
that it says latony modifiers not
9309
06:56:31,798 --> 06:56:33,660
allowed on properties of primitive types
9310
06:56:33,660 --> 06:56:37,740
because uh I'm not gonna get into much
9311
06:56:37,740 --> 06:56:40,260
of details but all we need to know to
9312
06:56:40,260 --> 06:56:41,760
know is that
9313
06:56:41,760 --> 06:56:43,860
when
9314
06:56:43,860 --> 06:56:46,978
the int class and all the Primitive
9315
06:56:46,978 --> 06:56:52,138
types which are classes uh at least
9316
06:56:52,138 --> 06:56:55,860
at the surface when they are compiled I
9317
06:56:55,860 --> 06:56:58,138
think they are compiled into primitive
9318
06:56:58,138 --> 06:56:59,580
types so they are not compiled into
9319
06:56:59,580 --> 06:57:02,760
classes so this I think is the reason
9320
06:57:02,760 --> 06:57:06,420
that you don't you can't use this latent
9321
06:57:06,420 --> 06:57:09,120
in the keyword but for other classes
9322
06:57:09,120 --> 06:57:12,478
because a string is is a data type but
9323
06:57:12,478 --> 06:57:14,760
it's a special data type as I said you
9324
06:57:14,760 --> 06:57:18,180
know in a previous previous video and
9325
06:57:18,180 --> 06:57:20,340
because of that you can use the latent
9326
06:57:20,340 --> 06:57:21,600
keyword and you can use the latent
9327
06:57:21,600 --> 06:57:23,400
latent keyword
9328
06:57:23,400 --> 06:57:25,260
with other classes so I can put here
9329
06:57:25,260 --> 06:57:27,180
user if you want
9330
06:57:27,180 --> 06:57:29,638
and it works because users is also class
9331
06:57:29,638 --> 06:57:32,340
is this class that we Define here
9332
06:57:32,340 --> 06:57:37,080
so it's good to to to remember this
9333
06:57:37,080 --> 06:57:39,840
now I'm going to end the video and see
9334
06:57:39,840 --> 06:57:41,940
you in the next video but first let's uh
9335
06:57:41,940 --> 06:57:44,280
press Ctrl G to have the code that you
9336
06:57:44,280 --> 06:57:46,878
had previously
9337
06:57:47,580 --> 06:57:51,298
so uh now I'm gonna end in the video and
9338
06:57:51,298 --> 06:57:52,620
see you in the next video
9339
06:57:52,620 --> 06:57:54,240
so now it's time to start a discussion
9340
06:57:54,240 --> 06:57:56,520
about companion object but to do that
9341
06:57:56,520 --> 06:57:59,820
I'm gonna delete the class that we have
9342
06:57:59,820 --> 06:58:01,440
here
9343
06:58:01,440 --> 06:58:03,058
and I'm going to paste it inside our
9344
06:58:03,058 --> 06:58:05,458
classes file because we're gonna need
9345
06:58:05,458 --> 06:58:07,978
this class later in our videos
9346
06:58:07,978 --> 06:58:09,478
and I'm gonna also delete this code
9347
06:58:09,478 --> 06:58:11,218
because you don't need this code
9348
06:58:11,218 --> 06:58:13,558
and let's say that
9349
06:58:13,558 --> 06:58:17,660
let's create the class called calculator
9350
06:58:20,760 --> 06:58:24,600
is not going to Define any properties
9351
06:58:24,600 --> 06:58:26,160
you know it's going to have a simple
9352
06:58:26,160 --> 06:58:28,620
function called sum which is going to
9353
06:58:28,620 --> 06:58:30,540
sum two numbers
9354
06:58:30,540 --> 06:58:32,160
so we're going to Define two parameters
9355
06:58:32,160 --> 06:58:34,440
here a
9356
06:58:34,440 --> 06:58:38,040
it's going to be an INT a b
9357
06:58:38,040 --> 06:58:40,080
also on it
9358
06:58:40,080 --> 06:58:42,298
n is going to return the sound so we put
9359
06:58:42,298 --> 06:58:45,020
colon at the end of our
9360
06:58:45,020 --> 06:58:47,520
parenthesis or function we put int
9361
06:58:47,520 --> 06:58:49,500
because this is what you're going to
9362
06:58:49,500 --> 06:58:50,580
return
9363
06:58:50,580 --> 06:58:54,840
and we put calibration type return a
9364
06:58:54,840 --> 06:58:57,420
plus b or you can put this in a single
9365
06:58:57,420 --> 06:59:00,600
body expression function so you can put
9366
06:59:00,600 --> 06:59:04,100
the code like this if you want
9367
06:59:05,100 --> 06:59:07,260
so we can put here uh you can also
9368
06:59:07,260 --> 06:59:08,760
delete a type you can put here equals
9369
06:59:08,760 --> 06:59:11,580
and this will work but I'm gonna
9370
06:59:11,580 --> 06:59:13,620
keep it like this
9371
06:59:13,620 --> 06:59:17,520
now if you want to use this sum function
9372
06:59:17,520 --> 06:59:19,920
that is defined in this uh
9373
06:59:19,920 --> 06:59:22,138
class calculator first you need to
9374
06:59:22,138 --> 06:59:24,360
create an object without class so we
9375
06:59:24,360 --> 06:59:26,520
type here Val I'm going to call it
9376
06:59:26,520 --> 06:59:28,820
calculator
9377
06:59:29,760 --> 06:59:32,160
and I'm gonna create here a new object
9378
06:59:32,160 --> 06:59:34,458
calculator
9379
06:59:35,458 --> 06:59:37,558
Constructor doesn't have any properties
9380
06:59:37,558 --> 06:59:41,040
any parameters and if you want to
9381
06:59:41,040 --> 06:59:43,500
to call that some function we need to
9382
06:59:43,500 --> 06:59:46,458
use our calculator
9383
06:59:46,620 --> 06:59:49,500
variable or I'll confrater object or
9384
06:59:49,500 --> 06:59:53,040
reference and we put dot like we did in
9385
06:59:53,040 --> 06:59:55,138
our research so use the dot notation so
9386
06:59:55,138 --> 06:59:57,478
now we're calling the sum function on
9387
06:59:57,478 --> 06:59:59,280
our calculator object that we created
9388
06:59:59,280 --> 07:00:02,160
online too let me put when we call our
9389
07:00:02,160 --> 07:00:04,260
function sum inside the buttons you pass
9390
07:00:04,260 --> 07:00:08,120
two numbers let's say 5 and 10.
9391
07:00:08,160 --> 07:00:09,718
and uh
9392
07:00:09,718 --> 07:00:11,760
let's capture this value that is
9393
07:00:11,760 --> 07:00:13,080
returned by the
9394
07:00:13,080 --> 07:00:15,120
by the function in a variable so let's
9395
07:00:15,120 --> 07:00:17,600
put here
9396
07:00:17,638 --> 07:00:20,478
Val result
9397
07:00:26,638 --> 07:00:28,320
because that is going to return the sum
9398
07:00:28,320 --> 07:00:30,660
of 5 and 10. of course that you can put
9399
07:00:30,660 --> 07:00:32,878
that in a printerland
9400
07:00:32,878 --> 07:00:35,820
statement if one but I put it in this
9401
07:00:35,820 --> 07:00:39,120
elaborate way to to make
9402
07:00:39,120 --> 07:00:41,520
more clear what you're doing and here
9403
07:00:41,520 --> 07:00:44,718
I'm going to type println
9404
07:00:44,940 --> 07:00:46,740
result
9405
07:00:46,740 --> 07:00:49,020
so now if I run the squad
9406
07:00:49,020 --> 07:00:52,260
you're gonna see 15 in output line in
9407
07:00:52,260 --> 07:00:54,739
the console
9408
07:00:55,260 --> 07:00:57,360
so let's increase this
9409
07:00:57,360 --> 07:01:00,958
so we have 15. and the way we did it is
9410
07:01:00,958 --> 07:01:02,878
we created an object like we did so many
9411
07:01:02,878 --> 07:01:05,700
times then we want that object use the
9412
07:01:05,700 --> 07:01:07,978
dot notation to curl that function on
9413
07:01:07,978 --> 07:01:10,378
that specific object on this object and
9414
07:01:10,378 --> 07:01:13,200
that function takes as arguments as easy
9415
07:01:13,200 --> 07:01:14,340
as
9416
07:01:14,340 --> 07:01:18,298
values to its parameters to integers we
9417
07:01:18,298 --> 07:01:20,340
pass 5 and 10 and then it returns as
9418
07:01:20,340 --> 07:01:23,280
well and we retain we
9419
07:01:23,280 --> 07:01:24,840
we capture that value in the result
9420
07:01:24,840 --> 07:01:26,520
variable and then we want to put that
9421
07:01:26,520 --> 07:01:30,058
value in the console and get 15. but um
9422
07:01:30,058 --> 07:01:32,760
if you think about the way we did this
9423
07:01:32,760 --> 07:01:36,360
thing is that the only way we can call
9424
07:01:36,360 --> 07:01:38,218
the sum function is by creating an
9425
07:01:38,218 --> 07:01:40,558
object and then calling the function on
9426
07:01:40,558 --> 07:01:43,798
that object but it doesn't make a lot of
9427
07:01:43,798 --> 07:01:47,458
sense if you have a calculator class
9428
07:01:47,458 --> 07:01:48,900
to call
9429
07:01:48,900 --> 07:01:51,540
to create every time through our code
9430
07:01:51,540 --> 07:01:53,100
every time you need to use the sound
9431
07:01:53,100 --> 07:01:54,958
function to create an object throughout
9432
07:01:54,958 --> 07:01:57,240
your code a better way it would be if
9433
07:01:57,240 --> 07:02:00,240
you could call that sum function
9434
07:02:00,240 --> 07:02:03,360
somehow without declaring uh without
9435
07:02:03,360 --> 07:02:05,638
creating an object and then using the
9436
07:02:05,638 --> 07:02:08,878
the object to call the function and to
9437
07:02:08,878 --> 07:02:10,740
you to do that we need to use what is
9438
07:02:10,740 --> 07:02:12,900
called a companion object and to use a
9439
07:02:12,900 --> 07:02:14,820
component object we go inside our class
9440
07:02:14,820 --> 07:02:18,780
we type companion object so we can press
9441
07:02:18,780 --> 07:02:21,298
enter to fill in a component object for
9442
07:02:21,298 --> 07:02:23,218
us or you can type that then we put
9443
07:02:23,218 --> 07:02:25,500
curly braces and inside the curl brace
9444
07:02:25,500 --> 07:02:27,540
you put our codes and in this case we're
9445
07:02:27,540 --> 07:02:30,680
going to put our sum function
9446
07:02:31,680 --> 07:02:34,340
now
9447
07:02:34,860 --> 07:02:36,660
you you're going to see something now we
9448
07:02:36,660 --> 07:02:38,400
have an error here
9449
07:02:38,400 --> 07:02:40,378
now this
9450
07:02:40,378 --> 07:02:43,260
function this sum function doesn't
9451
07:02:43,260 --> 07:02:46,920
belong to an object to an instance that
9452
07:02:46,920 --> 07:02:49,218
is created with the class calculator
9453
07:02:49,218 --> 07:02:53,400
instead it belongs to the class itself
9454
07:02:53,400 --> 07:02:56,160
and not to the not to any object that we
9455
07:02:56,160 --> 07:02:56,878
create
9456
07:02:56,878 --> 07:03:00,420
those the variables and the functions
9457
07:03:00,420 --> 07:03:02,458
that are the functions that are declared
9458
07:03:02,458 --> 07:03:05,700
inside the component object are also
9459
07:03:05,700 --> 07:03:06,780
called
9460
07:03:06,780 --> 07:03:09,900
class variables or
9461
07:03:09,900 --> 07:03:11,878
class functions because they don't
9462
07:03:11,878 --> 07:03:14,040
belong to they don't belong to a
9463
07:03:14,040 --> 07:03:15,840
specific object that you create with
9464
07:03:15,840 --> 07:03:19,378
that with that class instead they belong
9465
07:03:19,378 --> 07:03:21,718
to the class itself so you don't need an
9466
07:03:21,718 --> 07:03:24,600
instance to call that function and uh
9467
07:03:24,600 --> 07:03:26,820
that uh
9468
07:03:26,820 --> 07:03:30,298
implies that we can't use it with an
9469
07:03:30,298 --> 07:03:32,160
object because it belongs only to the
9470
07:03:32,160 --> 07:03:34,080
class and let's see how you can use it
9471
07:03:34,080 --> 07:03:36,360
using the class without creating an
9472
07:03:36,360 --> 07:03:40,400
object we just type calculator
9473
07:03:40,400 --> 07:03:44,218
dot sum and here you pass again
9474
07:03:44,218 --> 07:03:47,280
5 and 10. so
9475
07:03:47,280 --> 07:03:50,760
we just use our name or the name of our
9476
07:03:50,760 --> 07:03:54,600
class we put dot sum and now we can use
9477
07:03:54,600 --> 07:03:57,478
the function without uh then without us
9478
07:03:57,478 --> 07:03:59,280
needing to create an object and then
9479
07:03:59,280 --> 07:04:01,740
calling the function on that object so
9480
07:04:01,740 --> 07:04:05,160
this is a very powerful because uh
9481
07:04:05,160 --> 07:04:06,600
you will need this in your code
9482
07:04:06,600 --> 07:04:09,600
sometimes when you don't uh it it
9483
07:04:09,600 --> 07:04:12,120
actually doesn't make sense to create an
9484
07:04:12,120 --> 07:04:14,760
object every time you you call let's say
9485
07:04:14,760 --> 07:04:17,520
a sum function for a calculator
9486
07:04:17,520 --> 07:04:20,280
now we can also retain that value that
9487
07:04:20,280 --> 07:04:23,958
is written in a variable also result
9488
07:04:25,500 --> 07:04:28,020
put equals
9489
07:04:28,020 --> 07:04:31,940
now we can print this in the console
9490
07:04:41,340 --> 07:04:43,860
so we get also 15 but this time we
9491
07:04:43,860 --> 07:04:46,200
didn't create an object we didn't create
9492
07:04:46,200 --> 07:04:48,660
a new instance and we call that we call
9493
07:04:48,660 --> 07:04:50,820
the sum function on that object we just
9494
07:04:50,820 --> 07:04:53,280
type the calculator class and call that
9495
07:04:53,280 --> 07:04:56,160
function on directly on the class
9496
07:04:56,160 --> 07:04:57,840
all right so this is how you can use the
9497
07:04:57,840 --> 07:05:00,298
companion object
9498
07:05:00,298 --> 07:05:02,280
and this is also true for a variable so
9499
07:05:02,280 --> 07:05:05,040
if you put a variable here let's say Max
9500
07:05:05,040 --> 07:05:06,718
and you put an arbitrary number here
9501
07:05:06,718 --> 07:05:08,580
let's say 100
9502
07:05:08,580 --> 07:05:10,860
we can call this variable
9503
07:05:10,860 --> 07:05:14,160
also by typing calculator the name of
9504
07:05:14,160 --> 07:05:17,540
the class Dot and have our Max variable
9505
07:05:17,540 --> 07:05:21,798
uh like in contrast with how we did the
9506
07:05:21,798 --> 07:05:25,378
how you call the the properties in the
9507
07:05:25,378 --> 07:05:28,320
past by creating an object and using
9508
07:05:28,320 --> 07:05:30,540
that object to curl that property on
9509
07:05:30,540 --> 07:05:33,058
that object in this case you just use
9510
07:05:33,058 --> 07:05:35,340
the class and call the the variable and
9511
07:05:35,340 --> 07:05:37,680
this also can be can be useful if you
9512
07:05:37,680 --> 07:05:38,958
have some
9513
07:05:38,958 --> 07:05:42,240
constants to which you want to use in
9514
07:05:42,240 --> 07:05:45,180
our throughout your quad so let's delete
9515
07:05:45,180 --> 07:05:48,000
this so you can use also with variables
9516
07:05:48,000 --> 07:05:50,160
and if you think about uh
9517
07:05:50,160 --> 07:05:52,920
uh at our
9518
07:05:52,920 --> 07:05:56,218
one of our first lessons about the data
9519
07:05:56,218 --> 07:05:58,680
ties when you look at the minimum and
9520
07:05:58,680 --> 07:06:01,080
maximum value I I type something like
9521
07:06:01,080 --> 07:06:03,478
this Val Max
9522
07:06:03,478 --> 07:06:05,100
and I
9523
07:06:05,100 --> 07:06:07,500
to get the maximum volume I type int
9524
07:06:07,500 --> 07:06:09,780
that
9525
07:06:09,780 --> 07:06:12,360
max volume and this is using also a
9526
07:06:12,360 --> 07:06:13,978
companion object because as you can see
9527
07:06:13,978 --> 07:06:16,798
the integer class because the Primitive
9528
07:06:16,798 --> 07:06:18,718
types are also classes at least on the
9529
07:06:18,718 --> 07:06:19,680
surface
9530
07:06:19,680 --> 07:06:21,978
not when they are compiled on the jvm
9531
07:06:21,978 --> 07:06:25,558
they also use companion objects like we
9532
07:06:25,558 --> 07:06:29,638
have here to to get to to get the
9533
07:06:29,638 --> 07:06:32,218
properties and to get the functions
9534
07:06:32,218 --> 07:06:34,260
because you don't we didn't create an
9535
07:06:34,260 --> 07:06:37,080
instance of our integer of with our
9536
07:06:37,080 --> 07:06:40,020
integer class and then call that max
9537
07:06:40,020 --> 07:06:42,478
value on the object in fact if you hold
9538
07:06:42,478 --> 07:06:45,600
control on this integer class and if you
9539
07:06:45,600 --> 07:06:48,478
actually hover over it and after you
9540
07:06:48,478 --> 07:06:50,520
hold Ctrl it says
9541
07:06:50,520 --> 07:06:53,700
public companion object of int so if you
9542
07:06:53,700 --> 07:06:55,978
hold Ctrl and if you press and if you
9543
07:06:55,978 --> 07:06:57,478
press click now
9544
07:06:57,478 --> 07:07:02,360
it will open this primitives.kt file
9545
07:07:03,420 --> 07:07:05,100
and inside
9546
07:07:05,100 --> 07:07:08,940
primitive.gt if I have our end class and
9547
07:07:08,940 --> 07:07:10,440
we have a companion object inside the
9548
07:07:10,440 --> 07:07:13,138
companion you have this public constant
9549
07:07:13,138 --> 07:07:14,280
value we're going to look at the
9550
07:07:14,280 --> 07:07:16,320
constants in the negative but the point
9551
07:07:16,320 --> 07:07:19,138
here is that it's using the companion
9552
07:07:19,138 --> 07:07:22,080
object to to declare is declaring the
9553
07:07:22,080 --> 07:07:24,180
the this minimum value the maximum value
9554
07:07:24,180 --> 07:07:26,040
inside The Company audio so that you
9555
07:07:26,040 --> 07:07:27,478
don't need to create an instance an
9556
07:07:27,478 --> 07:07:30,840
object every time you need to use them
9557
07:07:30,840 --> 07:07:32,400
so
9558
07:07:32,400 --> 07:07:34,200
this is our discussion about compounding
9559
07:07:34,200 --> 07:07:36,420
object and see you on the next video I'm
9560
07:07:36,420 --> 07:07:37,978
going to close this file now
9561
07:07:37,978 --> 07:07:39,900
and if you if you want you can look you
9562
07:07:39,900 --> 07:07:41,638
can look in that Primitives file if you
9563
07:07:41,638 --> 07:07:45,540
want just take a look look what is there
9564
07:07:45,540 --> 07:07:47,700
so see in the next video
9565
07:07:47,700 --> 07:07:49,320
so now it's time to start a discussion
9566
07:07:49,320 --> 07:07:51,360
about Singleton but first i'm going to
9567
07:07:51,360 --> 07:07:52,798
copy this code I'm going to copy this
9568
07:07:52,798 --> 07:07:54,900
class with the companion object and I'm
9569
07:07:54,900 --> 07:07:56,218
going to paste it inside the classes
9570
07:07:56,218 --> 07:07:57,298
file
9571
07:07:57,298 --> 07:07:59,878
and if you don't have this code Discord
9572
07:07:59,878 --> 07:08:01,320
because you create a new project just
9573
07:08:01,320 --> 07:08:02,878
type the code that I'm going to type in
9574
07:08:02,878 --> 07:08:04,558
a second so let's delete the code inside
9575
07:08:04,558 --> 07:08:06,478
the main function
9576
07:08:06,478 --> 07:08:09,660
now what is a single then
9577
07:08:09,660 --> 07:08:11,580
a Singleton is a design pattern in
9578
07:08:11,580 --> 07:08:14,520
kotlin with which you want to have
9579
07:08:14,520 --> 07:08:17,280
only one instance of a particular web
9580
07:08:17,280 --> 07:08:19,798
object so you want to have only one
9581
07:08:19,798 --> 07:08:21,780
instance of a particular object and you
9582
07:08:21,780 --> 07:08:23,718
want to have that instance available
9583
07:08:23,718 --> 07:08:26,458
globally tutorial program so you don't
9584
07:08:26,458 --> 07:08:28,740
need to create an instance every time
9585
07:08:28,740 --> 07:08:30,540
you need to use that
9586
07:08:30,540 --> 07:08:33,600
that object in a different part of your
9587
07:08:33,600 --> 07:08:34,860
code
9588
07:08:34,860 --> 07:08:36,900
and there are
9589
07:08:36,900 --> 07:08:39,180
project practical reason for having only
9590
07:08:39,180 --> 07:08:40,920
one instance of a class let's say that
9591
07:08:40,920 --> 07:08:42,360
you have a database
9592
07:08:42,360 --> 07:08:44,218
you want to you want to have only one
9593
07:08:44,218 --> 07:08:46,680
instance of that database which creates
9594
07:08:46,680 --> 07:08:49,558
the database which gives the data and so
9595
07:08:49,558 --> 07:08:51,240
on because if you have multiple
9596
07:08:51,240 --> 07:08:53,638
instances if you able you are able to
9597
07:08:53,638 --> 07:08:56,218
create multiple objects with that class
9598
07:08:56,218 --> 07:08:59,280
then you'll have multiple databases in
9599
07:08:59,280 --> 07:09:00,240
your
9600
07:09:00,240 --> 07:09:03,360
phone or on our PC and that is not good
9601
07:09:03,360 --> 07:09:07,920
because you're using more memory than
9602
07:09:07,920 --> 07:09:10,798
you should and you have a duplicate
9603
07:09:10,798 --> 07:09:12,718
duplicated
9604
07:09:12,718 --> 07:09:16,320
duplicated and unnecessary code
9605
07:09:16,320 --> 07:09:19,020
so to solve that what we need to do is
9606
07:09:19,020 --> 07:09:21,718
to create only one instance of a
9607
07:09:21,718 --> 07:09:24,120
particular class and uh
9608
07:09:24,120 --> 07:09:27,240
having this example in mind this is what
9609
07:09:27,240 --> 07:09:30,180
we're gonna do next and first I'm going
9610
07:09:30,180 --> 07:09:33,360
to show you how we we did the
9611
07:09:33,360 --> 07:09:36,718
in the past how we created a Singleton
9612
07:09:36,718 --> 07:09:40,378
without having the object keyword so you
9613
07:09:40,378 --> 07:09:41,638
go down here
9614
07:09:41,638 --> 07:09:43,558
and first we're going to create a
9615
07:09:43,558 --> 07:09:45,120
Singleton in the world wave so we type
9616
07:09:45,120 --> 07:09:46,260
here class you're going to use our
9617
07:09:46,260 --> 07:09:48,240
example database not going to create a
9618
07:09:48,240 --> 07:09:50,520
real database we're gonna just use this
9619
07:09:50,520 --> 07:09:52,080
as an example
9620
07:09:52,080 --> 07:09:55,440
and to make to restrict the creation of
9621
07:09:55,440 --> 07:09:57,718
instances with this database class we
9622
07:09:57,718 --> 07:09:59,400
need to make the Constructor private so
9623
07:09:59,400 --> 07:10:03,000
we type private and uh when you use an
9624
07:10:03,000 --> 07:10:04,680
access modifier with the primary
9625
07:10:04,680 --> 07:10:06,840
Constructor you need to use the
9626
07:10:06,840 --> 07:10:09,780
Constructor keyword other otherwise will
9627
07:10:09,780 --> 07:10:11,820
have an error so if I delete the
9628
07:10:11,820 --> 07:10:14,878
Constructor keyword here
9629
07:10:14,878 --> 07:10:18,680
I have an error which says
9630
07:10:19,700 --> 07:10:23,458
after modifiers or Pro of primary
9631
07:10:23,458 --> 07:10:27,859
Constructors so we put Constructor
9632
07:10:28,280 --> 07:10:31,920
and inside the
9633
07:10:31,920 --> 07:10:34,260
inside the glass
9634
07:10:34,260 --> 07:10:37,320
now because we cannot create instances
9635
07:10:37,320 --> 07:10:39,360
of this class that means that we cannot
9636
07:10:39,360 --> 07:10:43,440
uh access any of the properties or the
9637
07:10:43,440 --> 07:10:45,718
functions of this class and to to to
9638
07:10:45,718 --> 07:10:47,878
have access to a function which is going
9639
07:10:47,878 --> 07:10:51,180
to return an instance of this class we
9640
07:10:51,180 --> 07:10:53,218
need to use a companion object because
9641
07:10:53,218 --> 07:10:56,218
with companion like this you can have
9642
07:10:56,218 --> 07:11:00,120
properties and functions being allowed
9643
07:11:00,120 --> 07:11:01,920
to being to be accessed outside of the
9644
07:11:01,920 --> 07:11:04,798
class without creating an instance
9645
07:11:04,798 --> 07:11:07,500
and here we type again
9646
07:11:07,500 --> 07:11:10,500
the private access modifier we call this
9647
07:11:10,500 --> 07:11:13,260
instance and it's going to be a database
9648
07:11:13,260 --> 07:11:15,540
a nullable database
9649
07:11:15,540 --> 07:11:18,978
should be valid there
9650
07:11:19,260 --> 07:11:22,080
instance
9651
07:11:22,080 --> 07:11:26,100
database and as I said nullable so we
9652
07:11:26,100 --> 07:11:29,940
can assign null to it
9653
07:11:29,940 --> 07:11:32,458
then we Define a function called get
9654
07:11:32,458 --> 07:11:34,200
instance
9655
07:11:34,200 --> 07:11:35,878
and this function is going to return an
9656
07:11:35,878 --> 07:11:37,920
instance so for instance of our database
9657
07:11:37,920 --> 07:11:39,420
class
9658
07:11:39,420 --> 07:11:42,058
only one instance one one single
9659
07:11:42,058 --> 07:11:43,978
instance not multiple instances
9660
07:11:43,978 --> 07:11:45,900
and to do that first to check to see if
9661
07:11:45,900 --> 07:11:47,940
the instance is null so if this is the
9662
07:11:47,940 --> 07:11:51,120
first time that we want to access
9663
07:11:51,120 --> 07:11:52,860
this uh
9664
07:11:52,860 --> 07:11:55,680
instance then it's gonna return true if
9665
07:11:55,680 --> 07:11:57,180
not it's going to return false so it
9666
07:11:57,180 --> 07:11:59,218
checks if it's equal to null if it's
9667
07:11:59,218 --> 07:12:01,500
equal to null so our instance hasn't
9668
07:12:01,500 --> 07:12:02,878
been initialized
9669
07:12:02,878 --> 07:12:05,400
you know type instance equals to
9670
07:12:05,400 --> 07:12:07,878
database
9671
07:12:10,440 --> 07:12:12,840
right so if the instance is equal to
9672
07:12:12,840 --> 07:12:14,700
nulled that means that the instance has
9673
07:12:14,700 --> 07:12:16,320
been initialized we initialize the
9674
07:12:16,320 --> 07:12:19,080
database so we can initialize the class
9675
07:12:19,080 --> 07:12:21,000
inside the class we cannot initialize it
9676
07:12:21,000 --> 07:12:22,260
outside
9677
07:12:22,260 --> 07:12:23,878
else
9678
07:12:23,878 --> 07:12:27,780
so if this condition fails the the code
9679
07:12:27,780 --> 07:12:30,180
below is going to be red else you're
9680
07:12:30,180 --> 07:12:32,878
gonna return the instance so if the
9681
07:12:32,878 --> 07:12:36,180
instance is not null and this uh this
9682
07:12:36,180 --> 07:12:39,000
code is not going to be a
9683
07:12:39,000 --> 07:12:41,160
this condition is going to be false and
9684
07:12:41,160 --> 07:12:42,180
the code will not be as a good inside
9685
07:12:42,180 --> 07:12:43,920
the if it's gonna we're gonna return the
9686
07:12:43,920 --> 07:12:45,680
instance but here you go you should
9687
07:12:45,680 --> 07:12:48,600
return the database
9688
07:12:48,600 --> 07:12:50,458
and it should be a knowledgeable
9689
07:12:50,458 --> 07:12:51,540
database
9690
07:12:51,540 --> 07:12:53,400
what is our Arrow disappeared let's
9691
07:12:53,400 --> 07:12:55,080
delete this
9692
07:12:55,080 --> 07:12:59,120
let's press Ctrl alt there
9693
07:12:59,760 --> 07:13:02,160
all right now how we're gonna use this
9694
07:13:02,160 --> 07:13:04,260
first
9695
07:13:04,260 --> 07:13:05,520
we type here
9696
07:13:05,520 --> 07:13:09,058
Val instance
9697
07:13:09,660 --> 07:13:13,440
input equals and type database Dot and
9698
07:13:13,440 --> 07:13:14,940
I'm going to use our function get
9699
07:13:14,940 --> 07:13:17,040
instance
9700
07:13:17,040 --> 07:13:22,020
and uh if we we print this instance in
9701
07:13:22,020 --> 07:13:25,760
the console so we type here instance
9702
07:13:26,400 --> 07:13:29,600
if you run the squad
9703
07:13:30,180 --> 07:13:32,700
so now we're calling the
9704
07:13:32,700 --> 07:13:34,920
fan gets instance inside the company of
9705
07:13:34,920 --> 07:13:37,217
object
9706
07:13:37,500 --> 07:13:39,420
we get this code that this code
9707
07:13:39,420 --> 07:13:42,120
represents the
9708
07:13:42,120 --> 07:13:44,700
location memory of the instance so we
9709
07:13:44,700 --> 07:13:48,080
have this code here and if we try to
9710
07:13:48,080 --> 07:13:50,940
instantiate this class so we put here
9711
07:13:50,940 --> 07:13:53,520
the Constructor if we do that then we
9712
07:13:53,520 --> 07:13:55,138
get an error because the Constructor is
9713
07:13:55,138 --> 07:13:56,760
private so we cannot create an instance
9714
07:13:56,760 --> 07:13:58,500
and
9715
07:13:58,500 --> 07:14:01,200
when you call the get instance function
9716
07:14:01,200 --> 07:14:04,638
so when we did this
9717
07:14:08,820 --> 07:14:11,280
first check to see if the instance was
9718
07:14:11,280 --> 07:14:13,440
equal to null and it was equal so now
9719
07:14:13,440 --> 07:14:15,478
then created the instance and then it
9720
07:14:15,478 --> 07:14:16,740
returned the instance
9721
07:14:16,740 --> 07:14:19,440
but if you call this
9722
07:14:19,440 --> 07:14:21,360
function again so let's call it
9723
07:14:21,360 --> 07:14:24,000
instance2
9724
07:14:24,000 --> 07:14:26,340
because database that
9725
07:14:26,340 --> 07:14:29,218
get instance now this will evaluate to
9726
07:14:29,218 --> 07:14:32,160
false because the instance is not equal
9727
07:14:32,160 --> 07:14:34,680
to null we have an instance and it's
9728
07:14:34,680 --> 07:14:36,478
going to come down here and it's going
9729
07:14:36,478 --> 07:14:37,798
to return the instance they're already
9730
07:14:37,798 --> 07:14:40,740
creating instance so if I
9731
07:14:40,740 --> 07:14:44,180
put a printerland here
9732
07:14:44,400 --> 07:14:47,820
and I'm gonna output the instance tool
9733
07:14:47,820 --> 07:14:49,740
you're gonna see that there is no
9734
07:14:49,740 --> 07:14:52,740
instance to you have the same instance
9735
07:14:52,740 --> 07:14:55,378
because it's you it's returning already
9736
07:14:55,378 --> 07:14:57,900
creating created the instance so we have
9737
07:14:57,900 --> 07:15:00,780
the same code here meaning
9738
07:15:00,780 --> 07:15:04,080
this translates to that you have the
9739
07:15:04,080 --> 07:15:06,120
same instance so no matter how many
9740
07:15:06,120 --> 07:15:08,520
times you use database I get instance if
9741
07:15:08,520 --> 07:15:10,020
it's the first time then it's going to
9742
07:15:10,020 --> 07:15:11,580
create the instance and it's going to
9743
07:15:11,580 --> 07:15:14,280
return in essence if you call that again
9744
07:15:14,280 --> 07:15:16,558
like we do for instance 2 then it's
9745
07:15:16,558 --> 07:15:19,160
gonna return the already created users
9746
07:15:19,160 --> 07:15:21,718
so in this way you have the same
9747
07:15:21,718 --> 07:15:23,878
instance no matter how many times you
9748
07:15:23,878 --> 07:15:26,160
you call the get instance function
9749
07:15:26,160 --> 07:15:28,500
because it will always return the same
9750
07:15:28,500 --> 07:15:30,478
instance because we
9751
07:15:30,478 --> 07:15:33,000
because of our logic that we created
9752
07:15:33,000 --> 07:15:35,458
here but there is a shorter way to
9753
07:15:35,458 --> 07:15:37,500
achieve the same thing by using the
9754
07:15:37,500 --> 07:15:40,740
object keyword and this is what we're
9755
07:15:40,740 --> 07:15:43,340
gonna do next
9756
07:15:43,920 --> 07:15:45,840
so let's close this
9757
07:15:45,840 --> 07:15:47,760
now let's see how we can create a
9758
07:15:47,760 --> 07:15:50,040
Singleton and which is going to have the
9759
07:15:50,040 --> 07:15:53,160
same behavior that we created here so
9760
07:15:53,160 --> 07:15:55,320
it's gonna be only one instance using
9761
07:15:55,320 --> 07:15:57,478
the object keyword so I'm going to type
9762
07:15:57,478 --> 07:16:01,020
down here object
9763
07:16:03,180 --> 07:16:06,558
I'm going to call it database
9764
07:16:06,900 --> 07:16:08,700
now let's delete this code because we
9765
07:16:08,700 --> 07:16:10,680
cannot have a
9766
07:16:10,680 --> 07:16:14,900
two objects which have the same name
9767
07:16:16,080 --> 07:16:18,180
and we cannot have a Constructor here
9768
07:16:18,180 --> 07:16:21,660
because it's a Singleton so you put only
9769
07:16:21,660 --> 07:16:23,280
curly braces
9770
07:16:23,280 --> 07:16:25,260
you can have properties and functions
9771
07:16:25,260 --> 07:16:28,138
inside the Singleton created with you
9772
07:16:28,138 --> 07:16:29,580
with the
9773
07:16:29,580 --> 07:16:32,040
object kios leslie.com inside Dimension
9774
07:16:32,040 --> 07:16:33,958
frame function too
9775
07:16:33,958 --> 07:16:35,820
so uh
9776
07:16:35,820 --> 07:16:39,360
Now by just typing object database now
9777
07:16:39,360 --> 07:16:41,040
we have the same thing that we had
9778
07:16:41,040 --> 07:16:43,740
previously just by typing object and
9779
07:16:43,740 --> 07:16:46,200
database so this will always return the
9780
07:16:46,200 --> 07:16:46,920
same
9781
07:16:46,920 --> 07:16:50,160
instance and we have only this code
9782
07:16:50,160 --> 07:16:53,340
object and the name of the object so the
9783
07:16:53,340 --> 07:16:56,700
in the instance of the object is going
9784
07:16:56,700 --> 07:16:58,680
to be created the first time you call it
9785
07:16:58,680 --> 07:17:01,020
and we can put an initializer block to
9786
07:17:01,020 --> 07:17:02,580
have some code to be executed here so
9787
07:17:02,580 --> 07:17:04,620
let's put a printerland
9788
07:17:04,620 --> 07:17:07,638
let's say yo
9789
07:17:10,440 --> 07:17:14,700
this is a database created
9790
07:17:14,700 --> 07:17:17,520
and to you to use this just type let's
9791
07:17:17,520 --> 07:17:19,978
put a printerland
9792
07:17:19,978 --> 07:17:23,120
and type database
9793
07:17:23,458 --> 07:17:26,717
and now if you run this
9794
07:17:28,740 --> 07:17:30,440
and the first time you call the
9795
07:17:30,440 --> 07:17:33,478
Singleton that time the Singleton is
9796
07:17:33,478 --> 07:17:34,740
going to be
9797
07:17:34,740 --> 07:17:37,558
it's going to be created so I have
9798
07:17:37,558 --> 07:17:39,120
database created then we have this code
9799
07:17:39,120 --> 07:17:40,978
which represents the
9800
07:17:40,978 --> 07:17:43,740
or Stringer is the string representation
9801
07:17:43,740 --> 07:17:46,920
of the object and if you press Ctrl D
9802
07:17:46,920 --> 07:17:49,798
here look what happens
9803
07:17:49,798 --> 07:17:51,958
we have
9804
07:17:51,958 --> 07:17:54,840
database created so because this is only
9805
07:17:54,840 --> 07:17:57,718
one instance this object is a single
9806
07:17:57,718 --> 07:18:01,080
turn it calls the initializer block only
9807
07:18:01,080 --> 07:18:03,840
once when it initializes itself so when
9808
07:18:03,840 --> 07:18:05,458
we call the database for the first time
9809
07:18:05,458 --> 07:18:08,638
it is initialized and it calls the code
9810
07:18:08,638 --> 07:18:10,680
inside the init block and after that it
9811
07:18:10,680 --> 07:18:11,760
only
9812
07:18:11,760 --> 07:18:14,340
it output this thing representation the
9813
07:18:14,340 --> 07:18:16,378
second time we call the database we
9814
07:18:16,378 --> 07:18:18,298
don't have database created because the
9815
07:18:18,298 --> 07:18:20,100
instance was already created and the
9816
07:18:20,100 --> 07:18:22,378
initializer block is not called again
9817
07:18:22,378 --> 07:18:24,360
because we have the same instance so if
9818
07:18:24,360 --> 07:18:26,420
you can press Ctrl D here multiple times
9819
07:18:26,420 --> 07:18:28,740
and we're gonna see that we have the
9820
07:18:28,740 --> 07:18:31,440
database created with only one
9821
07:18:31,440 --> 07:18:34,740
one uh once because
9822
07:18:34,740 --> 07:18:39,000
that is called only when the
9823
07:18:39,000 --> 07:18:41,100
only when the object is created for the
9824
07:18:41,100 --> 07:18:43,138
first time so on this line in the next
9825
07:18:43,138 --> 07:18:46,200
slide we have the same uh the same
9826
07:18:46,200 --> 07:18:48,420
objects we have here this
9827
07:18:48,420 --> 07:18:50,040
this is why I have the same code because
9828
07:18:50,040 --> 07:18:52,500
it's basically the same instance so as
9829
07:18:52,500 --> 07:18:56,520
you can see this is a lot simpler and um
9830
07:18:56,520 --> 07:18:58,440
more concise than the code that had
9831
07:18:58,440 --> 07:18:59,878
previously so this is how you can create
9832
07:18:59,878 --> 07:19:03,660
a Singleton in kotlin this is why this
9833
07:19:03,660 --> 07:19:05,280
is how we should do it
9834
07:19:05,280 --> 07:19:08,600
so see in the next video
9835
07:19:09,240 --> 07:19:10,798
so now it's time to start a discussion
9836
07:19:10,798 --> 07:19:13,620
about lazy initialization but first i'm
9837
07:19:13,620 --> 07:19:15,360
going to copy this Singleton and solid
9838
07:19:15,360 --> 07:19:18,120
hour classes file
9839
07:19:18,120 --> 07:19:20,000
and if you don't have this file because
9840
07:19:20,000 --> 07:19:22,920
you created a new project again just
9841
07:19:22,920 --> 07:19:24,120
type the code that I'm going to type in
9842
07:19:24,120 --> 07:19:26,760
a second so I'm going to delete this
9843
07:19:26,760 --> 07:19:29,520
now what is lazy initialization
9844
07:19:29,520 --> 07:19:33,298
lazy initialization is used when
9845
07:19:33,298 --> 07:19:35,878
creating an instance when creating an
9846
07:19:35,878 --> 07:19:38,700
object is expensive and the expensive in
9847
07:19:38,700 --> 07:19:40,260
the programming context means that it
9848
07:19:40,260 --> 07:19:41,700
will take some time
9849
07:19:41,700 --> 07:19:44,520
it will use some memory and in that case
9850
07:19:44,520 --> 07:19:46,440
it's recommended to use lazy
9851
07:19:46,440 --> 07:19:49,260
initialization and to illustrate the
9852
07:19:49,260 --> 07:19:51,660
lazy initialization I'm going to use our
9853
07:19:51,660 --> 07:19:53,520
user class so I'm going to compare user
9854
07:19:53,520 --> 07:19:55,558
class inside order
9855
07:19:55,558 --> 07:19:57,900
main file not because I can create here
9856
07:19:57,900 --> 07:20:01,200
an object by having the file here but
9857
07:20:01,200 --> 07:20:02,660
because
9858
07:20:02,660 --> 07:20:04,680
I want to
9859
07:20:04,680 --> 07:20:07,020
put the code here to make clear what I'm
9860
07:20:07,020 --> 07:20:09,000
doing so I'm gonna delete the code from
9861
07:20:09,000 --> 07:20:11,040
here
9862
07:20:11,040 --> 07:20:12,540
and if you don't have the file just
9863
07:20:12,540 --> 07:20:15,600
create a new class called user Define
9864
07:20:15,600 --> 07:20:17,940
the properties for its name last name
9865
07:20:17,940 --> 07:20:19,978
and age I'm gonna delete this because
9866
07:20:19,978 --> 07:20:21,660
you don't need this
9867
07:20:21,660 --> 07:20:24,180
and inside our user class I'm going to
9868
07:20:24,180 --> 07:20:25,978
put initialize an initializer block
9869
07:20:25,978 --> 07:20:27,840
which is going to be called every time
9870
07:20:27,840 --> 07:20:29,700
you create a new object and here we're
9871
07:20:29,700 --> 07:20:32,340
gonna put a printer printer print line
9872
07:20:32,340 --> 07:20:34,080
statement
9873
07:20:34,080 --> 07:20:37,280
which is gonna say
9874
07:20:37,500 --> 07:20:39,360
user
9875
07:20:39,360 --> 07:20:42,180
when I call the first name property so
9876
07:20:42,180 --> 07:20:43,740
the value that Sparks the first name
9877
07:20:43,740 --> 07:20:46,160
property
9878
07:20:50,820 --> 07:20:53,820
was created
9879
07:20:53,820 --> 07:20:57,200
so let's create a new object here
9880
07:20:57,360 --> 07:21:00,120
let's call it user1
9881
07:21:00,120 --> 07:21:01,978
equals user
9882
07:21:01,978 --> 07:21:04,200
now let's put some values to the
9883
07:21:04,200 --> 07:21:05,940
properties of the Constructor let's say
9884
07:21:05,940 --> 07:21:08,239
Alex
9885
07:21:14,218 --> 07:21:17,780
now if you run the squad
9886
07:21:25,260 --> 07:21:28,138
we get in the output user and the value
9887
07:21:28,138 --> 07:21:30,660
for the first name property Alex was
9888
07:21:30,660 --> 07:21:31,500
created
9889
07:21:31,500 --> 07:21:33,478
if I press Ctrl d
9890
07:21:33,478 --> 07:21:37,218
and I change this to user 2.
9891
07:21:37,978 --> 07:21:39,600
and let's change this to a generic name
9892
07:21:39,600 --> 07:21:41,638
user
9893
07:21:41,638 --> 07:21:44,700
for the last nameless
9894
07:21:44,700 --> 07:21:48,840
pass just the last name text
9895
07:21:48,840 --> 07:21:51,298
and let's skip that age so if I run this
9896
07:21:51,298 --> 07:21:52,860
code now
9897
07:21:52,860 --> 07:21:55,860
because now on the line 3 you create a
9898
07:21:55,860 --> 07:21:57,900
new object the initializer block is
9899
07:21:57,900 --> 07:21:59,160
going to be called again because we
9900
07:21:59,160 --> 07:22:02,458
create a new object which is user
9901
07:22:02,458 --> 07:22:04,978
user was created
9902
07:22:04,978 --> 07:22:07,680
now to initialize our user 2 with the
9903
07:22:07,680 --> 07:22:11,400
lazy initializer we just delete this
9904
07:22:11,400 --> 07:22:13,558
and to illustrate how the initializer
9905
07:22:13,558 --> 07:22:16,320
block works we delete this and we put
9906
07:22:16,320 --> 07:22:19,500
after user to buy so we put the keyword
9907
07:22:19,500 --> 07:22:21,000
by
9908
07:22:21,000 --> 07:22:24,240
and we type lazy and as I'm typing you
9909
07:22:24,240 --> 07:22:26,400
see that I have that black lace press
9910
07:22:26,400 --> 07:22:30,240
enter and put the curly braces and
9911
07:22:30,240 --> 07:22:31,920
inside the calibration we create our
9912
07:22:31,920 --> 07:22:35,218
object so user
9913
07:22:35,218 --> 07:22:37,200
and here to pass the values to the
9914
07:22:37,200 --> 07:22:40,020
properties so let's put
9915
07:22:40,020 --> 07:22:42,920
user one
9916
07:22:46,138 --> 07:22:50,120
let's pass last name
9917
07:22:51,360 --> 07:22:53,700
and for the age let's put
9918
07:22:53,700 --> 07:22:55,740
zero
9919
07:22:55,740 --> 07:22:57,478
now
9920
07:22:57,478 --> 07:22:59,700
previously when the
9921
07:22:59,700 --> 07:23:02,820
user was initialized
9922
07:23:02,820 --> 07:23:04,798
in the world way
9923
07:23:04,798 --> 07:23:07,138
I found out the user user was created so
9924
07:23:07,138 --> 07:23:09,120
the initializer block was called when he
9925
07:23:09,120 --> 07:23:11,100
created the second level but now
9926
07:23:11,100 --> 07:23:13,740
if I run this code
9927
07:23:13,740 --> 07:23:16,580
look what happens
9928
07:23:18,420 --> 07:23:21,478
we get only our first object we get the
9929
07:23:21,478 --> 07:23:23,160
initializer black only called for our
9930
07:23:23,160 --> 07:23:25,760
first object and for our second object
9931
07:23:25,760 --> 07:23:28,700
initialize using the lazy initialization
9932
07:23:28,700 --> 07:23:31,378
we don't have the initialize of law
9933
07:23:31,378 --> 07:23:34,620
because the object is not initialized is
9934
07:23:34,620 --> 07:23:36,420
the object is only going to be
9935
07:23:36,420 --> 07:23:37,798
initialized
9936
07:23:37,798 --> 07:23:41,100
when you use that um
9937
07:23:41,100 --> 07:23:43,860
object in your code so if you use that
9938
07:23:43,860 --> 07:23:46,440
object somewhere in a code the object
9939
07:23:46,440 --> 07:23:48,718
then is going to be initialized and it's
9940
07:23:48,718 --> 07:23:50,520
going to be used so if I type here a
9941
07:23:50,520 --> 07:23:51,958
println
9942
07:23:51,958 --> 07:23:53,700
which says
9943
07:23:53,700 --> 07:23:59,058
user DOT first name user tool
9944
07:24:03,478 --> 07:24:05,700
now if I run this
9945
07:24:05,700 --> 07:24:08,340
now because I know I'm I am using the
9946
07:24:08,340 --> 07:24:11,160
user 2 object now the user to object
9947
07:24:11,160 --> 07:24:13,200
instance is going to be created and then
9948
07:24:13,200 --> 07:24:15,958
I'm gonna see user 2. first name so now
9949
07:24:15,958 --> 07:24:18,660
we see user let's see user 1 was created
9950
07:24:18,660 --> 07:24:21,540
so the initializer block was called
9951
07:24:21,540 --> 07:24:24,600
because when we called the
9952
07:24:24,600 --> 07:24:26,840
the print line user to that first name
9953
07:24:26,840 --> 07:24:30,840
then in that at that moment it created
9954
07:24:30,840 --> 07:24:33,180
the instance and then it outputted the
9955
07:24:33,180 --> 07:24:36,000
user one so this is why now we see here
9956
07:24:36,000 --> 07:24:38,700
user one one was created and previously
9957
07:24:38,700 --> 07:24:40,378
we didn't because
9958
07:24:40,378 --> 07:24:44,280
now we're using the user to object in
9959
07:24:44,280 --> 07:24:48,478
our code so unless you are not using the
9960
07:24:48,478 --> 07:24:51,120
the instance the object that have
9961
07:24:51,120 --> 07:24:54,000
created using the lazy initializer it is
9962
07:24:54,000 --> 07:24:56,760
not going to be initialized so this is
9963
07:24:56,760 --> 07:24:59,580
initializer this is initialization by
9964
07:24:59,580 --> 07:25:00,718
lazy
9965
07:25:00,718 --> 07:25:02,820
so see in the next video
9966
07:25:02,820 --> 07:25:04,500
so now it's time to start a discussion
9967
07:25:04,500 --> 07:25:06,660
about enum classes but first i'm going
9968
07:25:06,660 --> 07:25:08,340
to copy our user class inside our
9969
07:25:08,340 --> 07:25:11,180
classes file
9970
07:25:12,780 --> 07:25:15,240
now I'm going to delete this class and
9971
07:25:15,240 --> 07:25:16,740
also I'm going to delete the code inside
9972
07:25:16,740 --> 07:25:18,540
the main function
9973
07:25:18,540 --> 07:25:21,120
now what are the num classes
9974
07:25:21,120 --> 07:25:24,240
in on classes are used when we want to
9975
07:25:24,240 --> 07:25:27,540
represent a fixed set of values so when
9976
07:25:27,540 --> 07:25:29,760
you want to represent we want to create
9977
07:25:29,760 --> 07:25:31,860
some constants you should use genomes
9978
07:25:31,860 --> 07:25:32,820
because
9979
07:25:32,820 --> 07:25:35,340
enums are like constants but they are
9980
07:25:35,340 --> 07:25:37,440
more powerful because you can they can
9981
07:25:37,440 --> 07:25:40,020
have properties and they can also have
9982
07:25:40,020 --> 07:25:41,638
functions
9983
07:25:41,638 --> 07:25:43,500
and you know I'm sorry
9984
07:25:43,500 --> 07:25:48,000
usually used in um if conditioners or if
9985
07:25:48,000 --> 07:25:50,700
statements or in in the when statement
9986
07:25:50,700 --> 07:25:54,058
or if I want to call it the if statement
9987
07:25:54,058 --> 07:25:56,280
expression or when statement expression
9988
07:25:56,280 --> 07:25:57,840
now let's see how I can create any
9989
07:25:57,840 --> 07:26:00,240
number class
9990
07:26:00,240 --> 07:26:02,340
and to create any num class just type
9991
07:26:02,340 --> 07:26:05,160
here enum the keyword enum
9992
07:26:05,160 --> 07:26:07,020
and as I'm typing I have some
9993
07:26:07,020 --> 07:26:08,940
suggestions
9994
07:26:08,940 --> 07:26:12,540
then we put class and let's call this uh
9995
07:26:12,540 --> 07:26:14,940
you know class directions
9996
07:26:14,940 --> 07:26:17,280
Direction actually
9997
07:26:17,280 --> 07:26:19,378
then you put curly braces
9998
07:26:19,378 --> 07:26:22,620
and inside the enumclass direction we
9999
07:26:22,620 --> 07:26:25,620
Define our enum values so here we Define
10000
07:26:25,620 --> 07:26:27,420
North
10001
07:26:27,420 --> 07:26:30,058
put comma enter
10002
07:26:30,058 --> 07:26:31,260
South
10003
07:26:31,260 --> 07:26:33,420
you can put them in one line just to
10004
07:26:33,420 --> 07:26:36,120
have comma between them
10005
07:26:36,120 --> 07:26:38,780
East
10006
07:26:39,840 --> 07:26:42,240
and west
10007
07:26:42,240 --> 07:26:44,638
right so we have our
10008
07:26:44,638 --> 07:26:47,458
enums values defined inside our
10009
07:26:47,458 --> 07:26:51,058
Direction enum class and those enums
10010
07:26:51,058 --> 07:26:53,160
values that we have here here they are
10011
07:26:53,160 --> 07:26:55,740
instances of our enum
10012
07:26:55,740 --> 07:26:58,138
class Direction so they are an instance
10013
07:26:58,138 --> 07:27:00,478
there an object created inside the
10014
07:27:00,478 --> 07:27:03,360
enumclass direction just that they have
10015
07:27:03,360 --> 07:27:05,580
this different name and as you can see
10016
07:27:05,580 --> 07:27:07,440
the name of the you know values should
10017
07:27:07,440 --> 07:27:09,600
be all the names should be in uppercase
10018
07:27:09,600 --> 07:27:11,580
letters now let's see how we can access
10019
07:27:11,580 --> 07:27:13,558
those you know values
10020
07:27:13,558 --> 07:27:16,138
to access the you know values we can
10021
07:27:16,138 --> 07:27:19,458
let's put the printer line here
10022
07:27:23,100 --> 07:27:25,500
we type Direction
10023
07:27:25,500 --> 07:27:27,420
then we press Dot
10024
07:27:27,420 --> 07:27:29,700
and we have some some suggestions here
10025
07:27:29,700 --> 07:27:32,100
we have west east north and south so we
10026
07:27:32,100 --> 07:27:35,160
have our enum values suggested so let's
10027
07:27:35,160 --> 07:27:37,978
choose West let's output this in the
10028
07:27:37,978 --> 07:27:40,280
console
10029
07:27:42,660 --> 07:27:45,298
so we get West so this is the
10030
07:27:45,298 --> 07:27:46,620
enum value
10031
07:27:46,620 --> 07:27:49,138
West and um
10032
07:27:49,138 --> 07:27:51,900
let's press Ctrl d
10033
07:27:51,900 --> 07:27:55,160
let's change this to East
10034
07:27:56,878 --> 07:28:01,280
so let's change this to East
10035
07:28:03,180 --> 07:28:06,500
let's change this to North
10036
07:28:08,100 --> 07:28:10,760
and South
10037
07:28:11,218 --> 07:28:14,458
now if you run this code
10038
07:28:14,458 --> 07:28:17,400
we get our enum values West is north and
10039
07:28:17,400 --> 07:28:18,360
south
10040
07:28:18,360 --> 07:28:20,900
so this is how you can get the you know
10041
07:28:20,900 --> 07:28:23,878
the how you can get the genome values
10042
07:28:23,878 --> 07:28:25,440
that you define inside your genome class
10043
07:28:25,440 --> 07:28:27,780
now we can also
10044
07:28:27,780 --> 07:28:29,820
you can also pass
10045
07:28:29,820 --> 07:28:31,920
you can define a Constructor here for
10046
07:28:31,920 --> 07:28:34,320
our enumclass and Define some properties
10047
07:28:34,320 --> 07:28:39,420
like VAR let's call it Direction
10048
07:28:41,820 --> 07:28:45,079
of type string
10049
07:28:46,320 --> 07:28:49,138
and VAR
10050
07:28:49,138 --> 07:28:51,298
distance
10051
07:28:51,298 --> 07:28:54,739
and this is going to be an integer
10052
07:28:55,860 --> 07:28:57,780
now if you hover over here you have some
10053
07:28:57,780 --> 07:28:59,760
underlines and the underlines if you
10054
07:28:59,760 --> 07:29:01,558
have always says that you Nam has not
10055
07:29:01,558 --> 07:29:04,378
the full Constructor so now because as I
10056
07:29:04,378 --> 07:29:06,958
said those elements values are instances
10057
07:29:06,958 --> 07:29:09,058
they are objects created inside the
10058
07:29:09,058 --> 07:29:11,458
original class now we need to pass some
10059
07:29:11,458 --> 07:29:13,020
values to
10060
07:29:13,020 --> 07:29:15,298
the properties that we Define here so
10061
07:29:15,298 --> 07:29:16,798
let's pass some values here so let's
10062
07:29:16,798 --> 07:29:19,558
press let's type here first
10063
07:29:19,558 --> 07:29:22,320
North again I'm gonna speed up this now
10064
07:29:22,320 --> 07:29:24,000
a little bit
10065
07:29:24,000 --> 07:29:26,100
all right so now what we did here we
10066
07:29:26,100 --> 07:29:28,200
passed some values to the
10067
07:29:28,200 --> 07:29:30,780
you know values Constructors because in
10068
07:29:30,780 --> 07:29:34,740
in up here in our enumclass direction we
10069
07:29:34,740 --> 07:29:36,958
Define some properties and those
10070
07:29:36,958 --> 07:29:40,260
properties because the enum values are
10071
07:29:40,260 --> 07:29:42,478
objects they are instances for a number
10072
07:29:42,478 --> 07:29:44,580
directions even though they have those
10073
07:29:44,580 --> 07:29:47,280
different names you need to pass to the
10074
07:29:47,280 --> 07:29:49,440
constructors then some value so we pass
10075
07:29:49,440 --> 07:29:51,540
here north south east west and password
10076
07:29:51,540 --> 07:29:54,718
for for the densest distance 10 20 50
10077
07:29:54,718 --> 07:29:56,340
and 40.
10078
07:29:56,340 --> 07:29:57,780
now
10079
07:29:57,780 --> 07:29:59,760
let's see how we can Loop through our
10080
07:29:59,760 --> 07:30:05,280
enum's values and to do that we just
10081
07:30:05,280 --> 07:30:07,138
delete this code
10082
07:30:07,138 --> 07:30:09,840
and what if and here we add the four
10083
07:30:09,840 --> 07:30:12,600
groups so we type four
10084
07:30:12,600 --> 07:30:15,478
let's call it the
10085
07:30:15,478 --> 07:30:17,458
Direction
10086
07:30:17,458 --> 07:30:20,520
in and here we type
10087
07:30:20,520 --> 07:30:23,580
Direction our enorm class
10088
07:30:23,580 --> 07:30:26,700
dot values and this is going to return
10089
07:30:26,700 --> 07:30:29,340
an array with all the enum values that
10090
07:30:29,340 --> 07:30:30,900
are defined in our class
10091
07:30:30,900 --> 07:30:33,600
now let's put calibrases
10092
07:30:33,600 --> 07:30:36,920
and let's draw the printerland
10093
07:30:37,320 --> 07:30:39,780
and let's put in the direction
10094
07:30:39,780 --> 07:30:41,700
so now if you run this
10095
07:30:41,700 --> 07:30:44,958
you'll see the same output
10096
07:30:47,280 --> 07:30:49,680
but in the order that they are defined
10097
07:30:49,680 --> 07:30:52,940
inside the class
10098
07:30:54,180 --> 07:30:57,718
so we see the same not the same output
10099
07:30:57,718 --> 07:30:59,520
because they are outputted in the order
10100
07:30:59,520 --> 07:31:00,840
in which they are defined in the class
10101
07:31:00,840 --> 07:31:03,660
so we have north south east and west and
10102
07:31:03,660 --> 07:31:06,298
this is how we can Loop through an enum
10103
07:31:06,298 --> 07:31:09,058
to through the enum values of a num
10104
07:31:09,058 --> 07:31:12,478
class so this is how you do it so now
10105
07:31:12,478 --> 07:31:15,378
let's delete this code
10106
07:31:15,900 --> 07:31:18,120
now if you want to access the arguments
10107
07:31:18,120 --> 07:31:20,700
that are passed to our enum values
10108
07:31:20,700 --> 07:31:22,020
Constructors
10109
07:31:22,020 --> 07:31:25,320
we just first put a printerland to
10110
07:31:25,320 --> 07:31:28,440
Output the values on the console
10111
07:31:28,440 --> 07:31:31,558
and type Direction so our rename class
10112
07:31:31,558 --> 07:31:33,120
Dot
10113
07:31:33,120 --> 07:31:35,700
and we choose our in our value let's
10114
07:31:35,700 --> 07:31:38,820
choose North and from here and here now
10115
07:31:38,820 --> 07:31:41,580
we type also that and now we have some
10116
07:31:41,580 --> 07:31:44,218
suggestions we have Direction distance
10117
07:31:44,218 --> 07:31:47,638
and we also have a name
10118
07:31:47,638 --> 07:31:49,500
and name is uh
10119
07:31:49,500 --> 07:31:52,920
is a variable which is not defined by us
10120
07:31:52,920 --> 07:31:55,620
so we've only defined those properties
10121
07:31:55,620 --> 07:31:58,458
direction and distance but name is
10122
07:31:58,458 --> 07:32:00,718
variable which is built in in the
10123
07:32:00,718 --> 07:32:05,820
enumclass similar to the the the
10124
07:32:05,820 --> 07:32:08,040
direction that values which return the
10125
07:32:08,040 --> 07:32:11,820
array is the the direction that values
10126
07:32:11,820 --> 07:32:15,840
values was also uh property which
10127
07:32:15,840 --> 07:32:18,420
actually it was an array property which
10128
07:32:18,420 --> 07:32:20,940
was built into the enumclass so we
10129
07:32:20,940 --> 07:32:22,680
didn't create that it's provided by the
10130
07:32:22,680 --> 07:32:25,080
enumclass so this is why we have that
10131
07:32:25,080 --> 07:32:27,958
name there so let's put Direction
10132
07:32:27,958 --> 07:32:30,620
let's press Ctrl d
10133
07:32:30,620 --> 07:32:34,080
uplicate this code and let's change this
10134
07:32:34,080 --> 07:32:36,240
to the instance
10135
07:32:36,240 --> 07:32:38,400
let's press Ctrl D again and let's also
10136
07:32:38,400 --> 07:32:40,860
print that name
10137
07:32:40,860 --> 07:32:44,280
which is the built-in variable which is
10138
07:32:44,280 --> 07:32:46,500
the built-in variable provided by the NM
10139
07:32:46,500 --> 07:32:49,440
class so let's put here name
10140
07:32:49,440 --> 07:32:52,699
now if we run this code
10141
07:32:54,478 --> 07:32:57,000
get North so this is the value that you
10142
07:32:57,000 --> 07:32:58,920
pass here we get 10 and then we get
10143
07:32:58,920 --> 07:33:02,600
North with capital letter so
10144
07:33:02,820 --> 07:33:04,940
so we get the value that we passed here
10145
07:33:04,940 --> 07:33:07,860
for direction and distance which are
10146
07:33:07,860 --> 07:33:10,080
defined in Direction class so we get the
10147
07:33:10,080 --> 07:33:12,478
value spots for this specific
10148
07:33:12,478 --> 07:33:15,718
you know more volume and then we get the
10149
07:33:15,718 --> 07:33:18,540
North and this is the built-in um
10150
07:33:18,540 --> 07:33:21,718
variable which is uh provided by you by
10151
07:33:21,718 --> 07:33:22,920
Dynam class
10152
07:33:22,920 --> 07:33:24,360
but as I said you can also have
10153
07:33:24,360 --> 07:33:26,400
functions so let's have a function which
10154
07:33:26,400 --> 07:33:29,340
Returns the direction and distance so
10155
07:33:29,340 --> 07:33:31,620
let's type here fun
10156
07:33:31,620 --> 07:33:33,660
we call it print data because it's gonna
10157
07:33:33,660 --> 07:33:35,878
print the data of a specific in our
10158
07:33:35,878 --> 07:33:37,680
value
10159
07:33:37,680 --> 07:33:40,020
it's not going to take any input put
10160
07:33:40,020 --> 07:33:41,638
curly braces
10161
07:33:41,638 --> 07:33:44,100
but now we have an error here that is
10162
07:33:44,100 --> 07:33:46,620
because uh when you declare a function
10163
07:33:46,620 --> 07:33:48,600
inside the nenum class you need to
10164
07:33:48,600 --> 07:33:52,138
provide a semicolon for the last in um
10165
07:33:52,138 --> 07:33:53,520
defined in our class so I need to put
10166
07:33:53,520 --> 07:33:55,860
here a semicolon and there will
10167
07:33:55,860 --> 07:33:58,020
disappear and let's print the data here
10168
07:33:58,020 --> 07:34:01,100
let's add the println
10169
07:34:03,000 --> 07:34:06,478
let's put quotation marks
10170
07:34:06,478 --> 07:34:09,440
as for Direction
10171
07:34:11,580 --> 07:34:13,440
equals
10172
07:34:13,440 --> 07:34:16,138
dollar sign
10173
07:34:16,138 --> 07:34:18,620
Direction
10174
07:34:20,700 --> 07:34:23,298
and
10175
07:34:25,978 --> 07:34:27,540
distance
10176
07:34:27,540 --> 07:34:29,458
equals dollar sign
10177
07:34:29,458 --> 07:34:32,057
distance
10178
07:34:33,120 --> 07:34:35,520
now if you call this wanna
10179
07:34:35,520 --> 07:34:36,958
our
10180
07:34:36,958 --> 07:34:41,520
wanna enam valueless type direction that
10181
07:34:41,520 --> 07:34:43,680
let's say West
10182
07:34:43,680 --> 07:34:46,620
Dot and now we have our function print
10183
07:34:46,620 --> 07:34:48,298
data
10184
07:34:48,298 --> 07:34:51,240
so this print data function is going to
10185
07:34:51,240 --> 07:34:54,900
put in the data of the West enum that is
10186
07:34:54,900 --> 07:34:56,100
defined here so it's going to Output
10187
07:34:56,100 --> 07:34:58,440
West and 40 in the console so you know
10188
07:34:58,440 --> 07:35:01,217
if you run this code
10189
07:35:05,638 --> 07:35:09,900
we get direction West and distance 40.
10190
07:35:09,900 --> 07:35:11,760
so this is how you can use functions
10191
07:35:11,760 --> 07:35:14,958
inside the noun classes
10192
07:35:15,120 --> 07:35:18,058
next let's see how we can use our enum
10193
07:35:18,058 --> 07:35:19,680
Direction and generally how we can use
10194
07:35:19,680 --> 07:35:22,500
enums with the when statement expression
10195
07:35:22,500 --> 07:35:25,200
so I'm going to close the console
10196
07:35:25,200 --> 07:35:26,820
I'm going to delete this code and the
10197
07:35:26,820 --> 07:35:30,000
print data function works for all of our
10198
07:35:30,000 --> 07:35:31,860
Renown values
10199
07:35:31,860 --> 07:35:34,798
so let's delete this
10200
07:35:34,798 --> 07:35:37,080
and here I'm going to Define a variable
10201
07:35:37,080 --> 07:35:38,700
it's going to be a Val it's going to be
10202
07:35:38,700 --> 07:35:40,138
called Direction
10203
07:35:40,138 --> 07:35:42,000
we're going to put equals and hero type
10204
07:35:42,000 --> 07:35:46,558
Direction our enumclass let's choose
10205
07:35:46,558 --> 07:35:48,298
East
10206
07:35:48,298 --> 07:35:51,660
so now the enum
10207
07:35:51,660 --> 07:35:54,900
actually is the west east so now our
10208
07:35:54,900 --> 07:35:57,840
Direction enum is is going to be stored
10209
07:35:57,840 --> 07:36:00,240
in our Direction variable let's use the
10210
07:36:00,240 --> 07:36:03,240
when statement expression
10211
07:36:03,240 --> 07:36:05,638
and we type here when
10212
07:36:05,638 --> 07:36:07,500
Direction
10213
07:36:07,500 --> 07:36:09,958
then you put curly braces and here we
10214
07:36:09,958 --> 07:36:11,760
type
10215
07:36:11,760 --> 07:36:13,680
Direction
10216
07:36:13,680 --> 07:36:16,520
Dot
10217
07:36:16,798 --> 07:36:21,020
East I'm gonna put
10218
07:36:21,900 --> 07:36:23,340
Auto
10219
07:36:23,340 --> 07:36:28,040
execute this course so print the Len
10220
07:36:31,440 --> 07:36:35,340
the direction is
10221
07:36:35,340 --> 07:36:37,440
yes and we'll do the same thing for the
10222
07:36:37,440 --> 07:36:40,558
next one so let's press Ctrl d
10223
07:36:40,558 --> 07:36:42,600
let's change this to
10224
07:36:42,600 --> 07:36:45,199
West
10225
07:36:51,900 --> 07:36:54,440
North
10226
07:36:56,878 --> 07:36:59,458
and South
10227
07:36:59,458 --> 07:37:02,218
so this is how you can use the enorms
10228
07:37:02,218 --> 07:37:06,958
with the one statement now if I run this
10229
07:37:06,958 --> 07:37:09,478
actually I should change the text there
10230
07:37:09,478 --> 07:37:11,520
but I'm going to change it
10231
07:37:11,520 --> 07:37:13,798
so let's close this to change the text
10232
07:37:13,798 --> 07:37:16,039
here
10233
07:37:16,740 --> 07:37:19,138
now if you run this you're gonna see
10234
07:37:19,138 --> 07:37:21,780
the direction it sees because this
10235
07:37:21,780 --> 07:37:24,500
condition is true
10236
07:37:24,840 --> 07:37:27,000
our Direction argument here is going to
10237
07:37:27,000 --> 07:37:30,120
match with this Branch so it's going to
10238
07:37:30,120 --> 07:37:31,620
evaluate true and then it's going to
10239
07:37:31,620 --> 07:37:32,820
execute this code you can pull
10240
07:37:32,820 --> 07:37:35,760
calibrations if you want but as I said
10241
07:37:35,760 --> 07:37:37,320
in our discussion about the when
10242
07:37:37,320 --> 07:37:39,000
statement expression
10243
07:37:39,000 --> 07:37:41,160
you should do scary braces if you have
10244
07:37:41,160 --> 07:37:43,440
multiple lines of code in our case we
10245
07:37:43,440 --> 07:37:45,240
have one single line of code but you can
10246
07:37:45,240 --> 07:37:47,400
put curly braces if you want and of
10247
07:37:47,400 --> 07:37:48,900
course that this Branch will always
10248
07:37:48,900 --> 07:37:51,058
evaluate to true because here we type
10249
07:37:51,058 --> 07:37:52,558
direction that is
10250
07:37:52,558 --> 07:37:55,138
into our Direction variable literally
10251
07:37:55,138 --> 07:37:57,478
directly and this value doesn't change
10252
07:37:57,478 --> 07:37:59,400
but in real up this value could come
10253
07:37:59,400 --> 07:38:01,320
from a database or from the user input
10254
07:38:01,320 --> 07:38:02,878
and in that case you need to use the
10255
07:38:02,878 --> 07:38:05,458
function which is also built into the
10256
07:38:05,458 --> 07:38:09,478
enorma class and that function
10257
07:38:09,478 --> 07:38:11,638
is called value of so we type Direction
10258
07:38:11,638 --> 07:38:15,840
dot value of and this value function
10259
07:38:15,840 --> 07:38:18,600
check his checks to see if our value
10260
07:38:18,600 --> 07:38:21,440
that we pass here exists inside our
10261
07:38:21,440 --> 07:38:24,000
enumclass so it checks to see if is
10262
07:38:24,000 --> 07:38:27,058
defined here and it all it also returns
10263
07:38:27,058 --> 07:38:30,900
that value so if I type here
10264
07:38:30,900 --> 07:38:32,340
yeast
10265
07:38:32,340 --> 07:38:35,540
now if I run this code
10266
07:38:37,798 --> 07:38:39,600
we get an error which has no num
10267
07:38:39,600 --> 07:38:42,120
constant direction that is because
10268
07:38:42,120 --> 07:38:45,180
all of our enum values have they are
10269
07:38:45,180 --> 07:38:47,280
defined in uppercase letters
10270
07:38:47,280 --> 07:38:49,680
but if I put here that
10271
07:38:49,680 --> 07:38:52,558
uppercase
10272
07:38:52,558 --> 07:38:56,000
and if I run this code
10273
07:38:58,798 --> 07:39:02,700
now we get the direction assist and uh
10274
07:39:02,700 --> 07:39:04,920
this value that we have here is this
10275
07:39:04,920 --> 07:39:06,840
value as I said could come from a
10276
07:39:06,840 --> 07:39:09,540
database or from the user input and the
10277
07:39:09,540 --> 07:39:11,878
direction that value of is going to
10278
07:39:11,878 --> 07:39:14,458
check to see if this
10279
07:39:14,458 --> 07:39:16,320
you know value exists in our rename
10280
07:39:16,320 --> 07:39:17,638
class and it's going to return that
10281
07:39:17,638 --> 07:39:18,780
value
10282
07:39:18,780 --> 07:39:21,780
in our variable so it's going to assign
10283
07:39:21,780 --> 07:39:23,280
that value to our variable
10284
07:39:23,280 --> 07:39:26,840
so see you in the next video
10285
07:39:27,420 --> 07:39:29,040
so now it's time to start a discussion
10286
07:39:29,040 --> 07:39:31,320
about inner classes so what are inner
10287
07:39:31,320 --> 07:39:33,660
classes in our classes are classes which
10288
07:39:33,660 --> 07:39:35,760
are declared inside another class and
10289
07:39:35,760 --> 07:39:37,320
they are generally used when you know
10290
07:39:37,320 --> 07:39:38,878
that there is a very close relationship
10291
07:39:38,878 --> 07:39:41,638
between two classes so when it doesn't
10292
07:39:41,638 --> 07:39:43,680
make sense to put two classes separated
10293
07:39:43,680 --> 07:39:46,378
then you should use inner classes and
10294
07:39:46,378 --> 07:39:48,420
let's look at an example and we're going
10295
07:39:48,420 --> 07:39:50,040
to imagine here that we have a list View
10296
07:39:50,040 --> 07:39:53,240
and this list video list view is gonna
10297
07:39:53,240 --> 07:39:57,900
display some items in a list in on the
10298
07:39:57,900 --> 07:40:00,900
screen and here we're going to use inner
10299
07:40:00,900 --> 07:40:03,900
classes because for displaying the each
10300
07:40:03,900 --> 07:40:06,298
individual item we're gonna create a
10301
07:40:06,298 --> 07:40:08,160
list view item so first you create a
10302
07:40:08,160 --> 07:40:11,298
class called list View
10303
07:40:20,400 --> 07:40:23,400
foreign
10304
07:40:26,580 --> 07:40:30,000
we Define a property called
10305
07:40:30,000 --> 07:40:32,520
items and it's going to be an array of
10306
07:40:32,520 --> 07:40:34,820
strings
10307
07:40:42,540 --> 07:40:45,958
now we put curly braces and inside the
10308
07:40:45,958 --> 07:40:48,660
class we type the keyword inner
10309
07:40:48,660 --> 07:40:51,600
then class and now we create a class
10310
07:40:51,600 --> 07:40:53,540
which is going to represent the
10311
07:40:53,540 --> 07:40:56,820
individual items in the list view so we
10312
07:40:56,820 --> 07:41:00,440
put type here list view item
10313
07:41:05,520 --> 07:41:07,080
put the
10314
07:41:07,080 --> 07:41:09,660
the primary Constructor but we put not
10315
07:41:09,660 --> 07:41:11,218
we don't Define any properties and then
10316
07:41:11,218 --> 07:41:13,020
we put curly braces
10317
07:41:13,020 --> 07:41:15,240
and this inner class let's use gonna
10318
07:41:15,240 --> 07:41:18,298
have a function called display
10319
07:41:18,298 --> 07:41:20,520
item and it's going to display the value
10320
07:41:20,520 --> 07:41:23,160
the values of an item of other specific
10321
07:41:23,160 --> 07:41:26,040
position so we put here position let's
10322
07:41:26,040 --> 07:41:27,840
say
10323
07:41:27,840 --> 07:41:30,298
Define a parameter to our function
10324
07:41:30,298 --> 07:41:33,260
it's going to be an end
10325
07:41:35,218 --> 07:41:37,260
and we're just gonna type here print
10326
07:41:37,260 --> 07:41:39,360
line
10327
07:41:39,360 --> 07:41:43,860
and the inner classes are have access to
10328
07:41:43,860 --> 07:41:45,780
the properties of the outer class so
10329
07:41:45,780 --> 07:41:48,298
inside our inner classes that we have
10330
07:41:48,298 --> 07:41:50,400
here we can have we can access the
10331
07:41:50,400 --> 07:41:52,080
properties that are defined inside the
10332
07:41:52,080 --> 07:41:54,240
outer class list view in this case we
10333
07:41:54,240 --> 07:41:55,920
have a single property called items and
10334
07:41:55,920 --> 07:41:57,500
here we can type
10335
07:41:57,500 --> 07:41:59,760
items and notice that I can use the
10336
07:41:59,760 --> 07:42:01,620
items property which is defined in our
10337
07:42:01,620 --> 07:42:04,020
outer class list view we type position
10338
07:42:04,020 --> 07:42:05,940
and this is going to return
10339
07:42:05,940 --> 07:42:08,520
an item at this position which is passed
10340
07:42:08,520 --> 07:42:10,920
as an argument here to to do our
10341
07:42:10,920 --> 07:42:12,478
parameter position
10342
07:42:12,478 --> 07:42:16,680
and we go up here and we type Val
10343
07:42:16,680 --> 07:42:18,718
list View
10344
07:42:18,718 --> 07:42:19,798
because we're going to create an
10345
07:42:19,798 --> 07:42:22,878
instance for this View
10346
07:42:23,400 --> 07:42:26,360
type list View
10347
07:42:30,840 --> 07:42:32,718
now we need to pass an array to our
10348
07:42:32,718 --> 07:42:35,580
primary Constructor to the property item
10349
07:42:35,580 --> 07:42:38,580
so we type here array off and we Define
10350
07:42:38,580 --> 07:42:40,920
some generic names here let's say uh
10351
07:42:40,920 --> 07:42:42,360
name
10352
07:42:42,360 --> 07:42:45,020
one
10353
07:42:47,458 --> 07:42:51,298
and name four now how can we access the
10354
07:42:51,298 --> 07:42:53,340
display item function which is inside
10355
07:42:53,340 --> 07:42:56,400
our inner class
10356
07:42:56,400 --> 07:42:59,878
list item list view items this this
10357
07:42:59,878 --> 07:43:02,700
should be list view item not items so
10358
07:43:02,700 --> 07:43:04,920
let's change this go right click on it
10359
07:43:04,920 --> 07:43:07,080
and go to refactor and rename
10360
07:43:07,080 --> 07:43:11,000
let's call it the items
10361
07:43:16,680 --> 07:43:19,280
press enter
10362
07:43:19,500 --> 07:43:21,958
now to access the display item function
10363
07:43:21,958 --> 07:43:23,580
which is inside the original class list
10364
07:43:23,580 --> 07:43:26,240
view item
10365
07:43:27,298 --> 07:43:29,940
we type list View
10366
07:43:29,940 --> 07:43:34,080
so our instance our object list view Dot
10367
07:43:34,080 --> 07:43:37,740
list view item and then again that and
10368
07:43:37,740 --> 07:43:39,360
now we have our function display item
10369
07:43:39,360 --> 07:43:44,540
and we can pass here let's say yo
10370
07:43:44,878 --> 07:43:48,000
let's say uh two
10371
07:43:48,000 --> 07:43:51,260
now if you run this code
10372
07:43:52,500 --> 07:43:55,500
so get in the output name tree and this
10373
07:43:55,500 --> 07:43:57,478
is correct because it entries the other
10374
07:43:57,478 --> 07:43:59,280
index too
10375
07:43:59,280 --> 07:44:01,260
and this is how you can access the
10376
07:44:01,260 --> 07:44:03,058
display how this how you can access
10377
07:44:03,058 --> 07:44:05,820
generally a function inside then uh in
10378
07:44:05,820 --> 07:44:08,700
inside an inner class you just create an
10379
07:44:08,700 --> 07:44:10,740
instance of the outer class then you
10380
07:44:10,740 --> 07:44:11,700
call a
10381
07:44:11,700 --> 07:44:13,920
you call the name of the class of the
10382
07:44:13,920 --> 07:44:15,420
name of the inner class and then you
10383
07:44:15,420 --> 07:44:19,138
call the function Auto so see you in the
10384
07:44:19,138 --> 07:44:20,218
next video
10385
07:44:20,218 --> 07:44:22,080
so now it's time to do a challenge using
10386
07:44:22,080 --> 07:44:23,580
the knowledge that we have about object
10387
07:44:23,580 --> 07:44:25,200
oriented programming
10388
07:44:25,200 --> 07:44:26,940
and the challenge is to create a class
10389
07:44:26,940 --> 07:44:28,378
which is going to represent a bank
10390
07:44:28,378 --> 07:44:32,760
account of a person and this class is
10391
07:44:32,760 --> 07:44:34,740
going to have three properties the first
10392
07:44:34,740 --> 07:44:36,420
one is going to be called the account
10393
07:44:36,420 --> 07:44:38,280
name the second one is going to be
10394
07:44:38,280 --> 07:44:41,520
called the balance and the third one is
10395
07:44:41,520 --> 07:44:43,798
going to be called transactions and it
10396
07:44:43,798 --> 07:44:46,080
should be a multiple list
10397
07:44:46,080 --> 07:44:49,400
also you should create functions for
10398
07:44:49,400 --> 07:44:52,978
deposit and withdraw and also function
10399
07:44:52,978 --> 07:44:55,500
for calculating the balance and you also
10400
07:44:55,500 --> 07:44:58,280
you also should add the
10401
07:44:58,280 --> 07:45:00,958
the checks for checking to see if the
10402
07:45:00,958 --> 07:45:04,620
amount is uh when you deposit the amount
10403
07:45:04,620 --> 07:45:06,600
is greater than zero and all of that
10404
07:45:06,600 --> 07:45:09,540
stuff so do this Challenge and then
10405
07:45:09,540 --> 07:45:12,240
watch my solution
10406
07:45:12,240 --> 07:45:13,978
so
10407
07:45:13,978 --> 07:45:16,440
my solution is this I'm going to create
10408
07:45:16,440 --> 07:45:18,120
the class
10409
07:45:18,120 --> 07:45:20,638
and for this challenge you should also
10410
07:45:20,638 --> 07:45:22,680
have in mind the discussion that you had
10411
07:45:22,680 --> 07:45:26,458
about access modifiers so I'm gonna call
10412
07:45:26,458 --> 07:45:28,940
it account
10413
07:45:29,878 --> 07:45:31,978
and I'm going to Define here a property
10414
07:45:31,978 --> 07:45:33,840
called the
10415
07:45:33,840 --> 07:45:36,058
we're gonna be called the Val is going
10416
07:45:36,058 --> 07:45:37,978
to be called the count uh
10417
07:45:37,978 --> 07:45:40,020
name
10418
07:45:40,020 --> 07:45:43,638
and it's going to be of type string
10419
07:45:47,520 --> 07:45:50,660
of type string
10420
07:45:50,878 --> 07:45:53,580
and we put curly braces and inside the
10421
07:45:53,580 --> 07:45:56,820
class I'm gonna declare the next two
10422
07:45:56,820 --> 07:45:59,760
properties and those one uh first I'm
10423
07:45:59,760 --> 07:46:00,958
going to declare it without the access
10424
07:46:00,958 --> 07:46:03,180
modifiers because IntelliJ probably is
10425
07:46:03,180 --> 07:46:05,878
gonna figure out that we need to put
10426
07:46:05,878 --> 07:46:07,440
access modifiers on them so I'm gonna
10427
07:46:07,440 --> 07:46:11,160
type VAR balance
10428
07:46:11,160 --> 07:46:14,760
and I'm gonna assign 0 so this is the
10429
07:46:14,760 --> 07:46:17,820
default value and
10430
07:46:17,820 --> 07:46:21,780
VAR transactions
10431
07:46:26,878 --> 07:46:29,160
and this is going to be equals to a
10432
07:46:29,160 --> 07:46:31,638
multiple
10433
07:46:33,780 --> 07:46:36,798
or Multiple List
10434
07:46:38,458 --> 07:46:41,340
of int because this this is going to
10435
07:46:41,340 --> 07:46:43,860
store the transactions
10436
07:46:43,860 --> 07:46:46,138
so these are the three properties of our
10437
07:46:46,138 --> 07:46:48,718
class now I'm gonna create the first
10438
07:46:48,718 --> 07:46:50,160
function which is going to be called
10439
07:46:50,160 --> 07:46:51,840
deposit and this function is going to be
10440
07:46:51,840 --> 07:46:54,120
used to deposit
10441
07:46:54,120 --> 07:46:56,638
money inside our bank account and it's
10442
07:46:56,638 --> 07:46:58,378
going to have a parameter called the
10443
07:46:58,378 --> 07:47:00,120
mount
10444
07:47:00,120 --> 07:47:02,878
it's going to be of type
10445
07:47:02,878 --> 07:47:05,420
end
10446
07:47:05,820 --> 07:47:07,558
and first we need to check if this
10447
07:47:07,558 --> 07:47:09,420
amount is greater than zero because you
10448
07:47:09,420 --> 07:47:12,240
don't want to deposit uh negative value
10449
07:47:12,240 --> 07:47:15,180
so if amount is greater than zero
10450
07:47:15,180 --> 07:47:17,218
and we choose greater than zero because
10451
07:47:17,218 --> 07:47:20,520
we also don't want to deposit zero so
10452
07:47:20,520 --> 07:47:22,680
this is why I put greater than zero then
10453
07:47:22,680 --> 07:47:24,360
if it is greater than zero then I'm
10454
07:47:24,360 --> 07:47:26,900
gonna add that to our transaction
10455
07:47:26,900 --> 07:47:29,760
list that add
10456
07:47:29,760 --> 07:47:31,558
amount
10457
07:47:31,558 --> 07:47:35,400
and then I'm gonna say balance
10458
07:47:35,400 --> 07:47:37,500
I'm gonna update our balance I'm gonna
10459
07:47:37,500 --> 07:47:39,360
put plus equals
10460
07:47:39,360 --> 07:47:41,878
amount then we're gonna add the print
10461
07:47:41,878 --> 07:47:44,700
line which is gonna say
10462
07:47:44,700 --> 07:47:46,798
I'm going to put the quotation marks
10463
07:47:46,798 --> 07:47:48,540
dollar sign
10464
07:47:48,540 --> 07:47:51,798
I'm gonna type amount
10465
07:47:52,740 --> 07:47:55,920
I'm gonna put dollar sign this dot
10466
07:47:55,920 --> 07:47:57,360
balance
10467
07:47:57,360 --> 07:47:59,100
so I'm gonna output in the console what
10468
07:47:59,100 --> 07:48:01,860
is the balance but if the amount that is
10469
07:48:01,860 --> 07:48:04,378
passed to this function is
10470
07:48:04,378 --> 07:48:07,138
a negative sum I'm gonna go in the else
10471
07:48:07,138 --> 07:48:08,940
part and we're gonna say
10472
07:48:08,940 --> 07:48:11,040
another print line here
10473
07:48:11,040 --> 07:48:12,900
which is going to say cannot deposit
10474
07:48:12,900 --> 07:48:15,540
negative sums so we're gonna put here
10475
07:48:15,540 --> 07:48:18,298
okay cannot
10476
07:48:18,298 --> 07:48:21,298
deposit
10477
07:48:21,620 --> 07:48:24,958
negative sums
10478
07:48:24,958 --> 07:48:27,900
so this is our deposit function next I'm
10479
07:48:27,900 --> 07:48:30,058
going to create another function and
10480
07:48:30,058 --> 07:48:32,458
this is going to be called withdraw and
10481
07:48:32,458 --> 07:48:34,260
this is going to be used for withdraw
10482
07:48:34,260 --> 07:48:36,120
withdrawing mine so we type fun with
10483
07:48:36,120 --> 07:48:38,539
draw
10484
07:48:38,760 --> 07:48:41,700
and we put parenthesis and this is gonna
10485
07:48:41,700 --> 07:48:45,320
take a parameter called with draw wool
10486
07:48:45,320 --> 07:48:48,620
so uh
10487
07:48:50,280 --> 07:48:52,740
it should be like this
10488
07:48:52,740 --> 07:48:55,080
and it's going to be an integer we put
10489
07:48:55,080 --> 07:48:56,760
curly braces
10490
07:48:56,760 --> 07:48:59,359
now
10491
07:48:59,458 --> 07:49:01,138
it should be with
10492
07:49:01,138 --> 07:49:04,558
with here with
10493
07:49:04,558 --> 07:49:06,240
with
10494
07:49:06,240 --> 07:49:08,940
draw
10495
07:49:08,940 --> 07:49:10,620
now we need to convert this into
10496
07:49:10,620 --> 07:49:12,600
negative sum because now we're uh
10497
07:49:12,600 --> 07:49:15,240
withdrawing money from autocon so first
10498
07:49:15,240 --> 07:49:18,000
we need to check if
10499
07:49:18,000 --> 07:49:20,760
and we're going to put minus because I'm
10500
07:49:20,760 --> 07:49:22,860
gonna convert this into a negative sum
10501
07:49:22,860 --> 07:49:25,620
withdrawal is less than zero
10502
07:49:25,620 --> 07:49:28,080
so if this is a negative
10503
07:49:28,080 --> 07:49:30,958
sum which is going to be withdrawal
10504
07:49:30,958 --> 07:49:33,500
you're gonna
10505
07:49:34,920 --> 07:49:37,558
I'm gonna add to our transactions is
10506
07:49:37,558 --> 07:49:39,660
transactions that add and you also put
10507
07:49:39,660 --> 07:49:41,940
the minus sign so you put minus with
10508
07:49:41,940 --> 07:49:44,280
drawal because now we're adding now
10509
07:49:44,280 --> 07:49:47,458
we're uh we're taking money off the of
10510
07:49:47,458 --> 07:49:49,620
our account then I'm gonna put this that
10511
07:49:49,620 --> 07:49:52,160
balance
10512
07:49:53,820 --> 07:49:57,440
my uh Plus
10513
07:49:57,440 --> 07:50:01,080
equals minus
10514
07:50:01,080 --> 07:50:03,298
withdrawal because now we're taking
10515
07:50:03,298 --> 07:50:04,320
money
10516
07:50:04,320 --> 07:50:06,120
and then we're going to add again a
10517
07:50:06,120 --> 07:50:08,520
print line it's going to be this one so
10518
07:50:08,520 --> 07:50:11,420
I'm going to copy this
10519
07:50:14,940 --> 07:50:16,860
but now this is going to be the
10520
07:50:16,860 --> 07:50:19,760
withdrawal
10521
07:50:22,740 --> 07:50:25,378
else if
10522
07:50:25,378 --> 07:50:27,360
this is not
10523
07:50:27,360 --> 07:50:30,120
a negative sum so
10524
07:50:30,120 --> 07:50:33,200
I'm gonna put else
10525
07:50:35,458 --> 07:50:39,440
I'm gonna type put in the line
10526
07:50:42,298 --> 07:50:45,000
cannot
10527
07:50:45,000 --> 07:50:47,780
wait
10528
07:50:47,820 --> 07:50:50,360
draw
10529
07:50:51,180 --> 07:50:55,740
negative sums so if he tries to withdraw
10530
07:50:55,740 --> 07:50:57,180
a negative sum then we're going to say
10531
07:50:57,180 --> 07:50:59,160
cannot withdraw a negative sum here we
10532
07:50:59,160 --> 07:51:01,860
put the minus because we we're taking
10533
07:51:01,860 --> 07:51:04,500
money out of our account so we need to
10534
07:51:04,500 --> 07:51:06,058
check for uh that's why we're checking
10535
07:51:06,058 --> 07:51:09,440
here if this is less than zero
10536
07:51:15,500 --> 07:51:18,360
shouldn't be negative here
10537
07:51:18,360 --> 07:51:19,680
and we're going to create another
10538
07:51:19,680 --> 07:51:21,478
function and this is going to be called
10539
07:51:21,478 --> 07:51:25,320
calculate balance so we type here fun
10540
07:51:25,320 --> 07:51:27,058
Let's uh
10541
07:51:27,058 --> 07:51:29,160
bring this
10542
07:51:29,160 --> 07:51:32,218
fun calculate
10543
07:51:32,218 --> 07:51:34,138
calculate
10544
07:51:34,138 --> 07:51:36,860
balance
10545
07:51:39,780 --> 07:51:41,638
this is not going to take any parameters
10546
07:51:41,638 --> 07:51:45,138
and it's going to return an INT
10547
07:51:46,860 --> 07:51:50,718
put curly braces
10548
07:51:52,860 --> 07:51:56,160
and I'm gonna put this that balance to
10549
07:51:56,160 --> 07:51:57,600
zero because
10550
07:51:57,600 --> 07:51:59,340
I want to calculate the balance I'm
10551
07:51:59,340 --> 07:52:01,760
going to add the transactions to this
10552
07:52:01,760 --> 07:52:04,440
property balance so here we're gonna say
10553
07:52:04,440 --> 07:52:06,478
and here we're gonna loop we're gonna
10554
07:52:06,478 --> 07:52:08,760
type four
10555
07:52:08,760 --> 07:52:12,120
turn suction
10556
07:52:12,120 --> 07:52:16,978
in our list of transactions
10557
07:52:16,978 --> 07:52:18,478
so
10558
07:52:18,478 --> 07:52:20,398
for transaction in our list of
10559
07:52:20,398 --> 07:52:23,820
transaction I'm gonna type this Dot
10560
07:52:23,820 --> 07:52:25,620
balance
10561
07:52:25,620 --> 07:52:29,398
plus and equals transaction
10562
07:52:29,398 --> 07:52:31,740
so our transaction and then we're going
10563
07:52:31,740 --> 07:52:34,320
to return the balance at the end of our
10564
07:52:34,320 --> 07:52:37,378
Loop when we end we ended looping so
10565
07:52:37,378 --> 07:52:40,558
return this Dot
10566
07:52:40,558 --> 07:52:43,920
balance so this is our solution to the
10567
07:52:43,920 --> 07:52:46,740
challenge and let's um let's create an
10568
07:52:46,740 --> 07:52:48,360
account and let's pass some values here
10569
07:52:48,360 --> 07:52:50,160
so I'm going to type here
10570
07:52:50,160 --> 07:52:51,600
Val
10571
07:52:51,600 --> 07:52:54,739
Alex account
10572
07:52:55,080 --> 07:52:57,298
I'm gonna put equals
10573
07:52:57,298 --> 07:52:59,958
account
10574
07:53:00,000 --> 07:53:01,978
I'm gonna put now I'm gonna call the
10575
07:53:01,978 --> 07:53:03,360
primary Constructor so I'm going to type
10576
07:53:03,360 --> 07:53:05,840
here Alex
10577
07:53:06,958 --> 07:53:10,138
and we still use I'm still using my name
10578
07:53:10,138 --> 07:53:11,638
here but
10579
07:53:11,638 --> 07:53:13,080
so I'm going to type here Alex account
10580
07:53:13,080 --> 07:53:15,660
Dot
10581
07:53:15,660 --> 07:53:17,638
deposit so I'm going to deposit let's
10582
07:53:17,638 --> 07:53:20,218
say one thousand
10583
07:53:20,218 --> 07:53:22,620
and uh
10584
07:53:22,620 --> 07:53:26,458
I'm gonna type OLX account that withdraw
10585
07:53:26,458 --> 07:53:29,040
I'm gonna withdraw
10586
07:53:29,040 --> 07:53:33,120
500 then I'm gonna put Alex account
10587
07:53:33,120 --> 07:53:35,458
and I'm gonna put uh
10588
07:53:35,458 --> 07:53:38,760
deposit the negative sign let's say
10589
07:53:38,760 --> 07:53:41,520
minus 20.
10590
07:53:41,520 --> 07:53:45,478
and Alex account Dot
10591
07:53:45,478 --> 07:53:48,180
that we draw also negative sign let's
10592
07:53:48,180 --> 07:53:50,478
say minus
10593
07:53:50,478 --> 07:53:52,378
100.
10594
07:53:52,378 --> 07:53:55,940
now if you're on this code
10595
07:53:56,218 --> 07:53:58,398
foreign
10596
07:54:06,320 --> 07:54:09,540
deposit balance is now 1000 so we have
10597
07:54:09,540 --> 07:54:12,420
our call card called from our deposit
10598
07:54:12,420 --> 07:54:13,440
function
10599
07:54:13,440 --> 07:54:16,280
then we have
10600
07:54:19,680 --> 07:54:22,500
so I should I didn't change the text
10601
07:54:22,500 --> 07:54:25,558
here so that's why it's saying uh
10602
07:54:25,558 --> 07:54:30,958
this should say with uh withdrawn so uh
10603
07:54:30,958 --> 07:54:33,240
sorry let's change this to
10604
07:54:33,240 --> 07:54:34,620
with
10605
07:54:34,620 --> 07:54:37,397
drone
10606
07:54:38,940 --> 07:54:42,680
so let's run our code again
10607
07:54:48,058 --> 07:54:50,700
so get 1000 deposit balance is now one
10608
07:54:50,700 --> 07:54:53,760
thousand now we get our uh correct
10609
07:54:53,760 --> 07:54:55,740
output to get the
10610
07:54:55,740 --> 07:54:59,580
then 500 withdrawn so get our call the
10611
07:54:59,580 --> 07:55:02,100
call from our withdrawn function we
10612
07:55:02,100 --> 07:55:05,340
withdrawn balance is now uh 500 then
10613
07:55:05,340 --> 07:55:07,138
then you get cannot deposit negative
10614
07:55:07,138 --> 07:55:09,420
sums cannot withdraw negative sounds
10615
07:55:09,420 --> 07:55:11,700
because we passed the negative numbers
10616
07:55:11,700 --> 07:55:13,558
to our deposits and withdrawal function
10617
07:55:13,558 --> 07:55:16,978
and here we're checking to see if those
10618
07:55:16,978 --> 07:55:20,638
are negative or positive so this is our
10619
07:55:20,638 --> 07:55:24,180
solution to our
10620
07:55:24,180 --> 07:55:26,280
to our Challenge and I hope that you
10621
07:55:26,280 --> 07:55:28,740
enjoyed this and that you solved this
10622
07:55:28,740 --> 07:55:32,820
and this is not again the best solution
10623
07:55:32,820 --> 07:55:34,260
this is just
10624
07:55:34,260 --> 07:55:36,478
my solution probably you'll find you
10625
07:55:36,478 --> 07:55:38,718
figure out a better solution
10626
07:55:38,718 --> 07:55:42,120
maybe your concern better and more
10627
07:55:42,120 --> 07:55:44,458
concise solution than this actually I
10628
07:55:44,458 --> 07:55:46,080
forgot before we end our video Let's
10629
07:55:46,080 --> 07:55:48,298
actually called also our calculate
10630
07:55:48,298 --> 07:55:51,600
balance function so I'm gonna type here
10631
07:55:51,600 --> 07:55:53,100
uh
10632
07:55:53,100 --> 07:55:56,000
Alex account
10633
07:55:56,160 --> 07:55:58,440
dot calculate balance and this is going
10634
07:55:58,440 --> 07:56:00,540
to return an integer so I'm gonna type
10635
07:56:00,540 --> 07:56:03,240
here Val because that function returns
10636
07:56:03,240 --> 07:56:04,798
something so I need to capture that in a
10637
07:56:04,798 --> 07:56:06,478
variable so I'm going to type here valve
10638
07:56:06,478 --> 07:56:08,040
balance
10639
07:56:08,040 --> 07:56:10,740
equals Alex account
10640
07:56:10,740 --> 07:56:14,600
so no other print line here
10641
07:56:15,180 --> 07:56:17,638
and there's gonna say
10642
07:56:17,638 --> 07:56:19,860
balance
10643
07:56:19,860 --> 07:56:22,740
is dollar sign
10644
07:56:22,740 --> 07:56:25,398
balance
10645
07:56:25,398 --> 07:56:27,420
format the code
10646
07:56:27,420 --> 07:56:30,978
now if we run this code
10647
07:56:33,120 --> 07:56:36,250
[Music]
10648
07:56:36,260 --> 07:56:40,080
now get our previous output to 1000
10649
07:56:40,080 --> 07:56:42,478
deposit balance now one thousand five
10650
07:56:42,478 --> 07:56:45,780
hundred withdrawn balance is now 500
10651
07:56:45,780 --> 07:56:47,760
then cannot deposit negative sums and
10652
07:56:47,760 --> 07:56:49,320
cannot withdraw negative sounds and then
10653
07:56:49,320 --> 07:56:52,798
get balances now 500 so our code works
10654
07:56:52,798 --> 07:56:53,940
well
10655
07:56:53,940 --> 07:56:57,058
so this is our code
10656
07:56:57,058 --> 07:56:59,458
one more thing that I forgot is that I
10657
07:56:59,458 --> 07:57:00,780
said at the beginning of the video that
10658
07:57:00,780 --> 07:57:02,700
you should have in mind the discussion
10659
07:57:02,700 --> 07:57:04,978
that we had about access modifiers when
10660
07:57:04,978 --> 07:57:06,420
doing this challenge
10661
07:57:06,420 --> 07:57:09,600
and I forgot to to add the access
10662
07:57:09,600 --> 07:57:11,700
modifiers for balance and transactions
10663
07:57:11,700 --> 07:57:14,520
because now what can I do for balance I
10664
07:57:14,520 --> 07:57:16,620
can
10665
07:57:16,620 --> 07:57:20,239
use our Alexa account
10666
07:57:20,760 --> 07:57:23,638
s and I can type here balance and now
10667
07:57:23,638 --> 07:57:26,280
because that is uh actually that is not
10668
07:57:26,280 --> 07:57:28,080
really probably because when I talked
10669
07:57:28,080 --> 07:57:31,138
about uh get us unsetters we said that
10670
07:57:31,138 --> 07:57:33,120
that when you access a property you
10671
07:57:33,120 --> 07:57:34,740
always access access that property
10672
07:57:34,740 --> 07:57:36,360
through get a run Center so we never
10673
07:57:36,360 --> 07:57:40,920
access directly the getter and the
10674
07:57:40,920 --> 07:57:42,780
property but uh
10675
07:57:42,780 --> 07:57:46,620
still you should not make that that
10676
07:57:46,620 --> 07:57:49,320
public because by default if you don't
10677
07:57:49,320 --> 07:57:51,718
provide an access modifier they are they
10678
07:57:51,718 --> 07:57:54,780
are by default public so if I type here
10679
07:57:54,780 --> 07:57:57,600
now account balance as you can see I can
10680
07:57:57,600 --> 07:58:00,958
call that and I can type here 1000 and I
10681
07:58:00,958 --> 07:58:03,478
can deposit now I can change the balance
10682
07:58:03,478 --> 07:58:06,240
of the account directly from here and if
10683
07:58:06,240 --> 07:58:07,860
you hover over here
10684
07:58:07,860 --> 07:58:11,100
uh IntelliJ figure out that you should
10685
07:58:11,100 --> 07:58:13,740
make this private so make this private
10686
07:58:13,740 --> 07:58:16,620
the balance and also the transactions
10687
07:58:16,620 --> 07:58:18,360
because
10688
07:58:18,360 --> 07:58:20,458
you don't use them inside the primary
10689
07:58:20,458 --> 07:58:22,200
Constructor and they are used only
10690
07:58:22,200 --> 07:58:24,360
through the to the functions that we
10691
07:58:24,360 --> 07:58:27,058
defined here the because of that you
10692
07:58:27,058 --> 07:58:29,218
should you should declare them as
10693
07:58:29,218 --> 07:58:31,500
private because you use them only
10694
07:58:31,500 --> 07:58:33,780
through the function that you declared
10695
07:58:33,780 --> 07:58:36,360
inside the class so declare them as
10696
07:58:36,360 --> 07:58:38,760
private and now you if you hover over
10697
07:58:38,760 --> 07:58:40,920
here it says you cannot access balance
10698
07:58:40,920 --> 07:58:42,958
because it's private in account and now
10699
07:58:42,958 --> 07:58:44,398
we cannot change the balance from
10700
07:58:44,398 --> 07:58:46,398
outside the class we can only
10701
07:58:46,398 --> 07:58:50,120
deposit or withdraw uh
10702
07:58:50,120 --> 07:58:53,760
sums of money from our bank account
10703
07:58:53,760 --> 07:58:56,280
by interacting only with the functions
10704
07:58:56,280 --> 07:58:59,100
and not by calling the property directly
10705
07:58:59,100 --> 07:59:02,520
and assigning a new balance to to the
10706
07:59:02,520 --> 07:59:05,520
property so make them private and this
10707
07:59:05,520 --> 07:59:09,958
way you cannot uh you don't uh you don't
10708
07:59:09,958 --> 07:59:13,260
access the property outside of the class
10709
07:59:13,260 --> 07:59:15,478
and you change data directly the the
10710
07:59:15,478 --> 07:59:17,218
value of the property again it's not
10711
07:59:17,218 --> 07:59:19,920
really direct because the code that you
10712
07:59:19,920 --> 07:59:22,138
had previously so this code actually is
10713
07:59:22,138 --> 07:59:24,420
calling the setter here but still you
10714
07:59:24,420 --> 07:59:26,700
can change the value of
10715
07:59:26,700 --> 07:59:28,860
the balance but now because we made that
10716
07:59:28,860 --> 07:59:33,058
private and the the compiler is smart
10717
07:59:33,058 --> 07:59:35,520
enough to figure out that because they
10718
07:59:35,520 --> 07:59:37,200
are not declared inside the primary
10719
07:59:37,200 --> 07:59:39,898
Concepts and they are only used you only
10720
07:59:39,898 --> 07:59:42,780
interact with them with the deposit and
10721
07:59:42,780 --> 07:59:43,920
withdraw
10722
07:59:43,920 --> 07:59:46,920
and uh calculate balance functions so we
10723
07:59:46,920 --> 07:59:48,840
don't use them outside you don't have
10724
07:59:48,840 --> 07:59:50,280
them also declaring the primary
10725
07:59:50,280 --> 07:59:52,200
Constructor it knows that you should
10726
07:59:52,200 --> 07:59:54,000
make them private because you interact
10727
07:59:54,000 --> 07:59:56,160
them only with the functions and now we
10728
07:59:56,160 --> 07:59:58,260
have this underline because it's private
10729
07:59:58,260 --> 07:59:59,940
so we cannot change this balance now
10730
07:59:59,940 --> 08:00:02,458
from outside the class we can only
10731
08:00:02,458 --> 08:00:05,218
interact them again using the functions
10732
08:00:05,218 --> 08:00:08,280
so I'm gonna delete this and
10733
08:00:08,280 --> 08:00:10,860
now see you in the next video
10734
08:00:10,860 --> 08:00:12,478
so let's delete this
10735
08:00:12,478 --> 08:00:13,978
so actually I'm gonna let it there
10736
08:00:13,978 --> 08:00:15,120
because
10737
08:00:15,120 --> 08:00:18,120
it's uh good to see so now it's time to
10738
08:00:18,120 --> 08:00:20,218
start a discussion about inheritance so
10739
08:00:20,218 --> 08:00:23,100
what is inheritance inheritance is the
10740
08:00:23,100 --> 08:00:24,898
concept in the quarterly programming
10741
08:00:24,898 --> 08:00:27,058
context with which you can create a
10742
08:00:27,058 --> 08:00:29,040
class using another class so in other
10743
08:00:29,040 --> 08:00:31,138
words you can get all the functions and
10744
08:00:31,138 --> 08:00:32,878
properties that have that the class
10745
08:00:32,878 --> 08:00:35,700
already has in your new class and in
10746
08:00:35,700 --> 08:00:37,440
addition to the getting all the
10747
08:00:37,440 --> 08:00:40,080
functions in properties in uh in your
10748
08:00:40,080 --> 08:00:42,958
new class you can add some new functions
10749
08:00:42,958 --> 08:00:44,760
some new properties which are specific
10750
08:00:44,760 --> 08:00:46,680
to this new class but they aren't for
10751
08:00:46,680 --> 08:00:49,138
the also this the class from which you
10752
08:00:49,138 --> 08:00:51,180
are inheriting is called The Base Class
10753
08:00:51,180 --> 08:00:53,700
the parent class or the super class
10754
08:00:53,700 --> 08:00:56,340
and you might be wondering why is this
10755
08:00:56,340 --> 08:00:58,978
useful why you want to do this
10756
08:00:58,978 --> 08:01:01,680
let's say that you have a very big class
10757
08:01:01,680 --> 08:01:05,580
which performs very hard task and has a
10758
08:01:05,580 --> 08:01:07,740
lot of functions in coding but you want
10759
08:01:07,740 --> 08:01:10,620
to create a new class which is slightly
10760
08:01:10,620 --> 08:01:13,080
different from this class what you can
10761
08:01:13,080 --> 08:01:15,420
do is you can get all that code copied
10762
08:01:15,420 --> 08:01:17,458
in the new class and then you can add
10763
08:01:17,458 --> 08:01:19,620
your new functions or new your new
10764
08:01:19,620 --> 08:01:23,718
Behavior to this class but dust is not
10765
08:01:23,718 --> 08:01:25,860
recommended because you have duplicate
10766
08:01:25,860 --> 08:01:28,500
code what you can do instead is you can
10767
08:01:28,500 --> 08:01:30,540
inherit all the functions and properties
10768
08:01:30,540 --> 08:01:33,660
which this big class already has and in
10769
08:01:33,660 --> 08:01:35,940
your new class you can add the new
10770
08:01:35,940 --> 08:01:38,280
functionality the new Behavior which is
10771
08:01:38,280 --> 08:01:40,138
specific to this new class but it's not
10772
08:01:40,138 --> 08:01:42,240
for the base class so in this way you
10773
08:01:42,240 --> 08:01:45,898
can uh you you avoid the duplicated code
10774
08:01:45,898 --> 08:01:47,940
and you increase the code reusability
10775
08:01:47,940 --> 08:01:50,340
and for that I'm going to show you in an
10776
08:01:50,340 --> 08:01:52,020
example and I'm going to create two
10777
08:01:52,020 --> 08:01:54,420
classes one is going to be called car
10778
08:01:54,420 --> 08:01:57,298
and one is going to be plain so we
10779
08:01:57,298 --> 08:02:00,420
create here a class we go down here
10780
08:02:00,420 --> 08:02:02,100
we type class
10781
08:02:02,100 --> 08:02:04,200
I'm going to call it car
10782
08:02:04,200 --> 08:02:06,120
is going to have some properties the
10783
08:02:06,120 --> 08:02:07,260
first one is going to be called name
10784
08:02:07,260 --> 08:02:08,760
this is going to be the brand name of
10785
08:02:08,760 --> 08:02:11,057
the car
10786
08:02:11,160 --> 08:02:13,378
the second one is going to be color also
10787
08:02:13,378 --> 08:02:15,860
string
10788
08:02:20,580 --> 08:02:23,820
engines the number of engines that this
10789
08:02:23,820 --> 08:02:25,138
car has
10790
08:02:25,138 --> 08:02:27,000
gonna be an integer
10791
08:02:27,000 --> 08:02:28,978
and Val
10792
08:02:28,978 --> 08:02:31,740
this is going to be
10793
08:02:31,740 --> 08:02:34,138
doors so we're gonna store the number of
10794
08:02:34,138 --> 08:02:35,340
doors
10795
08:02:35,340 --> 08:02:36,718
all right
10796
08:02:36,718 --> 08:02:38,398
we put curly braces because I'm gonna
10797
08:02:38,398 --> 08:02:41,218
add some functions to this class
10798
08:02:41,218 --> 08:02:44,280
to function actually fun
10799
08:02:44,280 --> 08:02:46,878
move
10800
08:02:48,840 --> 08:02:51,740
and uh
10801
08:02:52,860 --> 08:02:54,600
fun
10802
08:02:54,600 --> 08:02:56,897
stop
10803
08:02:59,100 --> 08:03:02,120
and here you're gonna say
10804
08:03:03,718 --> 08:03:06,620
print line
10805
08:03:08,280 --> 08:03:11,398
quotation marks dollar sign
10806
08:03:11,398 --> 08:03:13,500
the name of the car
10807
08:03:13,500 --> 08:03:16,500
is moving
10808
08:03:16,500 --> 08:03:18,360
similarly
10809
08:03:18,360 --> 08:03:20,218
down here in the stop we're going to say
10810
08:03:20,218 --> 08:03:23,240
the car has stopped
10811
08:03:23,340 --> 08:03:25,820
car
10812
08:03:25,920 --> 08:03:28,458
has
10813
08:03:31,020 --> 08:03:33,058
and we're gonna create a another class
10814
08:03:33,058 --> 08:03:36,020
called plane
10815
08:03:37,920 --> 08:03:40,320
which is going to have also Val
10816
08:03:40,320 --> 08:03:42,660
name
10817
08:03:42,660 --> 08:03:46,580
we're gonna be a string a vowel color
10818
08:03:46,580 --> 08:03:48,540
also string
10819
08:03:48,540 --> 08:03:52,978
Val engines an integer and the valve
10820
08:03:52,978 --> 08:03:55,080
doors
10821
08:03:55,080 --> 08:03:57,740
unint
10822
08:03:58,440 --> 08:04:00,600
put curly braces and we Define those two
10823
08:04:00,600 --> 08:04:02,340
function we're going to copy those
10824
08:04:02,340 --> 08:04:04,500
I'm gonna paste it here
10825
08:04:04,500 --> 08:04:06,958
so now what we did here is that we
10826
08:04:06,958 --> 08:04:09,240
created two classes
10827
08:04:09,240 --> 08:04:11,520
they both both have
10828
08:04:11,520 --> 08:04:13,620
uh properties for the primary
10829
08:04:13,620 --> 08:04:16,558
Constructors and but as you probably
10830
08:04:16,558 --> 08:04:18,898
already noticed we have the same
10831
08:04:18,898 --> 08:04:21,058
properties we have the name here we have
10832
08:04:21,058 --> 08:04:22,558
the name there we have the color there
10833
08:04:22,558 --> 08:04:24,000
we have the color here the color there
10834
08:04:24,000 --> 08:04:26,520
the engines also we have here and there
10835
08:04:26,520 --> 08:04:29,100
and we also have those functions which
10836
08:04:29,100 --> 08:04:32,700
are almost actually are the same so we
10837
08:04:32,700 --> 08:04:34,320
have what I said at the beginning
10838
08:04:34,320 --> 08:04:37,020
duplicated code so it doesn't make sense
10839
08:04:37,020 --> 08:04:40,260
to put this code here another
10840
08:04:40,260 --> 08:04:42,718
another way to do this is to create a
10841
08:04:42,718 --> 08:04:44,878
base class a generic class called
10842
08:04:44,878 --> 08:04:49,080
vehicle which is uh and usually
10843
08:04:49,080 --> 08:04:51,058
you create the generic class when you
10844
08:04:51,058 --> 08:04:52,200
know that
10845
08:04:52,200 --> 08:04:56,700
several classes we are gonna share those
10846
08:04:56,700 --> 08:04:59,580
properties and those behaviors so a car
10847
08:04:59,580 --> 08:05:02,218
in the plane both are Vehicles so we can
10848
08:05:02,218 --> 08:05:05,040
put here instead of declaring the the
10849
08:05:05,040 --> 08:05:08,280
name and the caller inside the our
10850
08:05:08,280 --> 08:05:10,978
inside our classes car and plane you can
10851
08:05:10,978 --> 08:05:14,700
put this in a vague vehicle uh
10852
08:05:14,700 --> 08:05:18,360
class in a base class and we can inherit
10853
08:05:18,360 --> 08:05:20,458
those properties in our classes instead
10854
08:05:20,458 --> 08:05:23,940
of declaring them your inside our car
10855
08:05:23,940 --> 08:05:26,340
and playing class so we can put here Val
10856
08:05:26,340 --> 08:05:29,340
name
10857
08:05:29,700 --> 08:05:31,740
it's going to be a string
10858
08:05:31,740 --> 08:05:35,040
and the Val
10859
08:05:35,040 --> 08:05:38,160
color also string
10860
08:05:38,160 --> 08:05:41,958
now I can copy those two
10861
08:05:42,298 --> 08:05:44,760
let's put curly braces here and I can
10862
08:05:44,760 --> 08:05:47,340
paste it paste it here so
10863
08:05:47,340 --> 08:05:51,058
now I can delete those those two
10864
08:05:51,058 --> 08:05:54,240
functions from our classes
10865
08:05:54,240 --> 08:05:57,718
and what we can do now is we can inherit
10866
08:05:57,718 --> 08:06:00,180
those properties so we can delete those
10867
08:06:00,180 --> 08:06:02,160
properties that we have here
10868
08:06:02,160 --> 08:06:04,440
we cannot we will as you'll see you'll
10869
08:06:04,440 --> 08:06:06,660
need to declare them as parameters but
10870
08:06:06,660 --> 08:06:07,978
uh
10871
08:06:07,978 --> 08:06:09,840
they are not they're not going to be as
10872
08:06:09,840 --> 08:06:12,420
properties in any they're not going to
10873
08:06:12,420 --> 08:06:13,740
be as properties here
10874
08:06:13,740 --> 08:06:15,058
now
10875
08:06:15,058 --> 08:06:17,280
what we can do is that we can inherit
10876
08:06:17,280 --> 08:06:20,638
this shared those share the functions
10877
08:06:20,638 --> 08:06:23,040
and those shared properties because both
10878
08:06:23,040 --> 08:06:25,378
the car and the plane
10879
08:06:25,378 --> 08:06:28,798
have a function to move have a function
10880
08:06:28,798 --> 08:06:31,500
to stop the baby they both share a name
10881
08:06:31,500 --> 08:06:34,020
they both share a color so they both
10882
08:06:34,020 --> 08:06:37,500
share those properties and uh functions
10883
08:06:37,500 --> 08:06:39,558
but uh
10884
08:06:39,558 --> 08:06:42,180
as we did previously is to put them
10885
08:06:42,180 --> 08:06:44,878
inside the the the classes would not
10886
08:06:44,878 --> 08:06:46,500
it's not good because we have a
10887
08:06:46,500 --> 08:06:49,260
duplicated code and in this way we put
10888
08:06:49,260 --> 08:06:50,458
this code here
10889
08:06:50,458 --> 08:06:52,740
and now we can inherit this code so we
10890
08:06:52,740 --> 08:06:56,218
can have this code directly you can you
10891
08:06:56,218 --> 08:06:59,840
can have this code the being uh
10892
08:06:59,840 --> 08:07:02,760
being accessed inside the car and plane
10893
08:07:02,760 --> 08:07:05,160
without declaring them in our classes so
10894
08:07:05,160 --> 08:07:07,558
to inherit from our class first you need
10895
08:07:07,558 --> 08:07:09,898
to Mark the class from which you want to
10896
08:07:09,898 --> 08:07:13,200
inherit as open because by default they
10897
08:07:13,200 --> 08:07:15,120
are they you cannot inherit from a class
10898
08:07:15,120 --> 08:07:19,320
so if I enter in head from a class we go
10899
08:07:19,320 --> 08:07:21,780
at the enclosing impurities of our
10900
08:07:21,780 --> 08:07:24,620
primary Constructor
10901
08:07:24,780 --> 08:07:27,420
we put colon and we type the name of the
10902
08:07:27,420 --> 08:07:29,218
class from which we want to inherit and
10903
08:07:29,218 --> 08:07:31,378
in this case it's going to be vehicle
10904
08:07:31,378 --> 08:07:32,520
now
10905
08:07:32,520 --> 08:07:34,740
if you hover over here
10906
08:07:34,740 --> 08:07:37,200
it says that this type has a Constructor
10907
08:07:37,200 --> 08:07:40,440
and thus mods B initialized here and and
10908
08:07:40,440 --> 08:07:42,478
next you say this type is final so it
10909
08:07:42,478 --> 08:07:45,120
cannot be inherited from and that means
10910
08:07:45,120 --> 08:07:46,740
that we cannot inherit from this class
10911
08:07:46,740 --> 08:07:49,860
unless we mark this class with the open
10912
08:07:49,860 --> 08:07:52,798
keyword in front of it so we need to put
10913
08:07:52,798 --> 08:07:55,320
here open
10914
08:07:55,320 --> 08:07:58,440
and now if we hover over here
10915
08:07:58,440 --> 08:08:00,718
now we don't have that error you only
10916
08:08:00,718 --> 08:08:02,398
have this here which is this type has a
10917
08:08:02,398 --> 08:08:05,100
Constructor and dust must be initialized
10918
08:08:05,100 --> 08:08:08,458
here and that basically means that it
10919
08:08:08,458 --> 08:08:11,760
says hey I'm trying to use this vehicle
10920
08:08:11,760 --> 08:08:14,820
class I'm trying to but in order to use
10921
08:08:14,820 --> 08:08:16,620
it I mean I need to initialize this
10922
08:08:16,620 --> 08:08:19,020
vehicle class so we need to pass some
10923
08:08:19,020 --> 08:08:21,298
values to the
10924
08:08:21,298 --> 08:08:23,160
construct of the primary Constructor
10925
08:08:23,160 --> 08:08:24,420
that is defined here you need to pass
10926
08:08:24,420 --> 08:08:26,638
some values to the name and to color
10927
08:08:26,638 --> 08:08:29,520
and to do that we Define here name we
10928
08:08:29,520 --> 08:08:31,620
type name
10929
08:08:31,620 --> 08:08:34,620
actually dot let's press Ctrl Z
10930
08:08:34,620 --> 08:08:38,280
let's put a space and we type name
10931
08:08:38,280 --> 08:08:40,200
we put
10932
08:08:40,200 --> 08:08:43,740
comma then we type
10933
08:08:43,740 --> 08:08:46,160
color
10934
08:08:47,100 --> 08:08:49,378
and notice that we don't have the VAR or
10935
08:08:49,378 --> 08:08:51,120
the Vault keyword in front of them that
10936
08:08:51,120 --> 08:08:54,739
is because the those
10937
08:08:54,920 --> 08:08:57,120
parameters that would have here because
10938
08:08:57,120 --> 08:08:58,440
they are parameters because they don't
10939
08:08:58,440 --> 08:09:01,200
have the value of our keyword they they
10940
08:09:01,200 --> 08:09:04,080
uh they they they have to be declared
10941
08:09:04,080 --> 08:09:05,760
only as parameters because the
10942
08:09:05,760 --> 08:09:08,218
properties are going to be declared and
10943
08:09:08,218 --> 08:09:09,780
they are declared by the primary
10944
08:09:09,780 --> 08:09:11,760
Constructor of the base class from which
10945
08:09:11,760 --> 08:09:14,660
we are inheriting from
10946
08:09:14,660 --> 08:09:18,478
and because of that we don't we we don't
10947
08:09:18,478 --> 08:09:20,100
we don't need to declare the property
10948
08:09:20,100 --> 08:09:22,200
again here as you can see if I put the
10949
08:09:22,200 --> 08:09:24,420
VAR keyword we have an error says name
10950
08:09:24,420 --> 08:09:27,600
hyzer anyway what that means that we
10951
08:09:27,600 --> 08:09:29,100
cannot have the property declared here
10952
08:09:29,100 --> 08:09:30,780
because the properties uh already
10953
08:09:30,780 --> 08:09:32,700
declared inside the Base Class inside
10954
08:09:32,700 --> 08:09:34,320
the vehicle class from which we are
10955
08:09:34,320 --> 08:09:36,958
inheriting from what
10956
08:09:36,958 --> 08:09:39,478
we can do here is Define the parameters
10957
08:09:39,478 --> 08:09:41,940
and the value that is passed to those
10958
08:09:41,940 --> 08:09:43,798
parameters so the primary Constructor of
10959
08:09:43,798 --> 08:09:46,320
the class cards are going to be passed
10960
08:09:46,320 --> 08:09:48,958
to the to the vehicle primary
10961
08:09:48,958 --> 08:09:51,000
Constructor so hero put the primary
10962
08:09:51,000 --> 08:09:52,680
Constructor so you put two parentheses
10963
08:09:52,680 --> 08:09:55,620
and we type name
10964
08:09:55,620 --> 08:09:58,920
and this name is this one from here
10965
08:09:58,920 --> 08:10:01,440
so we type name then we put comma
10966
08:10:01,440 --> 08:10:03,058
because we need to pass a value for the
10967
08:10:03,058 --> 08:10:05,040
second one for the color and the type
10968
08:10:05,040 --> 08:10:06,240
color
10969
08:10:06,240 --> 08:10:08,940
and this color is this color here
10970
08:10:08,940 --> 08:10:11,840
so we type color
10971
08:10:12,298 --> 08:10:13,740
now
10972
08:10:13,740 --> 08:10:17,458
if you go up here let's say for let's
10973
08:10:17,458 --> 08:10:20,040
look first as the actually let's also
10974
08:10:20,040 --> 08:10:24,780
inherit from inherit inherit also one in
10975
08:10:24,780 --> 08:10:27,180
on the plane class so we put colon also
10976
08:10:27,180 --> 08:10:29,340
here
10977
08:10:29,340 --> 08:10:31,620
we type vehicle
10978
08:10:31,620 --> 08:10:33,898
and now we need to also to Define here
10979
08:10:33,898 --> 08:10:37,080
the parameters the name and the color
10980
08:10:37,080 --> 08:10:40,160
so we type name
10981
08:10:40,200 --> 08:10:41,878
and at the time you can see that
10982
08:10:41,878 --> 08:10:43,680
IntelliJ has given us some hints so if I
10983
08:10:43,680 --> 08:10:45,360
type here color it says that color
10984
08:10:45,360 --> 08:10:48,180
string so it's very helpful
10985
08:10:48,180 --> 08:10:50,638
put colon we pass those values to
10986
08:10:50,638 --> 08:10:53,040
primary Constructor of the base class so
10987
08:10:53,040 --> 08:10:56,120
we type name
10988
08:10:56,878 --> 08:10:59,360
color
10989
08:11:02,398 --> 08:11:04,378
and now if I go up here on I create some
10990
08:11:04,378 --> 08:11:06,600
instances of our car and playing class
10991
08:11:06,600 --> 08:11:10,500
so we type here Val car let's put equals
10992
08:11:10,500 --> 08:11:13,520
this type card
10993
08:11:13,680 --> 08:11:18,058
let's choose for the name BMV
10994
08:11:18,298 --> 08:11:20,638
and the
10995
08:11:20,638 --> 08:11:25,280
for the color let's say red
10996
08:11:28,020 --> 08:11:31,200
for the engines one for the doors four
10997
08:11:31,200 --> 08:11:33,058
now I'm going to create also an object
10998
08:11:33,058 --> 08:11:35,580
with our plane so let's press Ctrl alt
10999
08:11:35,580 --> 08:11:37,320
form on the code
11000
08:11:37,320 --> 08:11:39,600
valve plane
11001
08:11:39,600 --> 08:11:43,320
equals plane so our class
11002
08:11:43,320 --> 08:11:45,958
we type here
11003
08:11:45,958 --> 08:11:48,798
for the name
11004
08:11:52,138 --> 08:11:55,320
for the color let's say
11005
08:11:55,320 --> 08:11:58,860
or white
11006
08:11:59,940 --> 08:12:02,040
and
11007
08:12:02,040 --> 08:12:04,580
blue
11008
08:12:05,340 --> 08:12:08,000
and
11009
08:12:08,398 --> 08:12:11,340
for the engines for
11010
08:12:11,340 --> 08:12:14,458
and for the doors let's see
11011
08:12:14,458 --> 08:12:17,218
also for
11012
08:12:17,218 --> 08:12:20,878
now if I go down here and I
11013
08:12:20,878 --> 08:12:24,420
type here car dot
11014
08:12:24,420 --> 08:12:26,280
if you look here you see that you see
11015
08:12:26,280 --> 08:12:28,978
that we have the function stop and the
11016
08:12:28,978 --> 08:12:31,500
function move but we didn't declare them
11017
08:12:31,500 --> 08:12:34,138
here you deleted them previously but
11018
08:12:34,138 --> 08:12:36,360
because we are inheriting from the
11019
08:12:36,360 --> 08:12:39,420
vehicle class because when you are in
11020
08:12:39,420 --> 08:12:43,200
editing the properties and the functions
11021
08:12:43,200 --> 08:12:44,940
from the class from which you are
11022
08:12:44,940 --> 08:12:48,298
inheriting from are part of the new
11023
08:12:48,298 --> 08:12:51,360
classes so the the the
11024
08:12:51,360 --> 08:12:53,520
move function in the stop function they
11025
08:12:53,520 --> 08:12:55,320
are now part of our color class
11026
08:12:55,320 --> 08:12:58,620
similarly it's the same in the same the
11027
08:12:58,620 --> 08:13:02,458
same here the the the the move and stop
11028
08:13:02,458 --> 08:13:05,878
are uh are part of the class plane so if
11029
08:13:05,878 --> 08:13:08,100
I type here car
11030
08:13:08,100 --> 08:13:11,057
dot move
11031
08:13:11,100 --> 08:13:16,920
and car dot stop and then plane dot move
11032
08:13:16,920 --> 08:13:19,200
and plane so look like then now I can
11033
08:13:19,200 --> 08:13:21,780
call them like they are declared inside
11034
08:13:21,780 --> 08:13:24,420
the our current plane but they are not
11035
08:13:24,420 --> 08:13:26,218
there but because we are inheriting they
11036
08:13:26,218 --> 08:13:27,958
are part of the class so you can think
11037
08:13:27,958 --> 08:13:29,280
of
11038
08:13:29,280 --> 08:13:31,320
you can think of this thing that we have
11039
08:13:31,320 --> 08:13:35,040
here as a single entity so it acts like
11040
08:13:35,040 --> 08:13:36,540
a single entity
11041
08:13:36,540 --> 08:13:39,978
so now if you run the squad
11042
08:13:43,138 --> 08:13:45,660
we get in the output band there is
11043
08:13:45,660 --> 08:13:47,398
moving then they have stopped bearing is
11044
08:13:47,398 --> 08:13:49,860
moving doing has stopped
11045
08:13:49,860 --> 08:13:51,360
and uh
11046
08:13:51,360 --> 08:13:54,000
this uh we get this output because as I
11047
08:13:54,000 --> 08:13:56,100
said those functions are part of our
11048
08:13:56,100 --> 08:13:59,580
classes and they are using the name
11049
08:13:59,580 --> 08:14:01,740
so in this case the name that is passed
11050
08:14:01,740 --> 08:14:03,600
to the car this name that we have here
11051
08:14:03,600 --> 08:14:04,860
been there
11052
08:14:04,860 --> 08:14:07,558
is passed to the to the primary
11053
08:14:07,558 --> 08:14:10,260
Constructor of the base class and then
11054
08:14:10,260 --> 08:14:12,240
it's used here so it's calling the
11055
08:14:12,240 --> 08:14:14,398
movement save and there is moving so in
11056
08:14:14,398 --> 08:14:16,920
the same way for the for the plane we
11057
08:14:16,920 --> 08:14:19,740
get the same output it gets the value
11058
08:14:19,740 --> 08:14:22,620
that is passed here viewing and then it
11059
08:14:22,620 --> 08:14:25,378
outputs the viewing is moving and then
11060
08:14:25,378 --> 08:14:26,580
it has stopped
11061
08:14:26,580 --> 08:14:29,600
but we can do even more we can
11062
08:14:29,600 --> 08:14:32,340
override those functions inside our
11063
08:14:32,340 --> 08:14:35,638
class so we can create a version which
11064
08:14:35,638 --> 08:14:37,920
is inside the base class from your
11065
08:14:37,920 --> 08:14:40,320
inheriting we can create a version of
11066
08:14:40,320 --> 08:14:42,898
this function which is specific to the
11067
08:14:42,898 --> 08:14:44,580
car or which is specific to the plane
11068
08:14:44,580 --> 08:14:46,558
and I'm gonna look at that
11069
08:14:46,558 --> 08:14:48,298
so now I'm going to override the move
11070
08:14:48,298 --> 08:14:50,218
function from our Base Class vehicle
11071
08:14:50,218 --> 08:14:52,740
inside our playing class because I want
11072
08:14:52,740 --> 08:14:54,478
to make the move function specific to
11073
08:14:54,478 --> 08:14:56,820
the playing class so I want to provide I
11074
08:14:56,820 --> 08:14:59,638
want to provide an implementation of the
11075
08:14:59,638 --> 08:15:01,558
move class which is specific to a plane
11076
08:15:01,558 --> 08:15:03,780
so how is our plane is moving a plane is
11077
08:15:03,780 --> 08:15:05,940
moving by flying so I'm not satisfied
11078
08:15:05,940 --> 08:15:07,798
with only this generic function which
11079
08:15:07,798 --> 08:15:11,458
says the name of the vehicle is moving
11080
08:15:11,458 --> 08:15:13,740
and to override the function you first
11081
08:15:13,740 --> 08:15:17,218
need to Mark Mark the function similarly
11082
08:15:17,218 --> 08:15:18,958
to
11083
08:15:18,958 --> 08:15:21,058
to the class we need to mark it with the
11084
08:15:21,058 --> 08:15:23,100
open keyword so we type open because
11085
08:15:23,100 --> 08:15:25,500
kotlin wants to make things explicit so
11086
08:15:25,500 --> 08:15:28,378
we put open and now we can
11087
08:15:28,378 --> 08:15:30,000
we can
11088
08:15:30,000 --> 08:15:31,798
we can override that function inside our
11089
08:15:31,798 --> 08:15:34,138
plane class
11090
08:15:34,138 --> 08:15:36,478
so we go to to override the function go
11091
08:15:36,478 --> 08:15:37,680
to code
11092
08:15:37,680 --> 08:15:40,200
generate and here we have vehicles in
11093
08:15:40,200 --> 08:15:42,360
harsh code tostring and go to override
11094
08:15:42,360 --> 08:15:45,240
methods and you you have those equals
11095
08:15:45,240 --> 08:15:46,798
hash code into string and those are here
11096
08:15:46,798 --> 08:15:48,298
because every class that you create in
11097
08:15:48,298 --> 08:15:51,540
Kathleen is implicitly inheriting from
11098
08:15:51,540 --> 08:15:53,340
the any class but we're gonna look at
11099
08:15:53,340 --> 08:15:56,520
that on but more more on that later
11100
08:15:56,520 --> 08:15:58,260
because you look at that in a different
11101
08:15:58,260 --> 08:16:01,378
video and we have our function move and
11102
08:16:01,378 --> 08:16:03,780
stop so
11103
08:16:03,780 --> 08:16:08,218
the the idea is telling us that those
11104
08:16:08,218 --> 08:16:09,780
are the functions which can be
11105
08:16:09,780 --> 08:16:11,878
overridden inside our plane class from
11106
08:16:11,878 --> 08:16:15,120
our vehicle Base Class so we choose move
11107
08:16:15,120 --> 08:16:17,638
we press ok
11108
08:16:17,638 --> 08:16:21,000
and you have this super dot move we're
11109
08:16:21,000 --> 08:16:23,340
gonna see middle what is this and you
11110
08:16:23,340 --> 08:16:24,898
can also override it
11111
08:16:24,898 --> 08:16:28,378
by just typing the keyword override and
11112
08:16:28,378 --> 08:16:30,420
you can override the function so you can
11113
08:16:30,420 --> 08:16:32,160
type here override
11114
08:16:32,160 --> 08:16:34,920
and while I type in you see that we have
11115
08:16:34,920 --> 08:16:37,138
this override fan move stop or and have
11116
08:16:37,138 --> 08:16:39,478
also those so we choose move
11117
08:16:39,478 --> 08:16:41,160
and I have the same thing
11118
08:16:41,160 --> 08:16:46,440
now super dot move means that call the
11119
08:16:46,440 --> 08:16:49,260
function so super means call something
11120
08:16:49,260 --> 08:16:52,200
from our base class in this case call
11121
08:16:52,200 --> 08:16:55,138
the move function from our Base Class so
11122
08:16:55,138 --> 08:16:57,360
we're gonna let that here but in
11123
08:16:57,360 --> 08:16:58,740
addition to that we're going to add a
11124
08:16:58,740 --> 08:17:00,420
new function so we're going to type here
11125
08:17:00,420 --> 08:17:02,280
fun
11126
08:17:02,280 --> 08:17:06,620
flying because the plane is flying
11127
08:17:07,020 --> 08:17:09,600
and we're just gonna have a simple print
11128
08:17:09,600 --> 08:17:11,958
line
11129
08:17:12,478 --> 08:17:14,100
which is going to say
11130
08:17:14,100 --> 08:17:17,700
put quotation marks let's bring this
11131
08:17:17,700 --> 08:17:20,420
down a little bit
11132
08:17:25,020 --> 08:17:27,798
a plane is
11133
08:17:28,920 --> 08:17:32,280
flying so now I'm gonna call this
11134
08:17:32,280 --> 08:17:35,160
function flying inside our move function
11135
08:17:35,160 --> 08:17:37,138
which was overridden
11136
08:17:37,138 --> 08:17:40,620
so we type flying
11137
08:17:40,620 --> 08:17:43,378
flying
11138
08:17:43,378 --> 08:17:46,680
now if we run this code look what
11139
08:17:46,680 --> 08:17:48,860
happens
11140
08:17:59,398 --> 08:18:01,138
if you look in the output to get when
11141
08:18:01,138 --> 08:18:02,820
they is moving when they have stopped
11142
08:18:02,820 --> 08:18:05,398
and then we get the apply the plane is
11143
08:18:05,398 --> 08:18:07,558
flying and then we get bearing is moving
11144
08:18:07,558 --> 08:18:11,100
and bearing has stopped so what what has
11145
08:18:11,100 --> 08:18:14,398
happened here now because we where when
11146
08:18:14,398 --> 08:18:16,620
this function move is called here on our
11147
08:18:16,620 --> 08:18:18,920
object plane created
11148
08:18:18,920 --> 08:18:22,320
now it's using our own implementation of
11149
08:18:22,320 --> 08:18:26,218
the move function and after that so it's
11150
08:18:26,218 --> 08:18:27,540
using our own implementation it's
11151
08:18:27,540 --> 08:18:29,040
calling the flying function that we
11152
08:18:29,040 --> 08:18:31,920
created here because a plane is moving
11153
08:18:31,920 --> 08:18:34,138
in a specific way is flying and then
11154
08:18:34,138 --> 08:18:36,058
it's calling super dot move and super
11155
08:18:36,058 --> 08:18:39,958
dot move is calling the function from
11156
08:18:39,958 --> 08:18:41,458
the base class so it's calling the
11157
08:18:41,458 --> 08:18:44,580
function from here so
11158
08:18:44,580 --> 08:18:46,860
by doing this we made our function
11159
08:18:46,860 --> 08:18:49,860
specific you make we make the we made
11160
08:18:49,860 --> 08:18:52,320
the function move specific make the
11161
08:18:52,320 --> 08:18:55,500
function move unique to the plane class
11162
08:18:55,500 --> 08:18:57,718
because a plane is moving in a specific
11163
08:18:57,718 --> 08:19:00,360
way and we can do the same thing for the
11164
08:19:00,360 --> 08:19:03,138
the car class so
11165
08:19:03,138 --> 08:19:07,020
uh now you may be wondering why why I
11166
08:19:07,020 --> 08:19:10,200
didn't declare the inside our Base Class
11167
08:19:10,200 --> 08:19:11,760
the engines because they are also
11168
08:19:11,760 --> 08:19:15,420
present in in both classes because when
11169
08:19:15,420 --> 08:19:18,080
you create a generic class you should
11170
08:19:18,080 --> 08:19:22,340
only include the commonly used functions
11171
08:19:22,340 --> 08:19:25,200
commonly used and shared functions and
11172
08:19:25,200 --> 08:19:28,620
the properties so uh
11173
08:19:28,620 --> 08:19:31,440
all all vehicles have a name and the
11174
08:19:31,440 --> 08:19:33,780
color but not all vehicles have engines
11175
08:19:33,780 --> 08:19:37,260
so this is why I put the engines here so
11176
08:19:37,260 --> 08:19:39,660
in addition to the name and the color
11177
08:19:39,660 --> 08:19:42,240
which all vehicles have we added some
11178
08:19:42,240 --> 08:19:44,340
new properties which are specific to a
11179
08:19:44,340 --> 08:19:45,058
car
11180
08:19:45,058 --> 08:19:48,360
similar here in addition to the name and
11181
08:19:48,360 --> 08:19:50,280
the color which all vehicles have and
11182
08:19:50,280 --> 08:19:52,378
we've inherited that from our class we
11183
08:19:52,378 --> 08:19:54,660
add that also engines and doors which
11184
08:19:54,660 --> 08:19:57,180
those things are in addition they are
11185
08:19:57,180 --> 08:20:00,718
specific to a plane and more we also
11186
08:20:00,718 --> 08:20:02,878
override the move function because we're
11187
08:20:02,878 --> 08:20:05,160
not satisfied with the with the simple
11188
08:20:05,160 --> 08:20:09,360
move function from our Base Class to
11189
08:20:09,360 --> 08:20:11,458
include to provide our own
11190
08:20:11,458 --> 08:20:13,740
implementation of how a plane is moving
11191
08:20:13,740 --> 08:20:15,780
so you can see that by using an
11192
08:20:15,780 --> 08:20:17,700
inheritance and
11193
08:20:17,700 --> 08:20:19,620
adding new properties are the new
11194
08:20:19,620 --> 08:20:22,440
function you can create more specific
11195
08:20:22,440 --> 08:20:25,978
classes you can inherit you can also
11196
08:20:25,978 --> 08:20:28,080
inherit from the car class to create a
11197
08:20:28,080 --> 08:20:29,878
more specific class you can create a
11198
08:20:29,878 --> 08:20:31,160
Tesla
11199
08:20:31,160 --> 08:20:34,080
class and so on so this is what
11200
08:20:34,080 --> 08:20:36,120
inheritance is and
11201
08:20:36,120 --> 08:20:37,740
see you in the next video
11202
08:20:37,740 --> 08:20:39,958
so I thought that's a good idea to show
11203
08:20:39,958 --> 08:20:41,878
you another example with inheritance and
11204
08:20:41,878 --> 08:20:44,820
I'm gonna go down here
11205
08:20:44,820 --> 08:20:46,680
and I'm going to define a class so I'm
11206
08:20:46,680 --> 08:20:48,540
going to type the keyword class and I'm
11207
08:20:48,540 --> 08:20:50,340
going to call it View
11208
08:20:50,340 --> 08:20:52,200
and this view is going to represent a
11209
08:20:52,200 --> 08:20:54,780
view in uh which is used in Android and
11210
08:20:54,780 --> 08:20:56,160
if you want Android is just a
11211
08:20:56,160 --> 08:20:58,138
rectangular area on the screen and it's
11212
08:20:58,138 --> 08:20:59,520
responsible for drawing and event
11213
08:20:59,520 --> 08:21:00,540
handling
11214
08:21:00,540 --> 08:21:03,478
but you're gonna mimic as I said what
11215
08:21:03,478 --> 08:21:05,580
the view is and uh you know Android or
11216
08:21:05,580 --> 08:21:08,280
not going to create a view like the one
11217
08:21:08,280 --> 08:21:10,620
that is in use like the one that's used
11218
08:21:10,620 --> 08:21:11,638
in Android
11219
08:21:11,638 --> 08:21:13,500
and we Define the primary Constructor
11220
08:21:13,500 --> 08:21:15,540
but we're gonna not Define we're not
11221
08:21:15,540 --> 08:21:16,860
going to Define any properties the
11222
08:21:16,860 --> 08:21:18,420
primary Constructor
11223
08:21:18,420 --> 08:21:20,340
and this view is going to have a simple
11224
08:21:20,340 --> 08:21:23,218
function called Draw and it's going to
11225
08:21:23,218 --> 08:21:25,020
be responsible for drawing this view on
11226
08:21:25,020 --> 08:21:29,100
the screen so we type draw
11227
08:21:29,100 --> 08:21:31,740
and create a function and we're gonna
11228
08:21:31,740 --> 08:21:33,600
add the simple a simple print line here
11229
08:21:33,600 --> 08:21:36,420
which is going to say drawing The View
11230
08:21:36,420 --> 08:21:39,120
so we put here
11231
08:21:39,120 --> 08:21:40,740
quotation marks
11232
08:21:40,740 --> 08:21:42,660
drawing
11233
08:21:42,660 --> 08:21:44,820
The View
11234
08:21:44,820 --> 08:21:47,218
and uh we're gonna
11235
08:21:47,218 --> 08:21:50,160
mark this class as open because I want
11236
08:21:50,160 --> 08:21:51,840
to inherit it from it
11237
08:21:51,840 --> 08:21:53,580
and we're also going to Mark the
11238
08:21:53,580 --> 08:21:54,958
function open because I'm going to
11239
08:21:54,958 --> 08:21:57,240
override that function in the
11240
08:21:57,240 --> 08:22:01,280
in the next class and because this is a
11241
08:22:01,280 --> 08:22:03,718
generic View and it's going to have this
11242
08:22:03,718 --> 08:22:06,600
draw function only this function what
11243
08:22:06,600 --> 08:22:08,398
you can do let's say that I want to I
11244
08:22:08,398 --> 08:22:11,458
don't want to create a simple uh a
11245
08:22:11,458 --> 08:22:13,080
simple view which is just a rectangular
11246
08:22:13,080 --> 08:22:14,940
area on the screen but I want to create
11247
08:22:14,940 --> 08:22:16,978
something more specific let's say that I
11248
08:22:16,978 --> 08:22:18,600
want to create a button
11249
08:22:18,600 --> 08:22:22,200
in that case instead of creating the
11250
08:22:22,200 --> 08:22:25,260
button the button from zero because this
11251
08:22:25,260 --> 08:22:26,340
View
11252
08:22:26,340 --> 08:22:28,378
in Android
11253
08:22:28,378 --> 08:22:31,440
has a lot of functions and properties
11254
08:22:31,440 --> 08:22:33,600
which are responsible for creating the
11255
08:22:33,600 --> 08:22:36,898
view for handling the events and so on
11256
08:22:36,898 --> 08:22:37,920
so
11257
08:22:37,920 --> 08:22:39,898
um because here we have on your simple
11258
08:22:39,898 --> 08:22:42,478
function but the real view has hundreds
11259
08:22:42,478 --> 08:22:45,718
of functions and instead of creating our
11260
08:22:45,718 --> 08:22:48,240
button from zero we can just inherit
11261
08:22:48,240 --> 08:22:51,000
from The View and we can inherit all the
11262
08:22:51,000 --> 08:22:52,440
functionality that the view already
11263
08:22:52,440 --> 08:22:55,798
provides to create a generic View and we
11264
08:22:55,798 --> 08:22:57,298
can add some
11265
08:22:57,298 --> 08:23:00,120
unique characteristics characteristics
11266
08:23:00,120 --> 08:23:02,940
which are specific to a button and in in
11267
08:23:02,940 --> 08:23:06,478
this case in this way we increase the
11268
08:23:06,478 --> 08:23:08,878
quad reusability and it's more easy for
11269
08:23:08,878 --> 08:23:11,700
us to to create the button than rather
11270
08:23:11,700 --> 08:23:13,260
than creating the button from zero
11271
08:23:13,260 --> 08:23:16,080
because the view already provides all
11272
08:23:16,080 --> 08:23:17,340
the tools that you need to create a
11273
08:23:17,340 --> 08:23:20,280
generic view only we you're gonna
11274
08:23:20,280 --> 08:23:23,520
inherit all that behavior all that
11275
08:23:23,520 --> 08:23:25,558
properties but I'm gonna but we're gonna
11276
08:23:25,558 --> 08:23:27,478
change it a little bit so this is what
11277
08:23:27,478 --> 08:23:29,458
we're gonna do with the inheritance so
11278
08:23:29,458 --> 08:23:33,540
we create a new class called button
11279
08:23:33,540 --> 08:23:36,420
so we Define the primary Constructor
11280
08:23:36,420 --> 08:23:38,398
and here you can Define some properties
11281
08:23:38,398 --> 08:23:40,740
which are specific to a button not not
11282
08:23:40,740 --> 08:23:43,080
just to a generic view which is a
11283
08:23:43,080 --> 08:23:45,898
rectangular area on the screen so we put
11284
08:23:45,898 --> 08:23:47,700
Val text and this is going to be a
11285
08:23:47,700 --> 08:23:50,398
string so our our button will have a
11286
08:23:50,398 --> 08:23:52,620
text which can be plugged in or signed
11287
08:23:52,620 --> 08:23:53,580
up
11288
08:23:53,580 --> 08:23:57,780
and also it's going to have orientation
11289
08:23:57,780 --> 08:24:00,478
because it's going to be one on a place
11290
08:24:00,478 --> 08:24:02,280
on the screen
11291
08:24:02,280 --> 08:24:04,740
on a specific place on the screen and
11292
08:24:04,740 --> 08:24:07,378
this is going to be a string
11293
08:24:07,378 --> 08:24:10,080
of course you just only Mimi only mimic
11294
08:24:10,080 --> 08:24:11,520
the
11295
08:24:11,520 --> 08:24:14,878
View and button which are are used in
11296
08:24:14,878 --> 08:24:17,458
Android this is why we have so many we
11297
08:24:17,458 --> 08:24:19,320
see we have so little properties and
11298
08:24:19,320 --> 08:24:22,138
only a simple function function in our
11299
08:24:22,138 --> 08:24:25,440
view draw and we put uh here colon to
11300
08:24:25,440 --> 08:24:27,478
inherit from our view because I want to
11301
08:24:27,478 --> 08:24:31,700
inherit all that functionality all that
11302
08:24:31,700 --> 08:24:34,020
properties that a view provides but I
11303
08:24:34,020 --> 08:24:35,700
wonder is just to change it a little bit
11304
08:24:35,700 --> 08:24:37,740
to make it more specific I want to make
11305
08:24:37,740 --> 08:24:40,920
a button and of course as I said this is
11306
08:24:40,920 --> 08:24:42,138
just uh
11307
08:24:42,138 --> 08:24:44,760
an example we don't have but in a real
11308
08:24:44,760 --> 08:24:47,760
in a real app this will have a hundreds
11309
08:24:47,760 --> 08:24:50,100
or thousands of line of code and we can
11310
08:24:50,100 --> 08:24:51,718
inherit all of that behavior or that
11311
08:24:51,718 --> 08:24:54,898
functionality in our new
11312
08:24:54,898 --> 08:24:58,020
New View which is a button but you can
11313
08:24:58,020 --> 08:24:59,878
override some functions and you can
11314
08:24:59,878 --> 08:25:02,218
provide some you can create a more
11315
08:25:02,218 --> 08:25:04,260
specific view than a general review so
11316
08:25:04,260 --> 08:25:07,138
you don't need to define the primary
11317
08:25:07,138 --> 08:25:08,520
Constructor
11318
08:25:08,520 --> 08:25:11,100
and you put the curly braces
11319
08:25:11,100 --> 08:25:13,378
now I want to override the draw
11320
08:25:13,378 --> 08:25:15,718
functions so that when the draw when the
11321
08:25:15,718 --> 08:25:18,478
draw function is called I want to not
11322
08:25:18,478 --> 08:25:21,180
drawing just a generic view but to draw
11323
08:25:21,180 --> 08:25:24,958
a button of course as I said this is
11324
08:25:24,958 --> 08:25:27,240
just an imaginary example so we type
11325
08:25:27,240 --> 08:25:29,520
overwrite fundra
11326
08:25:29,520 --> 08:25:33,860
and instead of calling the super
11327
08:25:33,860 --> 08:25:36,540
function which is inside our
11328
08:25:36,540 --> 08:25:39,120
our Base Class View or parent Class View
11329
08:25:39,120 --> 08:25:42,739
I'm gonna type here print line
11330
08:25:46,378 --> 08:25:50,100
and we're gonna put the text drawing
11331
08:25:50,100 --> 08:25:51,840
the
11332
08:25:51,840 --> 08:25:53,820
button
11333
08:25:53,820 --> 08:25:56,700
of course and now what we did here we
11334
08:25:56,700 --> 08:25:58,860
created the generic view as I said and
11335
08:25:58,860 --> 08:26:02,218
again this can have hundreds of maybe
11336
08:26:02,218 --> 08:26:04,200
thousands of line of code and functions
11337
08:26:04,200 --> 08:26:05,280
and different functions different
11338
08:26:05,280 --> 08:26:06,360
properties
11339
08:26:06,360 --> 08:26:09,540
but we we inherited from The View class
11340
08:26:09,540 --> 08:26:12,420
and we added some new functionality so
11341
08:26:12,420 --> 08:26:14,160
instead of just drawing a generic view
11342
08:26:14,160 --> 08:26:16,320
now of course we just add a simple print
11343
08:26:16,320 --> 08:26:18,478
line here but here you can have the code
11344
08:26:18,478 --> 08:26:20,160
which is going to be responsible for
11345
08:26:20,160 --> 08:26:22,798
drawing a speed a button so it's going
11346
08:26:22,798 --> 08:26:24,660
to be more specific than a simple
11347
08:26:24,660 --> 08:26:27,540
generic view so you can put here a here
11348
08:26:27,540 --> 08:26:29,700
is the code
11349
08:26:29,700 --> 08:26:30,958
for
11350
08:26:30,958 --> 08:26:32,580
creating
11351
08:26:32,580 --> 08:26:34,680
the button so I'm going to put the
11352
08:26:34,680 --> 08:26:37,920
Commander and let's say that
11353
08:26:37,920 --> 08:26:40,860
now I want to create a run around button
11354
08:26:40,860 --> 08:26:43,680
so instead of creating again all that
11355
08:26:43,680 --> 08:26:46,500
functionality which which is present in
11356
08:26:46,500 --> 08:26:49,260
the view and then create and then use
11357
08:26:49,260 --> 08:26:51,120
and then creating all the functionality
11358
08:26:51,120 --> 08:26:54,600
which is uh inside the button the button
11359
08:26:54,600 --> 08:26:57,958
class we can make this button uh open
11360
08:26:57,958 --> 08:27:00,420
and we can inherit all the functionality
11361
08:27:00,420 --> 08:27:03,540
that the button provides and we're gonna
11362
08:27:03,540 --> 08:27:06,058
add something new something unique to
11363
08:27:06,058 --> 08:27:07,200
this class that we're going to create
11364
08:27:07,200 --> 08:27:08,580
and this class is going to be a round
11365
08:27:08,580 --> 08:27:11,100
button so we type here round
11366
08:27:11,100 --> 08:27:13,020
button
11367
08:27:13,020 --> 08:27:16,500
and here you need to define the text
11368
08:27:16,500 --> 08:27:19,260
because we're going to inherit from the
11369
08:27:19,260 --> 08:27:21,420
for the from the class button
11370
08:27:21,420 --> 08:27:24,058
orientation and notice that I don't use
11371
08:27:24,058 --> 08:27:26,458
the VAR or the VAR keyword here because
11372
08:27:26,458 --> 08:27:28,680
they they're going to be only parameters
11373
08:27:28,680 --> 08:27:30,120
because the properties the actual
11374
08:27:30,120 --> 08:27:32,340
property is going to be are going to be
11375
08:27:32,340 --> 08:27:33,000
um
11376
08:27:33,000 --> 08:27:35,520
ultimately declared
11377
08:27:35,520 --> 08:27:37,798
in the button class
11378
08:27:37,798 --> 08:27:39,058
and
11379
08:27:39,058 --> 08:27:41,280
one thing which is going to be specific
11380
08:27:41,280 --> 08:27:42,840
to a round button that you're going to
11381
08:27:42,840 --> 08:27:45,180
create is going to be Corners so we type
11382
08:27:45,180 --> 08:27:48,540
Val corners
11383
08:27:49,558 --> 08:27:52,080
it was gonna be an integer because the
11384
08:27:52,080 --> 08:27:54,840
the corners basically here are going to
11385
08:27:54,840 --> 08:27:56,940
pass a value which is gonna in our
11386
08:27:56,940 --> 08:27:58,620
marginal example this is going to
11387
08:27:58,620 --> 08:28:01,320
receive a value which can be
11388
08:28:01,320 --> 08:28:03,360
um in degrees so how many degrees do you
11389
08:28:03,360 --> 08:28:04,920
want to how many degrees you want to
11390
08:28:04,920 --> 08:28:07,260
make the buttons by how many degrees you
11391
08:28:07,260 --> 08:28:09,600
want to make the button around so it can
11392
08:28:09,600 --> 08:28:12,298
pass here 20 or 30 anyway so we put
11393
08:28:12,298 --> 08:28:14,700
colon here and we inherit now from our
11394
08:28:14,700 --> 08:28:15,780
button
11395
08:28:15,780 --> 08:28:17,458
class
11396
08:28:17,458 --> 08:28:21,120
right and here you need to pass the
11397
08:28:21,120 --> 08:28:23,840
text
11398
08:28:24,058 --> 08:28:27,000
the orientation
11399
08:28:27,000 --> 08:28:28,920
and that's all
11400
08:28:28,920 --> 08:28:30,660
and you put calibrases now let's
11401
08:28:30,660 --> 08:28:33,298
override the draw function in our round
11402
08:28:33,298 --> 08:28:35,280
button because when that function is
11403
08:28:35,280 --> 08:28:37,620
called now I want to add some specific
11404
08:28:37,620 --> 08:28:40,260
code some new functionality which is
11405
08:28:40,260 --> 08:28:42,298
specific to a round button so it's not
11406
08:28:42,298 --> 08:28:46,320
gonna draw just a button but it's gonna
11407
08:28:46,320 --> 08:28:49,680
draw a round button so we of course
11408
08:28:49,680 --> 08:28:51,240
we're just going to add the simple print
11409
08:28:51,240 --> 08:28:53,218
line there but um
11410
08:28:53,218 --> 08:28:55,260
in a real app you can put here the
11411
08:28:55,260 --> 08:28:57,718
actual cottage which is gonna draw the
11412
08:28:57,718 --> 08:29:01,760
round butter so we put here print line
11413
08:29:03,240 --> 08:29:05,280
and we put
11414
08:29:05,280 --> 08:29:07,260
rowing
11415
08:29:07,260 --> 08:29:09,120
the round
11416
08:29:09,120 --> 08:29:11,580
button
11417
08:29:11,580 --> 08:29:13,920
now let's create some instances let's
11418
08:29:13,920 --> 08:29:15,540
create some objects with those classes
11419
08:29:15,540 --> 08:29:18,558
and we type here
11420
08:29:19,740 --> 08:29:22,378
Val view so we're going to create our
11421
08:29:22,378 --> 08:29:24,120
generic View
11422
08:29:24,120 --> 08:29:25,920
we Define the battery Constructor we
11423
08:29:25,920 --> 08:29:27,360
don't have to pass any values there
11424
08:29:27,360 --> 08:29:29,458
because we didn't Define any properties
11425
08:29:29,458 --> 08:29:31,500
inside our view
11426
08:29:31,500 --> 08:29:35,040
then we create a button so we put here
11427
08:29:35,040 --> 08:29:37,200
Val
11428
08:29:37,200 --> 08:29:39,718
button
11429
08:29:39,718 --> 08:29:43,500
equals button
11430
08:29:44,760 --> 08:29:46,320
and you don't need to pass some value so
11431
08:29:46,320 --> 08:29:47,820
I'm going to pass here the text let's
11432
08:29:47,820 --> 08:29:49,798
say login
11433
08:29:49,798 --> 08:29:52,798
the orientation let's say yo
11434
08:29:52,798 --> 08:29:54,420
it's going to be a text here so we put
11435
08:29:54,420 --> 08:29:57,440
here let's say Center
11436
08:29:58,978 --> 08:30:01,080
next we create a new
11437
08:30:01,080 --> 08:30:02,940
a new object but this is going to be now
11438
08:30:02,940 --> 08:30:06,120
a run around the button so we put around
11439
08:30:06,120 --> 08:30:07,798
button
11440
08:30:07,798 --> 08:30:11,218
equals round button
11441
08:30:11,218 --> 08:30:14,520
and you pass the text here round
11442
08:30:14,520 --> 08:30:17,240
button
11443
08:30:18,058 --> 08:30:22,878
actually let's pause it takes uh sign up
11444
08:30:25,200 --> 08:30:27,000
and uh
11445
08:30:27,000 --> 08:30:30,740
for the orientation let's put
11446
08:30:33,420 --> 08:30:36,540
Center also
11447
08:30:36,540 --> 08:30:38,760
lo you need to pass also value from for
11448
08:30:38,760 --> 08:30:40,798
the colors let's put let's say 30
11449
08:30:40,798 --> 08:30:42,958
degrees it will be
11450
08:30:42,958 --> 08:30:45,898
in our imaginary example now let's call
11451
08:30:45,898 --> 08:30:49,020
the function the function draw on all of
11452
08:30:49,020 --> 08:30:50,638
our objects that we create so we type
11453
08:30:50,638 --> 08:30:53,940
view dot draw
11454
08:30:53,940 --> 08:30:57,000
then we put button
11455
08:30:57,000 --> 08:30:58,798
dot draw
11456
08:30:58,798 --> 08:31:03,138
and round button
11457
08:31:05,100 --> 08:31:08,580
dot draw now if you
11458
08:31:08,580 --> 08:31:10,320
just press Ctrl Z
11459
08:31:10,320 --> 08:31:13,398
if you run this code
11460
08:31:36,478 --> 08:31:39,540
so we get in the output drawing The View
11461
08:31:39,540 --> 08:31:41,340
then we get drawn in the button drawing
11462
08:31:41,340 --> 08:31:43,740
The View again drawing the round button
11463
08:31:43,740 --> 08:31:46,138
and let's uh think about why you have
11464
08:31:46,138 --> 08:31:49,680
this output so first we see that we have
11465
08:31:49,680 --> 08:31:51,780
drawing The View which is the function
11466
08:31:51,780 --> 08:31:54,298
inside our view that you created here so
11467
08:31:54,298 --> 08:31:55,860
it's from the generic view so it's
11468
08:31:55,860 --> 08:31:58,620
drawing The View then we have drawing
11469
08:31:58,620 --> 08:32:01,020
the button so
11470
08:32:01,020 --> 08:32:01,978
here
11471
08:32:01,978 --> 08:32:04,860
when this line of code is uh red it says
11472
08:32:04,860 --> 08:32:07,320
drawing the button so you have
11473
08:32:07,320 --> 08:32:09,958
we have our we have our code which is
11474
08:32:09,958 --> 08:32:12,478
you know which is with just a print line
11475
08:32:12,478 --> 08:32:16,378
but uh so this is drawing the the button
11476
08:32:16,378 --> 08:32:18,718
then it's calling the super function so
11477
08:32:18,718 --> 08:32:19,978
it's calling the function inside the
11478
08:32:19,978 --> 08:32:22,200
view which it says drawing The View
11479
08:32:22,200 --> 08:32:23,820
next
11480
08:32:23,820 --> 08:32:27,240
when this line of code is red so round
11481
08:32:27,240 --> 08:32:29,638
that round button that draw now it's
11482
08:32:29,638 --> 08:32:31,798
calling the draw
11483
08:32:31,798 --> 08:32:33,478
function which is inside the round
11484
08:32:33,478 --> 08:32:36,240
button so instead of just now calling
11485
08:32:36,240 --> 08:32:39,120
the because now we are inheriting from
11486
08:32:39,120 --> 08:32:40,558
the button class
11487
08:32:40,558 --> 08:32:43,378
now uh we override this function here
11488
08:32:43,378 --> 08:32:45,898
and it's it's uh using our
11489
08:32:45,898 --> 08:32:47,940
implementation for creating a round
11490
08:32:47,940 --> 08:32:50,340
button it's uh executing this line of
11491
08:32:50,340 --> 08:32:52,080
code and then it's calling the super
11492
08:32:52,080 --> 08:32:54,180
implementation which is drawing the
11493
08:32:54,180 --> 08:32:56,100
button you can actually remove this if
11494
08:32:56,100 --> 08:32:58,280
you want because you let's say that
11495
08:32:58,280 --> 08:33:02,580
calling the code inside from the button
11496
08:33:02,580 --> 08:33:06,600
is not gonna affect our logic so if I
11497
08:33:06,600 --> 08:33:10,820
delete this now if you run this code
11498
08:33:17,780 --> 08:33:21,180
now we have drawing the view drawing the
11499
08:33:21,180 --> 08:33:23,100
button and we have drawing the view
11500
08:33:23,100 --> 08:33:25,378
drawing the wrong button so now it's not
11501
08:33:25,378 --> 08:33:27,298
calling here the super so it's not it's
11502
08:33:27,298 --> 08:33:30,420
not calling the code which is inside the
11503
08:33:30,420 --> 08:33:32,940
for the bottom because we are inheriting
11504
08:33:32,940 --> 08:33:35,218
first from the button but but the button
11505
08:33:35,218 --> 08:33:37,920
is also inheriting from The View so it's
11506
08:33:37,920 --> 08:33:39,840
calling first this then it's calling
11507
08:33:39,840 --> 08:33:42,360
draw which is inside here and we have
11508
08:33:42,360 --> 08:33:44,820
the round but now we only have drawing
11509
08:33:44,820 --> 08:33:47,398
the wrong button because here now it's
11510
08:33:47,398 --> 08:33:49,260
using our own implementation of the draw
11511
08:33:49,260 --> 08:33:52,138
so we have drawing the round button
11512
08:33:52,138 --> 08:33:55,020
so buy this just by
11513
08:33:55,020 --> 08:33:58,558
by making our view because as I said in
11514
08:33:58,558 --> 08:34:01,200
real app this view is can have hundreds
11515
08:34:01,200 --> 08:34:03,000
of maybe thousands of line of code of
11516
08:34:03,000 --> 08:34:05,398
different functions and properties so
11517
08:34:05,398 --> 08:34:07,260
you don't want to create all of that
11518
08:34:07,260 --> 08:34:10,440
from zero when I want only to let's say
11519
08:34:10,440 --> 08:34:12,138
I want to create a button I can't
11520
08:34:12,138 --> 08:34:14,580
inherit I can inherit from The View
11521
08:34:14,580 --> 08:34:17,100
which already provides all the functions
11522
08:34:17,100 --> 08:34:19,500
and properties and logic to create a
11523
08:34:19,500 --> 08:34:21,420
generic view but I can override some
11524
08:34:21,420 --> 08:34:22,860
functions which are responsible let's
11525
08:34:22,860 --> 08:34:27,180
say for like this draw and I can ex I
11526
08:34:27,180 --> 08:34:30,240
can Define some logic and when all of
11527
08:34:30,240 --> 08:34:31,798
that functionality is called It's Gonna
11528
08:34:31,798 --> 08:34:33,780
not draw only a generic view but it's
11529
08:34:33,780 --> 08:34:36,600
gonna draw a button so by inheriting
11530
08:34:36,600 --> 08:34:39,478
from The View we increase we increase
11531
08:34:39,478 --> 08:34:41,458
code the code reusability
11532
08:34:41,458 --> 08:34:43,740
and this is very very powerful because
11533
08:34:43,740 --> 08:34:47,040
let's say that uh like in our example
11534
08:34:47,040 --> 08:34:49,620
you want to you don't you're not
11535
08:34:49,620 --> 08:34:51,600
satisfied with the simple button which
11536
08:34:51,600 --> 08:34:53,580
is used in Android and you want to
11537
08:34:53,580 --> 08:34:55,798
create a draw a round button so instead
11538
08:34:55,798 --> 08:34:58,020
of creating all of that logic from zero
11539
08:34:58,020 --> 08:35:00,378
you can just inherit from a simple
11540
08:35:00,378 --> 08:35:03,058
button you can have override some
11541
08:35:03,058 --> 08:35:06,840
functions and when when you execute that
11542
08:35:06,840 --> 08:35:08,760
quad now it's gonna execute the code
11543
08:35:08,760 --> 08:35:10,740
with the implementation with the
11544
08:35:10,740 --> 08:35:13,020
additional implementation the with the
11545
08:35:13,020 --> 08:35:15,540
additional code which is which is gonna
11546
08:35:15,540 --> 08:35:17,878
generate a round button and it's just
11547
08:35:17,878 --> 08:35:20,878
not just a simple button so this is our
11548
08:35:20,878 --> 08:35:23,398
this is our discussion about inheritance
11549
08:35:23,398 --> 08:35:26,340
I hope that this clarifies some things
11550
08:35:26,340 --> 08:35:30,020
and see you in the next video
11551
08:35:31,798 --> 08:35:33,540
so now it's time to start a discussion
11552
08:35:33,540 --> 08:35:36,180
about seal classes so often you want to
11553
08:35:36,180 --> 08:35:37,798
represent only a fixed set of
11554
08:35:37,798 --> 08:35:40,020
possibilities so however case can either
11555
08:35:40,020 --> 08:35:43,260
success or it can fail a user can be a
11556
08:35:43,260 --> 08:35:45,718
standard user or a pro user and in those
11557
08:35:45,718 --> 08:35:47,700
cases you may be thinking now that you
11558
08:35:47,700 --> 08:35:49,860
can use enum classes but enumclasses
11559
08:35:49,860 --> 08:35:52,080
have some limitations and let's see why
11560
08:35:52,080 --> 08:35:54,240
you should use instead seal class so
11561
08:35:54,240 --> 08:35:55,500
let's type Main
11562
08:35:55,500 --> 08:35:57,600
I'm going to create a in on class here
11563
08:35:57,600 --> 08:36:01,217
call it the result
11564
08:36:01,680 --> 08:36:05,520
and let's put curly braces unless I hear
11565
08:36:05,520 --> 08:36:08,058
success
11566
08:36:10,620 --> 08:36:14,160
and the error
11567
08:36:14,160 --> 08:36:17,398
now the problem with this is that we
11568
08:36:17,398 --> 08:36:19,620
cannot encode more information here so
11569
08:36:19,620 --> 08:36:22,978
let's say that I want to to to Define
11570
08:36:22,978 --> 08:36:26,820
here a non-resolvable resolver error and
11571
08:36:26,820 --> 08:36:30,540
resolver error and I cannot do that here
11572
08:36:30,540 --> 08:36:33,718
so if I type here x dot exception I can
11573
08:36:33,718 --> 08:36:36,780
I cannot do that because
11574
08:36:36,780 --> 08:36:39,840
um the the the the types that are
11575
08:36:39,840 --> 08:36:41,940
defined here cannot have properties so
11576
08:36:41,940 --> 08:36:45,360
you cannot Define this here and for that
11577
08:36:45,360 --> 08:36:47,580
we need to use seal classes and to use
11578
08:36:47,580 --> 08:36:51,120
the seal class you just type seal class
11579
08:36:51,120 --> 08:36:53,878
I'm going to call it result
11580
08:36:53,878 --> 08:36:55,740
it's going to have a parameter called
11581
08:36:55,740 --> 08:36:58,260
Val message it's going to be of type
11582
08:36:58,260 --> 08:37:00,420
string
11583
08:37:00,420 --> 08:37:03,840
and you can Define your uh
11584
08:37:03,840 --> 08:37:05,760
classes which are gone inherit from the
11585
08:37:05,760 --> 08:37:08,458
seal class either outside the seal class
11586
08:37:08,458 --> 08:37:10,320
so you can Target class let's say
11587
08:37:10,320 --> 08:37:13,100
success
11588
08:37:13,320 --> 08:37:16,978
and let's type here
11589
08:37:16,978 --> 08:37:18,360
meh
11590
08:37:18,360 --> 08:37:20,520
message of type string and we can
11591
08:37:20,520 --> 08:37:23,160
inherit from the seal class so you can
11592
08:37:23,160 --> 08:37:26,218
type here result
11593
08:37:26,218 --> 08:37:28,940
message
11594
08:37:30,000 --> 08:37:33,840
or you can copy this and you can Define
11595
08:37:33,840 --> 08:37:36,718
it inside the enumclass so you can have
11596
08:37:36,718 --> 08:37:38,760
it as an SSC class so I'm going to type
11597
08:37:38,760 --> 08:37:41,580
another class here called
11598
08:37:41,580 --> 08:37:44,360
error
11599
08:37:45,898 --> 08:37:48,440
foreign
11600
08:37:51,320 --> 08:37:54,058
the parameter here
11601
08:37:54,058 --> 08:37:55,558
actually not
11602
08:37:55,558 --> 08:37:59,218
that is the parameter
11603
08:37:59,218 --> 08:38:02,058
message
11604
08:38:18,600 --> 08:38:20,780
foreign
11605
08:38:25,520 --> 08:38:28,740
the parameter here
11606
08:38:28,740 --> 08:38:31,200
message I'm gonna hit it from the result
11607
08:38:31,200 --> 08:38:34,978
you know from the results class
11608
08:38:34,978 --> 08:38:37,978
I'm gonna pass that parameter here
11609
08:38:37,978 --> 08:38:41,120
to the parent class
11610
08:38:41,700 --> 08:38:43,020
now
11611
08:38:43,020 --> 08:38:45,540
I'm gonna Define a function down here
11612
08:38:45,540 --> 08:38:49,920
called get data fun get data and it's
11613
08:38:49,920 --> 08:38:51,958
gonna have
11614
08:38:51,958 --> 08:38:56,398
a parameter called the result
11615
08:38:56,398 --> 08:38:59,520
it's going to be of type result
11616
08:38:59,520 --> 08:39:01,080
and here you're going to define a when
11617
08:39:01,080 --> 08:39:04,080
statement so we type when
11618
08:39:04,080 --> 08:39:06,718
the type result and now is the
11619
08:39:06,718 --> 08:39:08,580
interesting part
11620
08:39:08,580 --> 08:39:11,100
now if you hover over this underline
11621
08:39:11,100 --> 08:39:14,458
over when it says when expression must
11622
08:39:14,458 --> 08:39:19,440
be exhaustive unnecessary is error
11623
08:39:19,440 --> 08:39:22,860
comma is Success branches or else Branch
11624
08:39:22,860 --> 08:39:25,558
instead so in other words what is saying
11625
08:39:25,558 --> 08:39:27,298
here by uh
11626
08:39:27,298 --> 08:39:30,240
to have the one statement exhaustive
11627
08:39:30,240 --> 08:39:32,280
it's it's what it's saying another word
11628
08:39:32,280 --> 08:39:35,218
is that it needs to cover all the
11629
08:39:35,218 --> 08:39:37,978
possibilities so both possibilities that
11630
08:39:37,978 --> 08:39:40,138
we have here and you can type yourself
11631
08:39:40,138 --> 08:39:43,260
this or you can click on here click on
11632
08:39:43,260 --> 08:39:45,420
this right bulb and you can click here
11633
08:39:45,420 --> 08:39:47,458
add the remaining branches and as you
11634
08:39:47,458 --> 08:39:49,620
can see it's added the remaining browser
11635
08:39:49,620 --> 08:39:53,040
you add is result. error and easy result
11636
08:39:53,040 --> 08:39:56,160
does success and those are exhausted
11637
08:39:56,160 --> 08:39:58,138
those are the only two possibilities
11638
08:39:58,138 --> 08:40:00,000
which are
11639
08:40:00,000 --> 08:40:02,218
which are possible so here I'm gonna
11640
08:40:02,218 --> 08:40:04,020
type result
11641
08:40:04,020 --> 08:40:06,660
let's define a function inside the CL
11642
08:40:06,660 --> 08:40:09,120
class so I'm going to type here fun
11643
08:40:09,120 --> 08:40:10,500
show
11644
08:40:10,500 --> 08:40:13,458
message
11645
08:40:15,420 --> 08:40:18,540
it's gonna have a print line which is
11646
08:40:18,540 --> 08:40:21,798
going to say result
11647
08:40:23,280 --> 08:40:26,180
dollar sign
11648
08:40:28,920 --> 08:40:31,500
message
11649
08:40:31,500 --> 08:40:34,680
so and now I'm going to put dot
11650
08:40:34,680 --> 08:40:38,580
show message and also here result
11651
08:40:38,580 --> 08:40:43,440
dot show message and now this is
11652
08:40:43,440 --> 08:40:47,820
uh this is uh exhausted because now we
11653
08:40:47,820 --> 08:40:51,478
call we covered all the possibilities
11654
08:40:51,478 --> 08:40:56,898
which which are uh inside the which are
11655
08:40:56,898 --> 08:41:00,600
as a consequence of the fact that we our
11656
08:41:00,600 --> 08:41:03,240
classes are uh inheriting from the seal
11657
08:41:03,240 --> 08:41:08,458
class so if I add here an uh else Branch
11658
08:41:08,458 --> 08:41:11,940
so if I put here else as you can as you
11659
08:41:11,940 --> 08:41:13,680
you'll see this is going to be redundant
11660
08:41:13,680 --> 08:41:15,718
because we covered all the possibilities
11661
08:41:15,718 --> 08:41:18,000
and the compilers know this and this can
11662
08:41:18,000 --> 08:41:20,878
be very useful in some scenarios
11663
08:41:20,878 --> 08:41:22,798
so if I put calibrations as you can see
11664
08:41:22,798 --> 08:41:25,320
this is gray out because it's redundant
11665
08:41:25,320 --> 08:41:28,440
because it knows that we covered all the
11666
08:41:28,440 --> 08:41:30,240
possibilities and the possibilities are
11667
08:41:30,240 --> 08:41:32,160
only those two result that error and
11668
08:41:32,160 --> 08:41:36,079
result are success so if I type here now
11669
08:41:36,500 --> 08:41:40,940
success let's type success
11670
08:41:41,280 --> 08:41:44,180
equals
11671
08:41:44,340 --> 08:41:46,340
result
11672
08:41:46,340 --> 08:41:49,320
bo2s there
11673
08:41:49,320 --> 08:41:53,520
equals result dot success
11674
08:41:53,520 --> 08:41:57,260
let's type success here
11675
08:42:00,180 --> 08:42:05,660
let's press Ctrl D let's call this error
11676
08:42:08,280 --> 08:42:11,218
result let's change this to error so
11677
08:42:11,218 --> 08:42:16,398
this is how you create instances of the
11678
08:42:17,160 --> 08:42:21,500
nested classes let's call this failed
11679
08:42:24,660 --> 08:42:28,500
now let's type here get data down here
11680
08:42:28,500 --> 08:42:31,680
get data and let's let's pass success
11681
08:42:31,680 --> 08:42:34,620
and let's run our code now as you'll see
11682
08:42:34,620 --> 08:42:35,700
the message
11683
08:42:35,700 --> 08:42:39,020
result success
11684
08:42:50,160 --> 08:42:52,978
so we get results success in the output
11685
08:42:52,978 --> 08:42:54,000
so
11686
08:42:54,000 --> 08:42:56,458
if I change now this to error you will
11687
08:42:56,458 --> 08:42:58,378
see that this is the result error
11688
08:42:58,378 --> 08:43:01,680
because it is executing this branch
11689
08:43:01,680 --> 08:43:04,878
so if I run this
11690
08:43:08,398 --> 08:43:10,920
to get result failed
11691
08:43:10,920 --> 08:43:13,558
now the interesting part here is if I
11692
08:43:13,558 --> 08:43:16,218
add here another
11693
08:43:16,820 --> 08:43:19,138
possibilities so
11694
08:43:19,138 --> 08:43:21,420
if I type here class
11695
08:43:21,420 --> 08:43:23,458
progress
11696
08:43:23,458 --> 08:43:26,218
and I type message
11697
08:43:26,218 --> 08:43:29,398
string inheriting from result
11698
08:43:29,398 --> 08:43:31,138
let's type let's pass the message
11699
08:43:31,138 --> 08:43:33,120
parameter to the super class to the
11700
08:43:33,120 --> 08:43:35,160
parent class
11701
08:43:35,160 --> 08:43:38,700
now what what we have because our class
11702
08:43:38,700 --> 08:43:41,100
is a sealed class what we have here is
11703
08:43:41,100 --> 08:43:43,798
that the when is underlined and is
11704
08:43:43,798 --> 08:43:45,780
trying to say something to us is saying
11705
08:43:45,780 --> 08:43:47,760
that when expression must be exhaustive
11706
08:43:47,760 --> 08:43:50,878
unnecessary is progress so it knows that
11707
08:43:50,878 --> 08:43:54,540
there is another possibility for uh for
11708
08:43:54,540 --> 08:43:59,458
our result uh we it can be therefore you
11709
08:43:59,458 --> 08:44:01,500
need to add that possibility otherwise
11710
08:44:01,500 --> 08:44:03,120
I'm going to show you this order and if
11711
08:44:03,120 --> 08:44:04,740
you try to compile this code I'm gonna
11712
08:44:04,740 --> 08:44:08,878
throw an error so what what you can do
11713
08:44:08,878 --> 08:44:11,458
here is can we can type here is a result
11714
08:44:11,458 --> 08:44:14,100
to make it exhaustive so to cover all
11715
08:44:14,100 --> 08:44:18,660
the possibility result of that progress
11716
08:44:19,920 --> 08:44:22,860
let's put Arrow
11717
08:44:22,860 --> 08:44:26,100
result dot show message
11718
08:44:26,100 --> 08:44:28,860
and now if I create here another Val
11719
08:44:28,860 --> 08:44:31,740
let's call this uh
11720
08:44:31,740 --> 08:44:33,840
progress
11721
08:44:33,840 --> 08:44:37,558
because the result dot progress and
11722
08:44:37,558 --> 08:44:41,958
let's define for the parameter
11723
08:44:44,580 --> 08:44:47,340
progress here
11724
08:44:47,340 --> 08:44:51,540
now if I change this to progress
11725
08:44:51,540 --> 08:44:53,040
now you'll see the output result
11726
08:44:53,040 --> 08:44:55,760
progress
11727
08:44:59,218 --> 08:45:02,878
so we get result progress because
11728
08:45:02,878 --> 08:45:07,500
now this branch is true but if I let's
11729
08:45:07,500 --> 08:45:09,958
if I delete all of those look what
11730
08:45:09,958 --> 08:45:13,320
happens if I click here or is where you
11731
08:45:13,320 --> 08:45:15,660
have those on this underline it says add
11732
08:45:15,660 --> 08:45:17,760
else Branch so when expression must be
11733
08:45:17,760 --> 08:45:19,978
exhausted so it means that it needs to
11734
08:45:19,978 --> 08:45:21,600
cover all the possibility
11735
08:45:21,600 --> 08:45:24,540
possibilities are necessary is error is
11736
08:45:24,540 --> 08:45:26,878
progress and is Success so if we click
11737
08:45:26,878 --> 08:45:28,740
here on more actions
11738
08:45:28,740 --> 08:45:30,780
and if you click here on ADD remaining
11739
08:45:30,780 --> 08:45:34,920
branches it it knows that those are all
11740
08:45:34,920 --> 08:45:37,260
the possibilities on which our result
11741
08:45:37,260 --> 08:45:40,200
can be and it automatically generated
11742
08:45:40,200 --> 08:45:41,638
the
11743
08:45:41,638 --> 08:45:45,600
the check for us and we we only just
11744
08:45:45,600 --> 08:45:47,458
need to type here result
11745
08:45:47,458 --> 08:45:50,820
not show message and that's all and with
11746
08:45:50,820 --> 08:45:53,120
tnumps you you don't have this
11747
08:45:53,120 --> 08:45:58,820
auto-generated code for you because uh
11748
08:45:58,940 --> 08:46:03,377
I'm gonna explain immediately
11749
08:46:03,780 --> 08:46:06,860
so with with an enum if you add another
11750
08:46:06,860 --> 08:46:11,040
so if you have a Nemo class I can delete
11751
08:46:11,040 --> 08:46:13,680
the code now here because
11752
08:46:13,680 --> 08:46:15,600
but I'm going to keep the code as it is
11753
08:46:15,600 --> 08:46:18,420
and because all the subclasses of excel
11754
08:46:18,420 --> 08:46:20,878
class are known by the compiler the
11755
08:46:20,878 --> 08:46:23,280
compiler can fill all the possible cases
11756
08:46:23,280 --> 08:46:25,558
automatically for us if I declare this
11757
08:46:25,558 --> 08:46:28,378
Arrow class as a seal class and I
11758
08:46:28,378 --> 08:46:30,958
declare two classes inside here called
11759
08:46:30,958 --> 08:46:33,420
let's say class
11760
08:46:33,420 --> 08:46:39,320
resolvable error so or recoverable error
11761
08:46:42,180 --> 08:46:43,978
and it's going to have a parameter
11762
08:46:43,978 --> 08:46:46,080
called exception it's going to be of
11763
08:46:46,080 --> 08:46:48,898
type exception
11764
08:46:48,898 --> 08:46:51,680
this one
11765
08:46:52,978 --> 08:46:55,138
and we're going to extend from our error
11766
08:46:55,138 --> 08:46:57,360
seal class
11767
08:46:57,360 --> 08:47:00,058
not this one
11768
08:47:00,058 --> 08:47:01,440
this one
11769
08:47:01,440 --> 08:47:02,580
and we're going to pass the message
11770
08:47:02,580 --> 08:47:05,420
parameter
11771
08:47:06,058 --> 08:47:09,058
and I'm going to declare another class
11772
08:47:09,058 --> 08:47:12,058
non-recoverable
11773
08:47:15,780 --> 08:47:19,138
and it's going to have an exception
11774
08:47:19,138 --> 08:47:21,780
so this is more powerful than
11775
08:47:21,780 --> 08:47:24,420
in arms because you can have all those
11776
08:47:24,420 --> 08:47:27,600
properties and you can you can have
11777
08:47:27,600 --> 08:47:30,478
subtypes and and all of this and within
11778
08:47:30,478 --> 08:47:32,218
arms we cannot do this so let's extend
11779
08:47:32,218 --> 08:47:34,940
from error
11780
08:47:35,218 --> 08:47:38,340
let's pass the message
11781
08:47:38,340 --> 08:47:40,440
and now I need to delete this because we
11782
08:47:40,440 --> 08:47:43,260
cannot instantiate The Shield
11783
08:47:43,260 --> 08:47:46,638
class so let's delete this
11784
08:47:47,580 --> 08:47:50,218
now if I delete this now from here look
11785
08:47:50,218 --> 08:47:52,740
what happens I have an underline and it
11786
08:47:52,740 --> 08:47:54,478
says
11787
08:47:54,478 --> 08:47:55,680
add
11788
08:47:55,680 --> 08:47:57,478
it says one expression must be
11789
08:47:57,478 --> 08:47:59,940
exhaustive so it must cover all possible
11790
08:47:59,940 --> 08:48:03,840
cases that that exists for this cell
11791
08:48:03,840 --> 08:48:07,080
class unnecessary is not recoverable or
11792
08:48:07,080 --> 08:48:10,620
is recovered or branches so if I click
11793
08:48:10,620 --> 08:48:13,680
on this and I click on this bulb I click
11794
08:48:13,680 --> 08:48:16,080
here add remaining branches as you can
11795
08:48:16,080 --> 08:48:18,540
see it adds all the possible cases so
11796
08:48:18,540 --> 08:48:20,340
all the possible cases that we Define
11797
08:48:20,340 --> 08:48:22,558
inside our seal class for us
11798
08:48:22,558 --> 08:48:23,820
automatically
11799
08:48:23,820 --> 08:48:27,320
and we just type your result
11800
08:48:27,320 --> 08:48:30,000
dot show message
11801
08:48:30,000 --> 08:48:32,360
again here result
11802
08:48:32,360 --> 08:48:35,638
message and you cannot have this
11803
08:48:35,638 --> 08:48:37,500
Behavior with the non-class so this is
11804
08:48:37,500 --> 08:48:39,360
why still classes are more powerful
11805
08:48:39,360 --> 08:48:42,058
because they can have subclasses you can
11806
08:48:42,058 --> 08:48:43,020
have
11807
08:48:43,020 --> 08:48:45,478
more information on the classes so you
11808
08:48:45,478 --> 08:48:47,100
can have exceptions passed to them and
11809
08:48:47,100 --> 08:48:49,260
so on so
11810
08:48:49,260 --> 08:48:51,058
this is our discussion about seal
11811
08:48:51,058 --> 08:48:55,820
classes and see you in the next video
11812
08:48:57,000 --> 08:48:58,620
so now it's time to start a discussion
11813
08:48:58,620 --> 08:49:00,660
about abstract classes and to show you
11814
08:49:00,660 --> 08:49:02,340
what abstract classes are I'm going to
11815
08:49:02,340 --> 08:49:04,440
use the code that you had in our
11816
08:49:04,440 --> 08:49:06,958
discussion about inheritance but I'm
11817
08:49:06,958 --> 08:49:08,280
going to change it a little bit so I'm
11818
08:49:08,280 --> 08:49:09,840
going to delete for this class plane
11819
08:49:09,840 --> 08:49:13,798
because you don't need this class and
11820
08:49:13,798 --> 08:49:16,200
I'm gonna
11821
08:49:16,200 --> 08:49:18,840
I'm gonna delete this instances that we
11822
08:49:18,840 --> 08:49:21,200
have here
11823
08:49:22,080 --> 08:49:25,138
and I'm going to declare this as a VAR
11824
08:49:25,138 --> 08:49:27,420
and this also has a VAR so they're gonna
11825
08:49:27,420 --> 08:49:29,398
be now properties
11826
08:49:29,398 --> 08:49:31,320
I'm gonna delete the properties that we
11827
08:49:31,320 --> 08:49:33,240
have here
11828
08:49:33,240 --> 08:49:35,160
and uh
11829
08:49:35,160 --> 08:49:37,798
now we have this here so I'm gonna
11830
08:49:37,798 --> 08:49:39,780
delete these two
11831
08:49:39,780 --> 08:49:41,940
let's delete this
11832
08:49:41,940 --> 08:49:43,740
let's delete this
11833
08:49:43,740 --> 08:49:45,180
now
11834
08:49:45,180 --> 08:49:47,280
what are abstract classes
11835
08:49:47,280 --> 08:49:50,058
um abstract classes are very similar to
11836
08:49:50,058 --> 08:49:52,860
interfaces the only difference is that
11837
08:49:52,860 --> 08:49:54,840
in abstract classes you can declare
11838
08:49:54,840 --> 08:49:57,958
properties which can have a value and
11839
08:49:57,958 --> 08:49:59,700
you know in interfaces you can't
11840
08:49:59,700 --> 08:50:02,458
actually you can but you you need to
11841
08:50:02,458 --> 08:50:04,680
provide an access modifier
11842
08:50:04,680 --> 08:50:07,740
and to declare an abstract class we go
11843
08:50:07,740 --> 08:50:10,260
in front of the class that you I want to
11844
08:50:10,260 --> 08:50:12,540
make it abstract and we type the
11845
08:50:12,540 --> 08:50:14,398
abstract keyword
11846
08:50:14,398 --> 08:50:17,780
let's put the class keyword
11847
08:50:18,058 --> 08:50:21,360
and now our class is abstract and with
11848
08:50:21,360 --> 08:50:22,860
an abstract class you cannot create
11849
08:50:22,860 --> 08:50:25,680
instances abstract classes are created
11850
08:50:25,680 --> 08:50:29,160
only to be inherited from uh by other
11851
08:50:29,160 --> 08:50:31,978
classes and now if I try to create an
11852
08:50:31,978 --> 08:50:33,958
instance with this abstract class and I
11853
08:50:33,958 --> 08:50:37,378
just type Val vehicle
11854
08:50:37,378 --> 08:50:40,200
and we type vehicle here and we put the
11855
08:50:40,200 --> 08:50:42,058
parenthesis to call the go to the
11856
08:50:42,058 --> 08:50:43,138
Constructor
11857
08:50:43,138 --> 08:50:45,360
you have an error here and says cannot
11858
08:50:45,360 --> 08:50:46,798
create an instance of an abstract class
11859
08:50:46,798 --> 08:50:48,420
so you cannot create an object with an
11860
08:50:48,420 --> 08:50:50,100
abstract class an abstract class can
11861
08:50:50,100 --> 08:50:54,898
only be inherited by another class and
11862
08:50:54,898 --> 08:50:58,200
abstract classes usually are used like
11863
08:50:58,200 --> 08:51:00,860
interfaces when you want to provide
11864
08:51:00,860 --> 08:51:03,898
what you need what needs to be done but
11865
08:51:03,898 --> 08:51:05,398
you don't provide how it's going to be
11866
08:51:05,398 --> 08:51:07,320
done so you can you need abstract
11867
08:51:07,320 --> 08:51:09,780
classes are used when you define
11868
08:51:09,780 --> 08:51:13,260
uh functions without the body and the
11869
08:51:13,260 --> 08:51:16,558
code the body and the code is uh it's uh
11870
08:51:16,558 --> 08:51:18,780
implemented it's provided by the class
11871
08:51:18,780 --> 08:51:21,360
by the class which inherits from the
11872
08:51:21,360 --> 08:51:24,000
abstract class so they are as I say
11873
08:51:24,000 --> 08:51:25,798
similar very similar to interfaces the
11874
08:51:25,798 --> 08:51:27,360
only difference that in abstract classes
11875
08:51:27,360 --> 08:51:29,398
you can have properties so you can have
11876
08:51:29,398 --> 08:51:31,440
here a Val
11877
08:51:31,440 --> 08:51:33,478
text
11878
08:51:33,478 --> 08:51:36,000
and you can assign here some text in uh
11879
08:51:36,000 --> 08:51:38,040
in an interface you cannot do this let's
11880
08:51:38,040 --> 08:51:39,840
type some text
11881
08:51:39,840 --> 08:51:42,898
so this is the difference and uh
11882
08:51:42,898 --> 08:51:44,818
also
11883
08:51:44,818 --> 08:51:47,458
um you I should say that um
11884
08:51:47,458 --> 08:51:50,520
you can Implement many as many
11885
08:51:50,520 --> 08:51:52,218
interfaces as you want
11886
08:51:52,218 --> 08:51:55,260
but in a class but you can only inherit
11887
08:51:55,260 --> 08:51:58,080
from a single class right this I forgot
11888
08:51:58,080 --> 08:51:59,458
to say when I talked about the
11889
08:51:59,458 --> 08:52:01,680
interfaces now
11890
08:52:01,680 --> 08:52:05,218
if I want to make uh if I want to if I
11891
08:52:05,218 --> 08:52:08,218
to make now our functions abstract you
11892
08:52:08,218 --> 08:52:10,020
just type the keyword abstract in front
11893
08:52:10,020 --> 08:52:13,740
of them and now they are abstract and
11894
08:52:13,740 --> 08:52:15,718
we're gonna do the same for our stop
11895
08:52:15,718 --> 08:52:18,180
function but now we have this underline
11896
08:52:18,180 --> 08:52:21,180
because as I said abstract functions
11897
08:52:21,180 --> 08:52:23,878
like interfaces or the functions which
11898
08:52:23,878 --> 08:52:25,500
are defined in the interfaces cannot
11899
08:52:25,500 --> 08:52:28,680
have a body so we need to delete this uh
11900
08:52:28,680 --> 08:52:30,420
you need to delete the body you need to
11901
08:52:30,420 --> 08:52:31,740
delete the curly braces Because the
11902
08:52:31,740 --> 08:52:34,200
actual body the actual logic and the
11903
08:52:34,200 --> 08:52:35,818
code is going to be provided by the
11904
08:52:35,818 --> 08:52:38,340
class which uh in head is from the
11905
08:52:38,340 --> 08:52:39,840
abstract class now
11906
08:52:39,840 --> 08:52:42,660
to inherit from an abstract class we go
11907
08:52:42,660 --> 08:52:43,798
here
11908
08:52:43,798 --> 08:52:46,318
we put colon and we type the name of the
11909
08:52:46,318 --> 08:52:47,638
abstract class from which you want to
11910
08:52:47,638 --> 08:52:49,318
inherit vehicle
11911
08:52:49,318 --> 08:52:51,180
now
11912
08:52:51,180 --> 08:52:54,240
if you go out here now we have this red
11913
08:52:54,240 --> 08:52:57,058
bar which says
11914
08:52:57,058 --> 08:53:01,160
so need to call the Constructor here
11915
08:53:04,138 --> 08:53:06,240
all right actually I think I can delete
11916
08:53:06,240 --> 08:53:08,398
this but you still you still need to
11917
08:53:08,398 --> 08:53:09,958
call this Constructor here even though
11918
08:53:09,958 --> 08:53:13,080
we don't have any any parameters any
11919
08:53:13,080 --> 08:53:15,420
even though we didn't even Define The
11920
08:53:15,420 --> 08:53:19,080
Constructor here now we need because
11921
08:53:19,080 --> 08:53:22,138
like the interface the abstract class if
11922
08:53:22,138 --> 08:53:24,000
you inherit from an abstract cluster and
11923
08:53:24,000 --> 08:53:26,040
if that abstract abstract class has some
11924
08:53:26,040 --> 08:53:28,200
abstract abstract functions defined in
11925
08:53:28,200 --> 08:53:31,558
it you need to implement those functions
11926
08:53:31,558 --> 08:53:33,600
inside your class and you go here when
11927
08:53:33,600 --> 08:53:35,940
you have this underline if you click on
11928
08:53:35,940 --> 08:53:37,740
this right red bulb and it says
11929
08:53:37,740 --> 08:53:40,500
Implement members order make car
11930
08:53:40,500 --> 08:53:43,020
abstract and we click on Implement
11931
08:53:43,020 --> 08:53:45,898
members we select our two functions we
11932
08:53:45,898 --> 08:53:47,218
click ok
11933
08:53:47,218 --> 08:53:49,620
and now we have our functions
11934
08:53:49,620 --> 08:53:51,898
implemented inside our class
11935
08:53:51,898 --> 08:53:54,478
so this is what
11936
08:53:54,478 --> 08:53:57,180
Astra classes are and they are as I said
11937
08:53:57,180 --> 08:53:59,940
very similar to interfaces but the only
11938
08:53:59,940 --> 08:54:01,500
difference is that you can Define in
11939
08:54:01,500 --> 08:54:03,840
properties inside the an abstract class
11940
08:54:03,840 --> 08:54:05,638
and you can provide the value to that
11941
08:54:05,638 --> 08:54:10,200
property and here because we we have
11942
08:54:10,200 --> 08:54:12,120
inherited from our abstract class here
11943
08:54:12,120 --> 08:54:13,080
you can provide the actual
11944
08:54:13,080 --> 08:54:15,898
implementation of the functions so as
11945
08:54:15,898 --> 08:54:17,160
you can see we have this to do which
11946
08:54:17,160 --> 08:54:20,100
says not yet implemented so they are
11947
08:54:20,100 --> 08:54:23,100
they are very similar to interfaces
11948
08:54:23,100 --> 08:54:25,260
so I'm gonna end this video now and see
11949
08:54:25,260 --> 08:54:27,420
you in the next video but before I end
11950
08:54:27,420 --> 08:54:30,260
the video I saw that it's a good idea to
11951
08:54:30,260 --> 08:54:33,540
to say to you where abstract classes and
11952
08:54:33,540 --> 08:54:36,780
interfaces are used uh interfaces that
11953
08:54:36,780 --> 08:54:39,958
are using many many places but um
11954
08:54:39,958 --> 08:54:42,200
abstract classes are particularly used
11955
08:54:42,200 --> 08:54:46,080
when uh and also interfaces are used
11956
08:54:46,080 --> 08:54:49,378
when you have some apis and those apis
11957
08:54:49,378 --> 08:54:52,318
want to just to define the functions and
11958
08:54:52,318 --> 08:54:54,478
the API is going to generate the actual
11959
08:54:54,478 --> 08:54:57,240
code for uh for those function so you
11960
08:54:57,240 --> 08:54:59,818
can have a let's say a database
11961
08:54:59,818 --> 08:55:02,040
and you can provide the the name of the
11962
08:55:02,040 --> 08:55:03,840
functions which are responsible for
11963
08:55:03,840 --> 08:55:06,318
creating the database for getting
11964
08:55:06,318 --> 08:55:09,478
getting data from the database but you
11965
08:55:09,478 --> 08:55:11,700
don't provide the actual code which is
11966
08:55:11,700 --> 08:55:13,798
gonna select that data from the database
11967
08:55:13,798 --> 08:55:16,200
and return the data you just you just
11968
08:55:16,200 --> 08:55:18,600
declare your functions as as the
11969
08:55:18,600 --> 08:55:19,740
abstract
11970
08:55:19,740 --> 08:55:22,440
and the API is going to generate all
11971
08:55:22,440 --> 08:55:25,378
that code which is responsible for uh
11972
08:55:25,378 --> 08:55:28,798
returning the data and from the in other
11973
08:55:28,798 --> 08:55:30,360
words the API is going to do all the
11974
08:55:30,360 --> 08:55:32,878
hard work you just you you just Define
11975
08:55:32,878 --> 08:55:36,540
the the functions if you have if you
11976
08:55:36,540 --> 08:55:37,920
let's say if you have a property Define
11977
08:55:37,920 --> 08:55:39,600
an abstract class in the abstract
11978
08:55:39,600 --> 08:55:42,540
functions or interfaces and the API is
11979
08:55:42,540 --> 08:55:45,420
going to do the hard work of uh based on
11980
08:55:45,420 --> 08:55:48,958
what your function is doing by
11981
08:55:48,958 --> 08:55:50,700
editing the name of the function is
11982
08:55:50,700 --> 08:55:52,740
going to generate all that code which
11983
08:55:52,740 --> 08:55:56,040
you you've you've had to generate if you
11984
08:55:56,040 --> 08:55:58,440
do if you don't have if you're not
11985
08:55:58,440 --> 08:56:00,240
before the API to generate that for you
11986
08:56:00,240 --> 08:56:04,200
so this is where uh interfaces and after
11987
08:56:04,200 --> 08:56:07,860
classes are sometimes used and the
11988
08:56:07,860 --> 08:56:10,440
interfaces also are used in event
11989
08:56:10,440 --> 08:56:14,420
handling and that is one in one um uh
11990
08:56:14,420 --> 08:56:17,940
the you need to write code to respond to
11991
08:56:17,940 --> 08:56:20,340
respond to respond to graphical using
11992
08:56:20,340 --> 08:56:25,260
using your interface uh movements
11993
08:56:25,500 --> 08:56:27,600
so I'm gonna end the video now see you
11994
08:56:27,600 --> 08:56:29,340
next
11995
08:56:29,340 --> 08:56:31,020
so now it's time to start a discussion
11996
08:56:31,020 --> 08:56:32,818
about data classes but in order to
11997
08:56:32,818 --> 08:56:34,680
understand what data classes are we
11998
08:56:34,680 --> 08:56:36,180
first need to understand what is the
11999
08:56:36,180 --> 08:56:37,680
difference between structure equality
12000
08:56:37,680 --> 08:56:39,898
versus referential equality
12001
08:56:39,898 --> 08:56:41,580
and to restart the difference I'm going
12002
08:56:41,580 --> 08:56:43,138
to create two variables the first one is
12003
08:56:43,138 --> 08:56:44,700
going to be a Val it's going to be
12004
08:56:44,700 --> 08:56:46,200
called name one
12005
08:56:46,200 --> 08:56:48,660
and we're gonna assign the
12006
08:56:48,660 --> 08:56:50,458
follow Alex three so it's going to be of
12007
08:56:50,458 --> 08:56:51,840
type string
12008
08:56:51,840 --> 08:56:53,458
now I'm going to press Ctrl D to
12009
08:56:53,458 --> 08:56:55,558
duplicate that line of code
12010
08:56:55,558 --> 08:56:57,958
and to declare the second variable which
12011
08:56:57,958 --> 08:56:59,340
is going to be called name two and it's
12012
08:56:59,340 --> 08:57:01,680
also going to have the value RX
12013
08:57:01,680 --> 08:57:03,478
now I'm going to add the printerland
12014
08:57:03,478 --> 08:57:05,580
here to output something to the console
12015
08:57:05,580 --> 08:57:07,878
and here we're gonna type name one
12016
08:57:07,878 --> 08:57:10,200
equals equals
12017
08:57:10,200 --> 08:57:11,458
name to
12018
08:57:11,458 --> 08:57:14,760
so the equals to operator is doing what
12019
08:57:14,760 --> 08:57:17,218
is called is checking for structural
12020
08:57:17,218 --> 08:57:20,040
equality because it's checking to see if
12021
08:57:20,040 --> 08:57:22,260
the content of the variable name 1 is
12022
08:57:22,260 --> 08:57:23,520
the same with the content of the
12023
08:57:23,520 --> 08:57:25,200
variable name too so another loss is
12024
08:57:25,200 --> 08:57:26,818
checking to see if they have the same
12025
08:57:26,818 --> 08:57:30,180
value and it's they have because both
12026
08:57:30,180 --> 08:57:32,398
have Alex assigned to them so now if I
12027
08:57:32,398 --> 08:57:33,840
run this code
12028
08:57:33,840 --> 08:57:35,940
because this is a Boolean expression
12029
08:57:35,940 --> 08:57:37,978
this is going to return true or false
12030
08:57:37,978 --> 08:57:39,120
and we're going to see in the console
12031
08:57:39,120 --> 08:57:40,200
too
12032
08:57:40,200 --> 08:57:43,558
but if I change this to Alexandro now
12033
08:57:43,558 --> 08:57:45,540
the variable name 2 has a different
12034
08:57:45,540 --> 08:57:47,818
value so this is going to return false
12035
08:57:47,818 --> 08:57:50,040
because they are not structurally equal
12036
08:57:50,040 --> 08:57:52,080
their content is different they have
12037
08:57:52,080 --> 08:57:54,798
different values
12038
08:57:55,080 --> 08:57:58,020
now get false and this is called the
12039
08:57:58,020 --> 08:57:59,760
structure equality
12040
08:57:59,760 --> 08:58:02,100
the next type of equality is called
12041
08:58:02,100 --> 08:58:04,500
referential equality and the reference
12042
08:58:04,500 --> 08:58:06,540
equality is used when you want to check
12043
08:58:06,540 --> 08:58:09,898
if two variables or two objects are the
12044
08:58:09,898 --> 08:58:13,378
same so what the referential equality
12045
08:58:13,378 --> 08:58:16,020
operator is doing is checking to see if
12046
08:58:16,020 --> 08:58:18,600
a tool as I said variables or
12047
08:58:18,600 --> 08:58:20,580
objects are the same so if I type here
12048
08:58:20,580 --> 08:58:22,860
name one
12049
08:58:22,860 --> 08:58:25,138
and to use the the reference we call it
12050
08:58:25,138 --> 08:58:27,240
operator we put three equal sign here
12051
08:58:27,240 --> 08:58:29,520
and if I type here name one equals name
12052
08:58:29,520 --> 08:58:32,760
one so is name one equals to itself that
12053
08:58:32,760 --> 08:58:34,740
is going to be true
12054
08:58:34,740 --> 08:58:36,000
so you're gonna see through on the
12055
08:58:36,000 --> 08:58:37,200
console
12056
08:58:37,200 --> 08:58:40,798
outputted so we have two but if I change
12057
08:58:40,798 --> 08:58:43,920
name one equals equals equals to name
12058
08:58:43,920 --> 08:58:46,138
two that is going to return false
12059
08:58:46,138 --> 08:58:48,298
because they are distinct objects in
12060
08:58:48,298 --> 08:58:51,660
memory they are different uh variables
12061
08:58:51,660 --> 08:58:55,200
they are not the same there is the name
12062
08:58:55,200 --> 08:58:56,100
one
12063
08:58:56,100 --> 08:58:57,840
name one variable is not the same
12064
08:58:57,840 --> 08:58:59,580
variable as name two so we're gonna have
12065
08:58:59,580 --> 08:59:00,958
false here
12066
08:59:00,958 --> 08:59:02,700
so we have false so this is the
12067
08:59:02,700 --> 08:59:04,500
difference between a structural versus
12068
08:59:04,500 --> 08:59:06,898
referential equality so now I'm going to
12069
08:59:06,898 --> 08:59:08,280
close this
12070
08:59:08,280 --> 08:59:10,440
I'm going to close the console
12071
08:59:10,440 --> 08:59:12,898
and having this in mind we're going to
12072
08:59:12,898 --> 08:59:15,958
start a discussion about data classes
12073
08:59:15,958 --> 08:59:17,878
so let's go down here and let's create
12074
08:59:17,878 --> 08:59:21,240
our user class because I want to compare
12075
08:59:21,240 --> 08:59:23,160
to user objects to see if they are
12076
08:59:23,160 --> 08:59:25,798
structure equals so you type user let's
12077
08:59:25,798 --> 08:59:27,958
define the primary Constructor and let's
12078
08:59:27,958 --> 08:59:30,000
define three properties for the primary
12079
08:59:30,000 --> 08:59:31,620
Constructor the
12080
08:59:31,620 --> 08:59:34,579
first name
12081
08:59:36,298 --> 08:59:39,318
the last name
12082
08:59:39,540 --> 08:59:42,378
and the age
12083
08:59:46,558 --> 08:59:48,540
which is going to be an integer
12084
08:59:48,540 --> 08:59:51,420
now if I want to check if to
12085
08:59:51,420 --> 08:59:53,520
user objects are a star vehicle let's
12086
08:59:53,520 --> 08:59:55,260
first create those objects so let's
12087
08:59:55,260 --> 08:59:58,100
delete this code
12088
08:59:59,760 --> 09:00:02,638
let's say Val user1
12089
09:00:02,638 --> 09:00:04,978
equals now let's create our object and
12090
09:00:04,978 --> 09:00:06,958
let's pass some values to the
12091
09:00:06,958 --> 09:00:08,818
properties of the Constructor let's pass
12092
09:00:08,818 --> 09:00:11,360
here Alex
12093
09:00:13,378 --> 09:00:16,740
for the last name let's put open
12094
09:00:16,740 --> 09:00:19,798
and for the age 23.
12095
09:00:19,798 --> 09:00:21,600
now let's press Ctrl D to duplicate that
12096
09:00:21,600 --> 09:00:23,160
line of code and to create the second
12097
09:00:23,160 --> 09:00:26,040
user object let's give a different name
12098
09:00:26,040 --> 09:00:28,878
to this user too
12099
09:00:29,940 --> 09:00:33,000
now go down here let's press Ctrl alt
12100
09:00:33,000 --> 09:00:35,160
alt for mother code
12101
09:00:35,160 --> 09:00:37,440
we typed our println because I want to
12102
09:00:37,440 --> 09:00:39,958
check to see if our users objects are
12103
09:00:39,958 --> 09:00:40,798
equal
12104
09:00:40,798 --> 09:00:42,360
if they are structurally equal so we
12105
09:00:42,360 --> 09:00:43,740
type here user1
12106
09:00:43,740 --> 09:00:46,040
equals equals so the equals to operator
12107
09:00:46,040 --> 09:00:48,600
user 2.
12108
09:00:48,600 --> 09:00:51,298
now if you run this code what do you
12109
09:00:51,298 --> 09:00:53,340
think you're going to see in the output
12110
09:00:53,340 --> 09:00:54,718
what you're seeing the output is
12111
09:00:54,718 --> 09:00:57,478
actually that you'll get
12112
09:00:57,478 --> 09:01:00,240
we get false and you may be wondering
12113
09:01:00,240 --> 09:01:01,978
why because if you think about the air
12114
09:01:01,978 --> 09:01:03,058
structurally because they have the same
12115
09:01:03,058 --> 09:01:05,160
values for the properties but they are a
12116
09:01:05,160 --> 09:01:09,240
distinct objects but we said previously
12117
09:01:09,240 --> 09:01:11,398
that the equals
12118
09:01:11,398 --> 09:01:13,620
equals uh two equals operator is
12119
09:01:13,620 --> 09:01:15,898
checking for uh structure equality so
12120
09:01:15,898 --> 09:01:17,818
why get false here
12121
09:01:17,818 --> 09:01:20,040
this is because previously the equals to
12122
09:01:20,040 --> 09:01:22,700
operator is
12123
09:01:22,700 --> 09:01:25,318
it was checking to see if the two
12124
09:01:25,318 --> 09:01:27,840
strings are structurally equal and the
12125
09:01:27,840 --> 09:01:30,420
equals to operator is actually
12126
09:01:30,420 --> 09:01:33,000
a function which is defined inside the
12127
09:01:33,000 --> 09:01:35,398
any class and the string class what
12128
09:01:35,398 --> 09:01:37,080
actually was doing when we called the
12129
09:01:37,080 --> 09:01:38,700
equals to operator
12130
09:01:38,700 --> 09:01:42,298
it was using its own implementation of
12131
09:01:42,298 --> 09:01:45,058
the equals method so
12132
09:01:45,058 --> 09:01:49,620
uh every class that we create in kotlin
12133
09:01:49,620 --> 09:01:53,040
it's using its implicitly inheriting
12134
09:01:53,040 --> 09:01:54,898
from the any class and because every
12135
09:01:54,898 --> 09:01:56,458
class that we created it's implicitly
12136
09:01:56,458 --> 09:01:58,378
inheriting from the any class
12137
09:01:58,378 --> 09:02:01,440
every class is also going to use the
12138
09:02:01,440 --> 09:02:03,840
functions which are inside the any class
12139
09:02:03,840 --> 09:02:06,600
so if you don't override those functions
12140
09:02:06,600 --> 09:02:08,398
inside your class to provide your own
12141
09:02:08,398 --> 09:02:10,318
implementation like the string class did
12142
09:02:10,318 --> 09:02:12,958
for our strengths then it's going to use
12143
09:02:12,958 --> 09:02:16,200
the implicit uh the font is going to use
12144
09:02:16,200 --> 09:02:17,580
the function which inside which are
12145
09:02:17,580 --> 09:02:20,340
inside the any class and
12146
09:02:20,340 --> 09:02:24,780
if you type here user1 that and we have
12147
09:02:24,780 --> 09:02:26,818
equals to string and hashcode those
12148
09:02:26,818 --> 09:02:28,920
three but those three functions we are
12149
09:02:28,920 --> 09:02:30,360
particularly interested and the
12150
09:02:30,360 --> 09:02:32,100
particular one is interested inside the
12151
09:02:32,100 --> 09:02:34,798
equals they perform when you don't
12152
09:02:34,798 --> 09:02:37,020
Implement inside your own class those
12153
09:02:37,020 --> 09:02:38,700
functions
12154
09:02:38,700 --> 09:02:40,500
again particularly on Bay course class
12155
09:02:40,500 --> 09:02:43,200
the equals class spin forums only a
12156
09:02:43,200 --> 09:02:45,120
referential equality so it only checks
12157
09:02:45,120 --> 09:02:46,920
if those two users
12158
09:02:46,920 --> 09:02:49,620
are the same users and if they're not
12159
09:02:49,620 --> 09:02:52,020
that's gonna it's gonna return false so
12160
09:02:52,020 --> 09:02:53,580
this is why you get false here because
12161
09:02:53,580 --> 09:02:56,280
it's calling the function the function
12162
09:02:56,280 --> 09:02:58,318
which is inside any class so what you
12163
09:02:58,318 --> 09:03:01,020
have implicitly is something like this
12164
09:03:01,020 --> 09:03:03,958
so for any class that you you use or for
12165
09:03:03,958 --> 09:03:05,700
any class that you declare
12166
09:03:05,700 --> 09:03:07,920
you have implicity inherited from the
12167
09:03:07,920 --> 09:03:10,020
any class and because of that you have
12168
09:03:10,020 --> 09:03:11,398
those
12169
09:03:11,398 --> 09:03:13,138
three functions which
12170
09:03:13,138 --> 09:03:15,898
are defined and if you don't override
12171
09:03:15,898 --> 09:03:17,638
them to provide your own implementation
12172
09:03:17,638 --> 09:03:19,378
then it's going to use the code that's
12173
09:03:19,378 --> 09:03:21,660
inside there so actually if you press
12174
09:03:21,660 --> 09:03:23,160
Ctrl here
12175
09:03:23,160 --> 09:03:24,780
and if you
12176
09:03:24,780 --> 09:03:27,180
click it takes us inside the any class
12177
09:03:27,180 --> 09:03:30,718
so it's clear that this function
12178
09:03:30,718 --> 09:03:33,260
the equals function is inside any class
12179
09:03:33,260 --> 09:03:36,058
so because we don't have any functions
12180
09:03:36,058 --> 09:03:37,080
here so we need to override that
12181
09:03:37,080 --> 09:03:39,120
function inside our class and we need to
12182
09:03:39,120 --> 09:03:40,978
provide our own implementation to Define
12183
09:03:40,978 --> 09:03:44,520
our own code to determine if two user
12184
09:03:44,520 --> 09:03:46,978
are one of when two users are slash
12185
09:03:46,978 --> 09:03:48,420
legal this is what you're going to
12186
09:03:48,420 --> 09:03:51,780
Define inside our class so let's delete
12187
09:03:51,780 --> 09:03:53,580
this
12188
09:03:53,580 --> 09:03:55,378
let's pull the curly braces and actually
12189
09:03:55,378 --> 09:03:57,540
I'm gonna bring an image to show you how
12190
09:03:57,540 --> 09:04:00,420
the quarter and hierarchy actually looks
12191
09:04:00,420 --> 09:04:02,940
so this is the image and at the top as
12192
09:04:02,940 --> 09:04:04,558
you can see we have the any class and
12193
09:04:04,558 --> 09:04:06,718
then we have our Boolean class our
12194
09:04:06,718 --> 09:04:08,638
string class and then we have our user
12195
09:04:08,638 --> 09:04:10,378
class and all of those classes I have
12196
09:04:10,378 --> 09:04:12,718
here the Boolean the string the number
12197
09:04:12,718 --> 09:04:14,700
they are inheriting from the any class
12198
09:04:14,700 --> 09:04:16,138
they are
12199
09:04:16,138 --> 09:04:18,600
using the the functions which are
12200
09:04:18,600 --> 09:04:21,058
defined inside the any class and they
12201
09:04:21,058 --> 09:04:23,218
override the they are overriding those
12202
09:04:23,218 --> 09:04:25,260
methods inside them and they are
12203
09:04:25,260 --> 09:04:27,420
providing their own implementation for
12204
09:04:27,420 --> 09:04:29,760
those functions and this is what we need
12205
09:04:29,760 --> 09:04:31,378
to do also here as you can see they will
12206
09:04:31,378 --> 09:04:34,620
inherit from the any class implicitly so
12207
09:04:34,620 --> 09:04:36,478
we need to override them to provide our
12208
09:04:36,478 --> 09:04:39,240
own implementation for the equals and
12209
09:04:39,240 --> 09:04:40,500
for the next two because we're going to
12210
09:04:40,500 --> 09:04:42,000
look at also at the
12211
09:04:42,000 --> 09:04:44,340
as you saw there you are going to look
12212
09:04:44,340 --> 09:04:46,860
also at hash code because uh equals and
12213
09:04:46,860 --> 09:04:49,378
hash code are very linked together and
12214
09:04:49,378 --> 09:04:51,360
also the two string function
12215
09:04:51,360 --> 09:04:53,878
so we need to override those uh inside
12216
09:04:53,878 --> 09:04:54,958
our class
12217
09:04:54,958 --> 09:04:57,420
so this is what we're gonna do
12218
09:04:57,420 --> 09:04:59,520
so we go inside our user class and here
12219
09:04:59,520 --> 09:05:01,200
we need to override the equals function
12220
09:05:01,200 --> 09:05:03,420
when we type overwrite and while I'm
12221
09:05:03,420 --> 09:05:05,218
dumping you can see that we have three
12222
09:05:05,218 --> 09:05:07,200
suggestions here and we select the user
12223
09:05:07,200 --> 09:05:09,180
function because this is the one that we
12224
09:05:09,180 --> 09:05:11,218
are interested in and as you can see
12225
09:05:11,218 --> 09:05:12,120
here
12226
09:05:12,120 --> 09:05:15,120
this function is part of the any class
12227
09:05:15,120 --> 09:05:18,058
so we press enter we delete the super
12228
09:05:18,058 --> 09:05:19,260
return
12229
09:05:19,260 --> 09:05:20,700
and the first thing that we need to
12230
09:05:20,700 --> 09:05:23,340
check is to see if the current instance
12231
09:05:23,340 --> 09:05:25,620
the current object that on which this
12232
09:05:25,620 --> 09:05:28,700
equals function is called it's comparing
12233
09:05:28,700 --> 09:05:33,718
with its cell so we need to check if the
12234
09:05:33,718 --> 09:05:36,840
the if the object is the same as the
12235
09:05:36,840 --> 09:05:38,760
object that we are comparing with and to
12236
09:05:38,760 --> 09:05:41,540
do that we type here if this
12237
09:05:41,540 --> 09:05:45,120
so if this current object is current
12238
09:05:45,120 --> 09:05:48,360
instance equals equals so three equals
12239
09:05:48,360 --> 09:05:50,398
we check for referential equality to see
12240
09:05:50,398 --> 09:05:53,818
if they are the same equals
12241
09:05:53,818 --> 09:05:56,818
other so
12242
09:05:56,818 --> 09:05:59,818
we type other and if that is the case if
12243
09:05:59,818 --> 09:06:02,218
we call we are comparing the same object
12244
09:06:02,218 --> 09:06:05,940
so you have user one equals user one
12245
09:06:05,940 --> 09:06:08,339
then gonna return true because that
12246
09:06:08,339 --> 09:06:11,758
means that uh we are comparing uh with
12247
09:06:11,758 --> 09:06:15,480
the same object if that is not true then
12248
09:06:15,480 --> 09:06:17,820
the next thing that we need to check we
12249
09:06:17,820 --> 09:06:21,000
we're gonna go down here so if not we're
12250
09:06:21,000 --> 09:06:23,218
not comparing with uh it's not comparing
12251
09:06:23,218 --> 09:06:27,000
with itself then we need to check if the
12252
09:06:27,000 --> 09:06:29,640
other so the object which is passed as
12253
09:06:29,640 --> 09:06:31,800
an argument to this function which is
12254
09:06:31,800 --> 09:06:34,557
called on another object
12255
09:06:34,557 --> 09:06:37,980
and to check if other so the argument
12256
09:06:37,980 --> 09:06:42,417
is user because we we can uh we need to
12257
09:06:42,417 --> 09:06:44,519
check the expressly to see if this
12258
09:06:44,519 --> 09:06:46,377
object which is passed as an argument to
12259
09:06:46,377 --> 09:06:49,199
our equals function is an user object
12260
09:06:49,199 --> 09:06:51,718
because we don't want to check for a
12261
09:06:51,718 --> 09:06:53,820
different object we need we need to to
12262
09:06:53,820 --> 09:06:56,282
see if this is a new zero object so only
12263
09:06:56,282 --> 09:06:58,680
if the object which is passed as an
12264
09:06:58,680 --> 09:07:01,917
argument to the equals function is an
12265
09:07:01,917 --> 09:07:04,377
user object then we're gonna
12266
09:07:04,377 --> 09:07:07,438
return true so here you need to type now
12267
09:07:07,438 --> 09:07:09,300
we need to determine we need to Define
12268
09:07:09,300 --> 09:07:11,519
our Logic for what it means for two
12269
09:07:11,519 --> 09:07:15,417
users to be equal and it's up to you how
12270
09:07:15,417 --> 09:07:16,980
we Define this implementation I'm going
12271
09:07:16,980 --> 09:07:20,758
to type here only return so I'm gonna my
12272
09:07:20,758 --> 09:07:23,339
logic is if they have the same first
12273
09:07:23,339 --> 09:07:25,320
name the same last name and the same age
12274
09:07:25,320 --> 09:07:28,258
we're gonna say that they are equal we
12275
09:07:28,258 --> 09:07:30,300
have the same user of course that in a
12276
09:07:30,300 --> 09:07:32,398
real app you can have a more complex
12277
09:07:32,398 --> 09:07:34,078
implementation so we type here return
12278
09:07:34,078 --> 09:07:37,377
this so This current instance that first
12279
09:07:37,377 --> 09:07:39,237
name so
12280
09:07:39,237 --> 09:07:43,500
and then put two equals other so
12281
09:07:43,500 --> 09:07:46,438
if so the other is because we check here
12282
09:07:46,438 --> 09:07:48,960
explicit to see if this is another also
12283
09:07:48,960 --> 09:07:51,237
because this condition is going to only
12284
09:07:51,237 --> 09:07:54,300
pass if the other
12285
09:07:54,300 --> 09:07:57,718
argument is unusual so we type other
12286
09:07:57,718 --> 09:08:00,782
because we have access to the first name
12287
09:08:00,782 --> 09:08:02,699
and the last name and the age if other
12288
09:08:02,699 --> 09:08:06,122
DOT first name so this is our first
12289
09:08:06,122 --> 09:08:09,140
condition and
12290
09:08:10,199 --> 09:08:13,519
this Dot
12291
09:08:14,938 --> 09:08:18,480
this dot last name
12292
09:08:18,480 --> 09:08:21,718
so I'm going to check for the all uh for
12293
09:08:21,718 --> 09:08:23,098
our three properties
12294
09:08:23,098 --> 09:08:25,980
equals equals other so other is another
12295
09:08:25,980 --> 09:08:29,898
object because we determine that here
12296
09:08:31,140 --> 09:08:33,057
that last name
12297
09:08:33,057 --> 09:08:35,659
and
12298
09:08:37,377 --> 09:08:42,718
this dot age equals equals H so this
12299
09:08:42,718 --> 09:08:44,877
entire expression that is that we have
12300
09:08:44,877 --> 09:08:47,339
here is gonna return through only or if
12301
09:08:47,339 --> 09:08:49,377
all the our three conditionals so if
12302
09:08:49,377 --> 09:08:51,057
this DOT first name is equal to other
12303
09:08:51,057 --> 09:08:53,218
that first name so with the object that
12304
09:08:53,218 --> 09:08:55,800
is passed as an argument or equals and
12305
09:08:55,800 --> 09:08:58,258
if this data last name is equal to other
12306
09:08:58,258 --> 09:09:01,377
that last name and if this dot h is
12307
09:09:01,377 --> 09:09:02,699
equal to
12308
09:09:02,699 --> 09:09:07,098
the other that age let's put here
12309
09:09:07,980 --> 09:09:10,557
then and only then we're gonna have a
12310
09:09:10,557 --> 09:09:11,699
true here so it's going to return
12311
09:09:11,699 --> 09:09:14,699
through otherwise if one of those
12312
09:09:14,699 --> 09:09:17,160
condition fails it's gonna return false
12313
09:09:17,160 --> 09:09:20,282
and down here you need to if other let's
12314
09:09:20,282 --> 09:09:23,339
say doesn't pass so if other reason is
12315
09:09:23,339 --> 09:09:27,360
not an user we're gonna return false so
12316
09:09:27,360 --> 09:09:30,237
in that case going to turn false now if
12317
09:09:30,237 --> 09:09:34,098
I run this code look what happens
12318
09:09:34,438 --> 09:09:37,078
so previously we had the false and now
12319
09:09:37,078 --> 09:09:40,622
we have true here and this is because
12320
09:09:40,622 --> 09:09:43,500
now it's using the equals function which
12321
09:09:43,500 --> 09:09:46,078
is inside our user class so it's using
12322
09:09:46,078 --> 09:09:49,140
our own implementation so it's checked
12323
09:09:49,140 --> 09:09:53,122
here to see if uh our
12324
09:09:53,122 --> 09:09:55,622
first name is equal to other DOT first
12325
09:09:55,622 --> 09:09:58,199
name so it's checking uh so it's using
12326
09:09:58,199 --> 09:10:02,282
our logic inside the the equals function
12327
09:10:02,282 --> 09:10:05,578
so this is why you get true and you can
12328
09:10:05,578 --> 09:10:08,040
also you can now if you press if you
12329
09:10:08,040 --> 09:10:11,160
hold Ctrl here as you can see now it's
12330
09:10:11,160 --> 09:10:13,199
taking us inside the equals method that
12331
09:10:13,199 --> 09:10:15,000
we defined here it's not taking it
12332
09:10:15,000 --> 09:10:17,578
inside the ionic class this is because
12333
09:10:17,578 --> 09:10:19,680
now it's using our own implementation so
12334
09:10:19,680 --> 09:10:21,598
first to check to see if we are dealing
12335
09:10:21,598 --> 09:10:24,237
with the same object then we're going to
12336
09:10:24,237 --> 09:10:27,057
return true you're not going to go any
12337
09:10:27,057 --> 09:10:30,078
any we're not going to go down and if
12338
09:10:30,078 --> 09:10:33,122
this is false you're gonna come up here
12339
09:10:33,122 --> 09:10:34,800
you're gonna check to see if other the
12340
09:10:34,800 --> 09:10:36,300
object which is passed or equals
12341
09:10:36,300 --> 09:10:39,122
function is an user instance it's an
12342
09:10:39,122 --> 09:10:41,578
user object then I'm gonna check to see
12343
09:10:41,578 --> 09:10:44,519
if our current object that we are
12344
09:10:44,519 --> 09:10:46,800
calling on the sqls
12345
09:10:46,800 --> 09:10:49,140
function is equal which exclusive for
12346
09:10:49,140 --> 09:10:51,598
the first name there is the same we
12347
09:10:51,598 --> 09:10:53,040
check to see if the last name is the
12348
09:10:53,040 --> 09:10:54,898
same and we check to see if the age is
12349
09:10:54,898 --> 09:10:56,098
also the same then in that case it's
12350
09:10:56,098 --> 09:10:58,680
going to return true and we're gonna say
12351
09:10:58,680 --> 09:11:00,057
that they are structurally equal so
12352
09:11:00,057 --> 09:11:01,680
we're gonna see through in the output
12353
09:11:01,680 --> 09:11:04,500
else if other is not an user so if you
12354
09:11:04,500 --> 09:11:06,122
pass
12355
09:11:06,122 --> 09:11:08,160
to the function argument the different
12356
09:11:08,160 --> 09:11:10,737
parameter a different argument here
12357
09:11:10,737 --> 09:11:12,660
let's say you pass an integer then it's
12358
09:11:12,660 --> 09:11:14,519
going to return false here we're going
12359
09:11:14,519 --> 09:11:17,398
to come down here so now if you look
12360
09:11:17,398 --> 09:11:18,960
here you may be wondering how this
12361
09:11:18,960 --> 09:11:20,758
syntax works so we have those signs but
12362
09:11:20,758 --> 09:11:23,339
it's calling the equal smart function so
12363
09:11:23,339 --> 09:11:26,699
you can also put that equals if it makes
12364
09:11:26,699 --> 09:11:29,040
more sense and if we put that equals
12365
09:11:29,040 --> 09:11:31,377
user so
12366
09:11:31,377 --> 09:11:33,480
so uh
12367
09:11:33,480 --> 09:11:36,718
now you can see that but if you hover
12368
09:11:36,718 --> 09:11:39,000
over he says replace with the two equals
12369
09:11:39,000 --> 09:11:40,737
so it's the same thing as having that
12370
09:11:40,737 --> 09:11:42,180
equals and
12371
09:11:42,180 --> 09:11:45,122
passing that argument here which is user
12372
09:11:45,122 --> 09:11:48,360
two so again we are invoking uh the
12373
09:11:48,360 --> 09:11:51,839
equals method on the user one object
12374
09:11:51,839 --> 09:11:53,699
let's press Ctrl J
12375
09:11:53,699 --> 09:11:56,282
and we are passing our user to objects
12376
09:11:56,282 --> 09:11:58,860
and we are using our logic that we
12377
09:11:58,860 --> 09:12:00,718
Define here to determine if they are
12378
09:12:00,718 --> 09:12:02,898
equal
12379
09:12:04,377 --> 09:12:06,480
and you can replace this back to to
12380
09:12:06,480 --> 09:12:10,040
equals or if it makes more
12381
09:12:10,040 --> 09:12:12,782
visual sense to put the to use the
12382
09:12:12,782 --> 09:12:14,699
equals you can use equals it's the same
12383
09:12:14,699 --> 09:12:16,320
thing
12384
09:12:16,320 --> 09:12:19,699
we put user to here
12385
09:12:20,578 --> 09:12:23,098
so as you can see we are calling the
12386
09:12:23,098 --> 09:12:26,519
function so this means this object so
12387
09:12:26,519 --> 09:12:30,180
this object so defined here this if uh
12388
09:12:30,180 --> 09:12:32,578
when we're saying this that first name
12389
09:12:32,578 --> 09:12:36,237
it we we mean this object so if this is
12390
09:12:36,237 --> 09:12:39,237
that first name it's equal with the
12391
09:12:39,237 --> 09:12:42,898
other other that first name it's then we
12392
09:12:42,898 --> 09:12:44,578
are calling this object that is passed
12393
09:12:44,578 --> 09:12:47,820
here as an argument so
12394
09:12:47,820 --> 09:12:49,680
um next we will look we're going to look
12395
09:12:49,680 --> 09:12:52,078
at the next two functions because as I
12396
09:12:52,078 --> 09:12:54,417
said the equals and hash codes are very
12397
09:12:54,417 --> 09:12:56,339
linked together and we're going to also
12398
09:12:56,339 --> 09:13:00,480
look at the the two string function and
12399
09:13:00,480 --> 09:13:04,622
now if I change the first name for the
12400
09:13:04,622 --> 09:13:07,377
user 2 to join and if I run this code
12401
09:13:07,377 --> 09:13:09,480
you will get false in the output because
12402
09:13:09,480 --> 09:13:11,758
when it will come inside this if it will
12403
09:13:11,758 --> 09:13:13,019
check to see if this does first name
12404
09:13:13,019 --> 09:13:15,417
equals to other at first name and
12405
09:13:15,417 --> 09:13:17,460
because we're using the end operator and
12406
09:13:17,460 --> 09:13:19,019
all the conditions have to be true this
12407
09:13:19,019 --> 09:13:20,877
is going to return false so I get false
12408
09:13:20,877 --> 09:13:22,140
in the output
12409
09:13:22,140 --> 09:13:24,660
and um
12410
09:13:24,660 --> 09:13:28,878
but if I change it back to Alex
12411
09:13:28,917 --> 09:13:30,718
we'll have
12412
09:13:30,718 --> 09:13:33,300
true so the objects are structurally
12413
09:13:33,300 --> 09:13:35,820
equal based on the logic that we defined
12414
09:13:35,820 --> 09:13:37,199
here
12415
09:13:37,199 --> 09:13:40,199
inside our equals function
12416
09:13:40,199 --> 09:13:43,377
and as I said you also
12417
09:13:43,377 --> 09:13:45,360
you can also look at equals and hashcode
12418
09:13:45,360 --> 09:13:47,398
because they are very linked together
12419
09:13:47,398 --> 09:13:48,960
and uh
12420
09:13:48,960 --> 09:13:52,078
the rule is whenever you override equals
12421
09:13:52,078 --> 09:13:54,300
you also need to override the the
12422
09:13:54,300 --> 09:13:56,877
hashcode and if two objects are
12423
09:13:56,877 --> 09:13:58,980
comparing equal so if they are equal
12424
09:13:58,980 --> 09:14:00,782
based on the equals method that you
12425
09:14:00,782 --> 09:14:03,237
implemented they also have to have the
12426
09:14:03,237 --> 09:14:04,680
same hash code
12427
09:14:04,680 --> 09:14:08,098
and I'm not going to go into that but
12428
09:14:08,098 --> 09:14:11,160
this is because they are
12429
09:14:11,160 --> 09:14:13,680
the hash code is used for performers
12430
09:14:13,680 --> 09:14:16,438
reasons in collections but we're not to
12431
09:14:16,438 --> 09:14:19,980
go now into that we can just type here
12432
09:14:19,980 --> 09:14:22,820
override
12433
09:14:23,218 --> 09:14:24,480
and uh
12434
09:14:24,480 --> 09:14:27,300
here we just type return zero and this
12435
09:14:27,300 --> 09:14:29,339
uh this is not gonna affect our equality
12436
09:14:29,339 --> 09:14:32,218
at all as I said this hash code it will
12437
09:14:32,218 --> 09:14:34,860
make it makes more sense to implement
12438
09:14:34,860 --> 09:14:36,480
the hash code in a different way in
12439
09:14:36,480 --> 09:14:39,057
collections because they are used um for
12440
09:14:39,057 --> 09:14:41,160
performance reasons particularly with
12441
09:14:41,160 --> 09:14:43,860
hash set and hashmobs
12442
09:14:43,860 --> 09:14:47,218
but uh for now for our simple class
12443
09:14:47,218 --> 09:14:49,258
because we don't use this class inside
12444
09:14:49,258 --> 09:14:52,800
any collections we don't need because uh
12445
09:14:52,800 --> 09:14:55,377
we don't uh don't have to think about
12446
09:14:55,377 --> 09:14:58,199
that per the performance reasons to of
12447
09:14:58,199 --> 09:15:01,377
the hashcode and uh just if you want to
12448
09:15:01,377 --> 09:15:03,540
just uh just return zero here like I
12449
09:15:03,540 --> 09:15:05,160
return and yeah because it would work
12450
09:15:05,160 --> 09:15:06,660
fine it's not it's not going to affect
12451
09:15:06,660 --> 09:15:09,718
at all our equality but remember that
12452
09:15:09,718 --> 09:15:13,140
rulebook that if you override the equals
12453
09:15:13,140 --> 09:15:15,540
you also have to write a hash code and
12454
09:15:15,540 --> 09:15:17,460
uh if two objects are comparing equal
12455
09:15:17,460 --> 09:15:19,680
the more must have the same hashcode
12456
09:15:19,680 --> 09:15:22,019
this is true as I said particularly in
12457
09:15:22,019 --> 09:15:24,839
collections not now because we're not
12458
09:15:24,839 --> 09:15:27,360
using this code inside collection
12459
09:15:27,360 --> 09:15:29,460
and we can also override the tostring
12460
09:15:29,460 --> 09:15:31,019
method and this two string method is
12461
09:15:31,019 --> 09:15:33,122
used to return the string representation
12462
09:15:33,122 --> 09:15:36,122
of the class so we also type here
12463
09:15:36,122 --> 09:15:38,519
override and we have our two string
12464
09:15:38,519 --> 09:15:42,237
function I I said method a few times but
12465
09:15:42,237 --> 09:15:44,460
there are functions not methods methods
12466
09:15:44,460 --> 09:15:46,258
are different thing in Java
12467
09:15:46,258 --> 09:15:48,300
and uh
12468
09:15:48,300 --> 09:15:50,460
let's delete this function let's type it
12469
09:15:50,460 --> 09:15:54,078
again so if you can see
12470
09:15:54,122 --> 09:15:55,800
so this is going to return the string
12471
09:15:55,800 --> 09:15:57,898
representation of the class so I need to
12472
09:15:57,898 --> 09:16:00,898
to return here the values that that are
12473
09:16:00,898 --> 09:16:02,938
passed to our to our first name and last
12474
09:16:02,938 --> 09:16:06,360
name and age and if we don't do that if
12475
09:16:06,360 --> 09:16:09,300
you add the print line here
12476
09:16:09,300 --> 09:16:12,237
and you type user1
12477
09:16:12,237 --> 09:16:15,540
let's press Ctrl D and user 2. you're
12478
09:16:15,540 --> 09:16:18,860
gonna see just some
12479
09:16:19,140 --> 09:16:20,938
you're gonna see the user and you're
12480
09:16:20,938 --> 09:16:23,098
gonna see some numbers but we're not
12481
09:16:23,098 --> 09:16:24,898
gonna see the string representation of
12482
09:16:24,898 --> 09:16:27,000
the those two users you're not going to
12483
09:16:27,000 --> 09:16:29,578
see Alex so the name and the age so you
12484
09:16:29,578 --> 09:16:31,019
see this
12485
09:16:31,019 --> 09:16:33,898
dosto but uh
12486
09:16:33,898 --> 09:16:36,360
to avoid that because this also use
12487
09:16:36,360 --> 09:16:38,578
useful because previously when we want
12488
09:16:38,578 --> 09:16:40,320
to
12489
09:16:40,320 --> 09:16:41,938
um output in the console the values of
12490
09:16:41,938 --> 09:16:43,199
the properties you have to call those
12491
09:16:43,199 --> 09:16:45,839
properties and to add multiple Sprint
12492
09:16:45,839 --> 09:16:48,417
lines but with the two string
12493
09:16:48,417 --> 09:16:52,737
function we can just type here return
12494
09:16:52,737 --> 09:16:54,839
and type here
12495
09:16:54,839 --> 09:16:57,621
user
12496
09:16:58,320 --> 09:17:03,078
we put parenthesis and here we type
12497
09:17:03,237 --> 09:17:06,140
first name
12498
09:17:12,660 --> 09:17:15,480
we put equals we put dollar sign
12499
09:17:15,480 --> 09:17:18,237
let's put a single quotation mark dollar
12500
09:17:18,237 --> 09:17:19,737
sign
12501
09:17:19,737 --> 09:17:22,577
first name
12502
09:17:23,098 --> 09:17:24,839
let's put the quotation marks here a
12503
09:17:24,839 --> 09:17:26,699
single quotation marks
12504
09:17:26,699 --> 09:17:29,460
last name
12505
09:17:29,460 --> 09:17:33,540
equals dollar sign last name let's put
12506
09:17:33,540 --> 09:17:37,938
inside the single quotation marks this
12507
09:17:40,320 --> 09:17:44,758
comma and age we put also equals dollar
12508
09:17:44,758 --> 09:17:46,320
sign h
12509
09:17:46,320 --> 09:17:49,377
now if you run this code again now it's
12510
09:17:49,377 --> 09:17:52,699
going to use it's gonna one
12511
09:17:52,800 --> 09:17:55,860
when we are typing user one and user two
12512
09:17:55,860 --> 09:17:57,839
is going to use the tostring function
12513
09:17:57,839 --> 09:18:00,480
that we defined inside your class and
12514
09:18:00,480 --> 09:18:01,737
it's gonna return the string
12515
09:18:01,737 --> 09:18:04,800
representation of that specific object
12516
09:18:04,800 --> 09:18:07,878
so if you run this code
12517
09:18:13,980 --> 09:18:17,218
we get user and get first name Alex last
12518
09:18:17,218 --> 09:18:19,500
name Adobe in h23 and get for the second
12519
09:18:19,500 --> 09:18:21,237
you use their first name Alex last name
12520
09:18:21,237 --> 09:18:23,460
domain and H22 so now get the string
12521
09:18:23,460 --> 09:18:25,737
representation of this class and this is
12522
09:18:25,737 --> 09:18:27,718
uh more beautiful because you don't have
12523
09:18:27,718 --> 09:18:31,282
to work to always type that user one
12524
09:18:31,282 --> 09:18:34,680
user1.a user1. last name the first name
12525
09:18:34,680 --> 09:18:37,140
and so on and for the second object two
12526
09:18:37,140 --> 09:18:40,140
because now it's using the
12527
09:18:40,140 --> 09:18:42,360
two string function with our own
12528
09:18:42,360 --> 09:18:44,820
implementation inside here so this is
12529
09:18:44,820 --> 09:18:46,820
how you can use the tostring function
12530
09:18:46,820 --> 09:18:49,557
and this also how you can use the
12531
09:18:49,557 --> 09:18:53,820
password but as I said hashcode is it's
12532
09:18:53,820 --> 09:18:55,199
a different discussion which you're
12533
09:18:55,199 --> 09:18:56,699
gonna have when we're going to talk
12534
09:18:56,699 --> 09:18:59,218
about collections for now just type
12535
09:18:59,218 --> 09:19:01,860
return 0 here and then we could all fine
12536
09:19:01,860 --> 09:19:04,500
and if you think about there is a lot of
12537
09:19:04,500 --> 09:19:08,160
code just to compare to two objects we
12538
09:19:08,160 --> 09:19:10,622
have almost
12539
09:19:10,622 --> 09:19:13,860
38 line of hold just to to determine if
12540
09:19:13,860 --> 09:19:16,400
two objects two user objects are equal
12541
09:19:16,400 --> 09:19:20,220
and we also have to override this not
12542
09:19:20,220 --> 09:19:22,020
necessary but it will also variety this
12543
09:19:22,020 --> 09:19:24,720
hash code and the two strings so there
12544
09:19:24,720 --> 09:19:27,300
is a lot of code just to do this uh
12545
09:19:27,300 --> 09:19:29,640
simple thing to compare it is to user
12546
09:19:29,640 --> 09:19:32,160
objects are equal and because kotlin is
12547
09:19:32,160 --> 09:19:34,620
a byte it's about conciseness we don't
12548
09:19:34,620 --> 09:19:36,416
actually need to do all of this stuff
12549
09:19:36,416 --> 09:19:39,860
but I I thought I thought that it is a
12550
09:19:39,860 --> 09:19:42,416
okay to explain to you what you'll need
12551
09:19:42,416 --> 09:19:43,916
to do in the past in order to understand
12552
09:19:43,916 --> 09:19:46,320
what now questioning is doing better
12553
09:19:46,320 --> 09:19:49,680
especially with data classes so you
12554
09:19:49,680 --> 09:19:51,660
don't need to
12555
09:19:51,660 --> 09:19:54,060
type yourself all of this code that you
12556
09:19:54,060 --> 09:19:56,400
have here you can just so if I delete
12557
09:19:56,400 --> 09:19:58,916
all of this code let's copy it first and
12558
09:19:58,916 --> 09:20:00,240
then delete it
12559
09:20:00,240 --> 09:20:04,256
so if I delete all of this code also the
12560
09:20:04,256 --> 09:20:07,580
and now if I run this
12561
09:20:08,220 --> 09:20:13,020
now we get false and we get those we get
12562
09:20:13,020 --> 09:20:15,416
user and this tag text because now we
12563
09:20:15,416 --> 09:20:16,980
don't have an implementation of the
12564
09:20:16,980 --> 09:20:19,200
equals of the two string of the hashcode
12565
09:20:19,200 --> 09:20:21,480
and
12566
09:20:21,480 --> 09:20:23,756
if you think as I said it's a lot of
12567
09:20:23,756 --> 09:20:26,040
code to implement all of that and in
12568
09:20:26,040 --> 09:20:29,040
kotlin you can just type the data
12569
09:20:29,040 --> 09:20:31,680
keyword in front of the class and all of
12570
09:20:31,680 --> 09:20:34,200
that code not specifically the same
12571
09:20:34,200 --> 09:20:35,936
logic but all of that code that we typed
12572
09:20:35,936 --> 09:20:37,220
there is going to be generated
12573
09:20:37,220 --> 09:20:40,140
automatically for you so the equals
12574
09:20:40,140 --> 09:20:42,120
method is going to be generated the hash
12575
09:20:42,120 --> 09:20:44,640
code and the tostring and uh
12576
09:20:44,640 --> 09:20:46,620
the properties which are going to be
12577
09:20:46,620 --> 09:20:48,416
included inside the implementation of
12578
09:20:48,416 --> 09:20:51,900
the equals of the inside the of the hash
12579
09:20:51,900 --> 09:20:53,700
code and inside of the tostring are
12580
09:20:53,700 --> 09:20:56,160
gonna beat all the properties defined
12581
09:20:56,160 --> 09:20:59,280
inside the property Constructor so all
12582
09:20:59,280 --> 09:21:00,720
the properties defined inside the
12583
09:21:00,720 --> 09:21:03,000
property inside the primary Constructor
12584
09:21:03,000 --> 09:21:04,560
they're going to be used inside the
12585
09:21:04,560 --> 09:21:06,180
implementation of the equals of the
12586
09:21:06,180 --> 09:21:09,360
hashcode and the tostring so we just by
12587
09:21:09,360 --> 09:21:11,460
putting here the data keyword all of
12588
09:21:11,460 --> 09:21:14,580
that code all of that logic is going to
12589
09:21:14,580 --> 09:21:17,880
be implemented for us so if you run this
12590
09:21:17,880 --> 09:21:18,900
code now
12591
09:21:18,900 --> 09:21:22,620
so look at this how concise is compared
12592
09:21:22,620 --> 09:21:24,300
to what happens just one line of code
12593
09:21:24,300 --> 09:21:27,060
and if if you run our code with the data
12594
09:21:27,060 --> 09:21:28,740
keyword
12595
09:21:28,740 --> 09:21:30,840
now as you can see we have true and we
12596
09:21:30,840 --> 09:21:33,480
have user first name Alex last name so
12597
09:21:33,480 --> 09:21:36,000
you have the same output as previously
12598
09:21:36,000 --> 09:21:38,640
and just by putting data keyword
12599
09:21:38,640 --> 09:21:42,000
all of that was all of these calls so
12600
09:21:42,000 --> 09:21:46,640
let's press Ctrl J all of this squad
12601
09:21:46,916 --> 09:21:48,960
not specifically
12602
09:21:48,960 --> 09:21:51,120
exactly this code but all of this code
12603
09:21:51,120 --> 09:21:53,096
all the implementation of the equals of
12604
09:21:53,096 --> 09:21:54,540
the harsh code entry was generated
12605
09:21:54,540 --> 09:21:58,560
automatically for us just by uh just by
12606
09:21:58,560 --> 09:22:00,480
putting the data keyword in front of our
12607
09:22:00,480 --> 09:22:01,380
class
12608
09:22:01,380 --> 09:22:04,020
and if you don't want to have a property
12609
09:22:04,020 --> 09:22:06,960
included in the implementation of the
12610
09:22:06,960 --> 09:22:10,620
of the equals or of the harsh code or of
12611
09:22:10,620 --> 09:22:13,200
the the two string you can just omit it
12612
09:22:13,200 --> 09:22:15,240
from here and you just you declare it
12613
09:22:15,240 --> 09:22:16,560
inside the
12614
09:22:16,560 --> 09:22:19,436
inside the class so if I put let's say
12615
09:22:19,436 --> 09:22:22,740
here so if I delete this because data
12616
09:22:22,740 --> 09:22:25,380
classes cannot have parameters
12617
09:22:25,380 --> 09:22:27,000
and I declared it inside the class
12618
09:22:27,000 --> 09:22:30,180
inside the data class
12619
09:22:30,180 --> 09:22:33,360
VAR age and it will sign a value because
12620
09:22:33,360 --> 09:22:35,276
we cannot let
12621
09:22:35,276 --> 09:22:38,776
the property uninitialized
12622
09:22:38,936 --> 09:22:42,080
let's delete this now
12623
09:22:42,776 --> 09:22:45,660
and if you run this now that property is
12624
09:22:45,660 --> 09:22:47,040
going to be excluded from the
12625
09:22:47,040 --> 09:22:48,660
implementation of the equals the hash
12626
09:22:48,660 --> 09:22:51,240
code and of the two string so we have
12627
09:22:51,240 --> 09:22:54,000
here true but we have a first name Alex
12628
09:22:54,000 --> 09:22:56,756
last name Domi so now the limitation for
12629
09:22:56,756 --> 09:22:58,436
the equals only includes those
12630
09:22:58,436 --> 09:22:59,700
properties the first name and the last
12631
09:22:59,700 --> 09:23:02,340
name and also for the to string function
12632
09:23:02,340 --> 09:23:03,840
we have only the first name and the last
12633
09:23:03,840 --> 09:23:06,720
name the age is excluded but
12634
09:23:06,720 --> 09:23:09,980
let's put this back
12635
09:23:11,096 --> 09:23:13,500
so this is what data classes are they
12636
09:23:13,500 --> 09:23:15,000
are generating
12637
09:23:15,000 --> 09:23:17,580
Auto automatically for you the
12638
09:23:17,580 --> 09:23:20,040
implementation of the equals the harsh
12639
09:23:20,040 --> 09:23:22,436
code the two string also for the copy
12640
09:23:22,436 --> 09:23:24,360
function and for the component function
12641
09:23:24,360 --> 09:23:25,860
but we're going to look at the copy of
12642
09:23:25,860 --> 09:23:28,620
copy function in a separate video and
12643
09:23:28,620 --> 09:23:30,960
they generate all of that code that we
12644
09:23:30,960 --> 09:23:33,020
saw previously that we talked about
12645
09:23:33,020 --> 09:23:35,096
automatically for you by just putting
12646
09:23:35,096 --> 09:23:37,256
the data keyword you put the properties
12647
09:23:37,256 --> 09:23:39,060
inside the primary Constructor and all
12648
09:23:39,060 --> 09:23:40,680
of that implementation is going to be
12649
09:23:40,680 --> 09:23:42,596
generated for you for all the properties
12650
09:23:42,596 --> 09:23:44,120
you find inside the primary construction
12651
09:23:44,120 --> 09:23:47,360
and just that just by one line of code
12652
09:23:47,360 --> 09:23:49,620
all of that logic is generated
12653
09:23:49,620 --> 09:23:52,020
automatically so this is what data
12654
09:23:52,020 --> 09:23:54,596
classes are see you in the next video so
12655
09:23:54,596 --> 09:23:55,800
now it's time to start a discussion
12656
09:23:55,800 --> 09:23:59,120
about interfaces so what are interfaces
12657
09:23:59,120 --> 09:24:00,720
interfaces
12658
09:24:00,720 --> 09:24:03,240
um are you used when you want to have a
12659
09:24:03,240 --> 09:24:06,540
commonly used Behavior shared among
12660
09:24:06,540 --> 09:24:10,020
different classes but you don't
12661
09:24:10,020 --> 09:24:12,480
want to provide the actual code that
12662
09:24:12,480 --> 09:24:15,060
goes inside the class which is going to
12663
09:24:15,060 --> 09:24:17,756
implement that interface but you just
12664
09:24:17,756 --> 09:24:20,756
want to define the the name of the
12665
09:24:20,756 --> 09:24:22,080
function and the parameter of the
12666
09:24:22,080 --> 09:24:22,916
function
12667
09:24:22,916 --> 09:24:26,880
the actual code that goes inside the the
12668
09:24:26,880 --> 09:24:28,916
functions that you define in inside the
12669
09:24:28,916 --> 09:24:30,256
interface
12670
09:24:30,256 --> 09:24:33,300
they are particular to each object that
12671
09:24:33,300 --> 09:24:35,756
implements that specific interface
12672
09:24:35,756 --> 09:24:37,200
so
12673
09:24:37,200 --> 09:24:40,200
let's say that you have
12674
09:24:40,200 --> 09:24:43,256
let's say that we have some classes
12675
09:24:43,256 --> 09:24:45,000
let's say that you have a class car a
12676
09:24:45,000 --> 09:24:47,880
class which is uh which represents a
12677
09:24:47,880 --> 09:24:49,380
truck and you have a class which
12678
09:24:49,380 --> 09:24:50,960
represents
12679
09:24:50,960 --> 09:24:54,480
represents a plane you know that all of
12680
09:24:54,480 --> 09:24:57,660
those three objects have in common the
12681
09:24:57,660 --> 09:25:00,300
same behavior of starting the engine
12682
09:25:00,300 --> 09:25:03,180
let's say and you don't know how each
12683
09:25:03,180 --> 09:25:05,160
particular class is going to start the
12684
09:25:05,160 --> 09:25:08,400
engine but you know that each class is
12685
09:25:08,400 --> 09:25:11,040
gonna have in common this function which
12686
09:25:11,040 --> 09:25:11,880
is
12687
09:25:11,880 --> 09:25:14,040
I'm gonna start the engine again you
12688
09:25:14,040 --> 09:25:16,436
don't care about the particular how each
12689
09:25:16,436 --> 09:25:19,740
particular object is gonna uh what code
12690
09:25:19,740 --> 09:25:23,520
goes inside the or what uh logic goes
12691
09:25:23,520 --> 09:25:25,560
inside The Stand start engine function
12692
09:25:25,560 --> 09:25:28,680
for each of those particular objects but
12693
09:25:28,680 --> 09:25:31,380
to care only about the fact that they
12694
09:25:31,380 --> 09:25:34,436
they share the same behavior they they
12695
09:25:34,436 --> 09:25:37,020
all start the engine but how they start
12696
09:25:37,020 --> 09:25:40,500
the engine is uh particular to
12697
09:25:40,500 --> 09:25:43,436
each class and this is what we do with
12698
09:25:43,436 --> 09:25:46,500
interfaces we only Define the what so
12699
09:25:46,500 --> 09:25:48,960
what what we Define in the interface
12700
09:25:48,960 --> 09:25:50,040
only the
12701
09:25:50,040 --> 09:25:54,000
the the what and the what represents the
12702
09:25:54,000 --> 09:25:58,680
what those classes are gonna have in
12703
09:25:58,680 --> 09:26:01,560
common so you know that they uh gonna
12704
09:26:01,560 --> 09:26:03,416
have a function called start engine but
12705
09:26:03,416 --> 09:26:04,500
you don't
12706
09:26:04,500 --> 09:26:08,756
don't know how each object how each
12707
09:26:08,756 --> 09:26:12,596
class is gonna Implement that logic and
12708
09:26:12,596 --> 09:26:15,240
to that to to for that I'm gonna use an
12709
09:26:15,240 --> 09:26:18,060
interface so we Define what needs to be
12710
09:26:18,060 --> 09:26:20,220
done but we don't Define how it's going
12711
09:26:20,220 --> 09:26:22,436
to be done so to declare an interface we
12712
09:26:22,436 --> 09:26:24,596
go down here at the enclosing calibrates
12713
09:26:24,596 --> 09:26:26,520
of the main function and we type
12714
09:26:26,520 --> 09:26:29,276
interface and while I'm typing you see
12715
09:26:29,276 --> 09:26:31,436
that we have this suggestion we type we
12716
09:26:31,436 --> 09:26:34,740
press enter we can type the entire word
12717
09:26:34,740 --> 09:26:37,560
if you want so interface and I'm going
12718
09:26:37,560 --> 09:26:39,416
to call this internal phase engine and
12719
09:26:39,416 --> 09:26:41,520
the name of the interface should be with
12720
09:26:41,520 --> 09:26:43,436
a capital letter to start a capital
12721
09:26:43,436 --> 09:26:45,300
letter it should be in Pascal case if it
12722
09:26:45,300 --> 09:26:46,916
has multiple words
12723
09:26:46,916 --> 09:26:49,800
and the interfaces cannot have
12724
09:26:49,800 --> 09:26:51,840
Constructors because they cannot be
12725
09:26:51,840 --> 09:26:54,240
instantiated you cannot create you
12726
09:26:54,240 --> 09:26:56,160
cannot create an object with an
12727
09:26:56,160 --> 09:26:59,640
interface interfaces are used only they
12728
09:26:59,640 --> 09:27:01,980
are created only to be implemented by
12729
09:27:01,980 --> 09:27:05,096
classes they cannot be instantiated so
12730
09:27:05,096 --> 09:27:07,500
we need to put curly braces and inside
12731
09:27:07,500 --> 09:27:08,640
the
12732
09:27:08,640 --> 09:27:10,620
the body inside the curly braces we
12733
09:27:10,620 --> 09:27:12,660
Define what needs to be done so we
12734
09:27:12,660 --> 09:27:14,340
Define only
12735
09:27:14,340 --> 09:27:17,640
the function we Define we type the name
12736
09:27:17,640 --> 09:27:18,720
of the function I'm going to call it
12737
09:27:18,720 --> 09:27:19,916
start engine
12738
09:27:19,916 --> 09:27:22,680
but we don't Define the actual code for
12739
09:27:22,680 --> 09:27:24,416
the function so we put parenthesis here
12740
09:27:24,416 --> 09:27:25,980
you can Define parameters if you want
12741
09:27:25,980 --> 09:27:28,680
but we don't put curly braces and we
12742
09:27:28,680 --> 09:27:31,200
Define our logic here because we only
12743
09:27:31,200 --> 09:27:34,080
care what about what needs to be done so
12744
09:27:34,080 --> 09:27:37,320
we only care that each class needs to
12745
09:27:37,320 --> 09:27:39,240
start the engine how it's going to start
12746
09:27:39,240 --> 09:27:41,400
the engine it's up to each particular
12747
09:27:41,400 --> 09:27:42,660
class
12748
09:27:42,660 --> 09:27:46,320
so we go down here we type class and I'm
12749
09:27:46,320 --> 09:27:48,180
going to create a class called car and
12750
09:27:48,180 --> 09:27:49,800
it's going to have a
12751
09:27:49,800 --> 09:27:52,020
a vowel name which is going to be a
12752
09:27:52,020 --> 09:27:53,040
string
12753
09:27:53,040 --> 09:27:56,276
and a vaal color which is also going to
12754
09:27:56,276 --> 09:27:58,140
be a string
12755
09:27:58,140 --> 09:28:00,900
and now to implement the interface we
12756
09:28:00,900 --> 09:28:04,200
put colon and we type engine
12757
09:28:04,200 --> 09:28:06,300
and as you can see we have this engine
12758
09:28:06,300 --> 09:28:09,060
we have this eye for interface here so
12759
09:28:09,060 --> 09:28:11,460
you press enter then we put curly braces
12760
09:28:11,460 --> 09:28:13,740
to define the body of the class and now
12761
09:28:13,740 --> 09:28:16,140
we have this underline here and this
12762
09:28:16,140 --> 09:28:18,480
underline is here to tell us that we
12763
09:28:18,480 --> 09:28:21,480
need to in now we need to implement the
12764
09:28:21,480 --> 09:28:23,220
we need to implement this function
12765
09:28:23,220 --> 09:28:27,540
because the the interface and the class
12766
09:28:27,540 --> 09:28:29,880
have a contract is like a contract and
12767
09:28:29,880 --> 09:28:32,460
the contract says that if you implement
12768
09:28:32,460 --> 09:28:34,800
an interface you need to you must
12769
09:28:34,800 --> 09:28:37,436
Implement you must override the the the
12770
09:28:37,436 --> 09:28:39,480
function so you need to implement the
12771
09:28:39,480 --> 09:28:42,000
function you still will this is why we
12772
09:28:42,000 --> 09:28:44,400
have here here an underline so if I
12773
09:28:44,400 --> 09:28:46,800
click on this underline what if I hover
12774
09:28:46,800 --> 09:28:48,416
over there and I click
12775
09:28:48,416 --> 09:28:50,460
on the light on the red bulb you see
12776
09:28:50,460 --> 09:28:52,140
that it says Implement members so click
12777
09:28:52,140 --> 09:28:54,240
on Implement and have our
12778
09:28:54,240 --> 09:28:57,360
start engine function we click OK and
12779
09:28:57,360 --> 09:29:00,720
now our functions was overrided here and
12780
09:29:00,720 --> 09:29:02,820
we have this to do which is not yet
12781
09:29:02,820 --> 09:29:05,340
implemented and this is there to to tell
12782
09:29:05,340 --> 09:29:07,620
us that this method this function is not
12783
09:29:07,620 --> 09:29:09,720
Yeti implemented so we need to define
12784
09:29:09,720 --> 09:29:11,640
the actual logic here
12785
09:29:11,640 --> 09:29:15,060
and let's create two other classes let's
12786
09:29:15,060 --> 09:29:15,960
um
12787
09:29:15,960 --> 09:29:19,580
call this class track
12788
09:29:21,360 --> 09:29:24,540
and it's also going to have a name a Val
12789
09:29:24,540 --> 09:29:26,520
going to be a string
12790
09:29:26,520 --> 09:29:30,619
and the color also string
12791
09:29:35,276 --> 09:29:37,800
and we're gonna also
12792
09:29:37,800 --> 09:29:40,320
implement the interface so I'm going to
12793
09:29:40,320 --> 09:29:43,140
put colon engine then we put curabase
12794
09:29:43,140 --> 09:29:45,120
now we need to implement because I said
12795
09:29:45,120 --> 09:29:47,580
there is a contact between the interface
12796
09:29:47,580 --> 09:29:49,800
and the class and the contract says that
12797
09:29:49,800 --> 09:29:52,080
you need to implement the function
12798
09:29:52,080 --> 09:29:54,240
so we Implement a function here and we
12799
09:29:54,240 --> 09:29:56,340
create another class
12800
09:29:56,340 --> 09:29:59,479
call plane
12801
09:29:59,820 --> 09:30:04,520
and it's also going to have a Val name
12802
09:30:05,160 --> 09:30:07,200
it's going to be a string
12803
09:30:07,200 --> 09:30:12,240
and the eval color also string
12804
09:30:12,240 --> 09:30:14,340
and here we put colon and we're gonna
12805
09:30:14,340 --> 09:30:16,680
also implement the interface interface
12806
09:30:16,680 --> 09:30:19,200
inside the plane class
12807
09:30:19,200 --> 09:30:22,320
and we put call on engine our interface
12808
09:30:22,320 --> 09:30:24,120
then curly braces and now we have the
12809
09:30:24,120 --> 09:30:26,460
underline because again we there is the
12810
09:30:26,460 --> 09:30:28,320
contract now between the interface and
12811
09:30:28,320 --> 09:30:30,060
the class and that says that we need to
12812
09:30:30,060 --> 09:30:32,820
implement this function inside our plane
12813
09:30:32,820 --> 09:30:34,256
class
12814
09:30:34,256 --> 09:30:36,416
and we have those to do and those to do
12815
09:30:36,416 --> 09:30:39,416
are here to tell us that we need to
12816
09:30:39,416 --> 09:30:41,700
we need to imple we need to provide some
12817
09:30:41,700 --> 09:30:44,640
logic to the to the actual function so
12818
09:30:44,640 --> 09:30:46,140
let's delete those and let's put some
12819
09:30:46,140 --> 09:30:47,160
code here
12820
09:30:47,160 --> 09:30:50,180
so let's delete this
12821
09:30:56,276 --> 09:30:58,740
and let's add the print line here which
12822
09:30:58,740 --> 09:31:03,079
is gonna output some text to the console
12823
09:31:08,756 --> 09:31:10,380
and we're gonna have the text the car
12824
09:31:10,380 --> 09:31:11,820
you
12825
09:31:11,820 --> 09:31:13,560
starting
12826
09:31:13,560 --> 09:31:16,340
the engine
12827
09:31:18,660 --> 09:31:21,900
and I'm gonna copy this because now I'm
12828
09:31:21,900 --> 09:31:24,416
gonna put here some text
12829
09:31:24,416 --> 09:31:27,660
I'm gonna say the
12830
09:31:27,660 --> 09:31:30,300
track is starting the engine
12831
09:31:30,300 --> 09:31:32,640
and here is going to say the plane
12832
09:31:32,640 --> 09:31:35,659
starting the engine
12833
09:31:39,020 --> 09:31:43,080
now what what we have done here and what
12834
09:31:43,080 --> 09:31:45,060
we did here is that
12835
09:31:45,060 --> 09:31:48,540
we know that each class respectively our
12836
09:31:48,540 --> 09:31:51,480
car our tracking and our plane they all
12837
09:31:51,480 --> 09:31:54,480
gonna start the engine so they all gonna
12838
09:31:54,480 --> 09:31:58,140
share this start engine Behavior the one
12839
09:31:58,140 --> 09:32:00,840
all all gonna have this functionality
12840
09:32:00,840 --> 09:32:05,520
what we don't know is how each class how
12841
09:32:05,520 --> 09:32:08,580
each how each object is gonna start the
12842
09:32:08,580 --> 09:32:11,936
engine because they uh they start the
12843
09:32:11,936 --> 09:32:14,580
engine in a different way the engine of
12844
09:32:14,580 --> 09:32:17,220
of a car is different from an engine of
12845
09:32:17,220 --> 09:32:20,756
a truck or form of Lane so we know that
12846
09:32:20,756 --> 09:32:23,040
each each is gonna
12847
09:32:23,040 --> 09:32:26,340
not that each class or each object is
12848
09:32:26,340 --> 09:32:29,220
gonna start the engine but we don't know
12849
09:32:29,220 --> 09:32:31,140
how they're gonna start the engine so
12850
09:32:31,140 --> 09:32:34,800
here we go inside the actual uh feelings
12851
09:32:34,800 --> 09:32:36,720
inside the the function which we type
12852
09:32:36,720 --> 09:32:38,700
the text the car is starting the engine
12853
09:32:38,700 --> 09:32:40,680
the truck is starting the engine and the
12854
09:32:40,680 --> 09:32:43,436
plane is starting the engine so they
12855
09:32:43,436 --> 09:32:45,540
differed in how they start the engine
12856
09:32:45,540 --> 09:32:48,180
but you know that they all gonna they
12857
09:32:48,180 --> 09:32:50,040
all gonna start the engine so this is
12858
09:32:50,040 --> 09:32:52,980
what interfaces allows us to do
12859
09:32:52,980 --> 09:32:55,800
you can define an interface if you know
12860
09:32:55,800 --> 09:33:00,620
that uh multiple uh related or unrelated
12861
09:33:00,620 --> 09:33:03,540
classes or objects are gonna have a
12862
09:33:03,540 --> 09:33:06,480
function shared between them but you
12863
09:33:06,480 --> 09:33:08,580
don't know how each particular class or
12864
09:33:08,580 --> 09:33:12,416
object is gonna em is gonna uh how is
12865
09:33:12,416 --> 09:33:14,400
gonna how it's gonna have the actual
12866
09:33:14,400 --> 09:33:16,916
quad uh how it's gonna write the actual
12867
09:33:16,916 --> 09:33:19,916
code for that uh that behavior so you
12868
09:33:19,916 --> 09:33:22,620
don't you don't care about the how you
12869
09:33:22,620 --> 09:33:24,596
want to care about the what so we know
12870
09:33:24,596 --> 09:33:26,276
that in this case they will start the
12871
09:33:26,276 --> 09:33:28,740
engine but they they start the engine in
12872
09:33:28,740 --> 09:33:31,980
a different way and you can have here
12873
09:33:31,980 --> 09:33:34,256
another class let's say
12874
09:33:34,256 --> 09:33:38,120
that a person which creates another
12875
09:33:38,120 --> 09:33:41,700
another class and the class is gonna be
12876
09:33:41,700 --> 09:33:44,400
called Tesla and in that case again you
12877
09:33:44,400 --> 09:33:46,256
can implement the interface because you
12878
09:33:46,256 --> 09:33:48,960
know that uh the Tesla is also going to
12879
09:33:48,960 --> 09:33:51,180
start the engine you don't know how it's
12880
09:33:51,180 --> 09:33:52,740
going to start the engine but you know
12881
09:33:52,740 --> 09:33:55,916
that uh is going to start the engine and
12882
09:33:55,916 --> 09:33:57,120
how it's going to start the engine is
12883
09:33:57,120 --> 09:34:00,416
specific to a Tesla car so this is what
12884
09:34:00,416 --> 09:34:02,040
interfaces allow us to do we can Define
12885
09:34:02,040 --> 09:34:05,300
uh some shared Behavior between
12886
09:34:05,300 --> 09:34:09,060
unrelated or related classes and that
12887
09:34:09,060 --> 09:34:10,980
the actual code you don't Define any
12888
09:34:10,980 --> 09:34:13,020
code inside the interface you can put
12889
09:34:13,020 --> 09:34:15,620
you actually can Define here a property
12890
09:34:15,620 --> 09:34:20,460
but uh uh usually you just Define uh the
12891
09:34:20,460 --> 09:34:22,740
the function the function name some
12892
09:34:22,740 --> 09:34:25,020
parameters and the actual code goes
12893
09:34:25,020 --> 09:34:27,200
inside the class which implements that
12894
09:34:27,200 --> 09:34:29,640
interface let's let's actually create
12895
09:34:29,640 --> 09:34:34,560
here a class Tesla so we put here
12896
09:34:34,560 --> 09:34:37,320
let's put this a little bit up
12897
09:34:37,320 --> 09:34:41,276
so we put here class Tesla is gonna have
12898
09:34:41,276 --> 09:34:43,916
also a Val name
12899
09:34:43,916 --> 09:34:46,020
it's going to be a string
12900
09:34:46,020 --> 09:34:48,776
Naval color
12901
09:34:48,776 --> 09:34:51,300
and this is also going to be a string so
12902
09:34:51,300 --> 09:34:53,520
now we can Implement our engine
12903
09:34:53,520 --> 09:34:56,160
interface so we don't know how Tesla is
12904
09:34:56,160 --> 09:34:58,140
going to start the engine but we know
12905
09:34:58,140 --> 09:34:59,880
that it's going to start the engine so
12906
09:34:59,880 --> 09:35:02,460
we we Implement our interface put call
12907
09:35:02,460 --> 09:35:05,220
on the name of the interface and now we
12908
09:35:05,220 --> 09:35:07,680
need to implement the start engine so we
12909
09:35:07,680 --> 09:35:09,240
click on that
12910
09:35:09,240 --> 09:35:11,756
and we have not yet implemented let's
12911
09:35:11,756 --> 09:35:13,740
put our print line here I'm going to
12912
09:35:13,740 --> 09:35:15,900
press Ctrl V
12913
09:35:15,900 --> 09:35:18,180
and here's gonna say that's like
12914
09:35:18,180 --> 09:35:19,980
starting the engine
12915
09:35:19,980 --> 09:35:24,240
so this is what interfaces are and
12916
09:35:24,240 --> 09:35:26,220
see you in the next video
12917
09:35:26,220 --> 09:35:29,400
because I'm going to end the video now
12918
09:35:29,400 --> 09:35:31,560
so here you should say a Tesla is
12919
09:35:31,560 --> 09:35:34,256
starting the engine not the Tesla
12920
09:35:34,256 --> 09:35:37,756
so I collected that
12921
09:35:38,460 --> 09:35:40,436
so I thought it's a good idea to show
12922
09:35:40,436 --> 09:35:42,300
you a practical example using Android
12923
09:35:42,300 --> 09:35:45,416
studio where interfaces are used because
12924
09:35:45,416 --> 09:35:48,080
the when I first learned about
12925
09:35:48,080 --> 09:35:50,520
interfaces and after classes I found
12926
09:35:50,520 --> 09:35:52,256
them very confusing because they are
12927
09:35:52,256 --> 09:35:56,220
very very abstract and you don't see
12928
09:35:56,220 --> 09:35:59,756
when when you somebody teaches you those
12929
09:35:59,756 --> 09:36:01,800
Concepts you don't see how where and
12930
09:36:01,800 --> 09:36:04,800
where you're gonna use those uh those
12931
09:36:04,800 --> 09:36:07,680
Concepts interfaces and Abstract classes
12932
09:36:07,680 --> 09:36:09,180
and for that I'm going to open Android
12933
09:36:09,180 --> 09:36:10,860
Studio
12934
09:36:10,860 --> 09:36:13,380
to show you a practical example using
12935
09:36:13,380 --> 09:36:16,256
interfaces and also organize Android
12936
09:36:16,256 --> 09:36:18,900
Studio to show you a practical example
12937
09:36:18,900 --> 09:36:21,596
where we're going to use
12938
09:36:21,596 --> 09:36:23,820
abstract classes so I opened Android
12939
09:36:23,820 --> 09:36:26,096
Studio you don't need to it looks
12940
09:36:26,096 --> 09:36:28,980
similar to our IntelliJ because this
12941
09:36:28,980 --> 09:36:31,800
IntelliJ IDEA just that is it's Android
12942
09:36:31,800 --> 09:36:34,560
Studio so we will click on my
12943
09:36:34,560 --> 09:36:36,979
application
12944
09:36:42,480 --> 09:36:45,540
and here we have this thing called main
12945
09:36:45,540 --> 09:36:47,276
activity we don't need to worry about
12946
09:36:47,276 --> 09:36:51,300
this because as I said I just want to
12947
09:36:51,300 --> 09:36:53,276
show you where interfaces are used in
12948
09:36:53,276 --> 09:36:56,220
Android Studio to get to get an idea
12949
09:36:56,220 --> 09:36:59,640
about where they are used because I said
12950
09:36:59,640 --> 09:37:01,980
they are very abstract
12951
09:37:01,980 --> 09:37:04,160
foreign
12952
09:37:10,400 --> 09:37:14,040
this thing called activity.main This is
12953
09:37:14,040 --> 09:37:16,620
where we put our
12954
09:37:16,620 --> 09:37:19,256
UI this is where we put our buttons this
12955
09:37:19,256 --> 09:37:22,740
is where we put our list views uh this
12956
09:37:22,740 --> 09:37:26,040
is where we put generally our UI so as
12957
09:37:26,040 --> 09:37:27,720
you can see we have two buttons here and
12958
09:37:27,720 --> 09:37:29,276
they have an ID
12959
09:37:29,276 --> 09:37:32,276
again I'm not gonna go into this but if
12960
09:37:32,276 --> 09:37:34,680
you click here on design you see that we
12961
09:37:34,680 --> 09:37:38,276
have a login button and a sign up button
12962
09:37:38,276 --> 09:37:42,000
so this is the UI part of our app now
12963
09:37:42,000 --> 09:37:43,980
I'm gonna click on this main activity
12964
09:37:43,980 --> 09:37:45,660
and this is the part where you're gonna
12965
09:37:45,660 --> 09:37:48,256
put our code and now I'm gonna link
12966
09:37:48,256 --> 09:37:51,596
those UI buttons so let's go to quad so
12967
09:37:51,596 --> 09:37:53,040
this is the code for creating those
12968
09:37:53,040 --> 09:37:55,580
buttons
12969
09:37:55,800 --> 09:37:57,840
and I'm gonna link those buttons because
12970
09:37:57,840 --> 09:38:00,300
they have this ID here login button so
12971
09:38:00,300 --> 09:38:02,640
you have here Android ID
12972
09:38:02,640 --> 09:38:04,500
and if you come from Android Studio you
12973
09:38:04,500 --> 09:38:06,720
already know all of this I'm gonna link
12974
09:38:06,720 --> 09:38:09,480
those UI buttons with some objects
12975
09:38:09,480 --> 09:38:12,240
buttons by typing some code so I'm gonna
12976
09:38:12,240 --> 09:38:15,480
Define up here
12977
09:38:15,480 --> 09:38:18,480
foreign
12978
09:38:19,860 --> 09:38:23,720
called the login button
12979
09:38:24,720 --> 09:38:26,756
and it's going to be of type button so
12980
09:38:26,756 --> 09:38:29,040
this is a class button
12981
09:38:29,040 --> 09:38:31,380
then I'm gonna Define another later need
12982
09:38:31,380 --> 09:38:33,720
because I'm not gonna initialize it
12983
09:38:33,720 --> 09:38:37,220
right there so Laten it again
12984
09:38:37,916 --> 09:38:41,060
sign up button
12985
09:38:42,540 --> 09:38:45,140
and it's gonna be also of type button so
12986
09:38:45,140 --> 09:38:48,360
types like we have types for uh for our
12987
09:38:48,360 --> 09:38:50,880
numbers or for our just that this is a
12988
09:38:50,880 --> 09:38:53,400
class and this rep this class is a
12989
09:38:53,400 --> 09:38:55,320
button objectives and now we need to
12990
09:38:55,320 --> 09:38:58,020
link those those objects that we created
12991
09:38:58,020 --> 09:38:59,340
here
12992
09:38:59,340 --> 09:39:02,040
with those buttons that we have here
12993
09:39:02,040 --> 09:39:05,400
those UI buttons so to link those login
12994
09:39:05,400 --> 09:39:07,256
button and sign up button
12995
09:39:07,256 --> 09:39:09,900
and to do that to go down here
12996
09:39:09,900 --> 09:39:12,240
and I'm gonna I'm gonna go I'm gonna do
12997
09:39:12,240 --> 09:39:14,700
this in the longer way and here we type
12998
09:39:14,700 --> 09:39:16,680
login button again you don't need to
12999
09:39:16,680 --> 09:39:18,900
understand all of this this is just to
13000
09:39:18,900 --> 09:39:21,540
illustrate how interfaces work I'm gonna
13001
09:39:21,540 --> 09:39:22,916
put equals
13002
09:39:22,916 --> 09:39:25,380
and now I'm going to type find view by D
13003
09:39:25,380 --> 09:39:27,776
and this function find view by ID is
13004
09:39:27,776 --> 09:39:30,660
gonna link our button object that we
13005
09:39:30,660 --> 09:39:33,360
created here with our UI button
13006
09:39:33,360 --> 09:39:35,580
that we have
13007
09:39:35,580 --> 09:39:38,276
that we have created here and it's going
13008
09:39:38,276 --> 09:39:40,860
to be linked by its ID by this ID that
13009
09:39:40,860 --> 09:39:43,680
we have here login button so if you go
13010
09:39:43,680 --> 09:39:46,800
here hero Type R so R is this thing
13011
09:39:46,800 --> 09:39:48,180
which uh
13012
09:39:48,180 --> 09:39:50,160
again don't need to understand all of
13013
09:39:50,160 --> 09:39:53,220
these arrays is used to to get that ID
13014
09:39:53,220 --> 09:39:55,500
to link our button object with our
13015
09:39:55,500 --> 09:39:59,640
button UI so if you put r dot ID that
13016
09:39:59,640 --> 09:40:03,596
and we have our uh our two IDs so we
13017
09:40:03,596 --> 09:40:05,936
choose login button
13018
09:40:05,936 --> 09:40:10,256
so now we have linked our UI button with
13019
09:40:10,256 --> 09:40:14,820
our button object so we linked the UI in
13020
09:40:14,820 --> 09:40:18,120
other words with the code now let's link
13021
09:40:18,120 --> 09:40:20,340
also the setting up button so we type
13022
09:40:20,340 --> 09:40:22,200
here saying a button
13023
09:40:22,200 --> 09:40:25,436
equals find view by ID you you also type
13024
09:40:25,436 --> 09:40:28,980
r dot ID Dot
13025
09:40:28,980 --> 09:40:31,140
sign up button again you don't need to
13026
09:40:31,140 --> 09:40:32,460
type this code because probably you
13027
09:40:32,460 --> 09:40:34,620
don't even have Android Studio this is
13028
09:40:34,620 --> 09:40:36,960
just to illustrate something
13029
09:40:36,960 --> 09:40:38,460
now
13030
09:40:38,460 --> 09:40:40,980
to illustrate how interfaces work let's
13031
09:40:40,980 --> 09:40:43,200
say that I want to
13032
09:40:43,200 --> 09:40:45,416
let's say that I want to
13033
09:40:45,416 --> 09:40:48,360
to do something when this login button
13034
09:40:48,360 --> 09:40:50,040
is clicked and I want to do also
13035
09:40:50,040 --> 09:40:51,776
something when design up button is
13036
09:40:51,776 --> 09:40:53,820
clicked and it and here interface is
13037
09:40:53,820 --> 09:40:55,320
coming to action
13038
09:40:55,320 --> 09:40:57,480
what you can do is you can say login
13039
09:40:57,480 --> 09:40:59,700
button
13040
09:40:59,700 --> 09:41:02,640
Dot and there is a function called set
13041
09:41:02,640 --> 09:41:05,520
on click listener and this function what
13042
09:41:05,520 --> 09:41:07,020
is doing is
13043
09:41:07,020 --> 09:41:11,276
basically setting a listener on our UI
13044
09:41:11,276 --> 09:41:13,436
button so that when you click on that
13045
09:41:13,436 --> 09:41:16,680
button the function called on click is
13046
09:41:16,680 --> 09:41:20,040
going to be executed and the code that
13047
09:41:20,040 --> 09:41:22,680
is going to be in that function is
13048
09:41:22,680 --> 09:41:27,660
particular to each to each button so
13049
09:41:27,660 --> 09:41:31,080
you you you're gonna set the interface
13050
09:41:31,080 --> 09:41:33,720
so you types don't click listener and
13051
09:41:33,720 --> 09:41:36,596
hero type also this illustrates uh the
13052
09:41:36,596 --> 09:41:40,380
object also this by by this example we
13053
09:41:40,380 --> 09:41:42,000
also illustrate how the Anonymous
13054
09:41:42,000 --> 09:41:44,520
classes work so you type object
13055
09:41:44,520 --> 09:41:47,936
and now I'm gonna inherit actually gonna
13056
09:41:47,936 --> 09:41:50,820
Implement our interface on click
13057
09:41:50,820 --> 09:41:52,320
listener and this only listener
13058
09:41:52,320 --> 09:41:56,640
interface has a function called on click
13059
09:41:56,640 --> 09:41:58,916
and here to put curly braces and press
13060
09:41:58,916 --> 09:42:00,720
enter and now here we have this
13061
09:42:00,720 --> 09:42:02,640
underline
13062
09:42:02,640 --> 09:42:05,096
and if you click on it you have this red
13063
09:42:05,096 --> 09:42:08,276
bulb because it wants us to implement
13064
09:42:08,276 --> 09:42:11,460
to implement that on click function so
13065
09:42:11,460 --> 09:42:13,860
we're going to implement that
13066
09:42:13,860 --> 09:42:15,720
and it says to do not get implemented
13067
09:42:15,720 --> 09:42:17,160
I'm not going to do the same thing for
13068
09:42:17,160 --> 09:42:18,840
the
13069
09:42:18,840 --> 09:42:22,140
for the sign up button so I'm gonna copy
13070
09:42:22,140 --> 09:42:24,436
this
13071
09:42:25,140 --> 09:42:28,820
I'm gonna paste it down here
13072
09:42:28,936 --> 09:42:31,380
and I'm going to change this to sign up
13073
09:42:31,380 --> 09:42:34,320
button so now each individual each
13074
09:42:34,320 --> 09:42:37,500
individual button has us on has its own
13075
09:42:37,500 --> 09:42:41,220
on click listener interface and the code
13076
09:42:41,220 --> 09:42:43,140
which is going to be executed inside the
13077
09:42:43,140 --> 09:42:44,700
one click method because this function
13078
09:42:44,700 --> 09:42:46,740
is going to be triggered when you click
13079
09:42:46,740 --> 09:42:48,416
on the button is particular to each
13080
09:42:48,416 --> 09:42:50,040
button so this button is going to log in
13081
09:42:50,040 --> 09:42:53,400
the user this button you're gonna put
13082
09:42:53,400 --> 09:42:55,200
here the code to sign up the user but
13083
09:42:55,200 --> 09:42:56,276
you're not going to do that we're just
13084
09:42:56,276 --> 09:42:57,180
gonna
13085
09:42:57,180 --> 09:43:01,200
we're gonna uh show some text on our
13086
09:43:01,200 --> 09:43:03,960
emulator so we type here to show the
13087
09:43:03,960 --> 09:43:05,880
text toast
13088
09:43:05,880 --> 09:43:09,000
dot make text here you need to pass the
13089
09:43:09,000 --> 09:43:12,436
context so don't worry about all of this
13090
09:43:12,436 --> 09:43:16,256
main.activity here you need to pass uh
13091
09:43:16,256 --> 09:43:19,320
how how long this is gonna show on the
13092
09:43:19,320 --> 09:43:24,060
screen so we choose a short
13093
09:43:24,060 --> 09:43:29,479
and we put that show to show this on the
13094
09:43:33,900 --> 09:43:36,120
actually here we need to pass the text
13095
09:43:36,120 --> 09:43:37,980
so what text is going to be shown on the
13096
09:43:37,980 --> 09:43:39,720
screen so here we're gonna say
13097
09:43:39,720 --> 09:43:42,776
login button
13098
09:43:42,776 --> 09:43:44,580
clicked
13099
09:43:44,580 --> 09:43:47,580
and now here we need to pass how long
13100
09:43:47,580 --> 09:43:50,756
it's going to slow so you type those Dot
13101
09:43:50,756 --> 09:43:54,596
short so if it's going to be shown for a
13102
09:43:54,596 --> 09:43:57,180
short time and then we need to call the
13103
09:43:57,180 --> 09:43:59,160
show functions to show this on the
13104
09:43:59,160 --> 09:44:03,180
screen and I'm going to press Ctrl uh C
13105
09:44:03,180 --> 09:44:05,400
to copy this and I'm gonna paste inside
13106
09:44:05,400 --> 09:44:09,416
our on click function in our interface
13107
09:44:09,416 --> 09:44:11,520
that we have here for the sign up button
13108
09:44:11,520 --> 09:44:13,620
and this is gonna say
13109
09:44:13,620 --> 09:44:16,380
that
13110
09:44:16,380 --> 09:44:21,020
sign up button was click so sign up
13111
09:44:22,800 --> 09:44:24,540
button
13112
09:44:24,540 --> 09:44:26,340
clicked
13113
09:44:26,340 --> 09:44:28,860
now in Android Studio we have what is
13114
09:44:28,860 --> 09:44:30,660
called an emulator and this is basically
13115
09:44:30,660 --> 09:44:35,460
a virtual phone and we can run you can
13116
09:44:35,460 --> 09:44:37,800
run our code and we can test our code
13117
09:44:37,800 --> 09:44:39,960
and we're gonna see when
13118
09:44:39,960 --> 09:44:43,200
on that emulator those buttons and when
13119
09:44:43,200 --> 09:44:44,880
you're going to click on those buttons
13120
09:44:44,880 --> 09:44:48,060
this code that we Define here for for
13121
09:44:48,060 --> 09:44:50,520
those buttons because we linked them so
13122
09:44:50,520 --> 09:44:52,080
we linked
13123
09:44:52,080 --> 09:44:54,360
those two buttons that have the login
13124
09:44:54,360 --> 09:44:56,400
button the sign up button so this is
13125
09:44:56,400 --> 09:45:00,660
their UI code this is their XML code
13126
09:45:00,660 --> 09:45:03,776
you we've linked them through code here
13127
09:45:03,776 --> 09:45:06,240
and when we click on them the Code in
13128
09:45:06,240 --> 09:45:08,460
each particular object is going to be
13129
09:45:08,460 --> 09:45:11,936
executed so now if you run our code go
13130
09:45:11,936 --> 09:45:15,000
up here and I'm gonna create now the
13131
09:45:15,000 --> 09:45:17,400
virtual font to see our buttons and I'm
13132
09:45:17,400 --> 09:45:18,900
going to click on them to see how it
13133
09:45:18,900 --> 09:45:20,460
works
13134
09:45:20,460 --> 09:45:23,596
I'm going to close those
13135
09:45:23,700 --> 09:45:27,200
now it's connecting to the emulator
13136
09:45:27,480 --> 09:45:31,320
and this is our virtual virtual phone so
13137
09:45:31,320 --> 09:45:34,200
this is the virtual phones and here we
13138
09:45:34,200 --> 09:45:38,479
have our UI that we created here
13139
09:45:38,880 --> 09:45:42,720
so this is what the users see so this
13140
09:45:42,720 --> 09:45:45,180
the what the user say and this is what
13141
09:45:45,180 --> 09:45:47,580
we see it's the this is what happens
13142
09:45:47,580 --> 09:45:49,680
under the covers and now if I click on
13143
09:45:49,680 --> 09:45:53,596
this login button look what happens
13144
09:45:54,720 --> 09:45:57,180
it says login button click so it
13145
09:45:57,180 --> 09:45:59,096
executes this on click function inside
13146
09:45:59,096 --> 09:46:03,180
the login button if I click on the
13147
09:46:03,180 --> 09:46:06,240
sign up button it says sign up button
13148
09:46:06,240 --> 09:46:08,756
click because it's executing the one
13149
09:46:08,756 --> 09:46:11,276
click function and the code inside the
13150
09:46:11,276 --> 09:46:13,020
one click function for the sign up
13151
09:46:13,020 --> 09:46:13,916
button
13152
09:46:13,916 --> 09:46:17,756
and they are as I said linked by the by
13153
09:46:17,756 --> 09:46:20,460
the ID so this is what happens under the
13154
09:46:20,460 --> 09:46:22,436
covers this is what the user see
13155
09:46:22,436 --> 09:46:23,936
now
13156
09:46:23,936 --> 09:46:26,400
this is where interfaces can be used
13157
09:46:26,400 --> 09:46:29,160
because if you think about you can have
13158
09:46:29,160 --> 09:46:33,060
multiple uis different uis text views
13159
09:46:33,060 --> 09:46:35,900
lists or
13160
09:46:35,900 --> 09:46:38,820
items into the items in a list which can
13161
09:46:38,820 --> 09:46:41,580
be clicked and for all of those you know
13162
09:46:41,580 --> 09:46:44,040
that always go all of them are gonna
13163
09:46:44,040 --> 09:46:48,240
have on click function all all all have
13164
09:46:48,240 --> 09:46:51,660
this shared functionality to respond to
13165
09:46:51,660 --> 09:46:56,936
a click you don't know how that is gonna
13166
09:46:56,936 --> 09:46:59,936
how that that one click function is
13167
09:46:59,936 --> 09:47:00,860
gonna
13168
09:47:00,860 --> 09:47:04,020
execute or what code that on click
13169
09:47:04,020 --> 09:47:05,520
function is going to have for each
13170
09:47:05,520 --> 09:47:10,020
particular UI uh UI but you know that
13171
09:47:10,020 --> 09:47:13,680
each each one is gonna have one click
13172
09:47:13,680 --> 09:47:16,800
function what is going to go inside the
13173
09:47:16,800 --> 09:47:18,480
one click function for you for each
13174
09:47:18,480 --> 09:47:19,640
particular
13175
09:47:19,640 --> 09:47:23,880
UI it's up to that specific UI so it's
13176
09:47:23,880 --> 09:47:26,040
up to a button it's up to a text views
13177
09:47:26,040 --> 09:47:29,640
and so on so this is a over interfaces
13178
09:47:29,640 --> 09:47:32,160
are used because we know that they're
13179
09:47:32,160 --> 09:47:34,140
going to have a shared functionality of
13180
09:47:34,140 --> 09:47:36,900
one click but you don't know how that
13181
09:47:36,900 --> 09:47:38,400
functionality is going to be implemented
13182
09:47:38,400 --> 09:47:40,916
by each particular uis in our case we do
13183
09:47:40,916 --> 09:47:43,980
we know that login button and sign up
13184
09:47:43,980 --> 09:47:45,000
button
13185
09:47:45,000 --> 09:47:47,276
both both are going to have a one click
13186
09:47:47,276 --> 09:47:49,620
both we're gonna are gonna respond to
13187
09:47:49,620 --> 09:47:51,660
click events but you don't know what
13188
09:47:51,660 --> 09:47:54,120
they're gonna do so this is why you
13189
09:47:54,120 --> 09:47:57,596
define the interface and the developers
13190
09:47:57,596 --> 09:47:59,640
who write code gonna Implement that
13191
09:47:59,640 --> 09:48:02,820
interface and you're gonna write the
13192
09:48:02,820 --> 09:48:04,680
actual implementation for that function
13193
09:48:04,680 --> 09:48:08,096
which is specific to that object so this
13194
09:48:08,096 --> 09:48:10,200
is our discussion and if you find this
13195
09:48:10,200 --> 09:48:11,596
confusing
13196
09:48:11,596 --> 09:48:15,240
particularly if I'm confusing the uh if
13197
09:48:15,240 --> 09:48:17,276
because I touch a little bit on Android
13198
09:48:17,276 --> 09:48:19,800
Studio in our discussion don't be
13199
09:48:19,800 --> 09:48:22,500
because I just wanted to show you a
13200
09:48:22,500 --> 09:48:25,020
practical example where interfaces are
13201
09:48:25,020 --> 09:48:28,020
used using Android studio so let's click
13202
09:48:28,020 --> 09:48:30,000
again on login button
13203
09:48:30,000 --> 09:48:33,120
we get our toast login button clicked
13204
09:48:33,120 --> 09:48:34,916
slang a button click I can create
13205
09:48:34,916 --> 09:48:38,340
another button which can be used for a
13206
09:48:38,340 --> 09:48:40,200
I don't know if you forgot the password
13207
09:48:40,200 --> 09:48:42,776
and you you're gonna implement the on
13208
09:48:42,776 --> 09:48:45,540
click listener you're gonna override the
13209
09:48:45,540 --> 09:48:46,800
you're gonna override the one click
13210
09:48:46,800 --> 09:48:49,256
function I'm gonna write some code to
13211
09:48:49,256 --> 09:48:53,220
to send uh to send to help help the user
13212
09:48:53,220 --> 09:48:56,340
to recover its uh puzzle and so on so we
13213
09:48:56,340 --> 09:48:58,080
can see how interfaces can be used
13214
09:48:58,080 --> 09:49:01,080
practically in this with this example
13215
09:49:01,080 --> 09:49:03,300
so this is our discussion see you in the
13216
09:49:03,300 --> 09:49:05,340
next video so now it's time to start a
13217
09:49:05,340 --> 09:49:07,380
discussion about object expression but
13218
09:49:07,380 --> 09:49:09,596
first let's go down here and let's
13219
09:49:09,596 --> 09:49:11,580
create a class to see why you'll need to
13220
09:49:11,580 --> 09:49:13,140
use object expression in the first place
13221
09:49:13,140 --> 09:49:14,936
so you type here class
13222
09:49:14,936 --> 09:49:16,380
and now and our class is going to be
13223
09:49:16,380 --> 09:49:17,700
called button
13224
09:49:17,700 --> 09:49:20,220
we put the parenthesis to create the
13225
09:49:20,220 --> 09:49:21,660
primary Constructor we Define some
13226
09:49:21,660 --> 09:49:23,340
properties here the first one is going
13227
09:49:23,340 --> 09:49:27,020
to be text is going to be of type string
13228
09:49:31,140 --> 09:49:33,120
the second one is going to be also
13229
09:49:33,120 --> 09:49:35,276
available it's going to be called ID
13230
09:49:35,276 --> 09:49:37,200
it's gonna be an integer
13231
09:49:37,200 --> 09:49:39,180
and the last one is going to be called
13232
09:49:39,180 --> 09:49:42,960
One Click listener
13233
09:49:42,960 --> 09:49:44,520
it's going to be of type on click
13234
09:49:44,520 --> 09:49:46,380
listener so this is an interface that
13235
09:49:46,380 --> 09:49:48,979
we're gonna create
13236
09:49:53,460 --> 09:49:54,900
so let's go down here to create the
13237
09:49:54,900 --> 09:49:58,620
interface we type interface on and while
13238
09:49:58,620 --> 09:50:00,180
I'm jumping you see that you have this
13239
09:50:00,180 --> 09:50:02,096
suggestion one click listener
13240
09:50:02,096 --> 09:50:04,916
to create the interface press enter
13241
09:50:04,916 --> 09:50:06,960
put curly braces and let's define a
13242
09:50:06,960 --> 09:50:09,416
function fun
13243
09:50:09,416 --> 09:50:11,640
on click
13244
09:50:11,640 --> 09:50:14,096
let's put parenthesis
13245
09:50:14,096 --> 09:50:15,900
now let's create some objects with our
13246
09:50:15,900 --> 09:50:18,120
class button to see why you'll need to
13247
09:50:18,120 --> 09:50:19,140
use a
13248
09:50:19,140 --> 09:50:22,020
object expression in the first place so
13249
09:50:22,020 --> 09:50:23,580
let's go here
13250
09:50:23,580 --> 09:50:28,860
and here we type Val let's call it login
13251
09:50:28,860 --> 09:50:31,020
button
13252
09:50:31,020 --> 09:50:32,820
put equal and now let's create our
13253
09:50:32,820 --> 09:50:35,460
object type button
13254
09:50:35,460 --> 09:50:37,560
with parenthesis now let's pass some
13255
09:50:37,560 --> 09:50:39,840
values to the properties of the primary
13256
09:50:39,840 --> 09:50:41,160
Constructor
13257
09:50:41,160 --> 09:50:45,256
and this is gonna have the text to login
13258
09:50:45,360 --> 09:50:48,240
for the ID we type some arbitrary
13259
09:50:48,240 --> 09:50:50,880
numbers here one two one three
13260
09:50:50,880 --> 09:50:52,256
two
13261
09:50:52,256 --> 09:50:53,820
and um
13262
09:50:53,820 --> 09:50:56,720
now here you need to pass the one click
13263
09:50:56,720 --> 09:50:59,340
listener interface but do not we cannot
13264
09:50:59,340 --> 09:51:01,800
type here one click listener because
13265
09:51:01,800 --> 09:51:04,320
we cannot instantiate the interface so
13266
09:51:04,320 --> 09:51:08,096
what can we do to solve this problem
13267
09:51:08,096 --> 09:51:10,500
one solution to this would be to create
13268
09:51:10,500 --> 09:51:13,140
a separate class here
13269
09:51:13,140 --> 09:51:14,880
let's put it actually above the
13270
09:51:14,880 --> 09:51:17,299
interface
13271
09:51:17,520 --> 09:51:19,860
let's call it click listener this class
13272
09:51:19,860 --> 09:51:21,180
class
13273
09:51:21,180 --> 09:51:23,756
click listener and this class is going
13274
09:51:23,756 --> 09:51:25,860
to implement the interface
13275
09:51:25,860 --> 09:51:29,880
so we put here colon and we call our on
13276
09:51:29,880 --> 09:51:32,276
click listener interface put curly
13277
09:51:32,276 --> 09:51:33,240
braces
13278
09:51:33,240 --> 09:51:35,340
and now we need to implement that one
13279
09:51:35,340 --> 09:51:37,860
click method that one click function
13280
09:51:37,860 --> 09:51:39,720
that we defined actually
13281
09:51:39,720 --> 09:51:41,580
inside our own click listen interface so
13282
09:51:41,580 --> 09:51:44,936
press ok to override to add on click
13283
09:51:44,936 --> 09:51:46,256
function
13284
09:51:46,256 --> 09:51:48,300
and we have that
13285
09:51:48,300 --> 09:51:50,460
to do we have this to do which says to
13286
09:51:50,460 --> 09:51:52,680
do not yet implemented so this is there
13287
09:51:52,680 --> 09:51:55,320
to tell us that it's not implemented yet
13288
09:51:55,320 --> 09:51:58,820
so let's delete this
13289
09:51:59,460 --> 09:52:01,380
all right so what we could do is you can
13290
09:52:01,380 --> 09:52:04,740
create here an instance of the
13291
09:52:04,740 --> 09:52:09,000
click listener so we type here Val
13292
09:52:09,000 --> 09:52:12,060
click listener
13293
09:52:12,060 --> 09:52:15,300
equals create the instance we'll create
13294
09:52:15,300 --> 09:52:16,560
an object
13295
09:52:16,560 --> 09:52:18,480
and now you can pass here the click
13296
09:52:18,480 --> 09:52:20,900
listener
13297
09:52:21,596 --> 09:52:24,840
so now the arrow disappeared but if I
13298
09:52:24,840 --> 09:52:29,596
create a second object let's call it the
13299
09:52:31,500 --> 09:52:34,140
sign up button
13300
09:52:34,140 --> 09:52:37,096
equals button
13301
09:52:38,400 --> 09:52:40,500
let's pause the text so
13302
09:52:40,500 --> 09:52:42,980
sign up
13303
09:52:44,640 --> 09:52:48,416
for the ID was an arbitrary number
13304
09:52:48,416 --> 09:52:50,580
now
13305
09:52:50,580 --> 09:52:54,540
in our imaginary or
13306
09:52:54,540 --> 09:52:57,200
example that we have here
13307
09:52:57,200 --> 09:52:59,880
actually if you think for real scenario
13308
09:52:59,880 --> 09:53:02,220
with this button and the one click
13309
09:53:02,220 --> 09:53:03,840
listen what we want to achieve here is
13310
09:53:03,840 --> 09:53:05,756
that if you have a graphical user
13311
09:53:05,756 --> 09:53:06,840
interface
13312
09:53:06,840 --> 09:53:10,320
you want that you want to detect that
13313
09:53:10,320 --> 09:53:12,660
click so you want to declare detect one
13314
09:53:12,660 --> 09:53:14,936
that specific button let's see that this
13315
09:53:14,936 --> 09:53:16,500
button object that we have here is
13316
09:53:16,500 --> 09:53:20,880
linked to an uh graphical button which
13317
09:53:20,880 --> 09:53:22,860
on which you click
13318
09:53:22,860 --> 09:53:24,660
because they are linked when you click
13319
09:53:24,660 --> 09:53:26,640
on that you want to
13320
09:53:26,640 --> 09:53:30,000
call that one click function that is
13321
09:53:30,000 --> 09:53:32,276
defined inside our interface inside here
13322
09:53:32,276 --> 09:53:34,320
but you want to
13323
09:53:34,320 --> 09:53:38,276
make that one click interface we want
13324
09:53:38,276 --> 09:53:40,460
you want to make that one click function
13325
09:53:40,460 --> 09:53:43,140
particular to particular to that button
13326
09:53:43,140 --> 09:53:46,880
which was clicked so we we want to to
13327
09:53:46,880 --> 09:53:51,060
implement that uh one click function to
13328
09:53:51,060 --> 09:53:52,980
that particular button which was clicked
13329
09:53:52,980 --> 09:53:55,020
and the code that goes inside that
13330
09:53:55,020 --> 09:53:56,820
particular button so let's say that you
13331
09:53:56,820 --> 09:53:58,800
click on the login button it's
13332
09:53:58,800 --> 09:54:01,140
particular to that button so that in the
13333
09:54:01,140 --> 09:54:02,340
next
13334
09:54:02,340 --> 09:54:04,800
when the next button is clicked so when
13335
09:54:04,800 --> 09:54:07,256
if the user clicks for when the sign up
13336
09:54:07,256 --> 09:54:10,436
button then you know you need to also
13337
09:54:10,436 --> 09:54:12,720
have the one click
13338
09:54:12,720 --> 09:54:16,200
method called but that one click that
13339
09:54:16,200 --> 09:54:18,776
one click function actually I I keep
13340
09:54:18,776 --> 09:54:21,360
saying method but the one click function
13341
09:54:21,360 --> 09:54:23,220
should should have its own
13342
09:54:23,220 --> 09:54:25,500
implementation a particular to the sign
13343
09:54:25,500 --> 09:54:27,720
up button so what can we do in that case
13344
09:54:27,720 --> 09:54:30,120
we cannot pass the same instance here to
13345
09:54:30,120 --> 09:54:31,916
click listener because even if it
13346
09:54:31,916 --> 09:54:33,540
creates here another instance of the
13347
09:54:33,540 --> 09:54:35,640
click listener it will still have the
13348
09:54:35,640 --> 09:54:38,276
same code which is here so if you put
13349
09:54:38,276 --> 09:54:40,080
here some code this is code blue it will
13350
09:54:40,080 --> 09:54:42,720
be shared among all the objects that are
13351
09:54:42,720 --> 09:54:45,300
created so what you can do in this case
13352
09:54:45,300 --> 09:54:49,320
is to use an object expression
13353
09:54:49,320 --> 09:54:52,560
or also called anonymous
13354
09:54:52,560 --> 09:54:54,960
anonymous anonymous classes so what you
13355
09:54:54,960 --> 09:54:58,020
can do here we can type object
13356
09:54:58,020 --> 09:55:01,080
we put colon and now we
13357
09:55:01,080 --> 09:55:04,980
are inheriting from our we are actually
13358
09:55:04,980 --> 09:55:08,340
implementing our own click listener
13359
09:55:08,340 --> 09:55:11,220
interface so we type object colon on
13360
09:55:11,220 --> 09:55:13,680
click listener and we put curly braces
13361
09:55:13,680 --> 09:55:17,400
because here we declare and we also
13362
09:55:17,400 --> 09:55:21,240
instantiate the the anonymous class so
13363
09:55:21,240 --> 09:55:24,799
we need to implement that right there
13364
09:55:25,800 --> 09:55:28,980
so by doing this now we where we made
13365
09:55:28,980 --> 09:55:33,540
the one click function particular to
13366
09:55:33,540 --> 09:55:36,300
each object Which object button so we
13367
09:55:36,300 --> 09:55:38,220
can do the same thing here we type
13368
09:55:38,220 --> 09:55:40,640
object
13369
09:55:43,256 --> 09:55:46,096
and we Implement our on click listener
13370
09:55:46,096 --> 09:55:48,900
interface we put curly braces now we
13371
09:55:48,900 --> 09:55:50,640
need to implement
13372
09:55:50,640 --> 09:55:52,800
for this particular object for the sign
13373
09:55:52,800 --> 09:55:55,040
up
13374
09:55:55,580 --> 09:55:58,020
or button objects we need to implement
13375
09:55:58,020 --> 09:56:01,140
that function so if we type we click we
13376
09:56:01,140 --> 09:56:03,240
click on this object
13377
09:56:03,240 --> 09:56:06,120
we go to the red light bulb and we type
13378
09:56:06,120 --> 09:56:07,880
we press here where it says Implement
13379
09:56:07,880 --> 09:56:09,960
members so implement the one click
13380
09:56:09,960 --> 09:56:13,020
function and now what we did here is we
13381
09:56:13,020 --> 09:56:16,020
made the one click
13382
09:56:16,020 --> 09:56:19,200
function particular to each object so
13383
09:56:19,200 --> 09:56:21,180
each object now we can put here some
13384
09:56:21,180 --> 09:56:22,320
code
13385
09:56:22,320 --> 09:56:25,096
code
13386
09:56:25,860 --> 09:56:30,680
or we can type here this sign
13387
09:56:33,416 --> 09:56:35,460
in
13388
09:56:35,460 --> 09:56:38,520
actually let's type login
13389
09:56:38,520 --> 09:56:41,756
that was it so now we can have code here
13390
09:56:41,756 --> 09:56:44,040
which actually log logs in if you have a
13391
09:56:44,040 --> 09:56:46,140
real graphical user interface which logs
13392
09:56:46,140 --> 09:56:49,020
logs in the user and here you can also
13393
09:56:49,020 --> 09:56:51,360
code to sign up the user when that
13394
09:56:51,360 --> 09:56:54,916
specific button is clicked
13395
09:56:56,520 --> 09:56:58,436
so by doing by using the object
13396
09:56:58,436 --> 09:57:00,840
expression also called Anonymous class
13397
09:57:00,840 --> 09:57:03,416
because you don't have
13398
09:57:03,416 --> 09:57:06,300
reference you don't have a name for the
13399
09:57:06,300 --> 09:57:08,756
class here you just it's just Anonymous
13400
09:57:08,756 --> 09:57:10,500
for this
13401
09:57:10,500 --> 09:57:13,680
is anonymous so we cannot it does not
13402
09:57:13,680 --> 09:57:15,180
have a name so we cannot call it later
13403
09:57:15,180 --> 09:57:18,240
or do something because it's created and
13404
09:57:18,240 --> 09:57:20,640
also instantiating right there this is
13405
09:57:20,640 --> 09:57:21,900
why I need to implement the one click
13406
09:57:21,900 --> 09:57:23,460
function and now we made the one click
13407
09:57:23,460 --> 09:57:26,340
function particular to each button and
13408
09:57:26,340 --> 09:57:27,776
you have more
13409
09:57:27,776 --> 09:57:30,300
objects more objects of created with the
13410
09:57:30,300 --> 09:57:31,320
class
13411
09:57:31,320 --> 09:57:33,720
button they'll have their own
13412
09:57:33,720 --> 09:57:36,120
implementation of the one click
13413
09:57:36,120 --> 09:57:38,340
function which comes from the interface
13414
09:57:38,340 --> 09:57:40,256
on click listener
13415
09:57:40,256 --> 09:57:42,120
so this is why you need to use the
13416
09:57:42,120 --> 09:57:44,220
anonymous
13417
09:57:44,220 --> 09:57:46,680
class or also called object expression
13418
09:57:46,680 --> 09:57:49,080
and it's an expression because the value
13419
09:57:49,080 --> 09:57:52,580
that the because the object is always
13420
09:57:52,580 --> 09:57:56,276
it's created and instantiate it is
13421
09:57:56,276 --> 09:57:58,200
actually declared and instantiated at
13422
09:57:58,200 --> 09:58:01,080
the same time it's also assigned to the
13423
09:58:01,080 --> 09:58:03,300
property that we Define here so this is
13424
09:58:03,300 --> 09:58:06,060
why it's called an expression
13425
09:58:06,060 --> 09:58:09,060
so see you in the next video
13426
09:58:09,060 --> 09:58:10,620
all right so now it's time to start a
13427
09:58:10,620 --> 09:58:12,180
discussion about delegation but first
13428
09:58:12,180 --> 09:58:14,520
I'm going to add the main function here
13429
09:58:14,520 --> 09:58:17,880
now what is delegation delegation means
13430
09:58:17,880 --> 09:58:20,276
giving power given Authority from one
13431
09:58:20,276 --> 09:58:22,620
instance from one class to another class
13432
09:58:22,620 --> 09:58:26,580
and delegation is usually used in in
13433
09:58:26,580 --> 09:58:29,040
scenarios where inheritance starts to
13434
09:58:29,040 --> 09:58:32,040
break so when inheritance starts to
13435
09:58:32,040 --> 09:58:34,020
break when you want let's say it for
13436
09:58:34,020 --> 09:58:36,060
whatever reason to inherit from two
13437
09:58:36,060 --> 09:58:39,000
classes in that case you can't use
13438
09:58:39,000 --> 09:58:41,160
inheritance because with inheritance you
13439
09:58:41,160 --> 09:58:44,756
can you inherit just from only one class
13440
09:58:44,756 --> 09:58:47,756
but with delegation you can plug in
13441
09:58:47,756 --> 09:58:49,220
multiple
13442
09:58:49,220 --> 09:58:51,900
implementation of classes in your own
13443
09:58:51,900 --> 09:58:54,360
class and let's see how we can do that
13444
09:58:54,360 --> 09:58:55,560
so I'm going to create first an
13445
09:58:55,560 --> 09:58:58,980
interface called a and it's going to
13446
09:58:58,980 --> 09:59:02,520
have a function called print
13447
09:59:02,520 --> 09:59:03,720
then I'm going to create another
13448
09:59:03,720 --> 09:59:06,660
interface called B and it's going to
13449
09:59:06,660 --> 09:59:10,040
have a function print though
13450
09:59:11,340 --> 09:59:13,620
then I'm going to create a class so I'm
13451
09:59:13,620 --> 09:59:15,240
going to type here class and it's going
13452
09:59:15,240 --> 09:59:17,040
to be called first delegate and it's
13453
09:59:17,040 --> 09:59:19,256
going to implement the fun the interface
13454
09:59:19,256 --> 09:59:21,000
a here
13455
09:59:21,000 --> 09:59:23,040
and I'm gonna press Ctrl o to override
13456
09:59:23,040 --> 09:59:25,140
the function print here
13457
09:59:25,140 --> 09:59:27,020
and I'm going to create another class
13458
09:59:27,020 --> 09:59:30,840
called second delegate
13459
09:59:30,840 --> 09:59:32,640
and this is going to implement the
13460
09:59:32,640 --> 09:59:33,960
interface B
13461
09:59:33,960 --> 09:59:35,820
so I'm going to place Ctrl to override
13462
09:59:35,820 --> 09:59:36,776
the
13463
09:59:36,776 --> 09:59:39,540
function print tool
13464
09:59:39,540 --> 09:59:41,460
now I'm going to create up here a class
13465
09:59:41,460 --> 09:59:43,680
called up
13466
09:59:43,680 --> 09:59:46,320
so class up
13467
09:59:46,320 --> 09:59:48,416
now let's say that I want here to
13468
09:59:48,416 --> 09:59:51,000
inherit from the first delegate and from
13469
09:59:51,000 --> 09:59:52,380
the second regulator so I'm going to
13470
09:59:52,380 --> 09:59:54,776
inform the single delegate and if I type
13471
09:59:54,776 --> 09:59:57,120
here first delegate
13472
09:59:57,120 --> 09:59:59,520
I put parenthesis opening parentheses
13473
09:59:59,520 --> 10:00:01,860
closing parentheses
13474
10:00:01,860 --> 10:00:04,916
if I put curly braces now I have another
13475
10:00:04,916 --> 10:00:07,276
line because I should Mark this as open
13476
10:00:07,276 --> 10:00:11,520
so mark this as open and also this as
13477
10:00:11,520 --> 10:00:15,000
open because I want to show you that so
13478
10:00:15,000 --> 10:00:17,096
now if I want to inherit from the second
13479
10:00:17,096 --> 10:00:19,680
class called from the second delegate
13480
10:00:19,680 --> 10:00:21,596
because let's say that I want to use the
13481
10:00:21,596 --> 10:00:24,120
the print to function in my class for
13482
10:00:24,120 --> 10:00:25,916
whatever reason
13483
10:00:25,916 --> 10:00:28,860
I can't because I can't only inherit
13484
10:00:28,860 --> 10:00:31,320
from one class so if I hover over here
13485
10:00:31,320 --> 10:00:34,020
over this underline it says only only
13486
10:00:34,020 --> 10:00:36,060
one class may appear in a super type
13487
10:00:36,060 --> 10:00:37,740
list so in other words saying that you
13488
10:00:37,740 --> 10:00:40,380
can only inherit from one class and that
13489
10:00:40,380 --> 10:00:43,080
is all we cannot inherit from multiple
13490
10:00:43,080 --> 10:00:46,200
classes now with delegation we can plug
13491
10:00:46,200 --> 10:00:49,080
in multiple implementations in our own
13492
10:00:49,080 --> 10:00:52,800
class so what I can do here I can say
13493
10:00:52,800 --> 10:00:54,060
hey
13494
10:00:54,060 --> 10:00:56,460
implement the interface a so I'm going
13495
10:00:56,460 --> 10:00:58,680
to inherit it from the interface a but
13496
10:00:58,680 --> 10:01:01,380
you're gonna say use for the interface a
13497
10:01:01,380 --> 10:01:04,140
the implementation of the first delegate
13498
10:01:04,140 --> 10:01:05,936
class so this is what we're saying here
13499
10:01:05,936 --> 10:01:09,660
a and the keyword by first delegate
13500
10:01:09,660 --> 10:01:12,596
we're saying hey use for the interface a
13501
10:01:12,596 --> 10:01:15,240
the implementation which is provided by
13502
10:01:15,240 --> 10:01:17,400
the first delegate class and that will
13503
10:01:17,400 --> 10:01:18,240
work
13504
10:01:18,240 --> 10:01:19,916
then I'm going to put a comma here and
13505
10:01:19,916 --> 10:01:22,800
I'm going to say use also for the class
13506
10:01:22,800 --> 10:01:25,380
B so I'm going to type for the interface
13507
10:01:25,380 --> 10:01:28,500
B so we're gonna type B and we're gonna
13508
10:01:28,500 --> 10:01:30,360
use the by key you're also going to type
13509
10:01:30,360 --> 10:01:31,980
here by
13510
10:01:31,980 --> 10:01:34,436
second delegate so you're saying use for
13511
10:01:34,436 --> 10:01:37,800
the interface B the second delegate
13512
10:01:37,800 --> 10:01:40,380
implementation and it's gonna use that
13513
10:01:40,380 --> 10:01:42,180
and it's gonna be him
13514
10:01:42,180 --> 10:01:43,916
happy with that so you're gonna see that
13515
10:01:43,916 --> 10:01:47,340
we get no error and now I can override
13516
10:01:47,340 --> 10:01:50,460
here both the print function and also
13517
10:01:50,460 --> 10:01:52,916
the print to function so if I press Ctrl
13518
10:01:52,916 --> 10:01:55,140
o now I can override also the print too
13519
10:01:55,140 --> 10:01:58,080
and this is inherently very powerful
13520
10:01:58,080 --> 10:02:00,660
because we can plug in here multiple as
13521
10:02:00,660 --> 10:02:03,300
many implementations as you want in your
13522
10:02:03,300 --> 10:02:07,256
class so this is in the if you think
13523
10:02:07,256 --> 10:02:10,500
about in contrast with the inheritance
13524
10:02:10,500 --> 10:02:12,840
this is very powerful because here you
13525
10:02:12,840 --> 10:02:15,300
can plug in as many implementations as
13526
10:02:15,300 --> 10:02:18,240
you want and this is very powerful so
13527
10:02:18,240 --> 10:02:20,040
this is how you can use the delegation
13528
10:02:20,040 --> 10:02:21,720
in kotlin and next we're going to see
13529
10:02:21,720 --> 10:02:23,096
how you can use delegation with
13530
10:02:23,096 --> 10:02:24,596
properties
13531
10:02:24,596 --> 10:02:26,756
so to use delegation on properties first
13532
10:02:26,756 --> 10:02:28,380
I'm going to paste some code here so I'm
13533
10:02:28,380 --> 10:02:30,060
going to paste this code here and what
13534
10:02:30,060 --> 10:02:31,916
we are having here is a class format
13535
10:02:31,916 --> 10:02:33,776
delegate which is inheriting from the
13536
10:02:33,776 --> 10:02:35,580
read and write property
13537
10:02:35,580 --> 10:02:38,040
and it's overriding the get value and
13538
10:02:38,040 --> 10:02:40,320
the set value so we are overriding the
13539
10:02:40,320 --> 10:02:42,180
setter and the getter inside our class
13540
10:02:42,180 --> 10:02:44,276
and we provide our own implementation so
13541
10:02:44,276 --> 10:02:46,140
whenever you
13542
10:02:46,140 --> 10:02:48,240
you use the setter to set a value or to
13543
10:02:48,240 --> 10:02:51,120
get as a get a value we we are
13544
10:02:51,120 --> 10:02:52,620
overriding those functions and we
13545
10:02:52,620 --> 10:02:54,660
provide our own implementation next I'm
13546
10:02:54,660 --> 10:02:56,400
going to create here up here a class but
13547
10:02:56,400 --> 10:02:58,436
first I'm going to delete the code and
13548
10:02:58,436 --> 10:03:00,060
the interface that we have here because
13549
10:03:00,060 --> 10:03:01,740
we don't need this code
13550
10:03:01,740 --> 10:03:03,416
so I'm gonna delete this code and I'm
13551
10:03:03,416 --> 10:03:05,936
going to create another class
13552
10:03:05,936 --> 10:03:07,500
and this class is going to be called
13553
10:03:07,500 --> 10:03:10,620
user I'm going to put curly braces and
13554
10:03:10,620 --> 10:03:11,700
I'm going to define the properties
13555
10:03:11,700 --> 10:03:12,840
inside the class so I'm going to type
13556
10:03:12,840 --> 10:03:14,340
here VAR
13557
10:03:14,340 --> 10:03:17,580
first name so what we are doing now is
13558
10:03:17,580 --> 10:03:19,916
we're creating a property first it's
13559
10:03:19,916 --> 10:03:21,360
called first name and I'm going to use
13560
10:03:21,360 --> 10:03:23,520
the by queue we're just going to say buy
13561
10:03:23,520 --> 10:03:25,560
format delegate so what we are saying
13562
10:03:25,560 --> 10:03:28,160
now is use the
13563
10:03:28,160 --> 10:03:31,740
use the the we use the code which is
13564
10:03:31,740 --> 10:03:33,660
inside the format delegate Whenever you
13565
10:03:33,660 --> 10:03:35,460
set the value or get the value of the
13566
10:03:35,460 --> 10:03:36,776
first name I'm going to put the
13567
10:03:36,776 --> 10:03:39,180
parenthesis here next I'm going to type
13568
10:03:39,180 --> 10:03:42,000
another VAR last name and I'm going to
13569
10:03:42,000 --> 10:03:44,220
type here again bye and I'm going to
13570
10:03:44,220 --> 10:03:47,640
type format delegate so format delegate
13571
10:03:47,640 --> 10:03:49,380
and we put parentheses
13572
10:03:49,380 --> 10:03:51,480
so not now what is going to happen is
13573
10:03:51,480 --> 10:03:54,180
that whenever you set the first name
13574
10:03:54,180 --> 10:03:55,916
properly or we get the name the first
13575
10:03:55,916 --> 10:03:57,240
name property is going to use our
13576
10:03:57,240 --> 10:03:59,096
implementation that we provided here
13577
10:03:59,096 --> 10:04:00,840
this is what you're gonna do and the
13578
10:04:00,840 --> 10:04:02,220
same it will be true for the last name
13579
10:04:02,220 --> 10:04:04,080
because we are using the
13580
10:04:04,080 --> 10:04:07,080
delegation here on properties so I'm
13581
10:04:07,080 --> 10:04:08,520
going to create here a user so I'm going
13582
10:04:08,520 --> 10:04:09,900
to type here Val
13583
10:04:09,900 --> 10:04:14,640
user want to type equals to user
13584
10:04:14,640 --> 10:04:17,640
and I'm gonna type here with
13585
10:04:17,640 --> 10:04:20,936
I'm gonna type user this is the
13586
10:04:20,936 --> 10:04:22,740
scope function I'm going to put curly
13587
10:04:22,740 --> 10:04:24,840
curly braces and I'm going to type here
13588
10:04:24,840 --> 10:04:27,540
first name
13589
10:04:27,540 --> 10:04:29,936
equals Alex
13590
10:04:29,936 --> 10:04:32,276
and the last name
13591
10:04:32,276 --> 10:04:34,560
equals uh
13592
10:04:34,560 --> 10:04:36,120
dominga
13593
10:04:36,120 --> 10:04:37,980
now if I output those values in the
13594
10:04:37,980 --> 10:04:39,540
console look what happens so if I tap it
13595
10:04:39,540 --> 10:04:42,380
here print line
13596
10:04:42,620 --> 10:04:45,840
user that first name
13597
10:04:45,840 --> 10:04:48,680
and print line let's actually use again
13598
10:04:48,680 --> 10:04:53,460
with the scope function type here with
13599
10:04:53,460 --> 10:04:57,960
just pass here user curly braces
13600
10:04:57,960 --> 10:05:00,180
print line
13601
10:05:00,180 --> 10:05:01,680
first name
13602
10:05:01,680 --> 10:05:03,840
and print line
13603
10:05:03,840 --> 10:05:05,400
last name
13604
10:05:05,400 --> 10:05:06,900
now if you run this code you're gonna
13605
10:05:06,900 --> 10:05:07,860
see
13606
10:05:07,860 --> 10:05:11,276
that it's using the overriding functions
13607
10:05:11,276 --> 10:05:13,380
that we provide in our format delegate
13608
10:05:13,380 --> 10:05:15,180
so we have in uppercase letters like we
13609
10:05:15,180 --> 10:05:17,700
defined here we're gonna wanna whenever
13610
10:05:17,700 --> 10:05:20,160
we set about is going to be set it with
13611
10:05:20,160 --> 10:05:21,660
the it's going to change the value to
13612
10:05:21,660 --> 10:05:24,540
lowercase letter so get Alex and obinka
13613
10:05:24,540 --> 10:05:27,120
in lowercase letters so this is how you
13614
10:05:27,120 --> 10:05:30,596
can use the delegation on properties and
13615
10:05:30,596 --> 10:05:34,096
see you in the next video
13616
10:05:37,500 --> 10:05:39,416
so now it's time to start a discussion
13617
10:05:39,416 --> 10:05:41,276
about collections but first i'm going to
13618
10:05:41,276 --> 10:05:43,020
create a new project I'm going to call
13619
10:05:43,020 --> 10:05:45,916
it collections
13620
10:05:46,380 --> 10:05:48,300
for the language select link for the
13621
10:05:48,300 --> 10:05:50,096
build system IntelliJ make sure to have
13622
10:05:50,096 --> 10:05:52,560
the jdk selected and also check this box
13623
10:05:52,560 --> 10:05:54,720
to have the main function Auto generated
13624
10:05:54,720 --> 10:05:56,580
for us so I'm going to click on create
13625
10:05:56,580 --> 10:05:59,416
to create the project
13626
10:06:01,080 --> 10:06:02,340
now I'm going to delete this code
13627
10:06:02,340 --> 10:06:04,080
because you don't need this code
13628
10:06:04,080 --> 10:06:05,520
and also I'm going to hide the project
13629
10:06:05,520 --> 10:06:06,720
pane
13630
10:06:06,720 --> 10:06:09,180
now what are collections collections are
13631
10:06:09,180 --> 10:06:12,120
a group of objects stored together in a
13632
10:06:12,120 --> 10:06:14,096
single variable they are similar to
13633
10:06:14,096 --> 10:06:16,500
array but there are there are different
13634
10:06:16,500 --> 10:06:19,500
in some sense and
13635
10:06:19,500 --> 10:06:21,540
they are mainly three collections that
13636
10:06:21,540 --> 10:06:23,400
we're gonna look at and this those are
13637
10:06:23,400 --> 10:06:28,020
the list the set and the map and the and
13638
10:06:28,020 --> 10:06:29,720
those among those
13639
10:06:29,720 --> 10:06:32,400
collections they are separated in two
13640
10:06:32,400 --> 10:06:36,436
groups so there are multiple multiple
13641
10:06:36,436 --> 10:06:40,436
lists and set air maps and immutable
13642
10:06:40,436 --> 10:06:44,520
lists set and map and in the motable
13643
10:06:44,520 --> 10:06:48,000
means that you can write to that
13644
10:06:48,000 --> 10:06:51,240
collection so you can add or or remove
13645
10:06:51,240 --> 10:06:53,936
elements to it so the that collection
13646
10:06:53,936 --> 10:06:56,820
can grow or Shrink as you add or remove
13647
10:06:56,820 --> 10:06:59,880
elements to it and immutable means that
13648
10:06:59,880 --> 10:07:03,720
you can only add elements when it when
13649
10:07:03,720 --> 10:07:05,580
you instantiate that or we're going to
13650
10:07:05,580 --> 10:07:07,620
create that collection you cannot add
13651
10:07:07,620 --> 10:07:10,500
later elements to it the size is fixed
13652
10:07:10,500 --> 10:07:12,900
so we cannot add or remove element so
13653
10:07:12,900 --> 10:07:14,700
this is the difference between immutable
13654
10:07:14,700 --> 10:07:16,916
and multiple and we're gonna look at all
13655
10:07:16,916 --> 10:07:19,500
of those in this video so we're gonna
13656
10:07:19,500 --> 10:07:21,180
look at the list collection first and
13657
10:07:21,180 --> 10:07:23,160
we're gonna look at an immutable list so
13658
10:07:23,160 --> 10:07:25,320
we type Val I'm going to create a
13659
10:07:25,320 --> 10:07:28,740
variable names and here we're gonna type
13660
10:07:28,740 --> 10:07:32,220
to create an immutable list type list of
13661
10:07:32,220 --> 10:07:34,140
and here we need to specify the type
13662
10:07:34,140 --> 10:07:37,200
what kind of data I'm going to put here
13663
10:07:37,200 --> 10:07:41,096
and do type name one
13664
10:07:41,096 --> 10:07:43,820
name two
13665
10:07:46,256 --> 10:07:49,020
and name three now we cannot add or
13666
10:07:49,020 --> 10:07:51,300
remove elements from this list because
13667
10:07:51,300 --> 10:07:53,340
it's an immutable list but we can get on
13668
10:07:53,340 --> 10:07:55,320
Elemental output in the console and to
13669
10:07:55,320 --> 10:07:57,660
do that to type it in the line
13670
10:07:57,660 --> 10:08:00,540
we type names we put square brackets and
13671
10:08:00,540 --> 10:08:02,460
we get the element by its index and the
13672
10:08:02,460 --> 10:08:04,500
index also starts at zero so let's get
13673
10:08:04,500 --> 10:08:06,540
the first element
13674
10:08:06,540 --> 10:08:08,400
so if you're on this code you'll see
13675
10:08:08,400 --> 10:08:12,320
name one I'll put it in a new console
13676
10:08:13,800 --> 10:08:16,140
so you get name one so this is an
13677
10:08:16,140 --> 10:08:17,880
immutable list we cannot add or remove
13678
10:08:17,880 --> 10:08:20,416
elements from it after we
13679
10:08:20,416 --> 10:08:23,880
instantiate it here what now now let's
13680
10:08:23,880 --> 10:08:27,240
look at an mutably so a list which can
13681
10:08:27,240 --> 10:08:29,040
within which you can add and remove
13682
10:08:29,040 --> 10:08:31,916
elements to it so to do that we just
13683
10:08:31,916 --> 10:08:34,500
change this from a list of to a mutable
13684
10:08:34,500 --> 10:08:35,640
list of
13685
10:08:35,640 --> 10:08:37,980
so we type here multiple
13686
10:08:37,980 --> 10:08:40,140
list of
13687
10:08:40,140 --> 10:08:43,980
we can also remove the explicit type
13688
10:08:43,980 --> 10:08:46,436
here the explicit type declaration
13689
10:08:46,436 --> 10:08:48,360
because it's gray out
13690
10:08:48,360 --> 10:08:49,860
so remove that because it can be
13691
10:08:49,860 --> 10:08:53,096
inferred and to this mutable list now we
13692
10:08:53,096 --> 10:08:56,220
can add or remove the elements so I can
13693
10:08:56,220 --> 10:08:58,080
type here names
13694
10:08:58,080 --> 10:09:00,840
and we have this add function so you can
13695
10:09:00,840 --> 10:09:02,520
type add
13696
10:09:02,520 --> 10:09:05,460
and here we can
13697
10:09:05,460 --> 10:09:07,500
you can pass a new element so we pass
13698
10:09:07,500 --> 10:09:10,580
here let's say name
13699
10:09:10,580 --> 10:09:14,580
4. now if I uh let's type here names
13700
10:09:14,580 --> 10:09:17,700
that for each
13701
10:09:17,700 --> 10:09:20,596
print line
13702
10:09:21,660 --> 10:09:24,416
not in this footage
13703
10:09:24,416 --> 10:09:26,096
name start
13704
10:09:26,096 --> 10:09:29,756
for each this for each
13705
10:09:29,756 --> 10:09:32,596
opening the line
13706
10:09:34,140 --> 10:09:38,060
now if you run this
13707
10:09:38,756 --> 10:09:40,860
now you see you have name one name two
13708
10:09:40,860 --> 10:09:42,776
name three and name four so name four
13709
10:09:42,776 --> 10:09:44,520
was added to our Multiple List because
13710
10:09:44,520 --> 10:09:46,320
our list is multiple we can add or
13711
10:09:46,320 --> 10:09:50,096
remove new elements so down here I can
13712
10:09:50,096 --> 10:09:54,240
I can type names dot remove
13713
10:09:54,240 --> 10:09:56,520
and we can remove an element and we can
13714
10:09:56,520 --> 10:09:59,096
remove the element I think by
13715
10:09:59,096 --> 10:10:00,900
you can remove you can use this function
13716
10:10:00,900 --> 10:10:02,700
remove art so we can remove the element
13717
10:10:02,700 --> 10:10:05,096
by its index so we can type here the
13718
10:10:05,096 --> 10:10:06,180
index
13719
10:10:06,180 --> 10:10:07,860
and that is going to remove the first
13720
10:10:07,860 --> 10:10:11,480
element so if you run this code
13721
10:10:12,060 --> 10:10:15,860
name one is not going to be showed
13722
10:10:17,276 --> 10:10:19,436
so as you can see name one was removed
13723
10:10:19,436 --> 10:10:21,240
because you can you will remove it by
13724
10:10:21,240 --> 10:10:23,460
its index and we can also remove an
13725
10:10:23,460 --> 10:10:24,416
element
13726
10:10:24,416 --> 10:10:27,960
using this function by typing uh
13727
10:10:27,960 --> 10:10:30,060
the elements so we type here let's say
13728
10:10:30,060 --> 10:10:32,400
name
13729
10:10:32,400 --> 10:10:34,080
two
13730
10:10:34,080 --> 10:10:35,756
name two elements and now it's going to
13731
10:10:35,756 --> 10:10:39,860
be removed so now if you run this
13732
10:10:41,580 --> 10:10:43,560
name two was removed so those are two
13733
10:10:43,560 --> 10:10:45,060
functions with which you can remove
13734
10:10:45,060 --> 10:10:47,756
elements from the Multiple List so this
13735
10:10:47,756 --> 10:10:49,916
is a multiple list you can add and
13736
10:10:49,916 --> 10:10:51,960
remove elements to it and the list is
13737
10:10:51,960 --> 10:10:54,840
gonna grow and Shrink as you add or
13738
10:10:54,840 --> 10:10:56,580
remove elements to it so this is the
13739
10:10:56,580 --> 10:10:57,900
Multiple List
13740
10:10:57,900 --> 10:10:59,400
now
13741
10:10:59,400 --> 10:11:01,860
we're gonna look at a set and I set this
13742
10:11:01,860 --> 10:11:04,680
a collection which can store only unique
13743
10:11:04,680 --> 10:11:06,060
unique elements so you cannot have
13744
10:11:06,060 --> 10:11:10,860
duplicates so if I delete this
13745
10:11:10,860 --> 10:11:13,500
so in our
13746
10:11:13,500 --> 10:11:16,080
in our Multiple List you can put an
13747
10:11:16,080 --> 10:11:19,880
element which is the same
13748
10:11:20,580 --> 10:11:22,256
so we can have two elements which are
13749
10:11:22,256 --> 10:11:26,096
the same so if you run this
13750
10:11:26,096 --> 10:11:28,620
we have two elements which are the same
13751
10:11:28,620 --> 10:11:31,020
going to see name one at the end I have
13752
10:11:31,020 --> 10:11:32,580
name one name two name three and name
13753
10:11:32,580 --> 10:11:35,640
one again so you can have uh duplicates
13754
10:11:35,640 --> 10:11:37,980
in our collection on our multiple list
13755
10:11:37,980 --> 10:11:41,460
but in a set you can't so but first
13756
10:11:41,460 --> 10:11:43,380
we're gonna look at uh first we're gonna
13757
10:11:43,380 --> 10:11:45,960
look at the immutable set and then add
13758
10:11:45,960 --> 10:11:50,240
automotive ball set so we type here uh
13759
10:11:51,000 --> 10:11:53,936
let's delete this
13760
10:11:53,936 --> 10:11:57,596
we type here set off
13761
10:11:57,596 --> 10:12:01,560
and here you need to buy string
13762
10:12:01,560 --> 10:12:05,000
now if you run this
13763
10:12:05,880 --> 10:12:08,460
you will see that name one is not shown
13764
10:12:08,460 --> 10:12:09,776
two times here it's going to be shown
13765
10:12:09,776 --> 10:12:11,340
only one time so you have name one name
13766
10:12:11,340 --> 10:12:13,916
two and name three this is because the
13767
10:12:13,916 --> 10:12:17,820
set Seto the set is not allowing
13768
10:12:17,820 --> 10:12:21,960
duplicates in its uh in its list so
13769
10:12:21,960 --> 10:12:24,596
and this is a new multiple set set off
13770
10:12:24,596 --> 10:12:28,200
so if I type here names dot add we
13771
10:12:28,200 --> 10:12:30,660
cannot add or remove new elements but
13772
10:12:30,660 --> 10:12:32,220
you can also use
13773
10:12:32,220 --> 10:12:35,276
a multiple set of so we type here
13774
10:12:35,276 --> 10:12:38,540
Moto motable
13775
10:12:40,500 --> 10:12:42,360
set off
13776
10:12:42,360 --> 10:12:44,400
and now we can add and remove new
13777
10:12:44,400 --> 10:12:47,460
elements so you can type names that add
13778
10:12:47,460 --> 10:12:49,320
so here you can add a new element let's
13779
10:12:49,320 --> 10:12:50,640
say
13780
10:12:50,640 --> 10:12:53,300
name
13781
10:12:56,580 --> 10:12:59,900
so now if you run this
13782
10:13:03,240 --> 10:13:05,580
I'm going to name one name two name
13783
10:13:05,580 --> 10:13:07,080
three name four
13784
10:13:07,080 --> 10:13:10,140
and our duplicate is not strong because
13785
10:13:10,140 --> 10:13:13,200
I said a set can only
13786
10:13:13,200 --> 10:13:15,240
can only store unique elements not
13787
10:13:15,240 --> 10:13:18,060
duplicates but if I add objects that we
13788
10:13:18,060 --> 10:13:19,740
create look what happens so let's create
13789
10:13:19,740 --> 10:13:21,900
some user objects but first let's create
13790
10:13:21,900 --> 10:13:24,480
the class user
13791
10:13:24,480 --> 10:13:26,220
and it's gonna have a
13792
10:13:26,220 --> 10:13:30,360
Val name is going to be of type string
13793
10:13:30,360 --> 10:13:32,580
no let's create some user objects here
13794
10:13:32,580 --> 10:13:35,340
and we're gonna add to our multiple set
13795
10:13:35,340 --> 10:13:36,660
so we type here
13796
10:13:36,660 --> 10:13:39,720
wall user one
13797
10:13:39,720 --> 10:13:42,540
I'm gonna type here
13798
10:13:42,540 --> 10:13:44,820
user
13799
10:13:44,820 --> 10:13:46,500
I'm going to pass some generic names
13800
10:13:46,500 --> 10:13:49,460
like
13801
10:13:51,360 --> 10:13:53,880
name one and let's control the a few
13802
10:13:53,880 --> 10:13:55,320
times
13803
10:13:55,320 --> 10:13:58,680
let's change this to name two
13804
10:13:58,680 --> 10:13:59,700
five
13805
10:13:59,700 --> 10:14:03,500
and let's add here a Val
13806
10:14:05,040 --> 10:14:07,200
user
13807
10:14:07,200 --> 10:14:08,596
six
13808
10:14:08,596 --> 10:14:11,160
equals user
13809
10:14:11,160 --> 10:14:14,580
and let's type here Alex
13810
10:14:14,580 --> 10:14:16,860
let's press Ctrl d
13811
10:14:16,860 --> 10:14:18,840
and let's change this to
13812
10:14:18,840 --> 10:14:20,640
user 7.
13813
10:14:20,640 --> 10:14:23,520
now let's add those to our
13814
10:14:23,520 --> 10:14:25,740
multiple cells so let's change now the
13815
10:14:25,740 --> 10:14:27,979
type
13816
10:14:28,560 --> 10:14:33,079
so it's gonna store user objects
13817
10:14:36,256 --> 10:14:40,220
so we're gonna pass here
13818
10:14:42,120 --> 10:14:44,820
user one I'm going to speed up now this
13819
10:14:44,820 --> 10:14:46,916
a little bit
13820
10:14:46,916 --> 10:14:49,080
I'm gonna press Ctrl tilde format the
13821
10:14:49,080 --> 10:14:51,200
code
13822
10:14:51,300 --> 10:14:53,700
now we can add this we cannot add here a
13823
10:14:53,700 --> 10:14:57,416
name for because now the type is user
13824
10:14:57,416 --> 10:14:59,276
now if you're on this let's actually
13825
10:14:59,276 --> 10:15:04,020
change this print to it dot name
13826
10:15:04,020 --> 10:15:07,880
and let's let's run our code now
13827
10:15:08,640 --> 10:15:10,980
and we have name one name two name three
13828
10:15:10,980 --> 10:15:13,256
name four Nerf name five and have Alex
13829
10:15:13,256 --> 10:15:15,180
two times so why you have duplicates
13830
10:15:15,180 --> 10:15:18,060
even though I said that sets can only
13831
10:15:18,060 --> 10:15:19,740
store unique elements this is because
13832
10:15:19,740 --> 10:15:23,040
previously the set actually what is
13833
10:15:23,040 --> 10:15:26,460
doing to determine to if to if it has
13834
10:15:26,460 --> 10:15:28,500
duplicates is using the equals to
13835
10:15:28,500 --> 10:15:30,120
function that we talked about in our
13836
10:15:30,120 --> 10:15:32,580
video about data classes so when we
13837
10:15:32,580 --> 10:15:36,416
typed only strings then it was using the
13838
10:15:36,416 --> 10:15:39,120
equals to function which was overridden
13839
10:15:39,120 --> 10:15:42,060
inside the string class to determine if
13840
10:15:42,060 --> 10:15:46,080
there are uh if there are equal strings
13841
10:15:46,080 --> 10:15:48,596
but now because our class doesn't have
13842
10:15:48,596 --> 10:15:50,756
the equals to function over it then it's
13843
10:15:50,756 --> 10:15:54,240
in its class it's not it's using the
13844
10:15:54,240 --> 10:15:56,340
equals to function which is inside the
13845
10:15:56,340 --> 10:15:58,800
ending class so you cannot determine if
13846
10:15:58,800 --> 10:16:02,220
two users are are equal and to change
13847
10:16:02,220 --> 10:16:05,820
that just let's hide the console
13848
10:16:05,820 --> 10:16:08,340
and just put the data keyword here
13849
10:16:08,340 --> 10:16:10,680
because now this will generate the
13850
10:16:10,680 --> 10:16:13,436
equals to function the the the two
13851
10:16:13,436 --> 10:16:14,936
string and
13852
10:16:14,936 --> 10:16:19,096
the harsh code so now if you run this
13853
10:16:24,300 --> 10:16:26,276
now we have Alex only one time because
13854
10:16:26,276 --> 10:16:27,900
now it's using the equals to function
13855
10:16:27,900 --> 10:16:32,040
which was uh Auto generated by by uh by
13856
10:16:32,040 --> 10:16:34,436
the class user because you put here the
13857
10:16:34,436 --> 10:16:38,400
data key or so this is a good thing to
13858
10:16:38,400 --> 10:16:39,900
have in mind that
13859
10:16:39,900 --> 10:16:42,060
this model set is using the equals to
13860
10:16:42,060 --> 10:16:44,460
function to determine if the object that
13861
10:16:44,460 --> 10:16:48,240
you add to to yourself are equal so
13862
10:16:48,240 --> 10:16:51,240
having this in mind now let's look at
13863
10:16:51,240 --> 10:16:55,256
Maps so I'm going to close this
13864
10:16:55,256 --> 10:16:58,460
I'm gonna delete this
13865
10:16:59,276 --> 10:17:01,916
let's delete this
13866
10:17:01,916 --> 10:17:05,416
let's delete this also
13867
10:17:09,120 --> 10:17:10,620
now let's look at the next collection
13868
10:17:10,620 --> 10:17:12,240
that you can use in kotlin and that is
13869
10:17:12,240 --> 10:17:13,860
the map collection and use collection
13870
10:17:13,860 --> 10:17:16,560
when you want to store key value pairs
13871
10:17:16,560 --> 10:17:18,180
so I'm going to create a variable it's
13872
10:17:18,180 --> 10:17:20,040
going to be called users
13873
10:17:20,040 --> 10:17:22,436
and I'm going to put equals and I'm
13874
10:17:22,436 --> 10:17:24,240
going to create first animated bottom up
13875
10:17:24,240 --> 10:17:27,120
so we type map off
13876
10:17:27,120 --> 10:17:28,380
map
13877
10:17:28,380 --> 10:17:32,160
of and here you need to specify what
13878
10:17:32,160 --> 10:17:34,080
kind of type is going to be the key so
13879
10:17:34,080 --> 10:17:35,400
the key is going to be an integer but
13880
10:17:35,400 --> 10:17:37,380
you can put any class you want here
13881
10:17:37,380 --> 10:17:39,660
and the value is going to be a string so
13882
10:17:39,660 --> 10:17:42,060
I'm going to store the ID and the name
13883
10:17:42,060 --> 10:17:43,916
of an user
13884
10:17:43,916 --> 10:17:47,400
and to Define our element in pairs here
13885
10:17:47,400 --> 10:17:49,980
we type first the key and then we use
13886
10:17:49,980 --> 10:17:54,360
the to keyword to map our kit to its
13887
10:17:54,360 --> 10:17:57,660
element so 2 is value actually so type 1
13888
10:17:57,660 --> 10:18:00,960
2 and now the value let's first type
13889
10:18:00,960 --> 10:18:02,580
Maria so
13890
10:18:02,580 --> 10:18:07,080
now we have one element stored in paired
13891
10:18:07,080 --> 10:18:09,680
and the key is one the value is Maria
13892
10:18:09,680 --> 10:18:12,596
next I'm gonna do the same for the next
13893
10:18:12,596 --> 10:18:14,580
one so I'm gonna type here
13894
10:18:14,580 --> 10:18:16,436
two
13895
10:18:16,436 --> 10:18:17,820
two
13896
10:18:17,820 --> 10:18:19,916
the keyword tool
13897
10:18:19,916 --> 10:18:21,180
Alex
13898
10:18:21,180 --> 10:18:23,880
and then
13899
10:18:23,880 --> 10:18:26,276
I'm gonna type three
13900
10:18:26,276 --> 10:18:28,200
the keyword tool
13901
10:18:28,200 --> 10:18:30,300
so inside you need to type the value now
13902
10:18:30,300 --> 10:18:33,740
now type John
13903
10:18:35,880 --> 10:18:39,240
now to get one element from this map
13904
10:18:39,240 --> 10:18:41,640
with I print line
13905
10:18:41,640 --> 10:18:43,916
we type here users
13906
10:18:43,916 --> 10:18:47,060
square brackets
13907
10:18:47,756 --> 10:18:50,220
and we type we get the element by its
13908
10:18:50,220 --> 10:18:53,400
key as you can see by this hint key so
13909
10:18:53,400 --> 10:18:57,300
we get let's say the element one
13910
10:18:57,300 --> 10:19:00,740
so now if you run this code
13911
10:19:04,916 --> 10:19:07,800
we get Maria because Maria has because
13912
10:19:07,800 --> 10:19:11,220
this key code is for the for the key one
13913
10:19:11,220 --> 10:19:13,620
the value mapped the value which is
13914
10:19:13,620 --> 10:19:16,080
corresponding to this key is Maria and
13915
10:19:16,080 --> 10:19:18,900
if you change this to two
13916
10:19:18,900 --> 10:19:23,480
gonna get Alex if you run this
13917
10:19:26,640 --> 10:19:29,096
so this is an immutable map in this map
13918
10:19:29,096 --> 10:19:31,380
you cannot add or remove elements but if
13919
10:19:31,380 --> 10:19:33,000
I declare this as
13920
10:19:33,000 --> 10:19:35,340
a multiple map so let's change this to a
13921
10:19:35,340 --> 10:19:36,900
multiple map
13922
10:19:36,900 --> 10:19:39,180
so we type here mutable
13923
10:19:39,180 --> 10:19:41,960
map off
13924
10:19:42,660 --> 10:19:45,980
let's delete this from here
13925
10:19:47,756 --> 10:19:52,256
and let's pass here and
13926
10:19:52,256 --> 10:19:54,960
and string
13927
10:19:54,960 --> 10:19:58,340
now here now
13928
10:19:58,860 --> 10:20:02,840
so you need to put this in parenthesis
13929
10:20:03,120 --> 10:20:05,276
here we can add new elements so we can
13930
10:20:05,276 --> 10:20:08,180
type users
13931
10:20:09,120 --> 10:20:11,720
actually first let's Loop through our
13932
10:20:11,720 --> 10:20:13,740
mob to see
13933
10:20:13,740 --> 10:20:16,256
our elements and I'm going to use the
13934
10:20:16,256 --> 10:20:17,756
for each function to Loop so we type
13935
10:20:17,756 --> 10:20:21,900
here users dot for each
13936
10:20:21,900 --> 10:20:25,276
and we choose this one
13937
10:20:25,980 --> 10:20:28,436
and inside our Rook without we are the
13938
10:20:28,436 --> 10:20:29,640
print line
13939
10:20:29,640 --> 10:20:31,200
and
13940
10:20:31,200 --> 10:20:32,700
I'm going to put quotation marks and
13941
10:20:32,700 --> 10:20:34,740
here we're going to put dollar sign I'm
13942
10:20:34,740 --> 10:20:39,480
going to type T and T is the key and
13943
10:20:39,480 --> 10:20:42,240
dollar sign U and U is the value so now
13944
10:20:42,240 --> 10:20:45,020
if you're on this
13945
10:20:50,276 --> 10:20:52,680
you get one and Maria to and Alex Three
13946
10:20:52,680 --> 10:20:55,140
Angels so we have the key and we have
13947
10:20:55,140 --> 10:20:57,540
the corresponding values and you cannot
13948
10:20:57,540 --> 10:20:59,400
have duplicated keys and this is very
13949
10:20:59,400 --> 10:21:01,680
important to know you can also add or
13950
10:21:01,680 --> 10:21:03,180
remove elements
13951
10:21:03,180 --> 10:21:06,480
from this map so to add an element you
13952
10:21:06,480 --> 10:21:08,400
just type users
13953
10:21:08,400 --> 10:21:11,276
and we need to specify the key
13954
10:21:11,276 --> 10:21:14,040
so we put square brackets so we type
13955
10:21:14,040 --> 10:21:15,960
user square brackets and then put a key
13956
10:21:15,960 --> 10:21:17,756
let's say five
13957
10:21:17,756 --> 10:21:21,960
and we put here equals to and here we
13958
10:21:21,960 --> 10:21:23,700
need to pass a value so the value is
13959
10:21:23,700 --> 10:21:25,200
going to be let's say
13960
10:21:25,200 --> 10:21:29,360
Vlog now if you run this
13961
10:21:34,080 --> 10:21:37,436
we get one and Maria two and Alex three
13962
10:21:37,436 --> 10:21:40,080
and John N5 and the corresponding value
13963
10:21:40,080 --> 10:21:43,980
Vlad so Vlad has the Kiwi the key value
13964
10:21:43,980 --> 10:21:45,300
five
13965
10:21:45,300 --> 10:21:47,400
so you can also remove an element from
13966
10:21:47,400 --> 10:21:51,360
our map so we type users dot remove
13967
10:21:51,360 --> 10:21:54,180
you know you you here for this remove
13968
10:21:54,180 --> 10:21:57,416
function you pass the key of the of the
13969
10:21:57,416 --> 10:21:59,160
element that you want to be removed and
13970
10:21:59,160 --> 10:22:01,620
let's say that I remove two so I remove
13971
10:22:01,620 --> 10:22:04,916
Alex from our map so if I run this quad
13972
10:22:04,916 --> 10:22:06,900
now Alex you know it's not going to be
13973
10:22:06,900 --> 10:22:08,096
shown here
13974
10:22:08,096 --> 10:22:10,980
because it was removed on the line for
13975
10:22:10,980 --> 10:22:13,380
so Alex was removed so this is how you
13976
10:22:13,380 --> 10:22:16,320
can use the immutable and multiple Maps
13977
10:22:16,320 --> 10:22:18,360
this is how you can add and remove
13978
10:22:18,360 --> 10:22:21,360
elements and this is how you can Loop
13979
10:22:21,360 --> 10:22:23,520
through our map and this is our
13980
10:22:23,520 --> 10:22:25,800
discussion about Collections and see you
13981
10:22:25,800 --> 10:22:27,540
in the next video now it's time to start
13982
10:22:27,540 --> 10:22:29,220
a discussion about collections
13983
10:22:29,220 --> 10:22:30,660
operations and we're going to look at
13984
10:22:30,660 --> 10:22:32,220
the first operation and that is
13985
10:22:32,220 --> 10:22:34,200
Transformations and we're going to start
13986
10:22:34,200 --> 10:22:36,060
with the first one called mapping so I'm
13987
10:22:36,060 --> 10:22:39,020
going to create a new project
13988
10:22:39,060 --> 10:22:41,160
I'm going to call it
13989
10:22:41,160 --> 10:22:44,040
collections
13990
10:22:44,040 --> 10:22:46,936
operations
13991
10:22:47,240 --> 10:22:51,320
IntelliJ then click on create
13992
10:22:54,540 --> 10:22:57,596
we go on the left hand side
13993
10:22:57,596 --> 10:23:00,060
click on kotlin right click new I'm
13994
10:23:00,060 --> 10:23:01,560
going to create a new file I'm going to
13995
10:23:01,560 --> 10:23:03,720
call it Main
13996
10:23:03,720 --> 10:23:05,220
I'm going to create the main function
13997
10:23:05,220 --> 10:23:08,180
now
13998
10:23:08,520 --> 10:23:11,160
what are transformations as the name
13999
10:23:11,160 --> 10:23:14,220
implies Transformations are some
14000
10:23:14,220 --> 10:23:18,120
functions which which you can change a
14001
10:23:18,120 --> 10:23:20,096
specific collection
14002
10:23:20,096 --> 10:23:22,080
and we're going to look at the first one
14003
10:23:22,080 --> 10:23:24,360
which is called mapping so I'm going to
14004
10:23:24,360 --> 10:23:26,460
create first
14005
10:23:26,460 --> 10:23:29,040
a collection so I'm going to type Val
14006
10:23:29,040 --> 10:23:31,680
numbers and it's going to be a set so
14007
10:23:31,680 --> 10:23:33,300
I'm going to type numbers you're gonna
14008
10:23:33,300 --> 10:23:36,020
equals to set off
14009
10:23:36,020 --> 10:23:38,460
and here we're gonna Define some values
14010
10:23:38,460 --> 10:23:41,460
like one two
14011
10:23:41,460 --> 10:23:44,840
three four five
14012
10:23:45,776 --> 10:23:48,596
let's press Ctrl alt format the code
14013
10:23:48,596 --> 10:23:51,960
now I'm gonna add the print line here
14014
10:23:51,960 --> 10:23:55,256
now to use the mapping collection
14015
10:23:55,256 --> 10:23:58,560
transformation we just type numbers
14016
10:23:58,560 --> 10:24:01,620
and one important thing to know here is
14017
10:24:01,620 --> 10:24:03,416
that those
14018
10:24:03,416 --> 10:24:06,240
transformation functions are Lambda
14019
10:24:06,240 --> 10:24:08,460
functions so if you don't know what
14020
10:24:08,460 --> 10:24:11,276
Lambda functions are I suggest you to
14021
10:24:11,276 --> 10:24:13,256
watch the section which is called Lambda
14022
10:24:13,256 --> 10:24:14,936
function to First understand what our
14023
10:24:14,936 --> 10:24:17,820
Lambda functions because it will be it
14024
10:24:17,820 --> 10:24:19,140
will be very hard for you to understand
14025
10:24:19,140 --> 10:24:21,776
how this how those Transformations work
14026
10:24:21,776 --> 10:24:23,400
without understanding how Lambda
14027
10:24:23,400 --> 10:24:24,720
functions work
14028
10:24:24,720 --> 10:24:26,220
now
14029
10:24:26,220 --> 10:24:29,480
here we're gonna call our our
14030
10:24:29,480 --> 10:24:32,096
transformation function which is the
14031
10:24:32,096 --> 10:24:34,980
first one which as I said is mapping and
14032
10:24:34,980 --> 10:24:37,140
the function is called map I'm going to
14033
10:24:37,140 --> 10:24:38,756
explain immediately what it's doing and
14034
10:24:38,756 --> 10:24:40,560
as you can see it's a Lambda function so
14035
10:24:40,560 --> 10:24:42,060
we're going to press enter
14036
10:24:42,060 --> 10:24:44,640
and we we are inside the curly braces
14037
10:24:44,640 --> 10:24:46,560
and here we're gonna Define our
14038
10:24:46,560 --> 10:24:48,960
transformation so what we are saying
14039
10:24:48,960 --> 10:24:50,700
basically here is
14040
10:24:50,700 --> 10:24:55,680
do whatever I I type here in this those
14041
10:24:55,680 --> 10:24:56,756
uh
14042
10:24:56,756 --> 10:25:00,660
curly braces apply to every element of
14043
10:25:00,660 --> 10:25:03,240
the of this cell of this collection so
14044
10:25:03,240 --> 10:25:07,320
if I if uh now to refer to to refer to
14045
10:25:07,320 --> 10:25:10,620
the parameter uh uh to the specific
14046
10:25:10,620 --> 10:25:14,640
parameter from the numbers uh uh set we
14047
10:25:14,640 --> 10:25:16,560
type it
14048
10:25:16,560 --> 10:25:19,800
and I'm gonna type it times
14049
10:25:19,800 --> 10:25:24,916
10. so what we are saying here is
14050
10:25:24,960 --> 10:25:29,220
do this transformation so change this uh
14051
10:25:29,220 --> 10:25:34,680
change this uh this set by uh more
14052
10:25:34,680 --> 10:25:37,800
multiplying every element inside the
14053
10:25:37,800 --> 10:25:40,916
this uh this collection inside this set
14054
10:25:40,916 --> 10:25:44,580
by 10. so if you run this now every
14055
10:25:44,580 --> 10:25:46,620
number is going to be multiplied by 10.
14056
10:25:46,620 --> 10:25:49,400
so if I run this
14057
10:25:56,700 --> 10:26:00,060
now we get instead of one two three four
14058
10:26:00,060 --> 10:26:03,840
and five we get 10 20 30 45 so what this
14059
10:26:03,840 --> 10:26:07,200
map function and what this uh it times
14060
10:26:07,200 --> 10:26:10,380
10 did is is it transformed it changed
14061
10:26:10,380 --> 10:26:15,900
our set into into what it it was changed
14062
10:26:15,900 --> 10:26:18,240
by the expression that we defined here
14063
10:26:18,240 --> 10:26:21,660
so we said that I want every element we
14064
10:26:21,660 --> 10:26:23,936
said here to be multiplied by 10 and
14065
10:26:23,936 --> 10:26:26,276
then we display them here
14066
10:26:26,276 --> 10:26:28,916
we can go even further we can put here
14067
10:26:28,916 --> 10:26:32,220
an if statement so we can type here if
14068
10:26:32,220 --> 10:26:35,640
let's say it so it refers to the to a
14069
10:26:35,640 --> 10:26:38,220
specific element in that list in that
14070
10:26:38,220 --> 10:26:42,660
set so if it equals equals so if equals
14071
10:26:42,660 --> 10:26:46,256
three I'm gonna say
14072
10:26:46,256 --> 10:26:48,120
we're gonna say here
14073
10:26:48,120 --> 10:26:50,340
it
14074
10:26:50,340 --> 10:26:51,840
times
14075
10:26:51,840 --> 10:26:53,580
100.
14076
10:26:53,580 --> 10:26:57,060
so what we're saying here if one of
14077
10:26:57,060 --> 10:27:00,596
those elements inside these numbers set
14078
10:27:00,596 --> 10:27:04,740
is equal to 3 then multiply it change it
14079
10:27:04,740 --> 10:27:08,400
so change it by by the by this
14080
10:27:08,400 --> 10:27:11,520
expression so multiply it by 100 and you
14081
10:27:11,520 --> 10:27:13,080
can also put the else part so we put
14082
10:27:13,080 --> 10:27:17,936
else you want to put it times
14083
10:27:17,936 --> 10:27:21,060
so if the else part is true if the else
14084
10:27:21,060 --> 10:27:23,160
part is executed we're saying if the
14085
10:27:23,160 --> 10:27:25,820
number is not equal to 3 from this list
14086
10:27:25,820 --> 10:27:30,540
then multiply the number multiply the
14087
10:27:30,540 --> 10:27:35,416
number by uh by 10. so now if I run this
14088
10:27:36,060 --> 10:27:39,799
look what we have here
14089
10:27:41,880 --> 10:27:45,120
so we have 10 20 and we have 300 here
14090
10:27:45,120 --> 10:27:47,880
this is because this transformation now
14091
10:27:47,880 --> 10:27:51,180
is using the if and else statements that
14092
10:27:51,180 --> 10:27:53,936
you define here so this is what the map
14093
10:27:53,936 --> 10:27:57,000
Transformations allows you to do you can
14094
10:27:57,000 --> 10:27:59,096
change you can transform a specific
14095
10:27:59,096 --> 10:28:01,500
collection by an expression that we
14096
10:28:01,500 --> 10:28:03,416
defined inside the curly braces of the
14097
10:28:03,416 --> 10:28:05,040
map function
14098
10:28:05,040 --> 10:28:06,360
all right
14099
10:28:06,360 --> 10:28:10,140
now let's look at how we can can use the
14100
10:28:10,140 --> 10:28:12,256
map transformation to with another
14101
10:28:12,256 --> 10:28:14,936
collection unless you'll see how can you
14102
10:28:14,936 --> 10:28:17,820
can use the map transformation with a
14103
10:28:17,820 --> 10:28:20,520
map so with uh with the with the map
14104
10:28:20,520 --> 10:28:23,820
which has a key and a value so to do
14105
10:28:23,820 --> 10:28:26,640
that we just type here
14106
10:28:26,640 --> 10:28:27,900
Val
14107
10:28:27,900 --> 10:28:31,200
numbers map so I'm going to type file
14108
10:28:31,200 --> 10:28:32,660
numbers
14109
10:28:32,660 --> 10:28:35,756
equals I'm going to type map off
14110
10:28:35,756 --> 10:28:38,936
and here I'm gonna type
14111
10:28:38,936 --> 10:28:40,320
key
14112
10:28:40,320 --> 10:28:42,720
one
14113
10:28:42,720 --> 10:28:43,860
two
14114
10:28:43,860 --> 10:28:45,900
one
14115
10:28:45,900 --> 10:28:47,040
then
14116
10:28:47,040 --> 10:28:48,660
key
14117
10:28:48,660 --> 10:28:50,276
tool
14118
10:28:50,276 --> 10:28:53,276
two two then
14119
10:28:53,276 --> 10:28:55,620
key three
14120
10:28:55,620 --> 10:28:57,180
two
14121
10:28:57,180 --> 10:28:58,436
three
14122
10:28:58,436 --> 10:29:00,060
and
14123
10:29:00,060 --> 10:29:01,916
finally key
14124
10:29:01,916 --> 10:29:03,480
for
14125
10:29:03,480 --> 10:29:04,620
two
14126
10:29:04,620 --> 10:29:05,640
four
14127
10:29:05,640 --> 10:29:09,500
now if you wanna use the map map
14128
10:29:09,500 --> 10:29:13,080
transfer transformation function on the
14129
10:29:13,080 --> 10:29:17,640
map map collection you you you can use
14130
10:29:17,640 --> 10:29:21,480
it either on the keys so you can use it
14131
10:29:21,480 --> 10:29:24,596
on the keys or on the values so we can
14132
10:29:24,596 --> 10:29:27,480
type here print line
14133
10:29:27,480 --> 10:29:29,340
number map
14134
10:29:29,340 --> 10:29:33,140
number map so we choose here
14135
10:29:33,180 --> 10:29:37,080
Dot and we choose map Keys now so choose
14136
10:29:37,080 --> 10:29:40,860
this one map key Lambda expression so
14137
10:29:40,860 --> 10:29:45,180
map keys that one and here to type eat
14138
10:29:45,180 --> 10:29:50,340
dot key so get the key Dot uppercase so
14139
10:29:50,340 --> 10:29:52,620
that is going to upper case all the keys
14140
10:29:52,620 --> 10:29:55,256
inside our map then I'm gonna add an
14141
10:29:55,256 --> 10:29:57,540
another print line here
14142
10:29:57,540 --> 10:30:00,860
I'm gonna type number map
14143
10:30:01,320 --> 10:30:03,060
Dot
14144
10:30:03,060 --> 10:30:04,256
map
14145
10:30:04,256 --> 10:30:05,880
now choose
14146
10:30:05,880 --> 10:30:08,880
map values
14147
10:30:08,880 --> 10:30:12,660
and we type here it dot value
14148
10:30:12,660 --> 10:30:16,500
so the Value Plus
14149
10:30:16,756 --> 10:30:21,776
it dot key dot length
14150
10:30:21,776 --> 10:30:24,360
now if you run this though this
14151
10:30:24,360 --> 10:30:26,520
expression is going to be applied to all
14152
10:30:26,520 --> 10:30:28,860
of our keys inside our numbers map that
14153
10:30:28,860 --> 10:30:30,720
we Define above and the second
14154
10:30:30,720 --> 10:30:32,276
transformation is going to be applied to
14155
10:30:32,276 --> 10:30:33,960
all the values that we defined inside
14156
10:30:33,960 --> 10:30:36,120
our numbers map so if you run this code
14157
10:30:36,120 --> 10:30:38,096
now I'm going to see the
14158
10:30:38,096 --> 10:30:40,680
the keys being first uppercase and then
14159
10:30:40,680 --> 10:30:42,480
we're going to see the keys plus plus
14160
10:30:42,480 --> 10:30:45,060
the length so you see
14161
10:30:45,060 --> 10:30:50,096
first I get 10 20 300 for 40 and 50 for
14162
10:30:50,096 --> 10:30:53,040
our first transformation that we have
14163
10:30:53,040 --> 10:30:54,060
here
14164
10:30:54,060 --> 10:30:58,140
then we have our second then we have our
14165
10:30:58,140 --> 10:31:00,240
transformation on the map on the map
14166
10:31:00,240 --> 10:31:02,340
keys so we have we have the key and then
14167
10:31:02,340 --> 10:31:04,500
we have the value we have he and the the
14168
10:31:04,500 --> 10:31:07,680
key is uppercase because we said uh to
14169
10:31:07,680 --> 10:31:10,256
to do that by using this expression it
14170
10:31:10,256 --> 10:31:12,480
so it uppercase all the keys as you can
14171
10:31:12,480 --> 10:31:13,980
see here and then you see the
14172
10:31:13,980 --> 10:31:16,140
corresponding value for the key
14173
10:31:16,140 --> 10:31:18,120
after that you see
14174
10:31:18,120 --> 10:31:20,640
you have numbers map map that value so
14175
10:31:20,640 --> 10:31:23,220
you have it that Value Plus e dot key
14176
10:31:23,220 --> 10:31:25,436
dot length so we have key
14177
10:31:25,436 --> 10:31:29,276
and I have one and we have six we have
14178
10:31:29,276 --> 10:31:32,700
key two and half seven so the key the
14179
10:31:32,700 --> 10:31:36,060
value of the key is uh is uh to the
14180
10:31:36,060 --> 10:31:38,580
value of the key it's added the length
14181
10:31:38,580 --> 10:31:40,380
of the
14182
10:31:40,380 --> 10:31:44,880
so to the value that we have here we we
14183
10:31:44,880 --> 10:31:49,140
have added the the value length of the
14184
10:31:49,140 --> 10:31:51,596
string key that we defined here so this
14185
10:31:51,596 --> 10:31:55,080
way I have uh we have uh two then we
14186
10:31:55,080 --> 10:31:57,540
have seven then we have eight and then
14187
10:31:57,540 --> 10:31:58,800
we have nine
14188
10:31:58,800 --> 10:32:02,276
so this is our and you can also
14189
10:32:02,276 --> 10:32:05,640
you can also do for the uh map for the
14190
10:32:05,640 --> 10:32:07,680
map transformation and we type here
14191
10:32:07,680 --> 10:32:09,360
numbers
14192
10:32:09,360 --> 10:32:12,060
Dot map
14193
10:32:12,060 --> 10:32:14,220
and we choose this one index not node
14194
10:32:14,220 --> 10:32:17,460
and here we choose we have two
14195
10:32:17,460 --> 10:32:20,220
parameters first is the index and second
14196
10:32:20,220 --> 10:32:22,740
is the value so I'm gonna choose the
14197
10:32:22,740 --> 10:32:24,776
name for the first one index and for the
14198
10:32:24,776 --> 10:32:26,756
second one let's call it uh
14199
10:32:26,756 --> 10:32:29,756
value then you have the auto and here we
14200
10:32:29,756 --> 10:32:31,096
have we're going to Define our
14201
10:32:31,096 --> 10:32:33,840
transformation of our expression which
14202
10:32:33,840 --> 10:32:36,120
is going to be applied on our numbers so
14203
10:32:36,120 --> 10:32:38,460
here I can use the index so again I'm
14204
10:32:38,460 --> 10:32:40,140
going to type here an effects if a
14205
10:32:40,140 --> 10:32:42,960
statement so if
14206
10:32:42,960 --> 10:32:45,916
index
14207
10:32:46,080 --> 10:32:50,880
equals equals zero then I'm gonna
14208
10:32:50,880 --> 10:32:53,400
assign so I'm going to transform to the
14209
10:32:53,400 --> 10:32:55,080
to that we're going to put the value
14210
10:32:55,080 --> 10:32:57,120
null
14211
10:32:57,120 --> 10:33:00,916
to the index 0 else
14212
10:33:01,020 --> 10:33:04,700
I'm gonna type index
14213
10:33:05,276 --> 10:33:07,800
times value
14214
10:33:07,800 --> 10:33:10,860
so else you're gonna uh multiply the
14215
10:33:10,860 --> 10:33:13,200
index time times the value so if you run
14216
10:33:13,200 --> 10:33:15,500
this
14217
10:33:19,220 --> 10:33:24,120
we get 2 6 12 and 20. so for for the
14218
10:33:24,120 --> 10:33:26,820
first one we get nothing because we for
14219
10:33:26,820 --> 10:33:29,340
the first index we signed null
14220
10:33:29,340 --> 10:33:31,916
and then uh it's using the else part to
14221
10:33:31,916 --> 10:33:35,220
to transform our numbers list and it's
14222
10:33:35,220 --> 10:33:38,460
multiplying the index by the value so we
14223
10:33:38,460 --> 10:33:40,200
we first uh
14224
10:33:40,200 --> 10:33:43,620
we first have this indexed uh
14225
10:33:43,620 --> 10:33:47,040
zero multiply the here we have null for
14226
10:33:47,040 --> 10:33:48,900
this because we checked here so we have
14227
10:33:48,900 --> 10:33:51,480
not so at the index two we have one
14228
10:33:51,480 --> 10:33:54,840
times two and uh we have two then here
14229
10:33:54,840 --> 10:33:57,720
we have add indexed two we have two
14230
10:33:57,720 --> 10:34:00,060
times three so the index is two the
14231
10:34:00,060 --> 10:34:04,380
value is three and uh we we get uh we
14232
10:34:04,380 --> 10:34:07,500
get six here and so on so this is how we
14233
10:34:07,500 --> 10:34:09,416
can use also the index if you want in
14234
10:34:09,416 --> 10:34:11,640
your transformation now we're going to
14235
10:34:11,640 --> 10:34:13,256
look at the next transformation that you
14236
10:34:13,256 --> 10:34:14,936
can do in our collection and that is
14237
10:34:14,936 --> 10:34:17,540
zipping
14238
10:34:17,936 --> 10:34:19,436
now it's time to look at the
14239
10:34:19,436 --> 10:34:21,960
transformation function called zipping
14240
10:34:21,960 --> 10:34:23,820
so I'm going to add a comment here
14241
10:34:23,820 --> 10:34:25,380
called the
14242
10:34:25,380 --> 10:34:27,540
zipping
14243
10:34:27,540 --> 10:34:30,000
and I'm going to declared to list I'm
14244
10:34:30,000 --> 10:34:34,140
gonna call the first one colors
14245
10:34:34,140 --> 10:34:36,916
no particles
14246
10:34:37,560 --> 10:34:40,820
I'm gonna type please stop
14247
10:34:42,480 --> 10:34:45,380
I'm gonna type red
14248
10:34:46,980 --> 10:34:49,640
Brown
14249
10:34:49,680 --> 10:34:52,256
and
14250
10:34:52,256 --> 10:34:54,800
Gray
14251
10:34:55,256 --> 10:34:57,416
and I'm going to create another list
14252
10:34:57,416 --> 10:35:00,300
called the animals
14253
10:35:00,300 --> 10:35:03,300
stuff
14254
10:35:04,080 --> 10:35:08,460
I'm gonna type here Fox
14255
10:35:08,460 --> 10:35:10,256
beer
14256
10:35:10,256 --> 10:35:12,660
quotation marks
14257
10:35:12,660 --> 10:35:14,220
and uh
14258
10:35:14,220 --> 10:35:16,700
wolf
14259
10:35:17,880 --> 10:35:19,436
now
14260
10:35:19,436 --> 10:35:23,596
what does the zipping or the zip
14261
10:35:23,596 --> 10:35:27,200
functions allow us to do is to create
14262
10:35:27,200 --> 10:35:30,240
pairs of elements with the same position
14263
10:35:30,240 --> 10:35:33,360
in both collections so we can create
14264
10:35:33,360 --> 10:35:36,900
pairs with red we can create a pair of
14265
10:35:36,900 --> 10:35:40,916
fox and red a pair of bear and brown and
14266
10:35:40,916 --> 10:35:42,776
a pair of wolf and gray
14267
10:35:42,776 --> 10:35:45,060
and you can do that just by typing here
14268
10:35:45,060 --> 10:35:46,800
print line
14269
10:35:46,800 --> 10:35:48,840
and we have you can do this in two ways
14270
10:35:48,840 --> 10:35:51,720
to type colors
14271
10:35:51,720 --> 10:35:54,660
and you can type Dot
14272
10:35:54,660 --> 10:36:00,200
dot zip and you type here animals
14273
10:36:00,360 --> 10:36:01,680
and
14274
10:36:01,680 --> 10:36:04,080
if you run this
14275
10:36:04,080 --> 10:36:06,416
you'll see them in pairs so you're gonna
14276
10:36:06,416 --> 10:36:08,276
see Fox thread
14277
10:36:08,276 --> 10:36:12,416
bear brown and so on so you see here red
14278
10:36:12,416 --> 10:36:18,120
fox brown bear gray also the zip and the
14279
10:36:18,120 --> 10:36:20,640
zipping is used to create Pairs and the
14280
10:36:20,640 --> 10:36:24,776
pairs are at the same position in both
14281
10:36:24,776 --> 10:36:27,256
on both collections
14282
10:36:27,256 --> 10:36:30,360
or if you don't want to use this Z
14283
10:36:30,360 --> 10:36:32,820
function with parenthesis you can type
14284
10:36:32,820 --> 10:36:37,140
here colors zip and animals
14285
10:36:37,140 --> 10:36:39,240
and type here any models
14286
10:36:39,240 --> 10:36:40,800
now if you run this code you're going to
14287
10:36:40,800 --> 10:36:43,580
see the same output
14288
10:36:51,360 --> 10:36:54,120
now if you want to use a transformation
14289
10:36:54,120 --> 10:36:56,096
with the Z function you can type here
14290
10:36:56,096 --> 10:36:57,776
print line
14291
10:36:57,776 --> 10:37:02,220
you can type colors dot zip
14292
10:37:02,220 --> 10:37:04,380
and I'm going to use the parenthesis
14293
10:37:04,380 --> 10:37:07,140
animals
14294
10:37:07,140 --> 10:37:10,620
and animals and now I'm going to put
14295
10:37:10,620 --> 10:37:13,020
curly braces so the Lambda function here
14296
10:37:13,020 --> 10:37:14,520
I'm going to type
14297
10:37:14,520 --> 10:37:17,119
animal
14298
10:37:18,596 --> 10:37:22,436
for a let's put for scholar
14299
10:37:22,436 --> 10:37:26,756
and animal I'm gonna put the arrow sign
14300
10:37:26,756 --> 10:37:29,520
and here I'm gonna type day I'm gonna
14301
10:37:29,520 --> 10:37:32,160
but I'm gonna put first
14302
10:37:32,160 --> 10:37:35,160
quotation marks and quite the
14303
10:37:35,160 --> 10:37:38,220
and I'm gonna put dollar sign
14304
10:37:38,220 --> 10:37:41,820
and we're going to refer our animal uh
14305
10:37:41,820 --> 10:37:44,040
parameter that we defined here so we
14306
10:37:44,040 --> 10:37:45,416
type here there
14307
10:37:45,416 --> 10:37:47,220
animal
14308
10:37:47,220 --> 10:37:51,416
Dot and we call the replace first chart
14309
10:37:51,416 --> 10:37:53,936
function so type replace first chart and
14310
10:37:53,936 --> 10:37:56,520
as you can see this also has uh I think
14311
10:37:56,520 --> 10:37:57,680
along the
14312
10:37:57,680 --> 10:38:03,320
function and inside of this we type it
14313
10:38:03,320 --> 10:38:06,300
dot uppercase
14314
10:38:06,300 --> 10:38:08,756
uppercase and this is gonna uppercase
14315
10:38:08,756 --> 10:38:13,020
the first letter of the animal
14316
10:38:13,020 --> 10:38:14,640
uh
14317
10:38:14,640 --> 10:38:17,700
with the animal that is defined here and
14318
10:38:17,700 --> 10:38:20,400
I'm going to type yes
14319
10:38:20,400 --> 10:38:22,200
dollar sign
14320
10:38:22,200 --> 10:38:23,580
color
14321
10:38:23,580 --> 10:38:27,320
right now if you run this
14322
10:38:27,480 --> 10:38:30,900
you're gonna see the force and the f is
14323
10:38:30,900 --> 10:38:34,436
uppercase because it's uppercase by this
14324
10:38:34,436 --> 10:38:36,660
expression that we have here
14325
10:38:36,660 --> 10:38:39,720
it replaces and this it refers to the
14326
10:38:39,720 --> 10:38:43,980
animal uh an animal uh variable animal
14327
10:38:43,980 --> 10:38:45,960
variable that is defined here and it
14328
10:38:45,960 --> 10:38:48,660
refers to the first letter so it says
14329
10:38:48,660 --> 10:38:50,520
eat that uppercase so uppercase the
14330
10:38:50,520 --> 10:38:51,720
first letter
14331
10:38:51,720 --> 10:38:55,500
then uh then uh return that to the to
14332
10:38:55,500 --> 10:38:58,800
the animal uh any more variable and we
14333
10:38:58,800 --> 10:39:01,200
have the fox is red so I have the color
14334
10:39:01,200 --> 10:39:04,140
and then we have the bear is brown the
14335
10:39:04,140 --> 10:39:06,960
wolf is gray and all the animals start
14336
10:39:06,960 --> 10:39:08,936
the capital letter like we Define here
14337
10:39:08,936 --> 10:39:11,096
in this
14338
10:39:11,096 --> 10:39:13,200
in this expression
14339
10:39:13,200 --> 10:39:15,416
so this is the zip function so this is
14340
10:39:15,416 --> 10:39:16,916
how you can use the Z function to create
14341
10:39:16,916 --> 10:39:20,700
Pairs and also you can use the unzip
14342
10:39:20,700 --> 10:39:24,000
function to unzip a list of pairs so
14343
10:39:24,000 --> 10:39:26,276
first let's create a list of pairs so
14344
10:39:26,276 --> 10:39:28,080
I'm going to type Val
14345
10:39:28,080 --> 10:39:31,220
number pairs
14346
10:39:34,860 --> 10:39:36,416
equals
14347
10:39:36,416 --> 10:39:39,619
I'm gonna type list of
14348
10:39:41,340 --> 10:39:44,539
and here we're going to put
14349
10:39:44,880 --> 10:39:47,776
one
14350
10:39:48,300 --> 10:39:50,700
two
14351
10:39:50,700 --> 10:39:53,360
one
14352
10:39:53,880 --> 10:39:55,680
two
14353
10:39:55,680 --> 10:39:57,680
two
14354
10:39:59,460 --> 10:40:01,140
three
14355
10:40:01,140 --> 10:40:03,140
two
14356
10:40:04,560 --> 10:40:06,979
three
14357
10:40:07,560 --> 10:40:09,240
four
14358
10:40:09,240 --> 10:40:10,436
two
14359
10:40:10,436 --> 10:40:13,916
four now if you want to unzip those
14360
10:40:13,916 --> 10:40:15,776
pairs that we created you can just type
14361
10:40:15,776 --> 10:40:18,680
here print line
14362
10:40:19,380 --> 10:40:23,160
number pairs let's first display them as
14363
10:40:23,160 --> 10:40:24,540
pairs
14364
10:40:24,540 --> 10:40:27,960
and the necklace print line and so let's
14365
10:40:27,960 --> 10:40:30,740
type number pairs
14366
10:40:30,740 --> 10:40:33,120
dot unzip
14367
10:40:33,120 --> 10:40:34,980
now if you run this you're gonna see
14368
10:40:34,980 --> 10:40:36,540
them first in pairs and then you're
14369
10:40:36,540 --> 10:40:39,480
gonna see them unpaired because we use
14370
10:40:39,480 --> 10:40:41,520
here the anzi function so as you can see
14371
10:40:41,520 --> 10:40:44,220
here you have first one and you have one
14372
10:40:44,220 --> 10:40:46,436
two two three three four four so you
14373
10:40:46,436 --> 10:40:48,120
have in pairs first
14374
10:40:48,120 --> 10:40:50,756
and then we have them separated so we
14375
10:40:50,756 --> 10:40:52,980
have first one two three four
14376
10:40:52,980 --> 10:40:54,840
and then we have the numbers one two
14377
10:40:54,840 --> 10:40:57,300
three four so did you we unzip them we
14378
10:40:57,300 --> 10:40:58,800
separated them they're not pairs now
14379
10:40:58,800 --> 10:41:00,960
anymore with this unzip function this so
14380
10:41:00,960 --> 10:41:04,080
this is how you can use the NZ function
14381
10:41:04,080 --> 10:41:05,400
now it's time to look at the next
14382
10:41:05,400 --> 10:41:07,200
transformation that we can use and that
14383
10:41:07,200 --> 10:41:10,256
is Association now what is association
14384
10:41:10,256 --> 10:41:12,720
association Transformations allows you
14385
10:41:12,720 --> 10:41:14,640
to build maps from The Collection
14386
10:41:14,640 --> 10:41:16,980
elements and certain values associated
14387
10:41:16,980 --> 10:41:18,120
with them
14388
10:41:18,120 --> 10:41:21,000
and different Association types the
14389
10:41:21,000 --> 10:41:23,820
elements can be either keys or values in
14390
10:41:23,820 --> 10:41:25,200
the association map I'm going to see
14391
10:41:25,200 --> 10:41:27,596
what this means and the basic
14392
10:41:27,596 --> 10:41:30,916
Association function is the association
14393
10:41:30,916 --> 10:41:34,320
associated with which creates a mob on
14394
10:41:34,320 --> 10:41:35,756
which the elements of the original
14395
10:41:35,756 --> 10:41:39,360
collection are keys and the values are
14396
10:41:39,360 --> 10:41:41,700
produced from them by the given
14397
10:41:41,700 --> 10:41:43,500
transformation function so let's see an
14398
10:41:43,500 --> 10:41:46,080
example with uh with this so I'm going
14399
10:41:46,080 --> 10:41:48,360
to delete this code because
14400
10:41:48,360 --> 10:41:50,820
it will make a doubt but hard to
14401
10:41:50,820 --> 10:41:52,256
understand we could have so many things
14402
10:41:52,256 --> 10:41:53,820
here
14403
10:41:53,820 --> 10:41:56,220
and I'm going to type Val
14404
10:41:56,220 --> 10:41:59,820
numbers you know put equals
14405
10:41:59,820 --> 10:42:03,080
I'm gonna type really stuff
14406
10:42:03,360 --> 10:42:06,436
nanotype here
14407
10:42:06,900 --> 10:42:09,620
one
14408
10:42:13,256 --> 10:42:15,320
two
14409
10:42:16,256 --> 10:42:18,256
three
14410
10:42:18,776 --> 10:42:21,000
and four
14411
10:42:21,000 --> 10:42:24,480
now I'm going to type here print line
14412
10:42:24,480 --> 10:42:27,776
numbers and we are going to use the
14413
10:42:27,776 --> 10:42:30,660
association associate with the function
14414
10:42:30,660 --> 10:42:34,256
so we type associated with
14415
10:42:34,256 --> 10:42:36,900
and this is going to produce as I said
14416
10:42:36,900 --> 10:42:39,540
previously it's going to produce a map
14417
10:42:39,540 --> 10:42:41,460
in which the elements of the original
14418
10:42:41,460 --> 10:42:45,120
collection are keys so the keys of this
14419
10:42:45,120 --> 10:42:48,660
map are going to be those elements and
14420
10:42:48,660 --> 10:42:49,580
the
14421
10:42:49,580 --> 10:42:52,080
values are going to maybe are going to
14422
10:42:52,080 --> 10:42:54,180
be produced by what expression we Define
14423
10:42:54,180 --> 10:42:56,040
here inside the parenthesis so I'm going
14424
10:42:56,040 --> 10:42:57,960
to Define here
14425
10:42:57,960 --> 10:43:01,276
it so I'm going to type it
14426
10:43:01,276 --> 10:43:03,840
dot length
14427
10:43:03,840 --> 10:43:05,756
so now if you run this you're gonna see
14428
10:43:05,756 --> 10:43:07,860
the keys being one and then I'm gonna
14429
10:43:07,860 --> 10:43:10,256
see the value being the length of that
14430
10:43:10,256 --> 10:43:14,300
specific element so if you run this
14431
10:43:18,000 --> 10:43:21,300
we get the key one the value three the
14432
10:43:21,300 --> 10:43:23,820
key two the value three then you get
14433
10:43:23,820 --> 10:43:26,220
three the value is five then you get
14434
10:43:26,220 --> 10:43:30,540
four and the value is four so what has
14435
10:43:30,540 --> 10:43:32,580
happened now is that the associate with
14436
10:43:32,580 --> 10:43:35,400
function created the map in which the
14437
10:43:35,400 --> 10:43:39,416
elements the key the key are the
14438
10:43:39,416 --> 10:43:42,240
elements of the collections so those are
14439
10:43:42,240 --> 10:43:45,540
the keys inside our map and the values
14440
10:43:45,540 --> 10:43:49,080
of uh those uh with the association with
14441
10:43:49,080 --> 10:43:52,680
the associated with those keys are
14442
10:43:52,680 --> 10:43:55,020
produced by the expression that we
14443
10:43:55,020 --> 10:43:56,756
Define here by its dot length so
14444
10:43:56,756 --> 10:43:59,220
basically gonna use the length of that
14445
10:43:59,220 --> 10:44:01,380
specific element and it's going to be
14446
10:44:01,380 --> 10:44:04,080
the value so this is why I have one
14447
10:44:04,080 --> 10:44:09,900
three two three three five and so on now
14448
10:44:09,900 --> 10:44:10,980
for
14449
10:44:10,980 --> 10:44:13,560
building maps with collection elements
14450
10:44:13,560 --> 10:44:16,620
as values there is a function called
14451
10:44:16,620 --> 10:44:19,560
associate by it takes a function that
14452
10:44:19,560 --> 10:44:21,776
returns a key based on an element's
14453
10:44:21,776 --> 10:44:24,900
value and if two elements keys are equal
14454
10:44:24,900 --> 10:44:28,800
only that last one remains in the map so
14455
10:44:28,800 --> 10:44:30,120
let's look at this one so I'm going to
14456
10:44:30,120 --> 10:44:31,980
type here
14457
10:44:31,980 --> 10:44:34,936
print line
14458
10:44:36,060 --> 10:44:39,560
I'm going to put here numbers
14459
10:44:41,580 --> 10:44:43,860
so we type numbers
14460
10:44:43,860 --> 10:44:46,740
dot associate by
14461
10:44:46,740 --> 10:44:48,720
and now here going to define the
14462
10:44:48,720 --> 10:44:50,820
expression which is going to build the
14463
10:44:50,820 --> 10:44:54,480
key because now the the associate by
14464
10:44:54,480 --> 10:44:56,096
function is going to build a correlation
14465
10:44:56,096 --> 10:44:58,916
which the elements now are not Keys like
14466
10:44:58,916 --> 10:45:01,080
we had previously but they are values so
14467
10:45:01,080 --> 10:45:02,640
here in the expression need now to
14468
10:45:02,640 --> 10:45:05,700
Define how the key for those respective
14469
10:45:05,700 --> 10:45:07,080
values that we have here is going to be
14470
10:45:07,080 --> 10:45:10,860
created so we type here it
14471
10:45:10,860 --> 10:45:13,436
DOT first
14472
10:45:13,436 --> 10:45:15,596
dot uppercase
14473
10:45:15,596 --> 10:45:17,756
so this is what what is this is gonna do
14474
10:45:17,756 --> 10:45:20,220
it's gonna create a key
14475
10:45:20,220 --> 10:45:22,500
it's gonna use one let's say that we
14476
10:45:22,500 --> 10:45:23,936
have element one
14477
10:45:23,936 --> 10:45:27,060
and it's gonna take your element one
14478
10:45:27,060 --> 10:45:29,096
it's gonna take the first letter of the
14479
10:45:29,096 --> 10:45:31,200
element one and it's gonna uppercase it
14480
10:45:31,200 --> 10:45:34,800
and you're gonna have o and as the key
14481
10:45:34,800 --> 10:45:37,380
we're gonna have o and as the value
14482
10:45:37,380 --> 10:45:40,500
we're gonna have one and uh for the next
14483
10:45:40,500 --> 10:45:43,200
ones uh it will be the same so now if I
14484
10:45:43,200 --> 10:45:45,916
run this quad
14485
10:45:46,860 --> 10:45:49,820
foreign
14486
10:45:56,240 --> 10:45:59,340
elements are the values here so they are
14487
10:45:59,340 --> 10:46:00,776
the values previously The Collection
14488
10:46:00,776 --> 10:46:03,960
elements what the key is and the the
14489
10:46:03,960 --> 10:46:06,300
keys were builded by the expression that
14490
10:46:06,300 --> 10:46:09,480
we defined here by this so we have o and
14491
10:46:09,480 --> 10:46:12,776
we have t and we have four and we we
14492
10:46:12,776 --> 10:46:17,700
have uh t uh only one time and we don't
14493
10:46:17,700 --> 10:46:20,096
have two entry because uh you cannot
14494
10:46:20,096 --> 10:46:22,436
have duplicated keys in a map so this is
14495
10:46:22,436 --> 10:46:25,500
why you only have one only one t here
14496
10:46:25,500 --> 10:46:27,720
so uh
14497
10:46:27,720 --> 10:46:30,776
this is associated so this is
14498
10:46:30,776 --> 10:46:34,436
Association now associate by can also be
14499
10:46:34,436 --> 10:46:36,300
called with a value transformation
14500
10:46:36,300 --> 10:46:39,360
function and to do to do that you just
14501
10:46:39,360 --> 10:46:41,880
type here print line
14502
10:46:41,880 --> 10:46:44,479
numbers
14503
10:46:44,936 --> 10:46:48,480
that associate by
14504
10:46:48,480 --> 10:46:51,240
now we need to
14505
10:46:51,240 --> 10:46:54,480
apply a transformation now on the key
14506
10:46:54,480 --> 10:46:57,360
and on the value and how gonna how gonna
14507
10:46:57,360 --> 10:46:58,436
do that
14508
10:46:58,436 --> 10:47:01,320
we can type here actually let's delete
14509
10:47:01,320 --> 10:47:03,240
this because we don't need curly braces
14510
10:47:03,240 --> 10:47:07,200
we put parentheses and we type here key
14511
10:47:07,200 --> 10:47:09,596
and we have a suggestion key selector so
14512
10:47:09,596 --> 10:47:11,040
we're gonna apply the First
14513
10:47:11,040 --> 10:47:14,756
Transformation on the key so we type key
14514
10:47:14,756 --> 10:47:17,580
selector that named argument and we put
14515
10:47:17,580 --> 10:47:18,960
curly braces
14516
10:47:18,960 --> 10:47:25,160
and here we type it so the key DOT first
14517
10:47:25,160 --> 10:47:27,776
dot uppercase
14518
10:47:27,776 --> 10:47:30,360
so now what we're saying here apply this
14519
10:47:30,360 --> 10:47:31,640
transformation
14520
10:47:31,640 --> 10:47:34,200
for every key
14521
10:47:34,200 --> 10:47:37,320
in in in the map that is going to be
14522
10:47:37,320 --> 10:47:39,900
built so apply this transformation for
14523
10:47:39,900 --> 10:47:41,880
every key that is in the map that is
14524
10:47:41,880 --> 10:47:44,220
going to be built so we're saying to
14525
10:47:44,220 --> 10:47:46,620
uppercase uh to take the first letter
14526
10:47:46,620 --> 10:47:50,040
and uppercase it then we also need to
14527
10:47:50,040 --> 10:47:52,080
apply transformation for the value we
14528
10:47:52,080 --> 10:47:52,800
put
14529
10:47:52,800 --> 10:47:57,240
comma here at the end of our curly brace
14530
10:47:57,240 --> 10:47:59,220
and we type here
14531
10:47:59,220 --> 10:48:00,660
value
14532
10:48:00,660 --> 10:48:03,776
you also have a name argument you put
14533
10:48:03,776 --> 10:48:06,480
again curly braces
14534
10:48:06,480 --> 10:48:10,020
and here we're gonna type it dot length
14535
10:48:10,020 --> 10:48:11,580
so this is the transformation that is
14536
10:48:11,580 --> 10:48:12,840
going to be applied
14537
10:48:12,840 --> 10:48:15,416
to all the values in the map that is
14538
10:48:15,416 --> 10:48:17,276
going to be built so this is the
14539
10:48:17,276 --> 10:48:20,340
transformation so this is how all the
14540
10:48:20,340 --> 10:48:23,756
keys and the the in the map that is
14541
10:48:23,756 --> 10:48:26,276
going to be built are gonna be uh change
14542
10:48:26,276 --> 10:48:28,980
so the first letter is going to take
14543
10:48:28,980 --> 10:48:30,540
this thing is going to take the first
14544
10:48:30,540 --> 10:48:32,880
letter of the keys and it's going to
14545
10:48:32,880 --> 10:48:34,500
uppercase it
14546
10:48:34,500 --> 10:48:36,596
this will uh
14547
10:48:36,596 --> 10:48:40,916
we'll take the length of the the all the
14548
10:48:40,916 --> 10:48:43,500
values in the in the the in the in the
14549
10:48:43,500 --> 10:48:45,300
map that is going to be built and it's
14550
10:48:45,300 --> 10:48:47,400
gonna display the the length of that
14551
10:48:47,400 --> 10:48:51,380
element so if you run this
14552
10:48:51,960 --> 10:48:58,256
we get 0 3 T 5 F4 so we have the first
14553
10:48:58,256 --> 10:49:01,140
letter uppercase and then we have the
14554
10:49:01,140 --> 10:49:04,020
length three then we have t we have the
14555
10:49:04,020 --> 10:49:05,580
letter upper case and we have the value
14556
10:49:05,580 --> 10:49:08,820
five the length of that uh uh element
14557
10:49:08,820 --> 10:49:13,500
then we have F so we have a four and we
14558
10:49:13,500 --> 10:49:16,860
have the the the transformation apply on
14559
10:49:16,860 --> 10:49:21,000
that element on this element on on four
14560
10:49:21,000 --> 10:49:22,916
and we have the length and we have four
14561
10:49:22,916 --> 10:49:25,460
so this is how we can use transformation
14562
10:49:25,460 --> 10:49:27,840
Transformations with the associate by
14563
10:49:27,840 --> 10:49:30,416
function and the next we're going to
14564
10:49:30,416 --> 10:49:33,060
look at the next transformation fact
14565
10:49:33,060 --> 10:49:35,276
function and that is flattened now it's
14566
10:49:35,276 --> 10:49:36,540
time to look at the next transformation
14567
10:49:36,540 --> 10:49:38,580
function that you can use and that is
14568
10:49:38,580 --> 10:49:40,500
the flattened function but first let's
14569
10:49:40,500 --> 10:49:41,640
see why you'll need to use this
14570
10:49:41,640 --> 10:49:43,560
flattened function in the first place
14571
10:49:43,560 --> 10:49:45,300
so I'm gonna add the comment here called
14572
10:49:45,300 --> 10:49:47,840
flatten
14573
10:49:50,160 --> 10:49:52,080
and I'm going to create a variable here
14574
10:49:52,080 --> 10:49:53,820
so I'm going to die Val it's going to be
14575
10:49:53,820 --> 10:49:55,320
called numbers
14576
10:49:55,320 --> 10:49:58,860
sets and this is going to be a list with
14577
10:49:58,860 --> 10:50:01,256
list so I'm gonna type here
14578
10:50:01,256 --> 10:50:04,140
list of
14579
10:50:04,140 --> 10:50:06,540
and inside the list I'm gonna Pro I'm
14580
10:50:06,540 --> 10:50:09,300
gonna type set off so we have a list
14581
10:50:09,300 --> 10:50:12,000
inside the inside of our list
14582
10:50:12,000 --> 10:50:13,740
and I'm going to provide some values to
14583
10:50:13,740 --> 10:50:17,756
our set of like one two three
14584
10:50:17,756 --> 10:50:21,320
then another set of
14585
10:50:22,256 --> 10:50:25,436
four five and six
14586
10:50:25,436 --> 10:50:28,936
then another set of
14587
10:50:32,220 --> 10:50:34,740
seven
14588
10:50:34,740 --> 10:50:38,096
eight and nine so
14589
10:50:38,096 --> 10:50:41,096
what we are having now is we have a list
14590
10:50:41,096 --> 10:50:44,756
within a list where the index of this
14591
10:50:44,756 --> 10:50:48,480
list is it's its own list so we have a
14592
10:50:48,480 --> 10:50:51,000
list of sets of ins
14593
10:50:51,000 --> 10:50:53,220
now if you want to Loop through this
14594
10:50:53,220 --> 10:50:55,140
list if you want to display the elements
14595
10:50:55,140 --> 10:50:57,776
in this list uh it is not that easy
14596
10:50:57,776 --> 10:51:00,960
because you will first need to Loop
14597
10:51:00,960 --> 10:51:04,500
because let's say that because what we
14598
10:51:04,500 --> 10:51:05,936
have here
14599
10:51:05,936 --> 10:51:08,756
is what is called a multi-dimensional
14600
10:51:08,756 --> 10:51:11,700
array so more specifically what we have
14601
10:51:11,700 --> 10:51:14,040
here is a two-dimensional array and at
14602
10:51:14,040 --> 10:51:17,096
the index 0 we have a list at the index
14603
10:51:17,096 --> 10:51:19,500
one we have another list and at the
14604
10:51:19,500 --> 10:51:21,840
index two we have another list
14605
10:51:21,840 --> 10:51:24,180
and to navigate to this list we need to
14606
10:51:24,180 --> 10:51:28,200
first get inside that list that is at
14607
10:51:28,200 --> 10:51:30,596
that other specific index to Loop to
14608
10:51:30,596 --> 10:51:32,700
that list and then we need to move to
14609
10:51:32,700 --> 10:51:35,400
the next index and to to look to that
14610
10:51:35,400 --> 10:51:38,276
list to that next list which is that
14611
10:51:38,276 --> 10:51:39,416
index
14612
10:51:39,416 --> 10:51:41,096
but now let's see first how we can
14613
10:51:41,096 --> 10:51:43,380
access one individual element from our
14614
10:51:43,380 --> 10:51:45,720
two-dimensional array so from our array
14615
10:51:45,720 --> 10:51:48,060
of arrays and to do that to type here
14616
10:51:48,060 --> 10:51:49,740
print line
14617
10:51:49,740 --> 10:51:52,800
gonna put numbers set
14618
10:51:52,800 --> 10:51:56,820
and if we we put square brackets and we
14619
10:51:56,820 --> 10:52:00,060
Define the index from uh which from
14620
10:52:00,060 --> 10:52:01,680
which list we want to access the element
14621
10:52:01,680 --> 10:52:03,720
and now I want to access the element
14622
10:52:03,720 --> 10:52:06,300
nine and the element 9 is stored at the
14623
10:52:06,300 --> 10:52:09,240
index two so we put two
14624
10:52:09,240 --> 10:52:12,416
and let's say that I want and I want to
14625
10:52:12,416 --> 10:52:15,180
access as I said the the element 9 now
14626
10:52:15,180 --> 10:52:17,276
to access the element line I need to go
14627
10:52:17,276 --> 10:52:19,800
within this list and use the index to
14628
10:52:19,800 --> 10:52:22,080
get the element I and element 9 is
14629
10:52:22,080 --> 10:52:23,580
stored at the index two so I'm going to
14630
10:52:23,580 --> 10:52:25,436
put square brackets two
14631
10:52:25,436 --> 10:52:27,596
but this for whatever reason doesn't
14632
10:52:27,596 --> 10:52:29,880
work with a list of sets and we need to
14633
10:52:29,880 --> 10:52:31,980
change this to array of
14634
10:52:31,980 --> 10:52:35,400
array of also those
14635
10:52:35,400 --> 10:52:39,140
also change it to array of
14636
10:52:40,436 --> 10:52:43,140
array of also array of an hour
14637
10:52:43,140 --> 10:52:45,240
everything will work fine
14638
10:52:45,240 --> 10:52:47,756
so array of
14639
10:52:47,756 --> 10:52:50,040
so if you run this so now we are
14640
10:52:50,040 --> 10:52:51,776
accessing the
14641
10:52:51,776 --> 10:52:56,340
index tool where the array is stored
14642
10:52:56,340 --> 10:52:58,500
where our number is stored and we get
14643
10:52:58,500 --> 10:53:01,500
the element two we get element nine so
14644
10:53:01,500 --> 10:53:03,360
now if I run this we're gonna see nine
14645
10:53:03,360 --> 10:53:06,800
autopilot in the console
14646
10:53:08,180 --> 10:53:10,916
so we get nine so this is how you can
14647
10:53:10,916 --> 10:53:13,460
access one individual element from our
14648
10:53:13,460 --> 10:53:15,180
multi-dimensional array so I'm going to
14649
10:53:15,180 --> 10:53:16,500
delete this I'm going to change it back
14650
10:53:16,500 --> 10:53:17,520
to
14651
10:53:17,520 --> 10:53:20,756
to a list of and to set off
14652
10:53:20,756 --> 10:53:24,080
so let's type a list of
14653
10:53:26,756 --> 10:53:29,416
set off
14654
10:53:33,180 --> 10:53:35,960
set off
14655
10:53:37,500 --> 10:53:40,340
and set off
14656
10:53:44,040 --> 10:53:45,900
and now let's see how we can Loop
14657
10:53:45,900 --> 10:53:48,960
through this type to the dimensional
14658
10:53:48,960 --> 10:53:52,500
array and to do that to type here four
14659
10:53:52,500 --> 10:53:55,620
and type numbers I type specific numbers
14660
10:53:55,620 --> 10:54:00,240
because this is gonna as as you can see
14661
10:54:00,240 --> 10:54:02,756
um gonna hold on hold the numbers the
14662
10:54:02,756 --> 10:54:05,040
set of numbers four numbers in number
14663
10:54:05,040 --> 10:54:07,700
sets
14664
10:54:08,040 --> 10:54:10,436
so now you we are you we are getting the
14665
10:54:10,436 --> 10:54:12,960
as you can see here this is of type is
14666
10:54:12,960 --> 10:54:16,916
the the type set of ins so this uh
14667
10:54:16,916 --> 10:54:19,256
basically gets the element of the number
14668
10:54:19,256 --> 10:54:22,620
set index let's say zero and it gets the
14669
10:54:22,620 --> 10:54:24,480
first set and now we need to Loop
14670
10:54:24,480 --> 10:54:27,960
through that set so another four
14671
10:54:27,960 --> 10:54:29,580
number
14672
10:54:29,580 --> 10:54:32,820
in numbers so now we are looping
14673
10:54:32,820 --> 10:54:36,120
through the list through the set which
14674
10:54:36,120 --> 10:54:38,520
is inside the list of the index zero I
14675
10:54:38,520 --> 10:54:40,500
didn't zero
14676
10:54:40,500 --> 10:54:43,256
and you can add the print line to add to
14677
10:54:43,256 --> 10:54:45,060
do output that number I'm going to type
14678
10:54:45,060 --> 10:54:47,276
here number and also I'm gonna add the
14679
10:54:47,276 --> 10:54:48,480
space here so I'm gonna add the print
14680
10:54:48,480 --> 10:54:50,820
line here and I'm gonna type here four
14681
10:54:50,820 --> 10:54:52,320
or slash
14682
10:54:52,320 --> 10:54:53,936
n
14683
10:54:53,936 --> 10:54:57,140
now if you run this
14684
10:55:00,180 --> 10:55:03,060
as you can see you have on the output
14685
10:55:03,060 --> 10:55:06,540
one two three then we have a space four
14686
10:55:06,540 --> 10:55:10,020
five seven six then seven eight and nine
14687
10:55:10,020 --> 10:55:13,740
but uh we achieved this by using a loop
14688
10:55:13,740 --> 10:55:16,140
and then we use another loop so we have
14689
10:55:16,140 --> 10:55:17,756
a loop inside the loop we have a nested
14690
10:55:17,756 --> 10:55:21,240
Loop in order to get to the
14691
10:55:21,240 --> 10:55:23,840
to this uh two dimensional array because
14692
10:55:23,840 --> 10:55:28,256
as I said the in the index of this array
14693
10:55:28,256 --> 10:55:31,200
is its own list so it contains the list
14694
10:55:31,200 --> 10:55:34,380
now with the flattened uh with the
14695
10:55:34,380 --> 10:55:36,360
flattened function what you can do is
14696
10:55:36,360 --> 10:55:39,660
that we can convert this two-dimensional
14697
10:55:39,660 --> 10:55:42,180
array in one dimensional array so you
14698
10:55:42,180 --> 10:55:44,756
don't need to use this nested for Loops
14699
10:55:44,756 --> 10:55:47,220
in order to Loop through to them it will
14700
10:55:47,220 --> 10:55:50,300
return the nested the nested
14701
10:55:50,300 --> 10:55:52,980
uh is it to private it will provide
14702
10:55:52,980 --> 10:55:55,620
access to the nasty collection elements
14703
10:55:55,620 --> 10:55:59,840
without you needing to Loop through this
14704
10:55:59,840 --> 10:56:02,276
two-dimensional Ray it will it will
14705
10:56:02,276 --> 10:56:03,660
basically
14706
10:56:03,660 --> 10:56:06,240
take that two-dimensional array and it
14707
10:56:06,240 --> 10:56:08,756
will convert it in one dimensionally and
14708
10:56:08,756 --> 10:56:11,400
you will not need to create to have this
14709
10:56:11,400 --> 10:56:13,560
nested Loop inside you have to have this
14710
10:56:13,560 --> 10:56:15,416
Loop inside the loop in order to to
14711
10:56:15,416 --> 10:56:18,240
display the number so to do that to type
14712
10:56:18,240 --> 10:56:20,460
here uh Val
14713
10:56:20,460 --> 10:56:24,660
numbers let's say flatten
14714
10:56:24,660 --> 10:56:26,276
equals to
14715
10:56:26,276 --> 10:56:30,416
number sales so our our list to it list
14716
10:56:30,416 --> 10:56:32,096
that
14717
10:56:32,096 --> 10:56:34,935
flatten
14718
10:56:34,980 --> 10:56:37,560
now if you look at the type here now
14719
10:56:37,560 --> 10:56:39,720
this is a list of fins you no longer
14720
10:56:39,720 --> 10:56:44,520
have a list of sets of ins so this is a
14721
10:56:44,520 --> 10:56:47,580
very very uh informative because it
14722
10:56:47,580 --> 10:56:49,620
tells us that this now is just simply a
14723
10:56:49,620 --> 10:56:52,860
list of ins is not a list with the set
14724
10:56:52,860 --> 10:56:56,700
which is which is uh which itself is
14725
10:56:56,700 --> 10:57:01,140
also or a list of events so
14726
10:57:01,140 --> 10:57:03,900
this converts that two-dimensional array
14727
10:57:03,900 --> 10:57:06,360
in one dimensional array and because of
14728
10:57:06,360 --> 10:57:07,980
that now it's much easier to get one
14729
10:57:07,980 --> 10:57:09,720
element from our list because now our
14730
10:57:09,720 --> 10:57:11,880
list is not a two-dimensional array and
14731
10:57:11,880 --> 10:57:14,096
it's just one dimensional array so if I
14732
10:57:14,096 --> 10:57:16,080
type here print line
14733
10:57:16,080 --> 10:57:17,700
and the
14734
10:57:17,700 --> 10:57:20,220
let's say that I want to get the element
14735
10:57:20,220 --> 10:57:23,096
nine I'm going to type here uh
14736
10:57:23,096 --> 10:57:26,040
numbers flatten
14737
10:57:26,040 --> 10:57:28,080
square brackets and I'm going to type
14738
10:57:28,080 --> 10:57:30,180
here eight because it's stored at the
14739
10:57:30,180 --> 10:57:34,936
index 8. so now if I run this
14740
10:57:36,480 --> 10:57:39,596
so we get 9 here and as you can see this
14741
10:57:39,596 --> 10:57:43,200
is much easier than uh with the the two
14742
10:57:43,200 --> 10:57:45,300
that dimensional array so this is what
14743
10:57:45,300 --> 10:57:46,740
the flattening function is doing is
14744
10:57:46,740 --> 10:57:48,060
converting the
14745
10:57:48,060 --> 10:57:51,256
the two dimensional array or if a
14746
10:57:51,256 --> 10:57:53,276
multi-dimensional array in into one
14747
10:57:53,276 --> 10:57:55,200
dimensional array so now let's look how
14748
10:57:55,200 --> 10:57:58,680
we can Loop through our uh one dimension
14749
10:57:58,680 --> 10:57:59,520
array
14750
10:57:59,520 --> 10:58:02,596
so let's delete this
14751
10:58:04,200 --> 10:58:06,660
and if you want to to Loop through this
14752
10:58:06,660 --> 10:58:09,416
you just type 4
14753
10:58:09,416 --> 10:58:10,916
number
14754
10:58:10,916 --> 10:58:12,596
in
14755
10:58:12,596 --> 10:58:15,660
numbers flatten
14756
10:58:15,660 --> 10:58:19,020
and we either here a print line
14757
10:58:19,020 --> 10:58:21,560
number
14758
10:58:22,800 --> 10:58:24,180
and
14759
10:58:24,180 --> 10:58:27,320
let's comment this code
14760
10:58:30,960 --> 10:58:34,040
and if you run this code
14761
10:58:37,880 --> 10:58:41,820
now get the number displayed one after
14762
10:58:41,820 --> 10:58:43,380
another so get one two three four five
14763
10:58:43,380 --> 10:58:46,080
six seven nine and we got this because
14764
10:58:46,080 --> 10:58:49,020
our function our flattened function our
14765
10:58:49,020 --> 10:58:52,320
transformation function is transforming
14766
10:58:52,320 --> 10:58:55,080
it's converting our two-dimensional
14767
10:58:55,080 --> 10:58:57,060
array in one dimensional array so it's
14768
10:58:57,060 --> 10:58:59,220
easier for us to get the elements into
14769
10:58:59,220 --> 10:59:01,800
loop loop through them so this is what
14770
10:59:01,800 --> 10:59:04,916
the flatten function is doing and next
14771
10:59:04,916 --> 10:59:07,860
we're going to look at the
14772
10:59:07,860 --> 10:59:10,500
string representation and the functions
14773
10:59:10,500 --> 10:59:13,320
that you can use uh what we for the for
14774
10:59:13,320 --> 10:59:15,960
the string representation
14775
10:59:15,960 --> 10:59:17,880
I'm going to close the console
14776
10:59:17,880 --> 10:59:19,380
now it's time to start a discussion
14777
10:59:19,380 --> 10:59:21,840
about string representation so if you
14778
10:59:21,840 --> 10:59:23,460
need to retrieve the collection content
14779
10:59:23,460 --> 10:59:25,080
in a readable format you can use
14780
10:59:25,080 --> 10:59:27,180
functions that transform the collections
14781
10:59:27,180 --> 10:59:29,340
to Strings and there are two functions
14782
10:59:29,340 --> 10:59:31,680
to do that the join to string and the
14783
10:59:31,680 --> 10:59:34,916
join two The Joint to string build a
14784
10:59:34,916 --> 10:59:36,240
single string from the Collision
14785
10:59:36,240 --> 10:59:38,756
elements based on the provided arguments
14786
10:59:38,756 --> 10:59:42,000
the join 2 does the same but appends the
14787
10:59:42,000 --> 10:59:43,800
result of the given appendable object
14788
10:59:43,800 --> 10:59:45,480
and what that means is that it's gonna
14789
10:59:45,480 --> 10:59:49,140
add the text to the object on which that
14790
10:59:49,140 --> 10:59:51,300
function The Joint function is called
14791
10:59:51,300 --> 10:59:52,860
you're going to see an example with this
14792
10:59:52,860 --> 10:59:54,360
immediately
14793
10:59:54,360 --> 10:59:57,480
and now I'm going to create a variable
14794
10:59:57,480 --> 11:00:00,480
called numbers while numbers
14795
11:00:00,480 --> 11:00:02,220
I'm going to put equals I'm going to
14796
11:00:02,220 --> 11:00:05,700
call it actually numbers strings
14797
11:00:05,700 --> 11:00:06,900
equal
14798
11:00:06,900 --> 11:00:10,640
and I'm gonna type here list off
14799
11:00:12,180 --> 11:00:14,400
and I'm gonna put here
14800
11:00:14,400 --> 11:00:17,480
the text one
14801
11:00:17,700 --> 11:00:19,700
two
14802
11:00:21,000 --> 11:00:23,000
three
14803
11:00:25,080 --> 11:00:26,460
and four
14804
11:00:26,460 --> 11:00:28,980
now if you want to retrieve the
14805
11:00:28,980 --> 11:00:31,020
this if you want to get the string
14806
11:00:31,020 --> 11:00:32,936
representation of this list
14807
11:00:32,936 --> 11:00:35,756
if you just type here print line
14808
11:00:35,756 --> 11:00:38,220
and type you put your numbers strings
14809
11:00:38,220 --> 11:00:41,416
and if you run this
14810
11:00:44,160 --> 11:00:48,416
we get one two three and four but this I
14811
11:00:48,416 --> 11:00:49,980
I think is calling the two string
14812
11:00:49,980 --> 11:00:51,960
function it's not because we're not
14813
11:00:51,960 --> 11:00:54,900
using the joint to string function and I
14814
11:00:54,900 --> 11:00:56,820
don't like those
14815
11:00:56,820 --> 11:00:59,096
square brackets I want them to be
14816
11:00:59,096 --> 11:01:02,096
separated by comma with spaces to get
14817
11:01:02,096 --> 11:01:04,916
the to get data just type here
14818
11:01:04,916 --> 11:01:07,820
print line
14819
11:01:08,820 --> 11:01:10,800
so let's
14820
11:01:10,800 --> 11:01:14,000
put here print line
14821
11:01:16,680 --> 11:01:20,040
and to type numbers to string
14822
11:01:20,040 --> 11:01:22,756
Dot
14823
11:01:23,040 --> 11:01:25,200
joint to string
14824
11:01:25,200 --> 11:01:28,560
and uh we don't we delete this and put
14825
11:01:28,560 --> 11:01:31,276
parenthesis
14826
11:01:31,436 --> 11:01:34,256
now you throw on this
14827
11:01:34,256 --> 11:01:36,540
you'll see now the single presentation
14828
11:01:36,540 --> 11:01:38,340
of our list they are going to be
14829
11:01:38,340 --> 11:01:42,300
separated by comma when we do is going
14830
11:01:42,300 --> 11:01:44,880
to have a it's going to have
14831
11:01:44,880 --> 11:01:46,916
spaces now let's look at the Joint 2
14832
11:01:46,916 --> 11:01:49,800
function and for that
14833
11:01:49,800 --> 11:01:52,860
I'm gonna Define another list down here
14834
11:01:52,860 --> 11:01:54,360
so I'm going to type here let's press
14835
11:01:54,360 --> 11:01:57,980
Ctrl alt for model code
14836
11:01:58,140 --> 11:02:01,256
so I'm going to type here uh
14837
11:02:01,256 --> 11:02:02,640
Val
14838
11:02:02,640 --> 11:02:06,540
is going to be called list string
14839
11:02:06,540 --> 11:02:07,860
and now I'm going to use something
14840
11:02:07,860 --> 11:02:09,120
called
14841
11:02:09,120 --> 11:02:12,060
a string buffer and this is like the
14842
11:02:12,060 --> 11:02:14,400
string type is a little bit different
14843
11:02:14,400 --> 11:02:18,436
but it's gonna allow us to do
14844
11:02:18,436 --> 11:02:21,360
is going to allow us to present how the
14845
11:02:21,360 --> 11:02:24,360
joint function over so string buffer and
14846
11:02:24,360 --> 11:02:26,220
we put parenthesis and here we Define
14847
11:02:26,220 --> 11:02:29,276
our text and we're gonna type the list
14848
11:02:29,276 --> 11:02:31,916
of numbers I'm going to put colon so the
14849
11:02:31,916 --> 11:02:34,256
type here the list
14850
11:02:34,256 --> 11:02:37,340
of numbers
14851
11:02:37,800 --> 11:02:39,416
colon
14852
11:02:39,416 --> 11:02:43,256
now to use the join 2 function just type
14853
11:02:43,256 --> 11:02:46,640
here another print line
14854
11:02:46,640 --> 11:02:50,040
we we type number string
14855
11:02:50,040 --> 11:02:54,480
number strings dot join tool
14856
11:02:54,480 --> 11:02:56,700
and as you can see this receives a
14857
11:02:56,700 --> 11:02:58,740
buffer and this is the buffer that we
14858
11:02:58,740 --> 11:03:01,020
have created here so I'm gonna
14859
11:03:01,020 --> 11:03:03,776
press enter and we're going to type I'm
14860
11:03:03,776 --> 11:03:06,840
gonna give us the argument the listing
14861
11:03:06,840 --> 11:03:08,276
that we created
14862
11:03:08,276 --> 11:03:10,916
and what is this this is gonna do is
14863
11:03:10,916 --> 11:03:14,520
gonna append this text that we have here
14864
11:03:14,520 --> 11:03:17,880
in front of all of our elements that we
14865
11:03:17,880 --> 11:03:19,980
have in our list number strings so now
14866
11:03:19,980 --> 11:03:23,000
if you run the squad
14867
11:03:26,180 --> 11:03:29,520
as you can see it added the text the
14868
11:03:29,520 --> 11:03:32,276
list of numbers that we have here to our
14869
11:03:32,276 --> 11:03:34,140
number strings in front so we have the
14870
11:03:34,140 --> 11:03:36,660
list of numbers one two three and four
14871
11:03:36,660 --> 11:03:38,040
now
14872
11:03:38,040 --> 11:03:40,680
if you want to build the
14873
11:03:40,680 --> 11:03:43,560
custom string representation you can
14874
11:03:43,560 --> 11:03:46,020
specify its parameters in a function
14875
11:03:46,020 --> 11:03:49,200
arguments which are the separator prefix
14876
11:03:49,200 --> 11:03:52,020
and postfix and the result will start
14877
11:03:52,020 --> 11:03:55,320
with the prefix and end with the with
14878
11:03:55,320 --> 11:03:57,840
the postfix and the separator will come
14879
11:03:57,840 --> 11:04:00,120
will come after each element except the
14880
11:04:00,120 --> 11:04:02,460
last so let's see how we can do that
14881
11:04:02,460 --> 11:04:03,840
so
14882
11:04:03,840 --> 11:04:06,979
um I'm gonna use
14883
11:04:08,160 --> 11:04:10,200
I'm going to use the number numbers to
14884
11:04:10,200 --> 11:04:12,180
string so I'm going to type here print
14885
11:04:12,180 --> 11:04:14,460
line
14886
11:04:14,460 --> 11:04:17,160
numbers to string
14887
11:04:17,160 --> 11:04:20,596
and we type join
14888
11:04:20,820 --> 11:04:23,276
join to string and we select this one
14889
11:04:23,276 --> 11:04:25,140
with the you select this one with the
14890
11:04:25,140 --> 11:04:27,960
which with the separator parameter
14891
11:04:27,960 --> 11:04:30,540
and the type is separator so now we
14892
11:04:30,540 --> 11:04:33,120
Define the what is going to be the S the
14893
11:04:33,120 --> 11:04:36,060
separator here and we're gonna put
14894
11:04:36,060 --> 11:04:37,916
quotation marks
14895
11:04:37,916 --> 11:04:39,900
and we're going to put
14896
11:04:39,900 --> 11:04:41,460
this so this is going to be our
14897
11:04:41,460 --> 11:04:44,220
separator between our elements next
14898
11:04:44,220 --> 11:04:46,020
we're going to define the prefix so we
14899
11:04:46,020 --> 11:04:48,360
put we put here
14900
11:04:48,360 --> 11:04:51,900
comma type prefix and now I'm going to
14901
11:04:51,900 --> 11:04:53,640
put the prefix and the prefix is going
14902
11:04:53,640 --> 11:04:56,460
to be the text start and colon so I put
14903
11:04:56,460 --> 11:04:59,700
quotation marks start
14904
11:04:59,700 --> 11:05:01,436
and colon
14905
11:05:01,436 --> 11:05:04,680
and we also need to provide the post fix
14906
11:05:04,680 --> 11:05:08,160
and to do that we put another comma here
14907
11:05:08,160 --> 11:05:11,340
so those are named the arguments which
14908
11:05:11,340 --> 11:05:12,200
are
14909
11:05:12,200 --> 11:05:14,400
defined in this files and join the
14910
11:05:14,400 --> 11:05:16,560
string so we type here
14911
11:05:16,560 --> 11:05:19,200
postfix
14912
11:05:19,200 --> 11:05:22,276
and we put
14913
11:05:22,380 --> 11:05:23,880
colon
14914
11:05:23,880 --> 11:05:25,436
and
14915
11:05:25,436 --> 11:05:27,300
now if you run this you're going to see
14916
11:05:27,300 --> 11:05:29,640
them separated by this separator that we
14917
11:05:29,640 --> 11:05:31,256
Define here is going to have the prefix
14918
11:05:31,256 --> 11:05:33,240
start and it's going to have at the end
14919
11:05:33,240 --> 11:05:35,640
this post fix which is comma and N so
14920
11:05:35,640 --> 11:05:38,840
now if you run this quad
14921
11:05:42,900 --> 11:05:44,880
as you can see we have start and we have
14922
11:05:44,880 --> 11:05:46,860
one then it's separated by the sign that
14923
11:05:46,860 --> 11:05:50,520
we defined here then we have 2 3 4 and
14924
11:05:50,520 --> 11:05:53,640
we have the end so we have our post fix
14925
11:05:53,640 --> 11:05:56,220
that had defined here now
14926
11:05:56,220 --> 11:05:58,620
for bigger Coalition you may want to
14927
11:05:58,620 --> 11:05:59,880
specify
14928
11:05:59,880 --> 11:06:03,120
limit so a number of elements that will
14929
11:06:03,120 --> 11:06:05,936
be included into the result and if the
14930
11:06:05,936 --> 11:06:08,700
collection exists that size with that
14931
11:06:08,700 --> 11:06:10,620
limit all the other elements will be
14932
11:06:10,620 --> 11:06:13,020
replaced with a single value called the
14933
11:06:13,020 --> 11:06:15,540
truncated argument so let's see what all
14934
11:06:15,540 --> 11:06:18,000
of this means so let's say that I have a
14935
11:06:18,000 --> 11:06:19,620
list called the
14936
11:06:19,620 --> 11:06:23,460
numbers let's type here Val numbers and
14937
11:06:23,460 --> 11:06:26,460
I'm going to create a range from one
14938
11:06:26,460 --> 11:06:29,040
to 100 so what we have here is 100
14939
11:06:29,040 --> 11:06:30,180
numbers
14940
11:06:30,180 --> 11:06:32,640
and let's say that I type here print
14941
11:06:32,640 --> 11:06:33,960
line
14942
11:06:33,960 --> 11:06:36,720
but first let's convert this to a list
14943
11:06:36,720 --> 11:06:38,520
so I'm going to put this in parentheses
14944
11:06:38,520 --> 11:06:41,520
so you put this in parenthesis
14945
11:06:41,520 --> 11:06:45,240
and you put at the end that to list
14946
11:06:45,240 --> 11:06:47,276
and this is going to convert now our
14947
11:06:47,276 --> 11:06:49,436
range into a list and as you can see the
14948
11:06:49,436 --> 11:06:53,160
the type is a list of ins and now if I
14949
11:06:53,160 --> 11:06:55,916
type here numbers
14950
11:06:55,916 --> 11:06:58,620
and if I run this code
14951
11:06:58,620 --> 11:07:03,300
when I get all of our numbers displayed
14952
11:07:07,740 --> 11:07:11,580
now with as I said with the join join
14953
11:07:11,580 --> 11:07:13,860
tostring function we can specify a limit
14954
11:07:13,860 --> 11:07:17,040
and also a truncated this is going to be
14955
11:07:17,040 --> 11:07:19,256
the thing which is going to be sold out
14956
11:07:19,256 --> 11:07:22,916
after our number limits so what I can
14957
11:07:22,916 --> 11:07:27,120
type here is I can type join to string
14958
11:07:27,120 --> 11:07:31,560
and uh which is also
14959
11:07:31,560 --> 11:07:34,500
one with parenthesis so hero type limit
14960
11:07:34,500 --> 11:07:37,320
the named argument limit and let's say
14961
11:07:37,320 --> 11:07:39,960
that I don't want to get all the numbers
14962
11:07:39,960 --> 11:07:43,140
as you can see here from to 100 and I
14963
11:07:43,140 --> 11:07:46,500
want to get only the first 15 elements
14964
11:07:46,500 --> 11:07:50,220
to do this you just type here limit 15.
14965
11:07:50,220 --> 11:07:53,700
then we type truncated and that is going
14966
11:07:53,700 --> 11:07:57,240
to be that this trunky thing is going to
14967
11:07:57,240 --> 11:08:00,240
be showed after our limit number so I'm
14968
11:08:00,240 --> 11:08:03,596
gonna put here uh quotation marks
14969
11:08:03,596 --> 11:08:08,936
less than um sine dot dot and uh
14970
11:08:08,936 --> 11:08:11,756
greater than PSI so now if you run this
14971
11:08:11,756 --> 11:08:16,276
look at what we have in the output
14972
11:08:16,980 --> 11:08:20,040
now we get we only have the first 15
14973
11:08:20,040 --> 11:08:23,276
elements and then for the next elements
14974
11:08:23,276 --> 11:08:25,460
we only have this uh
14975
11:08:25,460 --> 11:08:28,320
string that we typed here this is this
14976
11:08:28,320 --> 11:08:30,120
expression that we have here and we can
14977
11:08:30,120 --> 11:08:31,620
change this if you can change this let's
14978
11:08:31,620 --> 11:08:35,040
say to 25 so if you run this now you'll
14979
11:08:35,040 --> 11:08:39,300
see only the first 25 elements displays
14980
11:08:39,300 --> 11:08:41,480
foreign
14981
11:08:41,840 --> 11:08:44,580
to string with the limit name argument
14982
11:08:44,580 --> 11:08:48,720
and the truncated now let's say that you
14983
11:08:48,720 --> 11:08:50,700
want to customize the representation of
14984
11:08:50,700 --> 11:08:53,460
elements of the elements themselves you
14985
11:08:53,460 --> 11:08:55,916
need to in that case you need to provide
14986
11:08:55,916 --> 11:08:58,620
the transform function so what we can do
14987
11:08:58,620 --> 11:09:02,936
is that we can type here print line
14988
11:09:02,936 --> 11:09:04,500
let's say
14989
11:09:04,500 --> 11:09:08,460
and I'm going to type number strings
14990
11:09:08,460 --> 11:09:12,060
put dot join to string and choose this
14991
11:09:12,060 --> 11:09:13,916
one with curly braces
14992
11:09:13,916 --> 11:09:16,800
and here I'm gonna Define how this uh
14993
11:09:16,800 --> 11:09:18,620
this uh
14994
11:09:18,620 --> 11:09:21,480
list is going to be transform so how's
14995
11:09:21,480 --> 11:09:22,800
going to be changed so I'm going to type
14996
11:09:22,800 --> 11:09:24,060
here
14997
11:09:24,060 --> 11:09:27,540
quotation marks element
14998
11:09:27,540 --> 11:09:29,700
and I'm gonna put
14999
11:09:29,700 --> 11:09:31,500
colon
15000
11:09:31,500 --> 11:09:34,800
we're going to put the dollar sign
15001
11:09:34,800 --> 11:09:36,300
and put it
15002
11:09:36,300 --> 11:09:39,800
dot uppercase so we're gonna uppercase
15003
11:09:39,800 --> 11:09:42,776
all the elements
15004
11:09:42,776 --> 11:09:46,400
so if I run the code now
15005
11:09:51,120 --> 11:09:54,540
get element and get one
15006
11:09:54,540 --> 11:09:57,840
so you can get one uppercase an element
15007
11:09:57,840 --> 11:10:00,240
to get two uppercase element three
15008
11:10:00,240 --> 11:10:03,960
uppercase element four uppercase so this
15009
11:10:03,960 --> 11:10:06,360
is how you can use the transformation uh
15010
11:10:06,360 --> 11:10:09,060
we can this I can transform your list we
15011
11:10:09,060 --> 11:10:11,220
using the joint to string function
15012
11:10:11,220 --> 11:10:13,500
so this is our discussion about the
15013
11:10:13,500 --> 11:10:15,776
string representation and now I'm going
15014
11:10:15,776 --> 11:10:19,080
to move to the next sections so I'm
15015
11:10:19,080 --> 11:10:21,000
gonna end the video now now it's time to
15016
11:10:21,000 --> 11:10:23,400
start a discussion about filtering so
15017
11:10:23,400 --> 11:10:26,276
what is filtering filtering is one of
15018
11:10:26,276 --> 11:10:28,080
the most popular tasks in collection
15019
11:10:28,080 --> 11:10:31,020
processing in kotlin filtering
15020
11:10:31,020 --> 11:10:33,660
conditions are defined by predicates and
15021
11:10:33,660 --> 11:10:36,416
that is Lambda function that take a
15022
11:10:36,416 --> 11:10:39,300
collection element and return a Boolean
15023
11:10:39,300 --> 11:10:41,520
value true means that the given element
15024
11:10:41,520 --> 11:10:44,756
matches the predicate false means the
15025
11:10:44,756 --> 11:10:45,720
opposite
15026
11:10:45,720 --> 11:10:47,880
the standard Library contains a group of
15027
11:10:47,880 --> 11:10:49,620
extension functions that lets you filter
15028
11:10:49,620 --> 11:10:51,960
collection in a single call those
15029
11:10:51,960 --> 11:10:53,460
functions leave the original collection
15030
11:10:53,460 --> 11:10:56,220
unchanged so they are available for both
15031
11:10:56,220 --> 11:10:59,640
multiple and read-only collections to
15032
11:10:59,640 --> 11:11:01,916
operate the filtering result you should
15033
11:11:01,916 --> 11:11:04,020
assign it to a variable or change the
15034
11:11:04,020 --> 11:11:06,120
function after filtering
15035
11:11:06,120 --> 11:11:09,180
now the basic filtering function is
15036
11:11:09,180 --> 11:11:12,060
filter when called with a predicate
15037
11:11:12,060 --> 11:11:14,276
filter Returns the collection elements
15038
11:11:14,276 --> 11:11:18,240
that match it for both list and set the
15039
11:11:18,240 --> 11:11:20,400
resulting Collision is a list for map
15040
11:11:20,400 --> 11:11:22,256
it's a map as well
15041
11:11:22,256 --> 11:11:25,080
now let's Define a variable here call
15042
11:11:25,080 --> 11:11:28,436
number so let's type in val numbers I'm
15043
11:11:28,436 --> 11:11:32,720
going to put equals to list of
15044
11:11:33,916 --> 11:11:37,256
and I'm gonna type here
15045
11:11:37,256 --> 11:11:39,980
one
15046
11:11:40,140 --> 11:11:42,620
two
15047
11:11:45,000 --> 11:11:47,000
three
15048
11:11:47,276 --> 11:11:50,060
and four
15049
11:11:53,040 --> 11:11:56,160
now if you want to filter this list what
15050
11:11:56,160 --> 11:11:57,720
you need to do is let's create another
15051
11:11:57,720 --> 11:12:00,120
variable here called
15052
11:12:00,120 --> 11:12:02,276
longer
15053
11:12:02,276 --> 11:12:05,119
then
15054
11:12:06,596 --> 11:12:08,096
three
15055
11:12:08,096 --> 11:12:10,080
I'm going to put equals I'm going to
15056
11:12:10,080 --> 11:12:11,640
type numbers
15057
11:12:11,640 --> 11:12:15,720
Dot and we type filter
15058
11:12:15,720 --> 11:12:18,120
so filter
15059
11:12:18,120 --> 11:12:20,340
and we choose this one
15060
11:12:20,340 --> 11:12:23,460
and here we defined how our list so we
15061
11:12:23,460 --> 11:12:25,500
will Define the expression which is
15062
11:12:25,500 --> 11:12:28,256
gonna determine how our list is going to
15063
11:12:28,256 --> 11:12:31,200
be is gonna be filtered so I'm gonna
15064
11:12:31,200 --> 11:12:36,776
type here it so the element dot length
15065
11:12:36,776 --> 11:12:39,240
let's say greater than
15066
11:12:39,240 --> 11:12:40,620
3.
15067
11:12:40,620 --> 11:12:43,740
now if we I add the print line down here
15068
11:12:43,740 --> 11:12:46,256
print line
15069
11:12:46,256 --> 11:12:49,860
now this list is going to contain all
15070
11:12:49,860 --> 11:12:54,360
the elements from our numbers list which
15071
11:12:54,360 --> 11:12:57,120
are longer than three so if I put here
15072
11:12:57,120 --> 11:13:01,800
longer than three and if I run this
15073
11:13:01,800 --> 11:13:04,436
we get 3 and 4 because those are the
15074
11:13:04,436 --> 11:13:07,980
only string numbers which have a length
15075
11:13:07,980 --> 11:13:09,776
longer than three so I'm going to close
15076
11:13:09,776 --> 11:13:11,880
the console next let's see how we can
15077
11:13:11,880 --> 11:13:13,380
filter them up and for that I'm going to
15078
11:13:13,380 --> 11:13:15,240
create another variable called numbers
15079
11:13:15,240 --> 11:13:17,720
map
15080
11:13:17,880 --> 11:13:20,119
foreign
15081
11:13:22,096 --> 11:13:24,960
here the key is going to be a string key
15082
11:13:24,960 --> 11:13:26,160
one
15083
11:13:26,160 --> 11:13:28,620
the value is going to be one
15084
11:13:28,620 --> 11:13:31,740
comma then
15085
11:13:31,740 --> 11:13:33,596
key to
15086
11:13:33,596 --> 11:13:35,899
to
15087
11:13:37,140 --> 11:13:39,000
then
15088
11:13:39,000 --> 11:13:41,276
key three
15089
11:13:41,276 --> 11:13:43,256
two three
15090
11:13:43,256 --> 11:13:48,300
and now I'm gonna put key 101
15091
11:13:48,360 --> 11:13:49,756
two
15092
11:13:49,756 --> 11:13:52,080
101 the value
15093
11:13:52,080 --> 11:13:54,180
now if you want to filter now this map
15094
11:13:54,180 --> 11:13:55,740
I'm going to create another variable
15095
11:13:55,740 --> 11:13:59,159
called filtered
15096
11:13:59,640 --> 11:14:01,740
mop and it's going to be equal to our
15097
11:14:01,740 --> 11:14:04,020
numbers map but it's going to be filter
15098
11:14:04,020 --> 11:14:06,840
so I'm going to type DOT number snap
15099
11:14:06,840 --> 11:14:08,460
that filter
15100
11:14:08,460 --> 11:14:10,860
and now I'm going to filter filter the
15101
11:14:10,860 --> 11:14:14,160
map both by the key and by the value so
15102
11:14:14,160 --> 11:14:17,540
we type here it dot key
15103
11:14:17,540 --> 11:14:21,360
dot ends with so we we want to filter it
15104
11:14:21,360 --> 11:14:23,756
first by the key and the one that key to
15105
11:14:23,756 --> 11:14:26,340
ends with a specific character and that
15106
11:14:26,340 --> 11:14:28,800
character is going to be one
15107
11:14:28,800 --> 11:14:33,120
and so we put the operator and we want
15108
11:14:33,120 --> 11:14:37,256
the the value to be greater than 100 so
15109
11:14:37,256 --> 11:14:38,820
we put here it
15110
11:14:38,820 --> 11:14:41,960
dot value
15111
11:14:42,120 --> 11:14:46,860
greater than 100 and the only key which
15112
11:14:46,860 --> 11:14:50,220
satisfy this condition that we find here
15113
11:14:50,220 --> 11:14:53,756
it's only is this key so now if I add
15114
11:14:53,756 --> 11:14:55,680
the print line here
15115
11:14:55,680 --> 11:14:58,080
and if I type filter map so the map
15116
11:14:58,080 --> 11:15:00,120
which was filtered using the expression
15117
11:15:00,120 --> 11:15:01,620
that we defined here
15118
11:15:01,620 --> 11:15:04,740
now if you run this you're gonna see
15119
11:15:04,740 --> 11:15:06,416
gonna see
15120
11:15:06,416 --> 11:15:08,276
this in the output because this is the
15121
11:15:08,276 --> 11:15:09,776
only one which satisfy the condition so
15122
11:15:09,776 --> 11:15:13,740
you get key 101 and we get the value 101
15123
11:15:13,740 --> 11:15:15,480
so this is how you can filter them up
15124
11:15:15,480 --> 11:15:19,620
using the both the key and the value now
15125
11:15:19,620 --> 11:15:22,680
the predicates in filter can only check
15126
11:15:22,680 --> 11:15:24,900
the values of the elements if you want
15127
11:15:24,900 --> 11:15:27,560
to use element position in the filter
15128
11:15:27,560 --> 11:15:31,680
use filter indexed it takes a predicate
15129
11:15:31,680 --> 11:15:33,840
with two arguments the index and the
15130
11:15:33,840 --> 11:15:35,276
value of an element
15131
11:15:35,276 --> 11:15:37,320
to filter correction by a negative
15132
11:15:37,320 --> 11:15:40,140
condition use filter not it's written as
15133
11:15:40,140 --> 11:15:41,640
a list of the elements for which the
15134
11:15:41,640 --> 11:15:44,520
predicate yields false so let's see how
15135
11:15:44,520 --> 11:15:46,200
we can do this so I'm gonna declare two
15136
11:15:46,200 --> 11:15:48,180
variables down here
15137
11:15:48,180 --> 11:15:50,580
file
15138
11:15:50,580 --> 11:15:52,500
filtered
15139
11:15:52,500 --> 11:15:54,776
index
15140
11:15:54,776 --> 11:15:57,720
is going to be equal to numbers
15141
11:15:57,720 --> 11:16:01,200
or a variable that is defined above
15142
11:16:01,200 --> 11:16:03,596
that
15143
11:16:03,596 --> 11:16:06,740
filter index
15144
11:16:09,180 --> 11:16:11,936
and this is gonna take
15145
11:16:11,936 --> 11:16:15,000
two parameters index and the value so
15146
11:16:15,000 --> 11:16:17,660
I'm going to type here
15147
11:16:18,060 --> 11:16:21,680
actually you need to put curly braces
15148
11:16:24,180 --> 11:16:26,840
index
15149
11:16:29,220 --> 11:16:32,700
let's change this to value
15150
11:16:32,700 --> 11:16:36,720
actually let's keep it as s
15151
11:16:36,720 --> 11:16:39,240
Ctrl Z
15152
11:16:39,240 --> 11:16:40,800
and
15153
11:16:40,800 --> 11:16:42,840
here we're gonna Define
15154
11:16:42,840 --> 11:16:49,200
our expression by a by which our list is
15155
11:16:49,200 --> 11:16:50,400
going to be filtered so I'm going to
15156
11:16:50,400 --> 11:16:55,080
type here that first I want the
15157
11:16:55,080 --> 11:16:57,800
index
15158
11:16:57,840 --> 11:17:02,416
to not be equal to zero
15159
11:17:04,320 --> 11:17:06,980
and
15160
11:17:08,640 --> 11:17:11,340
I I also want
15161
11:17:11,340 --> 11:17:14,299
that
15162
11:17:14,640 --> 11:17:17,700
the value of the element so actually
15163
11:17:17,700 --> 11:17:19,620
let's change this to valid to make more
15164
11:17:19,620 --> 11:17:21,596
sense so let's type here to value
15165
11:17:21,596 --> 11:17:24,720
because we're talking about those ones
15166
11:17:24,720 --> 11:17:27,840
so I want the value of that string so
15167
11:17:27,840 --> 11:17:31,200
the value dot length I want I want it to
15168
11:17:31,200 --> 11:17:34,640
be less than a 5.
15169
11:17:34,740 --> 11:17:36,960
all right so this is the transformation
15170
11:17:36,960 --> 11:17:40,256
if you want this is not a transformation
15171
11:17:40,256 --> 11:17:43,096
this is how we want to filter our
15172
11:17:43,096 --> 11:17:45,720
numbers list that we defined above I
15173
11:17:45,720 --> 11:17:46,916
want to
15174
11:17:46,916 --> 11:17:49,620
filter both by the index so this is why
15175
11:17:49,620 --> 11:17:51,960
we use the filter index and also by the
15176
11:17:51,960 --> 11:17:52,800
value
15177
11:17:52,800 --> 11:17:55,560
and I want the index to not be equal to
15178
11:17:55,560 --> 11:17:57,960
zero this is the first condition for the
15179
11:17:57,960 --> 11:18:01,200
filtering and I also want so use the end
15180
11:18:01,200 --> 11:18:04,140
operator that the valued of the element
15181
11:18:04,140 --> 11:18:07,140
the length to be less than five now if I
15182
11:18:07,140 --> 11:18:11,400
print this if I added here a print line
15183
11:18:11,400 --> 11:18:15,680
but first let's Also let's also use the
15184
11:18:15,680 --> 11:18:18,416
uh filter not so I'm going to type here
15185
11:18:18,416 --> 11:18:20,340
Val
15186
11:18:20,340 --> 11:18:23,119
foreign
15187
11:18:24,300 --> 11:18:26,700
filtered nut
15188
11:18:26,700 --> 11:18:30,560
and I'm going to type equals numbers
15189
11:18:30,900 --> 11:18:34,756
dot filter not
15190
11:18:35,520 --> 11:18:38,220
curly braces
15191
11:18:38,220 --> 11:18:41,580
and we type here it dot
15192
11:18:41,580 --> 11:18:42,960
length
15193
11:18:42,960 --> 11:18:45,960
so the value of the element
15194
11:18:45,960 --> 11:18:49,916
is to be less than equal to 3.
15195
11:18:49,916 --> 11:18:52,020
now if we output those two in the
15196
11:18:52,020 --> 11:18:54,120
console so if I add here print line and
15197
11:18:54,120 --> 11:18:57,776
I'm going to Output filter
15198
11:19:00,000 --> 11:19:02,160
Index this should be filtered in the
15199
11:19:02,160 --> 11:19:05,240
solicit Factor this
15200
11:19:09,300 --> 11:19:14,276
so let's type here in the index
15201
11:19:15,840 --> 11:19:17,520
I type it already
15202
11:19:17,520 --> 11:19:19,680
so let's type here
15203
11:19:19,680 --> 11:19:22,140
edex actually
15204
11:19:22,140 --> 11:19:25,159
it will make more sense
15205
11:19:26,880 --> 11:19:31,620
but I have Elixir so why anyway so I'm
15206
11:19:31,620 --> 11:19:34,140
gonna output that first then I'm gonna
15207
11:19:34,140 --> 11:19:35,756
hear a print line
15208
11:19:35,756 --> 11:19:39,540
and I'm gonna type filtered not
15209
11:19:39,540 --> 11:19:42,120
and filter not as I said is gonna return
15210
11:19:42,120 --> 11:19:44,096
a list of the elements for which the
15211
11:19:44,096 --> 11:19:46,500
predicate yields false
15212
11:19:46,500 --> 11:19:49,436
now if you run those two look what
15213
11:19:49,436 --> 11:19:53,300
you're gonna have on the output
15214
11:19:54,776 --> 11:19:58,680
so this is the output for our first for
15215
11:19:58,680 --> 11:20:00,596
this print line and for this print line
15216
11:20:00,596 --> 11:20:02,160
so let's think about what we have here
15217
11:20:02,160 --> 11:20:05,040
first we have two and four
15218
11:20:05,040 --> 11:20:08,820
and we Define the filter here first the
15219
11:20:08,820 --> 11:20:10,500
index which should not be equal to zero
15220
11:20:10,500 --> 11:20:15,000
so the one is first excluded and we also
15221
11:20:15,000 --> 11:20:17,756
want to that to the value that length to
15222
11:20:17,756 --> 11:20:20,220
be less than five so the only ones which
15223
11:20:20,220 --> 11:20:22,860
uh for which the value is less than five
15224
11:20:22,860 --> 11:20:27,300
is the 2 and 4 because the length of the
15225
11:20:27,300 --> 11:20:29,700
three is bigger than 5 because it has a
15226
11:20:29,700 --> 11:20:34,756
length of 0 1 2 3
15227
11:20:34,756 --> 11:20:37,916
4 and
15228
11:20:37,916 --> 11:20:40,380
the length is 5 but I want the length to
15229
11:20:40,380 --> 11:20:42,596
be less than five not less than equal
15230
11:20:42,596 --> 11:20:44,220
than five so this is why this is not
15231
11:20:44,220 --> 11:20:47,220
included and you only have two and four
15232
11:20:47,220 --> 11:20:49,020
next
15233
11:20:49,020 --> 11:20:51,776
uh now use the filter nut and it's gonna
15234
11:20:51,776 --> 11:20:54,240
as I said it's gonna return a list of
15235
11:20:54,240 --> 11:20:56,640
elements for which the predicate for
15236
11:20:56,640 --> 11:20:58,680
each this expression is gonna yield
15237
11:20:58,680 --> 11:21:02,700
false so it's gonna go through all of
15238
11:21:02,700 --> 11:21:05,340
our elements and it's gonna return the
15239
11:21:05,340 --> 11:21:08,700
elements for which this is false so we
15240
11:21:08,700 --> 11:21:10,020
get three
15241
11:21:10,020 --> 11:21:13,560
N4 because those are the only ones which
15242
11:21:13,560 --> 11:21:17,340
are not less than equal to three they
15243
11:21:17,340 --> 11:21:19,080
are greater than equal to three so this
15244
11:21:19,080 --> 11:21:20,700
is why we have here three and four
15245
11:21:20,700 --> 11:21:23,580
because this returns if Returns the
15246
11:21:23,580 --> 11:21:27,240
elements if the uh the the length of the
15247
11:21:27,240 --> 11:21:29,276
element is less than equal to 3 so this
15248
11:21:29,276 --> 11:21:32,276
is why you get here 3 and 4.
15249
11:21:32,276 --> 11:21:33,480
and
15250
11:21:33,480 --> 11:21:35,880
there are also functions that narrow the
15251
11:21:35,880 --> 11:21:38,460
element type by filtering elements of a
15252
11:21:38,460 --> 11:21:40,740
given type so there is a function called
15253
11:21:40,740 --> 11:21:44,820
filter is instance which returns a
15254
11:21:44,820 --> 11:21:46,680
collection of filaments of a given type
15255
11:21:46,680 --> 11:21:50,580
being called so let's see how you can
15256
11:21:50,580 --> 11:21:52,680
use that but first let's create a
15257
11:21:52,680 --> 11:21:54,720
Val mixed
15258
11:21:54,720 --> 11:21:57,900
let's call it mixed
15259
11:21:57,900 --> 11:21:59,160
list
15260
11:21:59,160 --> 11:22:01,560
I'm gonna put equals another card type
15261
11:22:01,560 --> 11:22:03,900
list off
15262
11:22:03,900 --> 11:22:05,820
and I'm gonna Define some mixed elements
15263
11:22:05,820 --> 11:22:07,256
here so I'm going to type here let's say
15264
11:22:07,256 --> 11:22:08,936
one
15265
11:22:08,936 --> 11:22:14,160
two three then some charts like
15266
11:22:14,160 --> 11:22:16,820
a
15267
11:22:18,480 --> 11:22:20,840
B
15268
11:22:21,900 --> 11:22:23,060
C
15269
11:22:23,060 --> 11:22:26,580
now so on strings let's type here
15270
11:22:26,580 --> 11:22:28,436
hello
15271
11:22:28,436 --> 11:22:31,620
World another string
15272
11:22:31,620 --> 11:22:37,096
Alex and let's also type
15273
11:22:37,320 --> 11:22:39,660
a Boolean so let's type here let's say
15274
11:22:39,660 --> 11:22:41,936
false
15275
11:22:41,936 --> 11:22:45,776
now there is a function as I said which
15276
11:22:45,776 --> 11:22:48,776
which Narrows the element type by
15277
11:22:48,776 --> 11:22:51,120
filtering the elements of a given type
15278
11:22:51,120 --> 11:22:52,740
so we can use this function called
15279
11:22:52,740 --> 11:22:56,220
filter is instance so what you can do
15280
11:22:56,220 --> 11:23:00,360
here is we can we can type
15281
11:23:00,360 --> 11:23:03,480
mixed list
15282
11:23:03,480 --> 11:23:04,980
Dot
15283
11:23:04,980 --> 11:23:06,596
filter
15284
11:23:06,596 --> 11:23:10,159
is instance
15285
11:23:11,820 --> 11:23:17,276
so we choose I think this one not this
15286
11:23:17,276 --> 11:23:18,416
one
15287
11:23:18,416 --> 11:23:21,916
filter is instance
15288
11:23:22,096 --> 11:23:26,580
of Y so mixture is that
15289
11:23:26,580 --> 11:23:29,120
filter
15290
11:23:29,580 --> 11:23:32,840
is instance
15291
11:23:35,596 --> 11:23:39,180
and here with inside the angle brackets
15292
11:23:39,180 --> 11:23:41,340
we Define the generic type for which
15293
11:23:41,340 --> 11:23:42,960
this list is going to be filtered so
15294
11:23:42,960 --> 11:23:46,140
let's first choose the chart Char then
15295
11:23:46,140 --> 11:23:49,380
I'm going to put that for each so I'm
15296
11:23:49,380 --> 11:23:51,540
going to use the for each Loop
15297
11:23:51,540 --> 11:23:54,240
and I'm gonna add print line
15298
11:23:54,240 --> 11:23:56,756
and I'm going to type it here so I'm
15299
11:23:56,756 --> 11:23:58,620
going to type the parameter
15300
11:23:58,620 --> 11:24:00,900
which is the each chart so now if I run
15301
11:24:00,900 --> 11:24:04,020
this we will get only the charts being
15302
11:24:04,020 --> 11:24:06,240
displayed here because it is filtering
15303
11:24:06,240 --> 11:24:08,520
by using the instance of charge so you
15304
11:24:08,520 --> 11:24:12,000
get the charge if I change this to
15305
11:24:12,000 --> 11:24:15,000
to in now I'll get the integers from our
15306
11:24:15,000 --> 11:24:18,560
mixed list so if I run this
15307
11:24:22,140 --> 11:24:24,360
now get the integers one two three that
15308
11:24:24,360 --> 11:24:25,800
we have defined here
15309
11:24:25,800 --> 11:24:28,860
and if I change this to let's say a
15310
11:24:28,860 --> 11:24:31,620
string I will get the strings inside our
15311
11:24:31,620 --> 11:24:34,020
mixed list because filter is instance
15312
11:24:34,020 --> 11:24:37,080
it's gonna take the specific title that
15313
11:24:37,080 --> 11:24:39,000
you type here and then it's gonna Loop
15314
11:24:39,000 --> 11:24:41,936
through all of those types which are of
15315
11:24:41,936 --> 11:24:44,340
this type in this list so if I run this
15316
11:24:44,340 --> 11:24:48,560
I'm gonna see Hello World Analytics
15317
11:24:50,880 --> 11:24:54,596
so we get hello world and Alex also I
15318
11:24:54,596 --> 11:24:57,840
can filter for Boolean values if I type
15319
11:24:57,840 --> 11:24:59,756
here Boolean is gonna
15320
11:24:59,756 --> 11:25:02,460
it's gonna output in the closet or the
15321
11:25:02,460 --> 11:25:03,900
one Boolean value that we have here
15322
11:25:03,900 --> 11:25:07,460
which is false so if I run this
15323
11:25:10,500 --> 11:25:12,416
I get false so this is how you can use
15324
11:25:12,416 --> 11:25:15,840
the filter is instant function on types
15325
11:25:15,840 --> 11:25:19,020
to filter the mixed list and you can
15326
11:25:19,020 --> 11:25:21,776
also put that because this Returns the
15327
11:25:21,776 --> 11:25:24,480
list this entire thing a filter at least
15328
11:25:24,480 --> 11:25:27,240
you can put that for each to call the
15329
11:25:27,240 --> 11:25:30,540
the this the for this for each Lambda
15330
11:25:30,540 --> 11:25:33,120
function and then we output them in the
15331
11:25:33,120 --> 11:25:34,860
console
15332
11:25:34,860 --> 11:25:38,340
uh the next thing that you can look and
15333
11:25:38,340 --> 11:25:40,256
that we're gonna look at is called
15334
11:25:40,256 --> 11:25:42,300
Partition so I'm going to add a comment
15335
11:25:42,300 --> 11:25:44,276
here but I'm going to hide the console
15336
11:25:44,276 --> 11:25:46,620
first so I'm going to put here a comment
15337
11:25:46,620 --> 11:25:51,380
I'm going to type here partition part
15338
11:25:51,380 --> 11:25:54,596
T1 so partition
15339
11:25:54,596 --> 11:25:57,900
and this is an alter another filtering
15340
11:25:57,900 --> 11:26:00,660
function called Partition and it filters
15341
11:26:00,660 --> 11:26:03,540
a collection by a predicate and it keeps
15342
11:26:03,540 --> 11:26:05,756
the elements that don't match in a
15343
11:26:05,756 --> 11:26:07,140
separate list
15344
11:26:07,140 --> 11:26:10,980
so you have a pair of lists as a return
15345
11:26:10,980 --> 11:26:13,140
value the first list is containing the
15346
11:26:13,140 --> 11:26:15,660
element that match the predicate the
15347
11:26:15,660 --> 11:26:17,640
predicate and the second one is
15348
11:26:17,640 --> 11:26:19,620
containing everything else from the
15349
11:26:19,620 --> 11:26:21,660
original collection so I'm gonna take
15350
11:26:21,660 --> 11:26:25,436
again our numbers that have defined the
15351
11:26:25,436 --> 11:26:27,776
path so I'm going to type here
15352
11:26:27,776 --> 11:26:29,276
uh
15353
11:26:29,276 --> 11:26:33,300
file and to to get this partition we put
15354
11:26:33,300 --> 11:26:36,436
we put parentheses and here to type
15355
11:26:36,436 --> 11:26:38,936
match so this is going to be the values
15356
11:26:38,936 --> 11:26:40,640
which are going to be
15357
11:26:40,640 --> 11:26:44,220
matched by the predicate and
15358
11:26:44,220 --> 11:26:46,080
I'm gonna type here rest and you're
15359
11:26:46,080 --> 11:26:47,700
gonna be stored the rest of the value
15360
11:26:47,700 --> 11:26:50,040
which which don't match the expression
15361
11:26:50,040 --> 11:26:52,560
the predicate that's going to be defined
15362
11:26:52,560 --> 11:26:55,080
and I put here equals
15363
11:26:55,080 --> 11:26:57,596
numbers
15364
11:26:57,596 --> 11:27:00,840
dot partition
15365
11:27:00,840 --> 11:27:03,720
and we type here it
15366
11:27:03,720 --> 11:27:06,380
Dot
15367
11:27:07,320 --> 11:27:10,040
length
15368
11:27:10,200 --> 11:27:11,820
greater
15369
11:27:11,820 --> 11:27:13,320
than 3.
15370
11:27:13,320 --> 11:27:15,840
now if I
15371
11:27:15,840 --> 11:27:18,300
other print line here
15372
11:27:18,300 --> 11:27:21,360
I can print first the match
15373
11:27:21,360 --> 11:27:24,596
let's press Ctrl D and now I can print
15374
11:27:24,596 --> 11:27:26,520
the rest of the elements which don't
15375
11:27:26,520 --> 11:27:29,096
match this this condition this
15376
11:27:29,096 --> 11:27:31,020
expression that is defined here so if I
15377
11:27:31,020 --> 11:27:32,640
run this code
15378
11:27:32,640 --> 11:27:34,860
you will see first all the elements
15379
11:27:34,860 --> 11:27:36,596
which are greater than 3 and then we're
15380
11:27:36,596 --> 11:27:37,916
gonna see the rest of the elements which
15381
11:27:37,916 --> 11:27:41,096
are not greater than 3. but let's add
15382
11:27:41,096 --> 11:27:43,500
the space here to make things more
15383
11:27:43,500 --> 11:27:44,700
clearly so we're going to add a hero
15384
11:27:44,700 --> 11:27:46,500
print line
15385
11:27:46,500 --> 11:27:50,700
quotation marks for recession so let's
15386
11:27:50,700 --> 11:27:53,479
run this again
15387
11:27:57,660 --> 11:28:00,840
so we get three and four so those are
15388
11:28:00,840 --> 11:28:04,040
the elements which are coming from our
15389
11:28:04,040 --> 11:28:09,060
match list which is defined here uh
15390
11:28:09,060 --> 11:28:11,700
which is uh which satisfy this condition
15391
11:28:11,700 --> 11:28:14,276
which is greater than 3 so we have three
15392
11:28:14,276 --> 11:28:16,256
and four so the length is greater than
15393
11:28:16,256 --> 11:28:17,400
three
15394
11:28:17,400 --> 11:28:19,800
and then we have one and two so those
15395
11:28:19,800 --> 11:28:21,960
are the rest of the elements which are
15396
11:28:21,960 --> 11:28:24,720
not satisfying this condition that is
15397
11:28:24,720 --> 11:28:27,416
defined here so we have one and two so
15398
11:28:27,416 --> 11:28:28,980
this is how you can use the partition
15399
11:28:28,980 --> 11:28:30,720
function to
15400
11:28:30,720 --> 11:28:34,320
to get the
15401
11:28:34,320 --> 11:28:37,380
basically to get a pair in which the
15402
11:28:37,380 --> 11:28:39,660
return of value the first is the first
15403
11:28:39,660 --> 11:28:41,276
is the list containing the elements that
15404
11:28:41,276 --> 11:28:43,080
match the predicate and the same code is
15405
11:28:43,080 --> 11:28:44,700
the one containing everything else from
15406
11:28:44,700 --> 11:28:46,200
the original collection
15407
11:28:46,200 --> 11:28:48,360
so this is how you can use the the
15408
11:28:48,360 --> 11:28:50,400
partition function so
15409
11:28:50,400 --> 11:28:52,916
now I'm gonna end the video and see you
15410
11:28:52,916 --> 11:28:54,660
in the next video now it's time to look
15411
11:28:54,660 --> 11:28:57,416
at testing predicates and those are
15412
11:28:57,416 --> 11:28:59,460
functions that simply test the predicate
15413
11:28:59,460 --> 11:29:01,140
against the collection elements and we
15414
11:29:01,140 --> 11:29:03,116
have three functions any
15415
11:29:03,116 --> 11:29:05,220
which returns true if at least one of
15416
11:29:05,220 --> 11:29:07,320
the element matches the given predicate
15417
11:29:07,320 --> 11:29:09,960
then we have none which returns true if
15418
11:29:09,960 --> 11:29:11,880
none of the elements match the given
15419
11:29:11,880 --> 11:29:14,096
predicate and then we have all which
15420
11:29:14,096 --> 11:29:15,776
returns true if all of the elements
15421
11:29:15,776 --> 11:29:18,240
match the given predicate so let's test
15422
11:29:18,240 --> 11:29:20,160
all of them so I'm gonna type here print
15423
11:29:20,160 --> 11:29:22,460
line
15424
11:29:22,800 --> 11:29:25,640
numbers
15425
11:29:25,680 --> 11:29:30,980
that let's start with any so we put any
15426
11:29:31,320 --> 11:29:37,200
so here we type it dot ends
15427
11:29:37,200 --> 11:29:38,520
with
15428
11:29:38,520 --> 11:29:41,580
and let's put let's say
15429
11:29:41,580 --> 11:29:44,116
e
15430
11:29:45,480 --> 11:29:49,500
then out of another print line
15431
11:29:49,500 --> 11:29:51,116
numbers
15432
11:29:51,116 --> 11:29:54,200
dot none
15433
11:29:54,900 --> 11:29:57,060
it should be
15434
11:29:57,060 --> 11:29:58,860
curly braces
15435
11:29:58,860 --> 11:30:00,360
eat
15436
11:30:00,360 --> 11:30:03,060
Dot and sweet
15437
11:30:03,060 --> 11:30:05,820
let's say w
15438
11:30:05,820 --> 11:30:09,256
and print line
15439
11:30:10,380 --> 11:30:12,000
numbers
15440
11:30:12,000 --> 11:30:14,160
dot all
15441
11:30:14,160 --> 11:30:17,700
let's say it dot length
15442
11:30:17,700 --> 11:30:20,880
so if if the length of all the elements
15443
11:30:20,880 --> 11:30:23,400
is greater than one
15444
11:30:23,400 --> 11:30:25,680
so now if you run this
15445
11:30:25,680 --> 11:30:28,680
all of those gonna return either true or
15446
11:30:28,680 --> 11:30:30,916
false
15447
11:30:33,060 --> 11:30:35,820
so get true true and true this is
15448
11:30:35,820 --> 11:30:39,116
because first testing any and then this
15449
11:30:39,116 --> 11:30:40,560
returns true with at least one of the
15450
11:30:40,560 --> 11:30:42,300
elements much in the given predicate so
15451
11:30:42,300 --> 11:30:45,180
it is true we have
15452
11:30:45,180 --> 11:30:48,300
two elements which ends with the E we
15453
11:30:48,300 --> 11:30:51,240
have three and one so that's why you
15454
11:30:51,240 --> 11:30:52,680
return true there
15455
11:30:52,680 --> 11:30:56,580
then we check to see if uh none of the
15456
11:30:56,580 --> 11:30:58,740
elements ends with W so we're checking
15457
11:30:58,740 --> 11:31:01,320
to see if none of the elements uh in our
15458
11:31:01,320 --> 11:31:04,860
numbers ends with w n is true not only
15459
11:31:04,860 --> 11:31:06,660
for elements will end through double so
15460
11:31:06,660 --> 11:31:09,116
we have two in the output
15461
11:31:09,116 --> 11:31:11,936
then we check numbers.org so we check
15462
11:31:11,936 --> 11:31:14,340
into if all the elements the line the
15463
11:31:14,340 --> 11:31:16,320
length of all the elements the element
15464
11:31:16,320 --> 11:31:18,060
strings that we have here one two and
15465
11:31:18,060 --> 11:31:20,340
three are greater than one and this row
15466
11:31:20,340 --> 11:31:22,616
all of them are greater than one so this
15467
11:31:22,616 --> 11:31:24,720
one have true true intro
15468
11:31:24,720 --> 11:31:26,936
so this is how you can use the test
15469
11:31:26,936 --> 11:31:30,960
predicates uh on your collections
15470
11:31:30,960 --> 11:31:32,880
first time to look at the plus and minus
15471
11:31:32,880 --> 11:31:35,820
operators so in Catherine plus and minus
15472
11:31:35,820 --> 11:31:38,580
operators are defined for collections
15473
11:31:38,580 --> 11:31:40,500
they take a collection as the first
15474
11:31:40,500 --> 11:31:42,840
operand and the second operand can be
15475
11:31:42,840 --> 11:31:44,640
either an element or another collection
15476
11:31:44,640 --> 11:31:46,740
the return value is a new read-only
15477
11:31:46,740 --> 11:31:48,416
collection and let's look at an example
15478
11:31:48,416 --> 11:31:51,240
on this I'm going to type here Val
15479
11:31:51,240 --> 11:31:53,160
numbers
15480
11:31:53,160 --> 11:31:55,320
you want to put equals two so now I'm
15481
11:31:55,320 --> 11:31:58,500
going to type your Multiple List off
15482
11:31:58,500 --> 11:32:01,256
Multiple List at least with we to which
15483
11:32:01,256 --> 11:32:03,360
you can add and remove elements and I'm
15484
11:32:03,360 --> 11:32:05,040
gonna type here
15485
11:32:05,040 --> 11:32:07,756
one
15486
11:32:08,340 --> 11:32:10,460
two
15487
11:32:11,096 --> 11:32:13,096
three
15488
11:32:13,916 --> 11:32:16,080
and four now if you want to add an
15489
11:32:16,080 --> 11:32:18,720
element to this list you need to type
15490
11:32:18,720 --> 11:32:21,596
here numbers dot add
15491
11:32:21,596 --> 11:32:23,880
and here you need to specify let's say
15492
11:32:23,880 --> 11:32:26,640
five and now this new element is going
15493
11:32:26,640 --> 11:32:28,800
to be added to the list but with the
15494
11:32:28,800 --> 11:32:30,776
plus and minus operator you can do we
15495
11:32:30,776 --> 11:32:33,180
can do this in an another way so you can
15496
11:32:33,180 --> 11:32:34,616
type here
15497
11:32:34,616 --> 11:32:36,480
uh
15498
11:32:36,480 --> 11:32:38,460
but I'm gonna put this in another list
15499
11:32:38,460 --> 11:32:41,040
so you can type here Val
15500
11:32:41,040 --> 11:32:43,936
plus list
15501
11:32:44,520 --> 11:32:48,060
so we put equals to and type numbers
15502
11:32:48,060 --> 11:32:49,616
Plus
15503
11:32:49,616 --> 11:32:52,140
and to type five and now this element is
15504
11:32:52,140 --> 11:32:55,680
going to be added to our list that we
15505
11:32:55,680 --> 11:32:57,660
have here it's going to be added to the
15506
11:32:57,660 --> 11:33:00,000
the string five and it's going to be
15507
11:33:00,000 --> 11:33:02,460
stored in this plus list now let's look
15508
11:33:02,460 --> 11:33:05,096
at the minus operator here Val I'm going
15509
11:33:05,096 --> 11:33:08,540
to call this minus list
15510
11:33:09,180 --> 11:33:11,700
and put equals 2 and I'm going to type
15511
11:33:11,700 --> 11:33:14,340
numbers so now I'm going to use the
15512
11:33:14,340 --> 11:33:16,740
minus operator so I put minus
15513
11:33:16,740 --> 11:33:19,436
and we type minus
15514
11:33:19,436 --> 11:33:21,596
multiple list of
15515
11:33:21,596 --> 11:33:23,580
and we put here
15516
11:33:23,580 --> 11:33:27,240
let's say three and four so we type here
15517
11:33:27,240 --> 11:33:29,596
three
15518
11:33:29,700 --> 11:33:32,479
and four
15519
11:33:33,416 --> 11:33:35,756
I have an underline let's see what it
15520
11:33:35,756 --> 11:33:37,860
says
15521
11:33:37,860 --> 11:33:40,560
now anyway let's run our code to see
15522
11:33:40,560 --> 11:33:43,520
what to get in the output
15523
11:33:44,096 --> 11:33:45,840
but we need to add some print lines
15524
11:33:45,840 --> 11:33:48,180
together so
15525
11:33:48,180 --> 11:33:50,340
I'm gonna add here a print line
15526
11:33:50,340 --> 11:33:53,115
plus list
15527
11:33:57,900 --> 11:34:01,580
and another print line
15528
11:34:02,276 --> 11:34:06,799
minus list now if I run this again
15529
11:34:11,936 --> 11:34:15,776
we get one two three four and our
15530
11:34:15,776 --> 11:34:18,180
Five Element added to our numbers list
15531
11:34:18,180 --> 11:34:20,276
from our class list which is outputted
15532
11:34:20,276 --> 11:34:21,180
here
15533
11:34:21,180 --> 11:34:25,380
and then we get 1 and 2 because 3 and 4
15534
11:34:25,380 --> 11:34:27,660
were removed using the minus operator so
15535
11:34:27,660 --> 11:34:29,276
this is how you can use the minus and
15536
11:34:29,276 --> 11:34:31,800
plus operators with the collections
15537
11:34:31,800 --> 11:34:34,680
so this is our discussion about the plus
15538
11:34:34,680 --> 11:34:36,840
and minus operators and I'm going to
15539
11:34:36,840 --> 11:34:38,460
look at we're gonna
15540
11:34:38,460 --> 11:34:39,900
uh
15541
11:34:39,900 --> 11:34:42,060
move to the next section so I'm going to
15542
11:34:42,060 --> 11:34:43,860
close the console and I'm going to end
15543
11:34:43,860 --> 11:34:45,720
the video
15544
11:34:45,720 --> 11:34:47,340
now it's time to start a discussion
15545
11:34:47,340 --> 11:34:49,616
about grouping so the kotlin standard
15546
11:34:49,616 --> 11:34:51,596
Library provides extension functions for
15547
11:34:51,596 --> 11:34:54,416
grouping collection elements the basic
15548
11:34:54,416 --> 11:34:56,580
function Group by takes a Lambda
15549
11:34:56,580 --> 11:34:59,096
function and returns them up in this map
15550
11:34:59,096 --> 11:35:01,800
each key is the Lambda result so the
15551
11:35:01,800 --> 11:35:03,540
result of that expression inside the
15552
11:35:03,540 --> 11:35:05,160
Lambda function
15553
11:35:05,160 --> 11:35:07,740
and the corresponding values is the list
15554
11:35:07,740 --> 11:35:09,720
of filaments on which this result is
15555
11:35:09,720 --> 11:35:12,596
returned this function can be used for
15556
11:35:12,596 --> 11:35:14,936
example to group a list of strings by
15557
11:35:14,936 --> 11:35:16,140
the first letter
15558
11:35:16,140 --> 11:35:19,140
you can also call Group by with a second
15559
11:35:19,140 --> 11:35:21,540
Lambda argument a value transformation
15560
11:35:21,540 --> 11:35:24,720
function in the result map of group y
15561
11:35:24,720 --> 11:35:28,320
with two lambdas the key is produced by
15562
11:35:28,320 --> 11:35:30,840
key produced by key selector function
15563
11:35:30,840 --> 11:35:33,000
are mapped to the result of the value
15564
11:35:33,000 --> 11:35:35,160
transformation function instead of the
15565
11:35:35,160 --> 11:35:37,080
original elements
15566
11:35:37,080 --> 11:35:39,596
so let's see what all of this means by
15567
11:35:39,596 --> 11:35:41,580
looking at an example so if I type here
15568
11:35:41,580 --> 11:35:44,000
Val
15569
11:35:46,616 --> 11:35:48,060
numbers
15570
11:35:48,060 --> 11:35:50,220
and I'm going to put
15571
11:35:50,220 --> 11:35:53,000
list of
15572
11:35:55,200 --> 11:35:58,140
again one
15573
11:35:58,140 --> 11:36:00,140
two
15574
11:36:03,596 --> 11:36:05,936
three
15575
11:36:05,936 --> 11:36:08,400
four
15576
11:36:08,400 --> 11:36:11,300
and five
15577
11:36:12,720 --> 11:36:15,960
now we I can add here a print line
15578
11:36:15,960 --> 11:36:19,276
I can type numbers
15579
11:36:19,560 --> 11:36:24,799
and I can type here dot Group by
15580
11:36:25,560 --> 11:36:28,320
and I'm gonna group it by
15581
11:36:28,320 --> 11:36:32,400
it dot first so the first letter in in
15582
11:36:32,400 --> 11:36:36,116
one of our elements that
15583
11:36:36,116 --> 11:36:38,520
uppercase
15584
11:36:38,520 --> 11:36:40,256
and now we're going to look at the next
15585
11:36:40,256 --> 11:36:43,560
the next Group by with the key selector
15586
11:36:43,560 --> 11:36:47,276
and with the transformation function so
15587
11:36:47,276 --> 11:36:50,720
I'm going to type here print line
15588
11:36:51,540 --> 11:36:55,140
numbers Dot
15589
11:36:55,740 --> 11:36:58,579
group y
15590
11:37:00,300 --> 11:37:02,220
so we have this one key selector so you
15591
11:37:02,220 --> 11:37:04,256
type here key selector
15592
11:37:04,256 --> 11:37:05,820
equals
15593
11:37:05,820 --> 11:37:09,256
and here we put
15594
11:37:09,776 --> 11:37:13,080
curly braces and we type eat that first
15595
11:37:13,080 --> 11:37:16,560
so the first letter in our element so
15596
11:37:16,560 --> 11:37:19,320
first it should be here
15597
11:37:19,320 --> 11:37:23,360
we put a comma
15598
11:37:24,300 --> 11:37:27,596
and we type value so the how the value
15599
11:37:27,596 --> 11:37:30,540
of the the value of this list is going
15600
11:37:30,540 --> 11:37:32,820
to are going to be transformed and here
15601
11:37:32,820 --> 11:37:35,220
we also put curly braces and we put it
15602
11:37:35,220 --> 11:37:38,580
that uppercase so put it curly braces it
15603
11:37:38,580 --> 11:37:39,720
should be
15604
11:37:39,720 --> 11:37:43,740
it dot uppercase
15605
11:37:43,740 --> 11:37:46,860
let's press Ctrl alt L to format code
15606
11:37:46,860 --> 11:37:49,200
and now let's run our code to see what
15607
11:37:49,200 --> 11:37:52,039
we get in the output
15608
11:37:57,776 --> 11:37:59,756
so we get
15609
11:37:59,756 --> 11:38:02,040
for the first one number the numbers are
15610
11:38:02,040 --> 11:38:04,080
grouped by for uh and we have this
15611
11:38:04,080 --> 11:38:06,660
Lambda it that first.apper case it takes
15612
11:38:06,660 --> 11:38:08,756
the first letter in Upper cases so it
15613
11:38:08,756 --> 11:38:11,640
gets whoa whoa so then you get one
15614
11:38:11,640 --> 11:38:15,660
we get T we get to we get F we get four
15615
11:38:15,660 --> 11:38:19,080
we get uh then we use this one which on
15616
11:38:19,080 --> 11:38:22,860
which we we uh you we we group uh
15617
11:38:22,860 --> 11:38:25,380
elements with a second Lambda argument
15618
11:38:25,380 --> 11:38:27,116
with the value transformation function
15619
11:38:27,116 --> 11:38:30,000
and the result uh map of group y with
15620
11:38:30,000 --> 11:38:32,460
two lambdas are the keys produced by the
15621
11:38:32,460 --> 11:38:34,680
key selector and the function are the
15622
11:38:34,680 --> 11:38:36,596
function are mapped to the result of the
15623
11:38:36,596 --> 11:38:39,240
value transformation function so the
15624
11:38:39,240 --> 11:38:41,460
results are mapped to this
15625
11:38:41,460 --> 11:38:43,500
transformation that is defined here so
15626
11:38:43,500 --> 11:38:47,276
this this is what creates the keys this
15627
11:38:47,276 --> 11:38:49,560
is what creates the key this uh Lambda
15628
11:38:49,560 --> 11:38:52,560
function and this is what creates the
15629
11:38:52,560 --> 11:38:56,096
the the the the values so we get all
15630
11:38:56,096 --> 11:38:58,200
because you get in that first so the
15631
11:38:58,200 --> 11:39:00,480
first letter and get one because this is
15632
11:39:00,480 --> 11:39:02,580
the value so the value is uppercased we
15633
11:39:02,580 --> 11:39:05,880
get one so we get one here then we get
15634
11:39:05,880 --> 11:39:08,640
again eat that first for the for all men
15635
11:39:08,640 --> 11:39:10,860
so you get T then you get two and I get
15636
11:39:10,860 --> 11:39:14,756
uppercased and get F lowercase f and get
15637
11:39:14,756 --> 11:39:18,240
four and uh and five
15638
11:39:18,240 --> 11:39:20,880
so this is our discussion about grouping
15639
11:39:20,880 --> 11:39:23,880
and uh my suggestion for you is to play
15640
11:39:23,880 --> 11:39:26,460
with the code try different values for
15641
11:39:26,460 --> 11:39:29,460
the key selector for the value transform
15642
11:39:29,460 --> 11:39:32,700
for uh try to group The Elements by
15643
11:39:32,700 --> 11:39:35,936
different expressions and just play with
15644
11:39:35,936 --> 11:39:39,300
the code until you get uh get uh until
15645
11:39:39,300 --> 11:39:42,000
you get comfortable with the code now
15646
11:39:42,000 --> 11:39:43,380
it's time to start a discussion about
15647
11:39:43,380 --> 11:39:45,660
the driven collection parts and the
15648
11:39:45,660 --> 11:39:47,340
quoting standard Library contest
15649
11:39:47,340 --> 11:39:49,256
accession functions for retrieving parts
15650
11:39:49,256 --> 11:39:50,700
of a collection
15651
11:39:50,700 --> 11:39:52,740
these functions provide a variety of
15652
11:39:52,740 --> 11:39:54,540
ways to select elements for the result
15653
11:39:54,540 --> 11:39:56,220
collection listing their position
15654
11:39:56,220 --> 11:39:59,040
explicitly specifying the result size
15655
11:39:59,040 --> 11:40:00,776
and others I'm going to start with the
15656
11:40:00,776 --> 11:40:02,936
first function called slice
15657
11:40:02,936 --> 11:40:04,740
which Returns the list of collection
15658
11:40:04,740 --> 11:40:07,616
elements with given integers the indices
15659
11:40:07,616 --> 11:40:10,616
may be based either as a range or as a
15660
11:40:10,616 --> 11:40:12,300
collection of integer values so I'm
15661
11:40:12,300 --> 11:40:14,936
gonna create first domain function
15662
11:40:14,936 --> 11:40:18,000
I'm going to type Val numbers I'm going
15663
11:40:18,000 --> 11:40:20,900
to create a list of
15664
11:40:21,240 --> 11:40:24,680
and I'm going to type here one
15665
11:40:25,200 --> 11:40:28,039
to
15666
11:40:28,616 --> 11:40:31,200
three
15667
11:40:31,200 --> 11:40:33,860
four
15668
11:40:34,080 --> 11:40:36,080
five
15669
11:40:37,200 --> 11:40:39,300
and six
15670
11:40:39,300 --> 11:40:41,700
now to use the slice function we simply
15671
11:40:41,700 --> 11:40:44,160
add here a print line
15672
11:40:44,160 --> 11:40:46,220
and we'll type your numbers
15673
11:40:46,220 --> 11:40:49,680
dot slice
15674
11:40:49,680 --> 11:40:52,980
and we create here arrange so we type 1
15675
11:40:52,980 --> 11:40:54,776
dot dot
15676
11:40:54,776 --> 11:40:56,416
3
15677
11:40:56,416 --> 11:40:59,640
and next we're going to add another
15678
11:40:59,640 --> 11:41:03,480
print line and I'm going to type numbers
15679
11:41:03,480 --> 11:41:06,740
dot slice
15680
11:41:07,616 --> 11:41:10,320
and I'm going to put here now zero dot
15681
11:41:10,320 --> 11:41:12,060
dot
15682
11:41:12,060 --> 11:41:13,980
four
15683
11:41:13,980 --> 11:41:16,580
and
15684
11:41:16,680 --> 11:41:20,160
I'm gonna type step
15685
11:41:20,160 --> 11:41:23,160
two
15686
11:41:31,140 --> 11:41:33,320
foreign
15687
11:41:38,240 --> 11:41:41,756
list so slice here
15688
11:41:41,756 --> 11:41:44,720
slice
15689
11:41:45,000 --> 11:41:47,220
and I'm gonna add another print line
15690
11:41:47,220 --> 11:41:49,580
here
15691
11:41:49,860 --> 11:41:52,520
numbers
15692
11:41:52,680 --> 11:41:56,040
dot slice
15693
11:41:56,040 --> 11:41:59,276
and I'm gonna add now a set here so I'm
15694
11:41:59,276 --> 11:42:02,299
going to type set off
15695
11:42:02,820 --> 11:42:04,800
and I'm gonna type
15696
11:42:04,800 --> 11:42:07,320
three
15697
11:42:07,320 --> 11:42:09,916
five
15698
11:42:10,140 --> 11:42:13,500
and zero so let's press Ctrl R till the
15699
11:42:13,500 --> 11:42:14,756
format the code
15700
11:42:14,756 --> 11:42:17,040
now if you run this code look at the
15701
11:42:17,040 --> 11:42:19,400
output
15702
11:42:36,416 --> 11:42:39,660
so you first get the first three
15703
11:42:39,660 --> 11:42:41,756
elements we get to
15704
11:42:41,756 --> 11:42:45,720
two three and four because uh we're
15705
11:42:45,720 --> 11:42:47,460
starting from the index one so we get
15706
11:42:47,460 --> 11:42:49,080
two three and four
15707
11:42:49,080 --> 11:42:53,276
then we get from 0 to 4 so you get one
15708
11:42:53,276 --> 11:42:55,380
but you don't get the two because you
15709
11:42:55,380 --> 11:42:58,080
have this step two which steps the
15710
11:42:58,080 --> 11:43:01,200
second element and then we have three
15711
11:43:01,200 --> 11:43:03,660
and five because it's stepping for the
15712
11:43:03,660 --> 11:43:05,640
four so this is what step two is doing
15713
11:43:05,640 --> 11:43:06,660
here
15714
11:43:06,660 --> 11:43:10,200
and next we have numbers slice and get
15715
11:43:10,200 --> 11:43:12,000
four
15716
11:43:12,000 --> 11:43:16,200
six and uh one but we get them as the
15717
11:43:16,200 --> 11:43:18,240
string representation we don't get them
15718
11:43:18,240 --> 11:43:20,460
as
15719
11:43:20,460 --> 11:43:24,900
as a integers so
15720
11:43:24,900 --> 11:43:27,240
all right
15721
11:43:27,240 --> 11:43:28,560
now
15722
11:43:28,560 --> 11:43:30,776
let's look at the next functions and
15723
11:43:30,776 --> 11:43:33,060
those are the take and drop
15724
11:43:33,060 --> 11:43:35,640
and to get the specified numbers of
15725
11:43:35,640 --> 11:43:38,340
elements starting from the first you can
15726
11:43:38,340 --> 11:43:41,400
use the take function for getting the
15727
11:43:41,400 --> 11:43:44,220
last elements use the take last when
15728
11:43:44,220 --> 11:43:46,140
call when when called with a number
15729
11:43:46,140 --> 11:43:48,300
larger than the collection size both
15730
11:43:48,300 --> 11:43:50,756
functions return the whole collection to
15731
11:43:50,756 --> 11:43:52,740
take all the elements except a given
15732
11:43:52,740 --> 11:43:55,680
number of first or last elements called
15733
11:43:55,680 --> 11:43:58,320
the drop or the and the drop loss
15734
11:43:58,320 --> 11:43:59,936
function respectively
15735
11:43:59,936 --> 11:44:03,256
so I'm gonna
15736
11:44:04,256 --> 11:44:07,436
add some print lines here I'm gonna add
15737
11:44:07,436 --> 11:44:08,936
print line
15738
11:44:08,936 --> 11:44:12,680
and I'm going to type numbers
15739
11:44:12,680 --> 11:44:15,980
dot take
15740
11:44:18,960 --> 11:44:20,460
three
15741
11:44:20,460 --> 11:44:23,840
Another Printer line
15742
11:44:24,060 --> 11:44:26,659
numbers
15743
11:44:27,300 --> 11:44:30,720
dot take last
15744
11:44:30,720 --> 11:44:33,436
hmm
15745
11:44:34,020 --> 11:44:39,020
also three then another print line
15746
11:44:39,180 --> 11:44:40,800
numbers
15747
11:44:40,800 --> 11:44:42,900
Dot
15748
11:44:42,900 --> 11:44:45,540
drop
15749
11:44:45,540 --> 11:44:47,340
one
15750
11:44:47,340 --> 11:44:50,719
and another print line
15751
11:44:54,540 --> 11:44:56,220
numbers
15752
11:44:56,220 --> 11:45:00,720
dot drop last
15753
11:45:01,560 --> 11:45:05,840
five now if you run this code
15754
11:45:06,740 --> 11:45:10,820
look what you get in the output
15755
11:45:12,300 --> 11:45:15,000
but let's add a space here so on another
15756
11:45:15,000 --> 11:45:17,096
print line
15757
11:45:17,096 --> 11:45:20,096
quotation marks photosyn now let's run
15758
11:45:20,096 --> 11:45:22,756
this code again
15759
11:45:27,660 --> 11:45:29,640
so we get uh
15760
11:45:29,640 --> 11:45:32,580
for you get numbers take three and this
15761
11:45:32,580 --> 11:45:36,116
function basically takes only three
15762
11:45:36,116 --> 11:45:36,960
um
15763
11:45:36,960 --> 11:45:39,300
three elements from our list
15764
11:45:39,300 --> 11:45:42,840
then numbers take last three is gonna
15765
11:45:42,840 --> 11:45:44,936
take the last three elements from hourly
15766
11:45:44,936 --> 11:45:47,040
so I have four five and six
15767
11:45:47,040 --> 11:45:50,040
then we have a print print line numbers
15768
11:45:50,040 --> 11:45:53,756
drop one so you have two three four five
15769
11:45:53,756 --> 11:45:56,040
and six and the first element is dropped
15770
11:45:56,040 --> 11:45:58,380
so this this is what this function is
15771
11:45:58,380 --> 11:45:59,460
doing
15772
11:45:59,460 --> 11:46:02,640
next we have numbers dot drop plus five
15773
11:46:02,640 --> 11:46:05,040
and this function is basically dropping
15774
11:46:05,040 --> 11:46:08,880
all the uh file the last five elements
15775
11:46:08,880 --> 11:46:11,220
and you have only the one so this is
15776
11:46:11,220 --> 11:46:14,096
what those functions are doing
15777
11:46:14,096 --> 11:46:16,616
you can also use predicates to define
15778
11:46:16,616 --> 11:46:18,596
the number of elements for taking or
15779
11:46:18,596 --> 11:46:21,000
dropping and there are four functions
15780
11:46:21,000 --> 11:46:23,820
similar to the ones described above
15781
11:46:23,820 --> 11:46:27,180
and those are take while take last while
15782
11:46:27,180 --> 11:46:30,660
drop while and drop drop last while and
15783
11:46:30,660 --> 11:46:33,240
let's look at all of them but I'm gonna
15784
11:46:33,240 --> 11:46:35,880
put a space between uh
15785
11:46:35,880 --> 11:46:37,436
our code that we have here so I'm gonna
15786
11:46:37,436 --> 11:46:40,520
have the print line here
15787
11:46:41,640 --> 11:46:45,616
quotation marks photo session
15788
11:46:46,400 --> 11:46:49,756
print line
15789
11:46:51,416 --> 11:46:53,820
so I'm going to type here numbers
15790
11:46:53,820 --> 11:46:55,800
dot take
15791
11:46:55,800 --> 11:46:58,800
while
15792
11:47:00,540 --> 11:47:03,116
and here we Define the predicate I'm
15793
11:47:03,116 --> 11:47:04,320
going to type
15794
11:47:04,320 --> 11:47:05,880
not
15795
11:47:05,880 --> 11:47:08,700
eat dot starts
15796
11:47:08,700 --> 11:47:11,180
with
15797
11:47:11,756 --> 11:47:13,380
f
15798
11:47:13,380 --> 11:47:16,256
in quotation marks
15799
11:47:16,256 --> 11:47:20,000
now another print line
15800
11:47:21,000 --> 11:47:23,220
numbers
15801
11:47:23,220 --> 11:47:26,360
dot take
15802
11:47:26,520 --> 11:47:29,359
last
15803
11:47:30,776 --> 11:47:33,380
while
15804
11:47:33,776 --> 11:47:37,860
and we Define here the predicate it
15805
11:47:37,860 --> 11:47:42,256
not equal to 3. not equal
15806
11:47:42,416 --> 11:47:45,360
two three
15807
11:47:45,360 --> 11:47:48,000
another print line
15808
11:47:48,000 --> 11:47:51,080
we type numbers
15809
11:47:54,960 --> 11:47:58,460
dot drop while
15810
11:48:01,256 --> 11:48:03,416
and we Define the expression the
15811
11:48:03,416 --> 11:48:05,756
predicate which is going to be applied
15812
11:48:05,756 --> 11:48:08,520
for our drop last while
15813
11:48:08,520 --> 11:48:10,680
so it should be
15814
11:48:10,680 --> 11:48:14,000
drop while here
15815
11:48:16,560 --> 11:48:19,460
and type it
15816
11:48:19,460 --> 11:48:23,096
dot length
15817
11:48:23,096 --> 11:48:25,800
equals equals to 3.
15818
11:48:25,800 --> 11:48:29,540
and what another print line
15819
11:48:29,880 --> 11:48:32,596
numbers
15820
11:48:33,180 --> 11:48:35,220
Dot
15821
11:48:35,220 --> 11:48:38,540
drop last while
15822
11:48:38,756 --> 11:48:42,180
and we type here it contains
15823
11:48:42,180 --> 11:48:44,700
it dot
15824
11:48:44,700 --> 11:48:47,700
contains
15825
11:48:50,460 --> 11:48:53,580
quotation marks I
15826
11:48:53,580 --> 11:48:55,080
no
15827
11:48:55,080 --> 11:48:58,276
if you run this code
15828
11:49:04,436 --> 11:49:07,680
we first get one
15829
11:49:07,680 --> 11:49:10,680
two and three so because here we check
15830
11:49:10,680 --> 11:49:13,256
in we're uh because using the logical
15831
11:49:13,256 --> 11:49:15,720
not operated we want all the elements
15832
11:49:15,720 --> 11:49:18,540
which don't start with f so we don't
15833
11:49:18,540 --> 11:49:20,820
have four and a five we have only one
15834
11:49:20,820 --> 11:49:22,200
two and three
15835
11:49:22,200 --> 11:49:23,756
next we have
15836
11:49:23,756 --> 11:49:26,640
numbers that take last while and we
15837
11:49:26,640 --> 11:49:29,040
Define the expression for which is going
15838
11:49:29,040 --> 11:49:30,680
to be
15839
11:49:30,680 --> 11:49:33,660
returned the elements by it not equal to
15840
11:49:33,660 --> 11:49:36,660
three so you only have four
15841
11:49:36,660 --> 11:49:42,180
five and six and uh the the the three
15842
11:49:42,180 --> 11:49:44,160
the three is is missing and we don't
15843
11:49:44,160 --> 11:49:46,500
have one because take last value only
15844
11:49:46,500 --> 11:49:49,980
takes the uh last elements so this is
15845
11:49:49,980 --> 11:49:52,580
why we have only
15846
11:49:52,880 --> 11:49:56,096
and six when you don't have three so it
15847
11:49:56,096 --> 11:49:58,740
doesn't care about the first element or
15848
11:49:58,740 --> 11:50:02,580
uh about uh the second element
15849
11:50:02,580 --> 11:50:06,360
next we have numbers drop while eat that
15850
11:50:06,360 --> 11:50:10,860
length equals to 3 and here we have
15851
11:50:10,860 --> 11:50:12,840
we have
15852
11:50:12,840 --> 11:50:14,820
so it at the lines equals to three and
15853
11:50:14,820 --> 11:50:17,180
we have
15854
11:50:17,220 --> 11:50:21,300
three four five and six so
15855
11:50:21,300 --> 11:50:24,720
this condition is satisfied so it
15856
11:50:24,720 --> 11:50:27,776
basically it drops all the elements
15857
11:50:27,776 --> 11:50:30,416
which are equal to three so we have
15858
11:50:30,416 --> 11:50:32,880
three four five and six and all of those
15859
11:50:32,880 --> 11:50:36,360
are bigger than three and the ones which
15860
11:50:36,360 --> 11:50:39,680
are equal to three respectively one
15861
11:50:39,680 --> 11:50:47,400
uh uh two and uh let's see and six
15862
11:50:47,400 --> 11:50:51,320
actually six is appearing here
15863
11:50:51,416 --> 11:50:54,020
so
15864
11:50:54,596 --> 11:50:56,520
one
15865
11:50:56,520 --> 11:50:59,000
two
15866
11:51:02,096 --> 11:51:04,500
I don't know why six supreme but this
15867
11:51:04,500 --> 11:51:06,776
basically saying that it should drop
15868
11:51:06,776 --> 11:51:10,020
while if that length equals uh to three
15869
11:51:10,020 --> 11:51:12,480
so it basically drops all the elements
15870
11:51:12,480 --> 11:51:15,900
which are equal to with the length three
15871
11:51:15,900 --> 11:51:20,756
so we have this is what we have here um
15872
11:51:20,756 --> 11:51:22,436
three four
15873
11:51:22,436 --> 11:51:24,960
five and six six should not be but I
15874
11:51:24,960 --> 11:51:26,640
don't know why it's here now
15875
11:51:26,640 --> 11:51:29,060
the next that we have here is numbers
15876
11:51:29,060 --> 11:51:32,276
dropped last while so again this is uh
15877
11:51:32,276 --> 11:51:34,380
Focus specifically about the last
15878
11:51:34,380 --> 11:51:38,640
element so it contains I so get uh
15879
11:51:38,640 --> 11:51:41,276
uh one two three and four so because
15880
11:51:41,276 --> 11:51:43,500
this drop last while is basically
15881
11:51:43,500 --> 11:51:46,616
basically dropping all the elements all
15882
11:51:46,616 --> 11:51:49,880
the last Elements which contain uh which
15883
11:51:49,880 --> 11:51:53,060
contains I so we have
15884
11:51:53,060 --> 11:51:56,659
six five
15885
11:51:56,756 --> 11:52:00,320
so we have a
15886
11:52:01,020 --> 11:52:03,480
drop last while so we have it that
15887
11:52:03,480 --> 11:52:06,320
contains I
15888
11:52:08,936 --> 11:52:11,040
so it drops all the elements which
15889
11:52:11,040 --> 11:52:13,140
contains I so sorry for what I said
15890
11:52:13,140 --> 11:52:15,180
previously so this way I have here one
15891
11:52:15,180 --> 11:52:18,060
two three and four and uh the five and
15892
11:52:18,060 --> 11:52:20,340
the six are missing because it basically
15893
11:52:20,340 --> 11:52:22,616
drops the old Elements which contains
15894
11:52:22,616 --> 11:52:25,320
the letter i so this is how you can use
15895
11:52:25,320 --> 11:52:27,840
the take and drop the next function that
15896
11:52:27,840 --> 11:52:31,200
you can use is called chunked
15897
11:52:31,200 --> 11:52:33,720
and
15898
11:52:33,720 --> 11:52:35,160
if you want to break a collection
15899
11:52:35,160 --> 11:52:37,916
through parts of a given size then you
15900
11:52:37,916 --> 11:52:40,436
can use the chunked function chunks take
15901
11:52:40,436 --> 11:52:42,720
a single argument the size of the chunk
15902
11:52:42,720 --> 11:52:45,000
and Returns the list of
15903
11:52:45,000 --> 11:52:46,436
[Music]
15904
11:52:46,436 --> 11:52:49,380
list of the given size the first chunk
15905
11:52:49,380 --> 11:52:51,660
starts from the first element and
15906
11:52:51,660 --> 11:52:53,640
contains the size of the elements and
15907
11:52:53,640 --> 11:52:55,916
the second chunk holds the next size
15908
11:52:55,916 --> 11:52:58,080
elements and so on the last chunk maybe
15909
11:52:58,080 --> 11:53:01,680
may have a similar size so if I have
15910
11:53:01,680 --> 11:53:05,840
here let's change this to numbers
15911
11:53:06,000 --> 11:53:11,300
strings and let's type here
15912
11:53:11,936 --> 11:53:13,200
Val
15913
11:53:13,200 --> 11:53:16,039
numbers
15914
11:53:16,680 --> 11:53:18,596
while I was changing that I should use
15915
11:53:18,596 --> 11:53:21,560
refactor so
15916
11:53:23,400 --> 11:53:27,140
let's use a refactor
15917
11:53:27,360 --> 11:53:29,460
rename
15918
11:53:29,460 --> 11:53:32,700
numbers strings so it will be changed in
15919
11:53:32,700 --> 11:53:35,700
all the places we're using that
15920
11:53:35,700 --> 11:53:39,416
and here have numbers and I'm gonna put
15921
11:53:39,416 --> 11:53:42,840
equals and I'll put parentheses
15922
11:53:42,840 --> 11:53:47,276
zero dot dot 13. and I'm gonna put that
15923
11:53:47,276 --> 11:53:50,820
to list to convert this to a list
15924
11:53:50,820 --> 11:53:53,820
and we add a print line to see how this
15925
11:53:53,820 --> 11:53:56,040
chunk function is working so we got the
15926
11:53:56,040 --> 11:53:58,980
print right let's close the console
15927
11:53:58,980 --> 11:54:02,960
and we type here numbers Dot
15928
11:54:03,720 --> 11:54:07,276
Dot chunked
15929
11:54:09,660 --> 11:54:12,116
and would put three so it's gonna chunk
15930
11:54:12,116 --> 11:54:16,520
our elements into three into
15931
11:54:17,700 --> 11:54:19,700
um
15932
11:54:19,800 --> 11:54:23,340
basically elements of three so we have
15933
11:54:23,340 --> 11:54:27,300
zero one two three four five six seven
15934
11:54:27,300 --> 11:54:31,860
eight and they're both on on chunks of
15935
11:54:31,860 --> 11:54:33,360
three elements so this is what this
15936
11:54:33,360 --> 11:54:34,860
chunks is doing
15937
11:54:34,860 --> 11:54:37,680
so this is why I have here this result
15938
11:54:37,680 --> 11:54:40,520
and
15939
11:54:41,400 --> 11:54:43,680
the next function that you're going to
15940
11:54:43,680 --> 11:54:47,276
look at is called windowed and
15941
11:54:47,276 --> 11:54:50,160
and we we can also use a transformation
15942
11:54:50,160 --> 11:54:53,040
on our chunked element so I cannot hear
15943
11:54:53,040 --> 11:54:54,776
a print line
15944
11:54:54,776 --> 11:54:57,860
we type numbers
15945
11:54:59,040 --> 11:55:02,540
Dot chunked
15946
11:55:08,096 --> 11:55:10,916
chunked which we also choose the size
15947
11:55:10,916 --> 11:55:12,416
three
15948
11:55:12,416 --> 11:55:16,020
then you put curly braces and here I'm
15949
11:55:16,020 --> 11:55:19,140
gonna Define how our chunked elements
15950
11:55:19,140 --> 11:55:22,380
are going to be transformed so we put it
15951
11:55:22,380 --> 11:55:24,840
dot
15952
11:55:24,840 --> 11:55:27,480
sum so it's going to sum the chunked
15953
11:55:27,480 --> 11:55:30,180
elements so if I press Ctrl I'll tell
15954
11:55:30,180 --> 11:55:31,860
the format the code and now if I run
15955
11:55:31,860 --> 11:55:33,240
this look what we're going to see in the
15956
11:55:33,240 --> 11:55:35,540
output
15957
11:55:39,740 --> 11:55:44,220
so we get uh you get first the chunked
15958
11:55:44,220 --> 11:55:45,776
elements here
15959
11:55:45,776 --> 11:55:50,520
in entry in three elements and then we
15960
11:55:50,520 --> 11:55:53,276
have our chunked elements but they are
15961
11:55:53,276 --> 11:55:55,520
summed up so we have first three because
15962
11:55:55,520 --> 11:55:58,200
zero plus one plus two is three then we
15963
11:55:58,200 --> 11:56:00,660
have three four five because that is
15964
11:56:00,660 --> 11:56:03,180
twelve don't have seven eight uh seven
15965
11:56:03,180 --> 11:56:04,380
uh
15966
11:56:04,380 --> 11:56:06,416
six seven eight and they have twenty one
15967
11:56:06,416 --> 11:56:08,040
then we have
15968
11:56:08,040 --> 11:56:12,360
uh 10 11 and 9 and we have 30 and so on
15969
11:56:12,360 --> 11:56:17,220
so this is uh how uh you can use the
15970
11:56:17,220 --> 11:56:19,680
chunked function to chunk the elements
15971
11:56:19,680 --> 11:56:21,840
together by a given size and you can
15972
11:56:21,840 --> 11:56:23,936
also use the transformation function to
15973
11:56:23,936 --> 11:56:26,040
do a transformation on them like adding
15974
11:56:26,040 --> 11:56:29,276
them so as you can see we have adding
15975
11:56:29,276 --> 11:56:30,596
them here
15976
11:56:30,596 --> 11:56:32,640
next we're going to look at the next
15977
11:56:32,640 --> 11:56:34,436
function function that you can use and
15978
11:56:34,436 --> 11:56:37,080
that is called the windowed function and
15979
11:56:37,080 --> 11:56:39,720
we can retrieve all possible ranges of
15980
11:56:39,720 --> 11:56:41,460
the collection elements of a given size
15981
11:56:41,460 --> 11:56:43,860
with the window function the function of
15982
11:56:43,860 --> 11:56:46,740
getting them is called windowed it
15983
11:56:46,740 --> 11:56:49,080
returns a list of element ranges that
15984
11:56:49,080 --> 11:56:50,880
you would see if you are looking at the
15985
11:56:50,880 --> 11:56:53,340
collation through a slightly wind off of
15986
11:56:53,340 --> 11:56:56,276
the given size unlike chunked window
15987
11:56:56,276 --> 11:56:59,640
returns element ranges Windows starting
15988
11:56:59,640 --> 11:57:01,980
from each collection element all the all
15989
11:57:01,980 --> 11:57:04,980
the windows are returned as an element
15990
11:57:04,980 --> 11:57:10,460
of a single list so if I have here
15991
11:57:11,096 --> 11:57:13,616
the
15992
11:57:13,616 --> 11:57:16,880
let's create another
15993
11:57:16,980 --> 11:57:20,340
variable here let's call it
15994
11:57:20,340 --> 11:57:24,179
Val numbers
15995
11:57:25,800 --> 11:57:27,300
string
15996
11:57:27,300 --> 11:57:28,860
two
15997
11:57:28,860 --> 11:57:33,200
and we're gonna type here let's say
15998
11:57:33,960 --> 11:57:36,720
or I can assign
15999
11:57:36,720 --> 11:57:39,240
I can assign the number of strings here
16000
11:57:39,240 --> 11:57:42,480
so I'm going to type here number strings
16001
11:57:42,480 --> 11:57:45,000
now what I can do here I I can add a
16002
11:57:45,000 --> 11:57:46,256
print line
16003
11:57:46,256 --> 11:57:52,160
and like I can type numbers string store
16004
11:57:52,380 --> 11:57:54,240
Dot
16005
11:57:54,240 --> 11:57:57,480
windowed and we choose let's say three
16006
11:57:57,480 --> 11:57:59,276
and let's see what we have on the output
16007
11:57:59,276 --> 11:58:02,479
so now if you run this code
16008
11:58:02,640 --> 11:58:05,096
I should have added here a space so
16009
11:58:05,096 --> 11:58:08,096
let's add the print line here
16010
11:58:08,096 --> 11:58:11,000
backslash
16011
11:58:11,936 --> 11:58:14,840
photo selection
16012
11:58:16,860 --> 11:58:19,040
foreign
16013
11:58:24,240 --> 11:58:26,460
so we have one two three then we have
16014
11:58:26,460 --> 11:58:29,220
two three four you have three four five
16015
11:58:29,220 --> 11:58:32,640
and four five six so this is similar to
16016
11:58:32,640 --> 11:58:34,800
junk but window provides more
16017
11:58:34,800 --> 11:58:38,776
flexibility because uh you can
16018
11:58:38,776 --> 11:58:41,400
specify a step which defines a distance
16019
11:58:41,400 --> 11:58:44,040
between the first element and and to a
16020
11:58:44,040 --> 11:58:47,040
DH and windows by default the value is
16021
11:58:47,040 --> 11:58:48,720
one so the result contains Windows
16022
11:58:48,720 --> 11:58:50,276
starting from all elements if you
16023
11:58:50,276 --> 11:58:52,380
increase the step to two you'll receive
16024
11:58:52,380 --> 11:58:55,560
only Windows 30 from the other element
16025
11:58:55,560 --> 11:58:59,340
first third and so on and finally you
16026
11:58:59,340 --> 11:59:00,776
can apply transformation to the return
16027
11:59:00,776 --> 11:59:03,300
ranges right away to do this you provide
16028
11:59:03,300 --> 11:59:05,096
the transformation as a Lambda function
16029
11:59:05,096 --> 11:59:08,160
when calling windows so it is similar to
16030
11:59:08,160 --> 11:59:09,300
chunked
16031
11:59:09,300 --> 11:59:12,180
and um
16032
11:59:12,180 --> 11:59:15,240
so with this I think we end our
16033
11:59:15,240 --> 11:59:16,916
discussion about the thieving collection
16034
11:59:16,916 --> 11:59:20,160
parts and see you in the next video and
16035
11:59:20,160 --> 11:59:22,860
if you find this confusing don't worry
16036
11:59:22,860 --> 11:59:26,116
because it's so confusing for uh for me
16037
11:59:26,116 --> 11:59:29,240
for me and for probably other people
16038
11:59:29,240 --> 11:59:33,540
because it takes time to to get used to
16039
11:59:33,540 --> 11:59:36,300
this and my suggestion for you just play
16040
11:59:36,300 --> 11:59:39,596
with the code change the the parameters
16041
11:59:39,596 --> 11:59:42,720
play with the functions try different uh
16042
11:59:42,720 --> 11:59:45,776
values and so on so see you in the next
16043
11:59:45,776 --> 11:59:47,160
video
16044
11:59:47,160 --> 11:59:48,480
now we're going to look at some
16045
11:59:48,480 --> 11:59:50,276
functions with which you can retrieve
16046
11:59:50,276 --> 11:59:52,916
single elements from our Collections and
16047
11:59:52,916 --> 11:59:55,916
the first one is one which retrieves an
16048
11:59:55,916 --> 11:59:58,800
element by its position so I'm gonna
16049
11:59:58,800 --> 12:00:02,640
create the Val numbers again
16050
12:00:02,640 --> 12:00:06,240
numbers equals to
16051
12:00:06,240 --> 12:00:09,320
list of
16052
12:00:10,916 --> 12:00:12,540
and
16053
12:00:12,540 --> 12:00:15,116
we type here
16054
12:00:15,116 --> 12:00:17,700
one
16055
12:00:17,700 --> 12:00:20,000
two
16056
12:00:20,276 --> 12:00:22,276
three
16057
12:00:23,460 --> 12:00:27,860
and four and also five
16058
12:00:30,840 --> 12:00:32,520
and let's say that I want to get the
16059
12:00:32,520 --> 12:00:34,436
element at position three to do that
16060
12:00:34,436 --> 12:00:37,680
just add here a print line and we type
16061
12:00:37,680 --> 12:00:40,040
numbers
16062
12:00:43,220 --> 12:00:46,880
dot element
16063
12:00:46,880 --> 12:00:49,436
and here you specify the position three
16064
12:00:49,436 --> 12:00:52,560
so zero one two three so we're going to
16065
12:00:52,560 --> 12:00:56,596
get four outputted in the console
16066
12:00:57,240 --> 12:00:59,340
so we get in the output as expected for
16067
12:00:59,340 --> 12:01:01,680
now there are there are also two
16068
12:01:01,680 --> 12:01:03,480
functions for retrieving the first and
16069
12:01:03,480 --> 12:01:05,936
last element and to do that just add
16070
12:01:05,936 --> 12:01:08,340
another print line here
16071
12:01:08,340 --> 12:01:11,640
and type numbers that first
16072
12:01:11,640 --> 12:01:13,256
so this is gonna retrieve the first
16073
12:01:13,256 --> 12:01:15,960
element and Ctrl d
16074
12:01:15,960 --> 12:01:18,480
numbers Dot
16075
12:01:18,480 --> 12:01:20,340
last so
16076
12:01:20,340 --> 12:01:22,380
this is gonna receive the first element
16077
12:01:22,380 --> 12:01:25,020
one and last is gonna retrieve the last
16078
12:01:25,020 --> 12:01:28,399
element five so if you run this
16079
12:01:29,840 --> 12:01:33,116
we get first four from our
16080
12:01:33,116 --> 12:01:35,580
disappearance line then we get first one
16081
12:01:35,580 --> 12:01:37,436
and again the last five
16082
12:01:37,436 --> 12:01:39,680
and we can also edit three
16083
12:01:39,680 --> 12:01:43,256
elements by condition so I can type here
16084
12:01:43,256 --> 12:01:46,560
uh I can type down here or
16085
12:01:46,560 --> 12:01:49,256
or I will type it here
16086
12:01:49,256 --> 12:01:50,820
actually I'm going to type it down here
16087
12:01:50,820 --> 12:01:53,460
it's going to add here a print line
16088
12:01:53,460 --> 12:01:56,416
numbers let's say
16089
12:01:56,416 --> 12:02:00,000
DOT first now I'm going to retrieve by a
16090
12:02:00,000 --> 12:02:01,680
condition so once I first I'm going to
16091
12:02:01,680 --> 12:02:03,840
choose this one with curly braces and
16092
12:02:03,840 --> 12:02:05,756
the condition is that I want to leave
16093
12:02:05,756 --> 12:02:08,040
the first element for which the length
16094
12:02:08,040 --> 12:02:09,480
is greater than 3 so I'm going to type
16095
12:02:09,480 --> 12:02:12,180
here it dot length
16096
12:02:12,180 --> 12:02:15,180
greater than 3. and I'm gonna add I'm
16097
12:02:15,180 --> 12:02:17,936
gonna press Ctrl D because I'm gonna do
16098
12:02:17,936 --> 12:02:20,776
the same thing for the
16099
12:02:20,776 --> 12:02:23,040
by condition so I'm going to type here
16100
12:02:23,040 --> 12:02:25,500
numbers that last but I'm gonna change
16101
12:02:25,500 --> 12:02:28,256
the condition I'm gonna change it to
16102
12:02:28,256 --> 12:02:33,540
eat that starts with the it that starts
16103
12:02:33,540 --> 12:02:35,520
with
16104
12:02:35,520 --> 12:02:37,560
the letter f
16105
12:02:37,560 --> 12:02:39,416
so now we're retrieving the first
16106
12:02:39,416 --> 12:02:42,300
element which is greater than three and
16107
12:02:42,300 --> 12:02:44,640
next retrieving the last element which
16108
12:02:44,640 --> 12:02:46,860
starts with the letter f
16109
12:02:46,860 --> 12:02:50,300
so now if you run this
16110
12:02:51,180 --> 12:02:53,820
we get 3 and 5 because 3 is the first
16111
12:02:53,820 --> 12:02:56,276
element which is has a length greater
16112
12:02:56,276 --> 12:02:58,500
than three and can get five because this
16113
12:02:58,500 --> 12:03:01,320
is the one the last element which starts
16114
12:03:01,320 --> 12:03:05,220
with the letter F and if you can other
16115
12:03:05,220 --> 12:03:08,096
space here if you want to make things
16116
12:03:08,096 --> 12:03:09,720
more clear so we're gonna add the print
16117
12:03:09,720 --> 12:03:12,080
line quotation marks
16118
12:03:12,080 --> 12:03:13,800
backslashing
16119
12:03:13,800 --> 12:03:15,360
actually
16120
12:03:15,360 --> 12:03:17,580
forward selection
16121
12:03:17,580 --> 12:03:20,660
so if you run this
16122
12:03:26,040 --> 12:03:28,500
we get three and five output today in
16123
12:03:28,500 --> 12:03:30,416
the console
16124
12:03:30,416 --> 12:03:32,220
and we can also retrieve a random
16125
12:03:32,220 --> 12:03:34,500
element from our numbers so I can type
16126
12:03:34,500 --> 12:03:36,416
here print line
16127
12:03:36,416 --> 12:03:37,640
numbers
16128
12:03:37,640 --> 12:03:40,860
dot random and this is going to return a
16129
12:03:40,860 --> 12:03:42,480
random number from our list of numbers
16130
12:03:42,480 --> 12:03:45,500
so if I run this
16131
12:03:49,160 --> 12:03:53,960
we get one and if I run this again
16132
12:03:55,200 --> 12:03:57,360
get four so every time you run this
16133
12:03:57,360 --> 12:03:59,520
you'll get a random number
16134
12:03:59,520 --> 12:04:01,380
and you can also check if the list is
16135
12:04:01,380 --> 12:04:04,680
empty so you can type here print line
16136
12:04:04,680 --> 12:04:07,220
numbers
16137
12:04:08,220 --> 12:04:10,500
that is empty then that is going to
16138
12:04:10,500 --> 12:04:14,040
return true if the list is empty and if
16139
12:04:14,040 --> 12:04:15,480
the list is not empty it's going to
16140
12:04:15,480 --> 12:04:19,220
return false so if you run this
16141
12:04:22,256 --> 12:04:25,200
we get false so this is our discussion
16142
12:04:25,200 --> 12:04:27,540
about how to retrieve single elements
16143
12:04:27,540 --> 12:04:29,700
see in the next video now it's time to
16144
12:04:29,700 --> 12:04:31,380
start a discussion about aggregate
16145
12:04:31,380 --> 12:04:33,720
operations so kotlin collections
16146
12:04:33,720 --> 12:04:35,640
contains functions for commonly used
16147
12:04:35,640 --> 12:04:37,680
aggregate operations
16148
12:04:37,680 --> 12:04:39,720
and those are operations that return a
16149
12:04:39,720 --> 12:04:41,400
single value based on the collection
16150
12:04:41,400 --> 12:04:44,040
content most of them are well known and
16151
12:04:44,040 --> 12:04:45,960
work in the same way as they do in other
16152
12:04:45,960 --> 12:04:47,936
programming languages
16153
12:04:47,936 --> 12:04:51,060
and we have minor null and Max ornal
16154
12:04:51,060 --> 12:04:52,680
which return the smallest and the
16155
12:04:52,680 --> 12:04:54,776
largest element respectively and on
16156
12:04:54,776 --> 12:04:57,060
empty collections the return null then
16157
12:04:57,060 --> 12:04:58,800
we have average which Returns the
16158
12:04:58,800 --> 12:05:00,000
average value of elements in the
16159
12:05:00,000 --> 12:05:01,740
collection then we have sum which
16160
12:05:01,740 --> 12:05:03,180
Returns the sum of elements in the
16161
12:05:03,180 --> 12:05:04,740
collection of numbers
16162
12:05:04,740 --> 12:05:06,776
and have count which Returns the number
16163
12:05:06,776 --> 12:05:09,840
of elements in our collections so please
16164
12:05:09,840 --> 12:05:13,340
declare a list numbers
16165
12:05:13,436 --> 12:05:16,220
list of
16166
12:05:19,500 --> 12:05:22,200
let's put so let's put some numbers here
16167
12:05:22,200 --> 12:05:25,460
like let's say six
16168
12:05:26,460 --> 12:05:28,616
then
16169
12:05:28,616 --> 12:05:30,416
14
16170
12:05:30,416 --> 12:05:33,255
4
16171
12:05:33,916 --> 12:05:35,880
let's say
16172
12:05:35,880 --> 12:05:37,680
100
16173
12:05:37,680 --> 12:05:40,800
now let's press Ctrl format the code and
16174
12:05:40,800 --> 12:05:42,900
let's add the print line here let's put
16175
12:05:42,900 --> 12:05:45,660
quotation marks and let's type the sum
16176
12:05:45,660 --> 12:05:47,936
is
16177
12:05:47,936 --> 12:05:50,776
dollar sign
16178
12:05:55,680 --> 12:05:59,520
and the type here numbers
16179
12:05:59,520 --> 12:06:02,539
dot sum
16180
12:06:02,880 --> 12:06:04,916
numbers
16181
12:06:04,916 --> 12:06:07,800
that's sum
16182
12:06:07,800 --> 12:06:10,436
all right and uh
16183
12:06:10,436 --> 12:06:13,616
I'll add another print line and now this
16184
12:06:13,616 --> 12:06:16,256
is gonna say
16185
12:06:16,256 --> 12:06:17,820
the
16186
12:06:17,820 --> 12:06:20,880
count is
16187
12:06:20,880 --> 12:06:23,700
again dollar sign
16188
12:06:23,700 --> 12:06:26,276
numbers
16189
12:06:26,276 --> 12:06:28,740
dot count
16190
12:06:28,740 --> 12:06:31,820
another print line
16191
12:06:34,700 --> 12:06:38,040
the average
16192
12:06:38,040 --> 12:06:40,680
is
16193
12:06:40,680 --> 12:06:43,560
again dollar sign
16194
12:06:43,560 --> 12:06:46,916
numbers that average
16195
12:06:46,916 --> 12:06:50,180
another print line
16196
12:06:51,116 --> 12:06:54,840
and now I'm going to say the
16197
12:06:54,840 --> 12:06:58,916
max value is
16198
12:06:58,916 --> 12:07:02,400
again dollar sign numbers
16199
12:07:02,400 --> 12:07:05,480
dot marks
16200
12:07:05,880 --> 12:07:09,360
numbers Dot
16201
12:07:10,680 --> 12:07:13,500
marks
16202
12:07:13,500 --> 12:07:16,020
or null
16203
12:07:16,020 --> 12:07:20,040
and I'll print line
16204
12:07:20,040 --> 12:07:24,960
the mean value is
16205
12:07:25,140 --> 12:07:30,480
dollar sign numbers that mean
16206
12:07:30,480 --> 12:07:33,860
or null
16207
12:07:33,960 --> 12:07:36,480
now if you run this code you're gonna
16208
12:07:36,480 --> 12:07:38,300
see in the output the corresponding
16209
12:07:38,300 --> 12:07:40,616
values for those expressions that we
16210
12:07:40,616 --> 12:07:43,560
have here so again the sum is 134 the
16211
12:07:43,560 --> 12:07:45,960
count is 5 because we have five elements
16212
12:07:45,960 --> 12:07:50,220
here the average is 26.28 the maximum
16213
12:07:50,220 --> 12:07:53,220
value is 100 and the minimum value is 4.
16214
12:07:53,220 --> 12:07:54,540
so this is how you can use those
16215
12:07:54,540 --> 12:07:58,140
aggregate operations and agree and those
16216
12:07:58,140 --> 12:08:00,436
aggregate um
16217
12:08:00,436 --> 12:08:04,200
functions and beside the regular sum
16218
12:08:04,200 --> 12:08:06,300
there is an advantage so much summation
16219
12:08:06,300 --> 12:08:09,116
function called sum of that takes a
16220
12:08:09,116 --> 12:08:11,160
selected function and Returns the sum
16221
12:08:11,160 --> 12:08:13,320
sum of its application to all collection
16222
12:08:13,320 --> 12:08:15,300
elements and let me show you how this
16223
12:08:15,300 --> 12:08:18,776
works so you just type here
16224
12:08:18,776 --> 12:08:22,520
so we type here print line
16225
12:08:23,220 --> 12:08:26,300
the sum is
16226
12:08:28,200 --> 12:08:30,960
dollar sign
16227
12:08:30,960 --> 12:08:32,700
numbers
16228
12:08:32,700 --> 12:08:35,640
dot sum of
16229
12:08:35,640 --> 12:08:37,200
we select this one
16230
12:08:37,200 --> 12:08:39,240
and here we type it
16231
12:08:39,240 --> 12:08:42,180
times two
16232
12:08:42,180 --> 12:08:43,980
now if you run this what it's going to
16233
12:08:43,980 --> 12:08:46,380
do is it's gonna sum all the numbers
16234
12:08:46,380 --> 12:08:48,060
like the first function that we have
16235
12:08:48,060 --> 12:08:51,416
here so it's going to sum 134 and it's
16236
12:08:51,416 --> 12:08:53,820
multi is going to multiply that that
16237
12:08:53,820 --> 12:08:57,680
number by two so if you run this
16238
12:09:00,240 --> 12:09:05,116
we get 260 which is 132 times
16239
12:09:05,116 --> 12:09:07,860
134 times 2. so this is how you can use
16240
12:09:07,860 --> 12:09:10,436
the sum off with this uh
16241
12:09:10,436 --> 12:09:12,560
transformation which is gonna basically
16242
12:09:12,560 --> 12:09:16,380
multiply the list with a number with the
16243
12:09:16,380 --> 12:09:18,480
with the sum of the list with the number
16244
12:09:18,480 --> 12:09:20,040
so this is our discussion about
16245
12:09:20,040 --> 12:09:22,320
aggregate operations and see you in the
16246
12:09:22,320 --> 12:09:24,300
next video now it's time to start a
16247
12:09:24,300 --> 12:09:26,160
discussion about ordering and in this
16248
12:09:26,160 --> 12:09:27,540
video we're going to look at comparable
16249
12:09:27,540 --> 12:09:30,416
and add comparator interfaces
16250
12:09:30,416 --> 12:09:33,480
so first I'm going to create a list of
16251
12:09:33,480 --> 12:09:35,776
numbers
16252
12:09:36,000 --> 12:09:37,560
and
16253
12:09:37,560 --> 12:09:39,596
I'm not gonna
16254
12:09:39,596 --> 12:09:41,820
I'm gonna get a multiple list so
16255
12:09:41,820 --> 12:09:44,640
Multiple List off and I'm gonna
16256
12:09:44,640 --> 12:09:46,740
Define some numbers here not in a
16257
12:09:46,740 --> 12:09:48,480
specific order
16258
12:09:48,480 --> 12:09:52,980
so we type two let's say five
16259
12:09:52,980 --> 12:09:54,360
one
16260
12:09:54,360 --> 12:09:56,096
let's say
16261
12:09:56,096 --> 12:09:58,436
40.
16262
12:09:58,436 --> 12:10:00,720
20.
16263
12:10:00,720 --> 12:10:04,220
100 let's say
16264
12:10:04,700 --> 12:10:08,480
60. now what if I want to
16265
12:10:08,480 --> 12:10:12,480
order the numbers that I have inside our
16266
12:10:12,480 --> 12:10:14,756
list numbers to do that you just need
16267
12:10:14,756 --> 12:10:17,520
you type numbers
16268
12:10:17,520 --> 12:10:19,140
Dot
16269
12:10:19,140 --> 12:10:21,960
and there is a function called sorted so
16270
12:10:21,960 --> 12:10:25,080
if I type number that's sorted now our
16271
12:10:25,080 --> 12:10:27,416
number on our numbers are going to be
16272
12:10:27,416 --> 12:10:30,900
sorted so if you type here for each
16273
12:10:30,900 --> 12:10:33,060
put in the line
16274
12:10:33,060 --> 12:10:35,659
it
16275
12:10:36,060 --> 12:10:38,820
so now if you run this
16276
12:10:38,820 --> 12:10:42,116
even though we didn't type our numbers
16277
12:10:42,116 --> 12:10:45,416
in ascending order there will be sorted
16278
12:10:45,416 --> 12:10:47,096
and as you can see they are sorted we
16279
12:10:47,096 --> 12:10:48,596
have one two
16280
12:10:48,596 --> 12:10:51,416
five twenty forty six three and one
16281
12:10:51,416 --> 12:10:54,776
hundred so they are sorted by this
16282
12:10:54,776 --> 12:10:56,580
function called sorted and why this is
16283
12:10:56,580 --> 12:10:57,840
working
16284
12:10:57,840 --> 12:10:59,400
it's working because if you click on
16285
12:10:59,400 --> 12:11:02,520
this sorted function
16286
12:11:02,520 --> 12:11:03,900
what a
16287
12:11:03,900 --> 12:11:07,436
actually if you type here let's say
16288
12:11:07,436 --> 12:11:11,240
let's say that I put the type here
16289
12:11:11,276 --> 12:11:12,960
and
16290
12:11:12,960 --> 12:11:16,340
if I click on the type
16291
12:11:17,520 --> 12:11:21,416
this tie this type integer this class is
16292
12:11:21,416 --> 12:11:22,860
implementing this thing called
16293
12:11:22,860 --> 12:11:25,256
comparable and this comparable has a
16294
12:11:25,256 --> 12:11:28,380
function called compare to so when we
16295
12:11:28,380 --> 12:11:30,560
actually type numbers that are sorted
16296
12:11:30,560 --> 12:11:33,596
what what internally was happening is
16297
12:11:33,596 --> 12:11:36,300
that the integer class was using the
16298
12:11:36,300 --> 12:11:39,916
compared to function which is which is a
16299
12:11:39,916 --> 12:11:43,080
function which you want to you need to
16300
12:11:43,080 --> 12:11:44,460
implement if you implement the
16301
12:11:44,460 --> 12:11:48,300
comparable interface to compare if
16302
12:11:48,300 --> 12:11:51,776
to compare our numbers and to based on
16303
12:11:51,776 --> 12:11:54,960
that to to sub them to to sort them
16304
12:11:54,960 --> 12:11:59,580
and now let's say that I have or let's
16305
12:11:59,580 --> 12:12:03,116
say that I have a data class
16306
12:12:03,116 --> 12:12:05,936
called laptop
16307
12:12:05,936 --> 12:12:07,860
and it's gonna have a
16308
12:12:07,860 --> 12:12:10,680
variable called brand it's going to be
16309
12:12:10,680 --> 12:12:13,320
of type string
16310
12:12:13,320 --> 12:12:16,080
another variable called here this could
16311
12:12:16,080 --> 12:12:19,640
be the the year one one this specific
16312
12:12:19,640 --> 12:12:24,140
version was released on int
16313
12:12:24,560 --> 12:12:26,936
also need
16314
12:12:26,936 --> 12:12:28,860
and the wall
16315
12:12:28,860 --> 12:12:31,436
let's say um
16316
12:12:31,436 --> 12:12:33,960
price also an integer
16317
12:12:33,960 --> 12:12:38,936
right now if I create here a list of
16318
12:12:38,936 --> 12:12:42,480
laptops laptops
16319
12:12:43,200 --> 12:12:46,740
equals to Multiple List of
16320
12:12:46,740 --> 12:12:50,160
and I type here laptop
16321
12:12:50,160 --> 12:12:54,060
let's say that I type here Dell
16322
12:12:54,060 --> 12:12:58,860
and I put the year let's say 2021
16323
12:12:58,860 --> 12:13:01,616
the ram let's say that is four gigabytes
16324
12:13:01,616 --> 12:13:04,220
RAM and the price will say that is
16325
12:13:04,220 --> 12:13:06,540
600 okay
16326
12:13:06,540 --> 12:13:08,520
comma I'm going to create another laptop
16327
12:13:08,520 --> 12:13:09,416
here
16328
12:13:09,416 --> 12:13:11,820
so another object I'm gonna type here
16329
12:13:11,820 --> 12:13:13,436
let's call it
16330
12:13:13,436 --> 12:13:15,660
Acer
16331
12:13:15,660 --> 12:13:19,080
is going to be let's say the year um
16332
12:13:19,080 --> 12:13:22,160
let's put 20
16333
12:13:22,200 --> 12:13:24,540
20.
16334
12:13:24,540 --> 12:13:27,360
the para is let's say the ram let's say
16335
12:13:27,360 --> 12:13:30,240
that is eight gigabytes Ram
16336
12:13:30,240 --> 12:13:34,256
and the prices say that it's 100 dollars
16337
12:13:34,256 --> 12:13:37,020
comma and let's create another laptop
16338
12:13:37,020 --> 12:13:39,300
object so we type laptop
16339
12:13:39,300 --> 12:13:42,596
and create an Apple laptop which is
16340
12:13:42,596 --> 12:13:44,096
which you're gonna for which you're
16341
12:13:44,096 --> 12:13:45,660
gonna choose the
16342
12:13:45,660 --> 12:13:47,756
the year let's say
16343
12:13:47,756 --> 12:13:49,756
um
16344
12:13:51,300 --> 12:13:54,020
Tony
16345
12:13:54,540 --> 12:13:56,276
2020
16346
12:13:56,276 --> 12:13:57,900
2022
16347
12:13:57,900 --> 12:14:00,060
and
16348
12:14:00,060 --> 12:14:02,360
for the ram let's say they're choose 16
16349
12:14:02,360 --> 12:14:06,540
gigabytes RAM and 1 000 for the price
16350
12:14:06,540 --> 12:14:10,680
now if I type here laptops
16351
12:14:10,680 --> 12:14:12,480
that sorted let's say that I want to
16352
12:14:12,480 --> 12:14:15,540
sort the laptops as if you can see while
16353
12:14:15,540 --> 12:14:18,300
on time pink the IntelliJ is not giving
16354
12:14:18,300 --> 12:14:21,416
me any hints to have this function
16355
12:14:21,416 --> 12:14:23,240
sorted
16356
12:14:23,240 --> 12:14:26,460
as a true fill in that function because
16357
12:14:26,460 --> 12:14:30,180
that function is not working on our
16358
12:14:30,180 --> 12:14:32,880
laptops list and why why is happening
16359
12:14:32,880 --> 12:14:34,020
that
16360
12:14:34,020 --> 12:14:36,980
that is happening because the IntelliJ
16361
12:14:36,980 --> 12:14:38,936
doesn't know
16362
12:14:38,936 --> 12:14:41,880
how we want to sort this laptop list
16363
12:14:41,880 --> 12:14:44,160
that we have here because you want you
16364
12:14:44,160 --> 12:14:45,720
can sort it in many ways you can sort it
16365
12:14:45,720 --> 12:14:48,116
by the name you can sort it by the year
16366
12:14:48,116 --> 12:14:49,860
you can sort it by Dynamic and sort it
16367
12:14:49,860 --> 12:14:52,916
by the price so when you say laptops are
16368
12:14:52,916 --> 12:14:56,520
sorted the IntelliJ is not it's not
16369
12:14:56,520 --> 12:14:58,200
annoying
16370
12:14:58,200 --> 12:15:00,480
what do you mean because previously it
16371
12:15:00,480 --> 12:15:03,180
was working with our integers or and
16372
12:15:03,180 --> 12:15:05,580
without integer class because the
16373
12:15:05,580 --> 12:15:09,060
integer class if you type if you put the
16374
12:15:09,060 --> 12:15:10,380
type here
16375
12:15:10,380 --> 12:15:12,720
back integer
16376
12:15:12,720 --> 12:15:15,540
the integer class or the int class what
16377
12:15:15,540 --> 12:15:17,700
I was doing actually it was inheriting
16378
12:15:17,700 --> 12:15:20,756
from this comparable interface and it
16379
12:15:20,756 --> 12:15:22,616
was implementing the compared to
16380
12:15:22,616 --> 12:15:26,096
function which is used when you call the
16381
12:15:26,096 --> 12:15:29,096
sorted function and inside this
16382
12:15:29,096 --> 12:15:32,820
comparative function it has defined its
16383
12:15:32,820 --> 12:15:35,820
own Logic for comparing integers for
16384
12:15:35,820 --> 12:15:38,460
comparing whole numbers and what we need
16385
12:15:38,460 --> 12:15:40,740
to do in order to be able to
16386
12:15:40,740 --> 12:15:43,436
sort our list of laptops is we need to
16387
12:15:43,436 --> 12:15:45,900
implement the comparable interface and
16388
12:15:45,900 --> 12:15:48,720
you need to Define our own logic which
16389
12:15:48,720 --> 12:15:50,360
is going to determine
16390
12:15:50,360 --> 12:15:53,276
how our list is going to be awarded by
16391
12:15:53,276 --> 12:15:56,700
by which uh property by the Year by the
16392
12:15:56,700 --> 12:15:58,380
ram by the price so this is what you
16393
12:15:58,380 --> 12:16:00,960
need to do and to do that just put here
16394
12:16:00,960 --> 12:16:03,480
a colon and we'll implement the
16395
12:16:03,480 --> 12:16:05,460
interface comparable so we type it
16396
12:16:05,460 --> 12:16:07,436
comparable and we have a suggestion you
16397
12:16:07,436 --> 12:16:09,180
should choose this one
16398
12:16:09,180 --> 12:16:12,596
we put angle brackets and here we type
16399
12:16:12,596 --> 12:16:14,700
laptop because you want to implement
16400
12:16:14,700 --> 12:16:17,640
this we want to compare laptops so
16401
12:16:17,640 --> 12:16:19,740
laptops so this swipe water level there
16402
12:16:19,740 --> 12:16:21,480
then you put curly braces and now we
16403
12:16:21,480 --> 12:16:24,960
need to implement the function so click
16404
12:16:24,960 --> 12:16:27,180
on that it says Implement members you
16405
12:16:27,180 --> 12:16:30,060
have the compare to function now
16406
12:16:30,060 --> 12:16:32,700
you don't need to Define our logic which
16407
12:16:32,700 --> 12:16:38,400
is gonna determine what makes uh uh zero
16408
12:16:38,400 --> 12:16:40,500
the final logic which is going to
16409
12:16:40,500 --> 12:16:42,480
determine how our elements is going to
16410
12:16:42,480 --> 12:16:44,880
be watered by what criteria and I'm
16411
12:16:44,880 --> 12:16:47,880
gonna order the the laptops first by the
16412
12:16:47,880 --> 12:16:50,936
price and to do that to do that just
16413
12:16:50,936 --> 12:16:55,080
type if and to type this and we type in
16414
12:16:55,080 --> 12:16:56,640
this because this is going to be called
16415
12:16:56,640 --> 12:17:00,480
on an inst an instance of our laptop
16416
12:17:00,480 --> 12:17:03,596
left of class is going to call the
16417
12:17:03,596 --> 12:17:05,820
compared to function and it's going to
16418
12:17:05,820 --> 12:17:08,640
compare with another argument so if this
16419
12:17:08,640 --> 12:17:10,740
dot price we say here
16420
12:17:10,740 --> 12:17:13,560
is greater than other
16421
12:17:13,560 --> 12:17:15,000
that price
16422
12:17:15,000 --> 12:17:18,116
we we return as positive number so we
16423
12:17:18,116 --> 12:17:22,140
type return one and a positive Value
16424
12:17:22,140 --> 12:17:25,200
Plus simply shows that the receiver
16425
12:17:25,200 --> 12:17:28,020
object so the objects
16426
12:17:28,020 --> 12:17:32,820
this object is greater than the the uh
16427
12:17:32,820 --> 12:17:35,460
the object which was passed so this is
16428
12:17:35,460 --> 12:17:39,360
what this means this one else lcf is the
16429
12:17:39,360 --> 12:17:42,660
receiver object is not so if this dot
16430
12:17:42,660 --> 12:17:45,776
price is less than other that price
16431
12:17:45,776 --> 12:17:48,596
let's say then gonna return
16432
12:17:48,596 --> 12:17:51,180
-1 a negative number so I'm going to put
16433
12:17:51,180 --> 12:17:53,880
here minus one and what this basically
16434
12:17:53,880 --> 12:17:55,560
logic is I'm just going to swap them so
16435
12:17:55,560 --> 12:17:56,936
it's going to swap the elements based on
16436
12:17:56,936 --> 12:18:00,060
this logic else I'm going to return 0
16437
12:18:00,060 --> 12:18:01,980
and that is going to mean that they are
16438
12:18:01,980 --> 12:18:05,340
equal so if you type here return 0. and
16439
12:18:05,340 --> 12:18:07,200
if you click here it says that you can
16440
12:18:07,200 --> 12:18:09,540
write this in a different way so if you
16441
12:18:09,540 --> 12:18:11,340
click on this
16442
12:18:11,340 --> 12:18:13,500
and let's add the print line to see that
16443
12:18:13,500 --> 12:18:17,160
our if if uh are called so if I type
16444
12:18:17,160 --> 12:18:19,020
here print line
16445
12:18:19,020 --> 12:18:22,096
I'm gonna say
16446
12:18:24,660 --> 12:18:28,936
in if statement
16447
12:18:29,756 --> 12:18:32,540
dollar sign
16448
12:18:33,116 --> 12:18:36,776
actually let's type swapping
16449
12:18:36,776 --> 12:18:39,000
dollar sign
16450
12:18:39,000 --> 12:18:43,276
this dot brand name
16451
12:18:45,540 --> 12:18:46,740
with
16452
12:18:46,740 --> 12:18:48,416
dollar sign
16453
12:18:48,416 --> 12:18:50,276
other
16454
12:18:50,276 --> 12:18:53,096
dot brand
16455
12:18:53,096 --> 12:18:55,140
so let's add this print line also in our
16456
12:18:55,140 --> 12:18:58,020
lcf
16457
12:18:58,020 --> 12:19:00,300
and I'm not gonna add in inside the ears
16458
12:19:00,300 --> 12:19:02,400
because our we don't have Elements which
16459
12:19:02,400 --> 12:19:03,776
are equal but
16460
12:19:03,776 --> 12:19:06,360
you can add that if you want now if I
16461
12:19:06,360 --> 12:19:09,840
type laptops that's sorted now I have a
16462
12:19:09,840 --> 12:19:14,580
suggestion and if I type for each
16463
12:19:14,580 --> 12:19:18,900
print line it so print the element
16464
12:19:18,900 --> 12:19:21,116
now if you run this look you're gonna
16465
12:19:21,116 --> 12:19:25,020
see the elements sorted and uh ascending
16466
12:19:25,020 --> 12:19:28,200
order by their uh their price so you see
16467
12:19:28,200 --> 12:19:31,500
that we have first the brand the brand
16468
12:19:31,500 --> 12:19:35,460
name Dell and we have price 600 then we
16469
12:19:35,460 --> 12:19:36,116
have
16470
12:19:36,116 --> 12:19:39,900
the price uh you have laptop Acer and we
16471
12:19:39,900 --> 12:19:41,880
have eight hundred dollars and have left
16472
12:19:41,880 --> 12:19:44,276
to apoll the brand and you have the
16473
12:19:44,276 --> 12:19:45,840
price one thousand dollars so the
16474
12:19:45,840 --> 12:19:49,020
nearest so now they are sorted using our
16475
12:19:49,020 --> 12:19:51,660
own logic which was defined inside this
16476
12:19:51,660 --> 12:19:55,140
comparable interface and which was
16477
12:19:55,140 --> 12:19:58,500
defined inside the which was defined by
16478
12:19:58,500 --> 12:20:00,776
this comparable interface and which was
16479
12:20:00,776 --> 12:20:02,640
defined inside this function compared to
16480
12:20:02,640 --> 12:20:05,640
that we've overrided inside our client
16481
12:20:05,640 --> 12:20:07,916
and as you can see
16482
12:20:07,916 --> 12:20:11,400
if uh you look you see here that he says
16483
12:20:11,400 --> 12:20:13,980
if an if statement swapping Acer window
16484
12:20:13,980 --> 12:20:16,560
so the elements are swapped based on our
16485
12:20:16,560 --> 12:20:19,256
road in if statements it says swapping
16486
12:20:19,256 --> 12:20:21,960
apple with Acer so they are they are
16487
12:20:21,960 --> 12:20:24,300
swapped based on our logic
16488
12:20:24,300 --> 12:20:25,916
so this is how you can use the
16489
12:20:25,916 --> 12:20:28,436
comparable uh interface but what if I
16490
12:20:28,436 --> 12:20:30,960
want now to compare by the
16491
12:20:30,960 --> 12:20:34,500
let's say uh RAM what can I do I can't
16492
12:20:34,500 --> 12:20:37,140
implement the comparable interface again
16493
12:20:37,140 --> 12:20:40,820
here and I can't uh
16494
12:20:40,860 --> 12:20:43,980
I I can I can add the that property here
16495
12:20:43,980 --> 12:20:45,900
but that is gonna conflict with my logic
16496
12:20:45,900 --> 12:20:49,080
with for the price so what can we do and
16497
12:20:49,080 --> 12:20:51,480
here comes into action comparator
16498
12:20:51,480 --> 12:20:54,720
with comparator you can compare your uh
16499
12:20:54,720 --> 12:20:58,616
your list you can you can uh com you can
16500
12:20:58,616 --> 12:21:00,840
order actually I should say you can sort
16501
12:21:00,840 --> 12:21:04,740
your list using multiple properties so
16502
12:21:04,740 --> 12:21:07,860
you can order it by the uh brand name
16503
12:21:07,860 --> 12:21:09,776
you can order by the year we can already
16504
12:21:09,776 --> 12:21:11,220
buy the ram you can already buy the
16505
12:21:11,220 --> 12:21:14,276
price and with compare because with the
16506
12:21:14,276 --> 12:21:15,776
comparable interface
16507
12:21:15,776 --> 12:21:19,500
we can do that just only by one property
16508
12:21:19,500 --> 12:21:23,400
and that is pretty uh inflexible and
16509
12:21:23,400 --> 12:21:26,880
it's not what you want and how how to
16510
12:21:26,880 --> 12:21:30,116
use this comparator to use the
16511
12:21:30,116 --> 12:21:32,400
comparator we just type here
16512
12:21:32,400 --> 12:21:34,800
class
16513
12:21:34,800 --> 12:21:36,860
I'm gonna call it
16514
12:21:36,860 --> 12:21:41,460
comparator let's say Ram
16515
12:21:41,460 --> 12:21:45,180
and it's gonna extend
16516
12:21:45,180 --> 12:21:48,360
from comparator so type comparator and
16517
12:21:48,360 --> 12:21:50,520
you have the type and for the type here
16518
12:21:50,520 --> 12:21:52,680
we want to compare laptop so you type
16519
12:21:52,680 --> 12:21:54,240
laptop here
16520
12:21:54,240 --> 12:21:56,880
angle brackets curly braces and press
16521
12:21:56,880 --> 12:21:59,115
enter
16522
12:22:00,060 --> 12:22:02,640
and now we need to override
16523
12:22:02,640 --> 12:22:05,040
the
16524
12:22:05,040 --> 12:22:07,680
compare function inside our class so you
16525
12:22:07,680 --> 12:22:09,720
override that function and here again
16526
12:22:09,720 --> 12:22:13,936
let's change those to laptop one
16527
12:22:15,960 --> 12:22:18,116
so no now we have laptop one and laptop
16528
12:22:18,116 --> 12:22:20,416
two
16529
12:22:20,936 --> 12:22:23,900
and we Define
16530
12:22:24,060 --> 12:22:28,256
the same logic so we type here uh
16531
12:22:28,256 --> 12:22:30,240
return
16532
12:22:30,240 --> 12:22:32,580
if because you need to return an integer
16533
12:22:32,580 --> 12:22:34,080
if
16534
12:22:34,080 --> 12:22:36,900
this dot Ram
16535
12:22:36,900 --> 12:22:40,256
actually not this that ram laptop
16536
12:22:40,256 --> 12:22:43,080
laptop1.com
16537
12:22:43,080 --> 12:22:46,560
is greater than laptop 2.
16538
12:22:46,560 --> 12:22:48,776
Ram
16539
12:22:48,776 --> 12:22:51,000
and we need to delete this nullables
16540
12:22:51,000 --> 12:22:52,740
because you need to
16541
12:22:52,740 --> 12:22:55,080
have some errors there
16542
12:22:55,080 --> 12:23:01,640
and put curly braces return one else if
16543
12:23:03,480 --> 12:23:06,660
else if laptop
16544
12:23:06,660 --> 12:23:09,480
one dot Ram
16545
12:23:09,480 --> 12:23:13,880
is less than laptop 2.
16546
12:23:13,880 --> 12:23:19,740
laptop two dot Ram then gonna return I'm
16547
12:23:19,740 --> 12:23:21,900
gonna put curly braces I'm gonna type
16548
12:23:21,900 --> 12:23:22,820
return
16549
12:23:22,820 --> 12:23:25,860
minus one else
16550
12:23:25,860 --> 12:23:28,800
we're gonna return
16551
12:23:28,800 --> 12:23:33,300
zero now if I want to now to compare our
16552
12:23:33,300 --> 12:23:35,820
let's add the space here now I want to
16553
12:23:35,820 --> 12:23:38,580
sort our elements based on the RAM and
16554
12:23:38,580 --> 12:23:41,756
another print line here to add a space
16555
12:23:41,756 --> 12:23:44,936
so I'm gonna have the biggest photos in
16556
12:23:44,936 --> 12:23:47,220
also here one
16557
12:23:47,220 --> 12:23:50,580
so go quickly space it here
16558
12:23:50,580 --> 12:23:53,416
should be print
16559
12:23:55,616 --> 12:23:58,520
and now what we can do is we type
16560
12:23:58,520 --> 12:24:01,320
laptops Dot
16561
12:24:01,320 --> 12:24:02,840
and we type here
16562
12:24:02,840 --> 12:24:06,060
sorted with and which is we choose this
16563
12:24:06,060 --> 12:24:08,640
one which has the parameter Define a
16564
12:24:08,640 --> 12:24:10,500
comparator and we're going to pass our
16565
12:24:10,500 --> 12:24:12,776
comparator that we defined here with the
16566
12:24:12,776 --> 12:24:14,820
comparative function so I'm gonna pause
16567
12:24:14,820 --> 12:24:17,276
I'm going to type sorted with and we
16568
12:24:17,276 --> 12:24:20,276
type comparator
16569
12:24:20,276 --> 12:24:22,220
foreign
16570
12:24:22,220 --> 12:24:24,540
our class
16571
12:24:24,540 --> 12:24:27,256
and
16572
12:24:28,020 --> 12:24:30,616
create an instance and within the type
16573
12:24:30,616 --> 12:24:34,800
dot for each now this one
16574
12:24:34,800 --> 12:24:37,560
for each print line
16575
12:24:37,560 --> 12:24:38,640
it
16576
12:24:38,640 --> 12:24:40,500
so now if you run this you're gonna see
16577
12:24:40,500 --> 12:24:44,640
them sorted by their border Ram
16578
12:24:44,640 --> 12:24:47,276
so if you run this
16579
12:24:47,276 --> 12:24:52,579
as you can see uh if you look at the
16580
12:24:52,800 --> 12:24:55,200
if you look at the first there are they
16581
12:24:55,200 --> 12:24:57,300
are First Source did by their price we
16582
12:24:57,300 --> 12:25:03,116
have 68 800 and 1000 dollars and then we
16583
12:25:03,116 --> 12:25:05,340
have them sorted by the ram so we have
16584
12:25:05,340 --> 12:25:08,040
four for the four which is the dell and
16585
12:25:08,040 --> 12:25:10,800
then we have the Acer which is uh eight
16586
12:25:10,800 --> 12:25:12,416
gigabytes around and you have the Apple
16587
12:25:12,416 --> 12:25:15,840
which is 16 and uh if you look uh
16588
12:25:15,840 --> 12:25:18,116
actually they are in this order in the
16589
12:25:18,116 --> 12:25:20,340
list so let's change this to to let's
16590
12:25:20,340 --> 12:25:24,060
say to 16 and these two to wait
16591
12:25:24,060 --> 12:25:25,140
to
16592
12:25:25,140 --> 12:25:27,300
to make it more clear so let's run this
16593
12:25:27,300 --> 12:25:29,416
again
16594
12:25:31,616 --> 12:25:33,720
so as you can see they are still started
16595
12:25:33,720 --> 12:25:36,180
so we have first the Dell which has four
16596
12:25:36,180 --> 12:25:39,000
gigabytes of RAM then we have the Apple
16597
12:25:39,000 --> 12:25:40,980
which is eight then we have the Acer
16598
12:25:40,980 --> 12:25:43,740
which is 16. so this is why so now it's
16599
12:25:43,740 --> 12:25:47,040
sorting our list this sorted width using
16600
12:25:47,040 --> 12:25:49,860
comparator Ram that we defined we
16601
12:25:49,860 --> 12:25:52,020
created and then we Loop through all our
16602
12:25:52,020 --> 12:25:53,880
all elements because that Returns the
16603
12:25:53,880 --> 12:25:55,380
list so this is how you can use
16604
12:25:55,380 --> 12:25:59,340
comparator and if you want to uh to to
16605
12:25:59,340 --> 12:26:00,900
sort of let's say now the list by the
16606
12:26:00,900 --> 12:26:02,700
year you just create another comparator
16607
12:26:02,700 --> 12:26:04,520
so you type class
16608
12:26:04,520 --> 12:26:07,320
comparator let's say here
16609
12:26:07,320 --> 12:26:10,916
and it's also going to extend comparator
16610
12:26:10,916 --> 12:26:12,660
comparator
16611
12:26:12,660 --> 12:26:15,540
is going to have for the type laptops
16612
12:26:15,540 --> 12:26:18,300
because it's gonna Source laptops curly
16613
12:26:18,300 --> 12:26:21,596
braces we implement the function compare
16614
12:26:21,596 --> 12:26:23,520
and to define the logic for comparing
16615
12:26:23,520 --> 12:26:27,116
elements using uh using the ear so let's
16616
12:26:27,116 --> 12:26:29,400
delete this nullables
16617
12:26:29,400 --> 12:26:32,936
let's type here laptop one
16618
12:26:32,936 --> 12:26:36,380
and here laptop too
16619
12:26:36,776 --> 12:26:41,240
and I'm gonna copy this chord
16620
12:26:43,080 --> 12:26:46,340
I'm gonna paste it here
16621
12:26:47,700 --> 12:26:50,520
but I'm going to change this from Ram to
16622
12:26:50,520 --> 12:26:51,720
here
16623
12:26:51,720 --> 12:26:54,380
so here
16624
12:26:54,416 --> 12:26:56,899
here
16625
12:26:59,580 --> 12:27:02,480
also here
16626
12:27:05,400 --> 12:27:07,800
again
16627
12:27:07,800 --> 12:27:11,400
and we have an error here
16628
12:27:11,400 --> 12:27:13,800
missing so let's add the curly brace
16629
12:27:13,800 --> 12:27:16,040
here
16630
12:27:16,256 --> 12:27:19,020
all right so now if I want to to have
16631
12:27:19,020 --> 12:27:21,540
the element sorted by the year we just
16632
12:27:21,540 --> 12:27:22,680
type here
16633
12:27:22,680 --> 12:27:26,460
let's put a space Also print line
16634
12:27:26,460 --> 12:27:29,096
quotation marks for us this run and
16635
12:27:29,096 --> 12:27:31,740
found the laptops to be sorted by the by
16636
12:27:31,740 --> 12:27:33,900
the by the year I just like laptops that
16637
12:27:33,900 --> 12:27:35,276
sorted with
16638
12:27:35,276 --> 12:27:37,500
and this expects a comparator so I'm
16639
12:27:37,500 --> 12:27:39,660
gonna call our comparator that we
16640
12:27:39,660 --> 12:27:43,020
created comparator here comparator
16641
12:27:43,020 --> 12:27:44,276
here
16642
12:27:44,276 --> 12:27:47,880
an instance of that then for each
16643
12:27:47,880 --> 12:27:51,240
now this one again for each
16644
12:27:51,240 --> 12:27:53,220
print line
16645
12:27:53,220 --> 12:27:54,840
it
16646
12:27:54,840 --> 12:27:57,540
so if you run this
16647
12:27:57,540 --> 12:27:59,640
now we're gonna see the last element
16648
12:27:59,640 --> 12:28:03,240
sorted by their ear I have as you can
16649
12:28:03,240 --> 12:28:04,020
see
16650
12:28:04,020 --> 12:28:06,480
you have uh we have them sorted by the
16651
12:28:06,480 --> 12:28:10,140
ram so this is from this one
16652
12:28:10,140 --> 12:28:12,180
and now it's using comparator here so
16653
12:28:12,180 --> 12:28:13,680
you're sorting the embodies we have 20
16654
12:28:13,680 --> 12:28:17,640
20 21 and 2022 so this is just not like
16655
12:28:17,640 --> 12:28:20,160
the order that they are defined in uh in
16656
12:28:20,160 --> 12:28:22,080
the list so this is how you can use
16657
12:28:22,080 --> 12:28:24,116
comparable and comparable but if you
16658
12:28:24,116 --> 12:28:26,276
think about there is a lot of code here
16659
12:28:26,276 --> 12:28:29,756
to just have to have our
16660
12:28:29,756 --> 12:28:33,660
our list so that you have to we found
16661
12:28:33,660 --> 12:28:35,640
to
16662
12:28:35,640 --> 12:28:38,160
you have to you can use the compare the
16663
12:28:38,160 --> 12:28:40,140
comparable and with the comparable you
16664
12:28:40,140 --> 12:28:43,256
can use it with just one property and if
16665
12:28:43,256 --> 12:28:44,936
you want to use it with another property
16666
12:28:44,936 --> 12:28:46,680
you have to create those comparators
16667
12:28:46,680 --> 12:28:48,596
which gives you some flexibility but
16668
12:28:48,596 --> 12:28:51,360
still you have a lot of code and we can
16669
12:28:51,360 --> 12:28:54,540
remove all of this code
16670
12:28:54,540 --> 12:28:56,580
and use Lambda
16671
12:28:56,580 --> 12:28:58,256
functions
16672
12:28:58,256 --> 12:29:01,080
so I can delete all of this
16673
12:29:01,080 --> 12:29:04,380
all of this comparable that we have here
16674
12:29:04,380 --> 12:29:07,200
and instead of using those comparators
16675
12:29:07,200 --> 12:29:09,360
and all of this stuff what I can do is I
16676
12:29:09,360 --> 12:29:12,256
can delete all of this
16677
12:29:12,900 --> 12:29:16,080
I can little or this also
16678
12:29:16,080 --> 12:29:18,180
now what I can do is I can add here
16679
12:29:18,180 --> 12:29:22,200
laptops dot sorted
16680
12:29:22,200 --> 12:29:25,320
with and here
16681
12:29:25,320 --> 12:29:27,596
so we choose this one sorted with which
16682
12:29:27,596 --> 12:29:30,180
has this parameter comparator press
16683
12:29:30,180 --> 12:29:34,460
enter and here we type
16684
12:29:34,460 --> 12:29:36,180
compare
16685
12:29:36,180 --> 12:29:37,616
by
16686
12:29:37,616 --> 12:29:40,616
and here we type and this line of code
16687
12:29:40,616 --> 12:29:43,860
that we have here we it what it actually
16688
12:29:43,860 --> 12:29:46,560
is doing is creating a comparator but is
16689
12:29:46,560 --> 12:29:48,596
creating a comparator using a Lambda
16690
12:29:48,596 --> 12:29:52,680
expression so here you can type it dot
16691
12:29:52,680 --> 12:29:55,740
it's dot let's say uh price
16692
12:29:55,740 --> 12:30:00,240
right and then for each print line
16693
12:30:00,240 --> 12:30:03,540
it so this is equivalent to creating
16694
12:30:03,540 --> 12:30:07,560
that comparator here and inheriting from
16695
12:30:07,560 --> 12:30:09,776
the comparator interface and defining
16696
12:30:09,776 --> 12:30:13,680
the logic for uh uh or ordering for
16697
12:30:13,680 --> 12:30:16,020
sorting them so now if you if you run
16698
12:30:16,020 --> 12:30:18,180
this this will have the same effect like
16699
12:30:18,180 --> 12:30:21,480
the comparator price that we have
16700
12:30:21,480 --> 12:30:23,160
implemented previous as you can see they
16701
12:30:23,160 --> 12:30:25,020
are ordered now by the price and I can
16702
12:30:25,020 --> 12:30:25,980
do
16703
12:30:25,980 --> 12:30:29,460
now on another line of code Source dot
16704
12:30:29,460 --> 12:30:32,640
laptop dot sorted weight
16705
12:30:32,640 --> 12:30:35,936
and we type here compare by we type it
16706
12:30:35,936 --> 12:30:40,276
dot let's say uh
16707
12:30:40,916 --> 12:30:43,460
Ram
16708
12:30:43,616 --> 12:30:46,980
dot for each
16709
12:30:46,980 --> 12:30:48,900
print line
16710
12:30:48,900 --> 12:30:50,220
and it
16711
12:30:50,220 --> 12:30:52,436
now if you run this I'm gonna have the
16712
12:30:52,436 --> 12:30:53,936
same output let's add the space between
16713
12:30:53,936 --> 12:30:57,020
them print line
16714
12:30:59,400 --> 12:31:01,800
so let's add the space to see them to
16715
12:31:01,800 --> 12:31:04,560
see the code more clearly
16716
12:31:04,560 --> 12:31:06,776
and now as the as you can see the first
16717
12:31:06,776 --> 12:31:08,580
they are sorted by their price and then
16718
12:31:08,580 --> 12:31:11,340
they are sorted by their uh their RAM
16719
12:31:11,340 --> 12:31:14,580
memory so this code is equivalent to
16720
12:31:14,580 --> 12:31:16,320
creating what we had previously with the
16721
12:31:16,320 --> 12:31:19,860
comparator and implementing the dotting
16722
12:31:19,860 --> 12:31:21,960
interface and the function and actually
16723
12:31:21,960 --> 12:31:24,720
there is a shorter way even to do this
16724
12:31:24,720 --> 12:31:28,320
we can delete this let's say so the
16725
12:31:28,320 --> 12:31:31,020
sorted with and compare bear compare by
16726
12:31:31,020 --> 12:31:36,360
and we can just type here sort by
16727
12:31:36,360 --> 12:31:40,020
so this this will create internally the
16728
12:31:40,020 --> 12:31:43,380
same thing that we had previously so if
16729
12:31:43,380 --> 12:31:46,200
I type it dot ROM
16730
12:31:46,200 --> 12:31:48,300
and I can do the same thing here I can
16731
12:31:48,300 --> 12:31:50,880
delete all of this and I just
16732
12:31:50,880 --> 12:31:54,360
could type here selector the sort by
16733
12:31:54,360 --> 12:31:57,720
eat that price now this will have the
16734
12:31:57,720 --> 12:31:59,880
same effect like all the code that we
16735
12:31:59,880 --> 12:32:03,116
had previously but now it's extremely
16736
12:32:03,116 --> 12:32:05,880
concise and easier to read you just say
16737
12:32:05,880 --> 12:32:07,980
laptop sorted by sorted them by their
16738
12:32:07,980 --> 12:32:10,320
price so the elliptos by the ram so if
16739
12:32:10,320 --> 12:32:12,180
you run this code
16740
12:32:12,180 --> 12:32:14,580
you'll see the exact output in the
16741
12:32:14,580 --> 12:32:17,936
console because what this function is
16742
12:32:17,936 --> 12:32:20,700
doing uh actually we should Loop through
16743
12:32:20,700 --> 12:32:23,520
them so let's add the print line here
16744
12:32:23,520 --> 12:32:25,616
print line it
16745
12:32:25,616 --> 12:32:28,256
so let's put here for each
16746
12:32:28,256 --> 12:32:29,880
put into line
16747
12:32:29,880 --> 12:32:31,256
it
16748
12:32:31,256 --> 12:32:33,300
so now if you run this you will see the
16749
12:32:33,300 --> 12:32:36,480
exact same output we just this simple
16750
12:32:36,480 --> 12:32:37,860
line of code
16751
12:32:37,860 --> 12:32:39,360
so as you can see you have the same
16752
12:32:39,360 --> 12:32:41,220
output we have them sorted divide their
16753
12:32:41,220 --> 12:32:44,096
price first and then they are sorted by
16754
12:32:44,096 --> 12:32:47,880
their RAM memory and if you hold Ctrl on
16755
12:32:47,880 --> 12:32:51,435
this sorted by function
16756
12:32:54,540 --> 12:32:56,820
as you can see this actually internally
16757
12:32:56,820 --> 12:32:58,800
calls the sorted width which receives
16758
12:32:58,800 --> 12:33:00,360
the comparator and is calling the
16759
12:33:00,360 --> 12:33:02,400
compound the compare buy so this is like
16760
12:33:02,400 --> 12:33:05,700
a syntactical sugar for us which makes
16761
12:33:05,700 --> 12:33:07,680
the code more concise but internally
16762
12:33:07,680 --> 12:33:11,040
it's using using that sorted weight and
16763
12:33:11,040 --> 12:33:13,080
uh if you want to use the salt to do it
16764
12:33:13,080 --> 12:33:15,480
one advance for using a sorted with
16765
12:33:15,480 --> 12:33:17,096
let's say that I type here another print
16766
12:33:17,096 --> 12:33:18,720
line to add a space
16767
12:33:18,720 --> 12:33:21,720
let's put photos lesson
16768
12:33:21,720 --> 12:33:24,540
should be in quotation marks
16769
12:33:24,540 --> 12:33:28,680
forward slash n one
16770
12:33:28,680 --> 12:33:30,960
good thing about the sorted width so
16771
12:33:30,960 --> 12:33:32,936
let's type laptops
16772
12:33:32,936 --> 12:33:34,700
Dot
16773
12:33:34,700 --> 12:33:37,500
sorted weight
16774
12:33:37,500 --> 12:33:39,500
and here we type
16775
12:33:39,500 --> 12:33:42,800
compare by
16776
12:33:42,960 --> 12:33:47,580
let's say it dot year one thing that you
16777
12:33:47,580 --> 12:33:49,860
can do with the sorted with if you call
16778
12:33:49,860 --> 12:33:51,540
specific restorative function you can
16779
12:33:51,540 --> 12:33:55,200
put that and then buy so it will sort by
16780
12:33:55,200 --> 12:33:59,096
year and then by I know let's say uh it
16781
12:33:59,096 --> 12:34:02,276
dot price so this is what you would get
16782
12:34:02,276 --> 12:34:04,436
what you can do with the sorted with you
16783
12:34:04,436 --> 12:34:07,436
can sorted by year first and then by uh
16784
12:34:07,436 --> 12:34:11,060
by the price so if you run this
16785
12:34:13,580 --> 12:34:16,020
as you can see they are sorted first by
16786
12:34:16,020 --> 12:34:18,660
the year so
16787
12:34:18,660 --> 12:34:21,116
and then buy their uh their price so
16788
12:34:21,116 --> 12:34:23,880
this way we get this in the output
16789
12:34:23,880 --> 12:34:28,436
now I'm gonna end this video and uh you
16790
12:34:28,436 --> 12:34:31,140
should you should use this uh this one
16791
12:34:31,140 --> 12:34:32,880
always because it's
16792
12:34:32,880 --> 12:34:36,960
concise monitable yeah very easy to to
16793
12:34:36,960 --> 12:34:40,936
to read and to and to type is
16794
12:34:40,936 --> 12:34:43,436
in contrast with what we had previously
16795
12:34:43,436 --> 12:34:47,276
with the or creating that comparators
16796
12:34:47,276 --> 12:34:48,480
and implementing the comparator
16797
12:34:48,480 --> 12:34:50,400
interface or the comparable
16798
12:34:50,400 --> 12:34:52,980
this is a lot a lot more concise and
16799
12:34:52,980 --> 12:34:55,320
easier to read so this is our discussion
16800
12:34:55,320 --> 12:34:57,000
about the comparable and comparator
16801
12:34:57,000 --> 12:34:58,980
interface so this line of code I repeat
16802
12:34:58,980 --> 12:35:02,096
this line of code that we have here
16803
12:35:02,096 --> 12:35:04,860
this sorted by it does RAM is creating
16804
12:35:04,860 --> 12:35:06,596
internally that comparator that we
16805
12:35:06,596 --> 12:35:07,500
talked about
16806
12:35:07,500 --> 12:35:11,880
the node we're just using this Lambda
16807
12:35:11,880 --> 12:35:13,616
function
16808
12:35:13,616 --> 12:35:16,140
so I'm going to end the video now and
16809
12:35:16,140 --> 12:35:18,180
see you in the next video so there is
16810
12:35:18,180 --> 12:35:20,220
one mistake that I did previously so if
16811
12:35:20,220 --> 12:35:24,300
I press Ctrl Z to have this code uh when
16812
12:35:24,300 --> 12:35:26,756
I run this code you you didn't sound
16813
12:35:26,756 --> 12:35:29,400
output then the list sorted by the year
16814
12:35:29,400 --> 12:35:30,720
and then by the price and that is
16815
12:35:30,720 --> 12:35:33,776
because I forgot to add the four each
16816
12:35:33,776 --> 12:35:35,936
to Loop through our elements so if I add
16817
12:35:35,936 --> 12:35:38,220
the four each and now if I run this now
16818
12:35:38,220 --> 12:35:39,960
we're gonna see them
16819
12:35:39,960 --> 12:35:41,936
sorted first by the year and then buy
16820
12:35:41,936 --> 12:35:43,800
the price because previously it wasn't
16821
12:35:43,800 --> 12:35:46,616
so this is why it wasn't now if you look
16822
12:35:46,616 --> 12:35:48,660
they are sorted by the first by the year
16823
12:35:48,660 --> 12:35:52,380
so we have 2020 2021 and 22 and then buy
16824
12:35:52,380 --> 12:35:54,720
uh by their price so this is why
16825
12:35:54,720 --> 12:35:58,980
previously you saw this one this output
16826
12:35:58,980 --> 12:36:01,916
and also I saw this output that but uh
16827
12:36:01,916 --> 12:36:05,640
some way I uh I didn't uh didn't saw
16828
12:36:05,640 --> 12:36:09,560
that I forgot to add the foliage here so
16829
12:36:09,560 --> 12:36:13,080
this is how you can also sort the sorted
16830
12:36:13,080 --> 12:36:15,596
width you can um sort it by more
16831
12:36:15,596 --> 12:36:17,096
elements you can put here I think
16832
12:36:17,096 --> 12:36:18,180
another
16833
12:36:18,180 --> 12:36:20,580
then buy
16834
12:36:20,580 --> 12:36:22,560
not here
16835
12:36:22,560 --> 12:36:26,596
we can put another so
16836
12:36:26,596 --> 12:36:29,580
dot then by you can put another then by
16837
12:36:29,580 --> 12:36:33,480
here if you want so eat that so let's
16838
12:36:33,480 --> 12:36:35,936
say your RAM
16839
12:36:35,936 --> 12:36:39,180
so you can add more more than buy if you
16840
12:36:39,180 --> 12:36:40,200
want so
16841
12:36:40,200 --> 12:36:42,116
now I'm gonna end with the video see you
16842
12:36:42,116 --> 12:36:43,800
and see you in the next video now it's
16843
12:36:43,800 --> 12:36:45,540
time to start a discussion about binding
16844
12:36:45,540 --> 12:36:47,340
the search but before you look at binary
16845
12:36:47,340 --> 12:36:48,720
search you're going to first look at
16846
12:36:48,720 --> 12:36:51,660
linear search to see why a binary search
16847
12:36:51,660 --> 12:36:53,936
works better so I'm gonna type a
16848
12:36:53,936 --> 12:36:56,040
function here I'm going to type private
16849
12:36:56,040 --> 12:36:59,820
fun it's going to be called search
16850
12:36:59,820 --> 12:37:01,980
element
16851
12:37:01,980 --> 12:37:03,900
and you're going to type search elements
16852
12:37:03,900 --> 12:37:05,700
so this parameter is going to be the
16853
12:37:05,700 --> 12:37:08,899
element that we are searching for
16854
12:37:12,416 --> 12:37:15,720
and it's gonna then another parameter is
16855
12:37:15,720 --> 12:37:18,240
going to be a list
16856
12:37:18,240 --> 12:37:19,980
of numbers so it's going to be a
16857
12:37:19,980 --> 12:37:21,240
multiple list
16858
12:37:21,240 --> 12:37:24,200
of ins
16859
12:37:24,960 --> 12:37:27,000
and it's going to return a number the
16860
12:37:27,000 --> 12:37:29,220
search element or minus one if you don't
16861
12:37:29,220 --> 12:37:31,140
find the search element
16862
12:37:31,140 --> 12:37:32,820
and here we're going to Loop through our
16863
12:37:32,820 --> 12:37:35,400
release so I'm going to type for number
16864
12:37:35,400 --> 12:37:38,776
in our list of numbers
16865
12:37:38,936 --> 12:37:41,580
if
16866
12:37:41,580 --> 12:37:46,080
at a particular iteration or number
16867
12:37:46,080 --> 12:37:48,360
it's equal to the search
16868
12:37:48,360 --> 12:37:50,276
element that means that we found the
16869
12:37:50,276 --> 12:37:51,660
element that we found the number so
16870
12:37:51,660 --> 12:37:54,360
we're going to return number
16871
12:37:54,360 --> 12:37:57,720
else if you come down here and after we
16872
12:37:57,720 --> 12:37:59,936
Loop and we did our if checks we didn't
16873
12:37:59,936 --> 12:38:01,980
return the number that means we didn't
16874
12:38:01,980 --> 12:38:02,936
find a number we're going to return
16875
12:38:02,936 --> 12:38:05,276
minus on here
16876
12:38:05,276 --> 12:38:08,936
now I'm gonna add the print line here
16877
12:38:08,936 --> 12:38:11,700
and I'm gonna type search
16878
12:38:11,700 --> 12:38:14,040
element because this is going to return
16879
12:38:14,040 --> 12:38:15,360
an element and then you're gonna output
16880
12:38:15,360 --> 12:38:17,400
with the print line dot in the console
16881
12:38:17,400 --> 12:38:21,000
and I'm going to type here 27
16882
12:38:21,000 --> 12:38:22,740
and for the next parameter I'm going to
16883
12:38:22,740 --> 12:38:24,596
define a list of numbers one time
16884
12:38:24,596 --> 12:38:26,400
Multiple List off
16885
12:38:26,400 --> 12:38:28,080
and here I'm going to define a number
16886
12:38:28,080 --> 12:38:31,140
from 1 to 30 so I'm going to type here
16887
12:38:31,140 --> 12:38:34,616
the numbers one
16888
12:38:38,340 --> 12:38:42,480
and our last element 30.
16889
12:38:42,480 --> 12:38:44,756
now if you run this code you're gonna
16890
12:38:44,756 --> 12:38:48,720
see in the output 27
16891
12:38:50,520 --> 12:38:54,000
so we get as expected 27 but let's
16892
12:38:54,000 --> 12:38:56,400
declare a variable here I
16893
12:38:56,400 --> 12:38:59,460
to number the number of iteration it
16894
12:38:59,460 --> 12:39:01,860
took it took to find the elements I'm
16895
12:39:01,860 --> 12:39:05,000
going to assign 0 to it
16896
12:39:05,040 --> 12:39:06,776
I'm going to increment it every time you
16897
12:39:06,776 --> 12:39:09,180
Loop so I'm going to type Val I'm going
16898
12:39:09,180 --> 12:39:11,820
to type I sorry plus plus
16899
12:39:11,820 --> 12:39:14,756
and I'm gonna print line here
16900
12:39:14,756 --> 12:39:19,740
and I'm gonna it's gonna say search
16901
12:39:20,160 --> 12:39:21,660
number
16902
12:39:21,660 --> 12:39:22,916
colon
16903
12:39:22,916 --> 12:39:25,860
dollar sign I
16904
12:39:25,860 --> 12:39:28,680
so now if you run this we're gonna see
16905
12:39:28,680 --> 12:39:31,320
how many iteration it took to found our
16906
12:39:31,320 --> 12:39:33,560
element
16907
12:39:34,256 --> 12:39:36,596
and if you look at the console as you
16908
12:39:36,596 --> 12:39:38,720
can see it took 27
16909
12:39:38,720 --> 12:39:42,240
27 iteration before we found our element
16910
12:39:42,240 --> 12:39:46,256
because the way the linear search is
16911
12:39:46,256 --> 12:39:49,500
working is it's going through all of the
16912
12:39:49,500 --> 12:39:51,840
elements is checking out each iteration
16913
12:39:51,840 --> 12:39:54,900
if the number of that particular
16914
12:39:54,900 --> 12:39:56,756
iteration is equal to the search element
16915
12:39:56,756 --> 12:39:58,320
if not it's going to loop again and
16916
12:39:58,320 --> 12:40:01,436
again again until it reaches 27 then it
16917
12:40:01,436 --> 12:40:03,360
found that element and it Returns the
16918
12:40:03,360 --> 12:40:04,580
element and you get that element
16919
12:40:04,580 --> 12:40:06,900
outputted here in the console
16920
12:40:06,900 --> 12:40:09,200
so this is what we call a linear search
16921
12:40:09,200 --> 12:40:12,360
and this is not particularly good
16922
12:40:12,360 --> 12:40:15,116
because imagine if you have 1 billion
16923
12:40:15,116 --> 12:40:17,756
elements or 10 billion elements and you
16924
12:40:17,756 --> 12:40:19,740
need to find I don't know
16925
12:40:19,740 --> 12:40:24,360
maybe the 7th billion element in the in
16926
12:40:24,360 --> 12:40:26,756
that array that is going to be very hard
16927
12:40:26,756 --> 12:40:28,740
and it's going to take a
16928
12:40:28,740 --> 12:40:31,256
time and memory and because of that we
16929
12:40:31,256 --> 12:40:33,596
have another
16930
12:40:33,596 --> 12:40:37,020
data structure for a searching element
16931
12:40:37,020 --> 12:40:39,116
and that is called binary search and I'm
16932
12:40:39,116 --> 12:40:40,560
going to type the code for binary search
16933
12:40:40,560 --> 12:40:42,360
here and also you're going to see the
16934
12:40:42,360 --> 12:40:44,460
number of iteration it tooks to
16935
12:40:44,460 --> 12:40:46,320
find element and also I'm going to
16936
12:40:46,320 --> 12:40:48,360
explain how binary search works so I'm
16937
12:40:48,360 --> 12:40:51,020
going to delete this
16938
12:40:53,276 --> 12:40:56,756
I'm going to type here VAR low I'm going
16939
12:40:56,756 --> 12:40:58,500
to put equals to zero
16940
12:40:58,500 --> 12:41:00,320
VAR
16941
12:41:00,320 --> 12:41:02,820
High equals to
16942
12:41:02,820 --> 12:41:06,776
numbers that size minus one I'm going to
16943
12:41:06,776 --> 12:41:09,116
explain the code immediately
16944
12:41:09,116 --> 12:41:11,820
here I'm going to type while
16945
12:41:11,820 --> 12:41:14,040
low is less than equal
16946
12:41:14,040 --> 12:41:16,640
to high
16947
12:41:17,220 --> 12:41:20,520
here I'm gonna type Val mid so now we're
16948
12:41:20,520 --> 12:41:22,680
going to get the middle element so I'm
16949
12:41:22,680 --> 12:41:25,616
going to put parenthesis slow
16950
12:41:25,616 --> 12:41:29,700
plus High divided by 2 and that's going
16951
12:41:29,700 --> 12:41:31,980
to give us the middle element 15.
16952
12:41:31,980 --> 12:41:33,180
then
16953
12:41:33,180 --> 12:41:35,400
I'm going to type Val comparison so I'm
16954
12:41:35,400 --> 12:41:37,916
going to type CMP
16955
12:41:37,916 --> 12:41:41,400
equals to numbers
16956
12:41:41,400 --> 12:41:44,640
dot get the middle element compared to
16957
12:41:44,640 --> 12:41:45,480
we're going to compare the middle
16958
12:41:45,480 --> 12:41:49,160
element with the search element
16959
12:41:50,160 --> 12:41:54,140
then I'm going to type here if
16960
12:42:02,880 --> 12:42:05,300
now I'm going to type if
16961
12:42:05,300 --> 12:42:08,520
comparison is less than zero
16962
12:42:08,520 --> 12:42:11,756
then I'm going to type here low
16963
12:42:11,756 --> 12:42:13,616
equals to
16964
12:42:13,616 --> 12:42:15,480
mid
16965
12:42:15,480 --> 12:42:17,220
plus one
16966
12:42:17,220 --> 12:42:19,560
lcf
16967
12:42:19,560 --> 12:42:22,980
if comparison is not less than zero
16968
12:42:22,980 --> 12:42:25,080
so it's greater than zero
16969
12:42:25,080 --> 12:42:27,720
I'm going to type here that
16970
12:42:27,720 --> 12:42:29,096
low
16971
12:42:29,096 --> 12:42:31,800
is equal to
16972
12:42:31,800 --> 12:42:33,900
High
16973
12:42:33,900 --> 12:42:35,096
sorry
16974
12:42:35,096 --> 12:42:37,436
is equal to my mid
16975
12:42:37,436 --> 12:42:39,560
-1
16976
12:42:40,680 --> 12:42:42,240
else
16977
12:42:42,240 --> 12:42:44,220
when I type here return
16978
12:42:44,220 --> 12:42:46,080
numbers
16979
12:42:46,080 --> 12:42:48,360
dot get meet and that is going to give
16980
12:42:48,360 --> 12:42:49,980
us the middle element
16981
12:42:49,980 --> 12:42:52,616
now if I declare here a variable
16982
12:42:52,616 --> 12:42:53,880
VAR
16983
12:42:53,880 --> 12:42:56,820
I and if I assign 0 to 8
16984
12:42:56,820 --> 12:43:00,060
and if I increment that I at each
16985
12:43:00,060 --> 12:43:01,800
iteration
16986
12:43:01,800 --> 12:43:05,596
and if I add a print line here
16987
12:43:05,640 --> 12:43:08,340
search
16988
12:43:08,340 --> 12:43:11,040
number
16989
12:43:11,040 --> 12:43:13,380
dollar sign I
16990
12:43:13,380 --> 12:43:16,616
now if you run this you're gonna see uh
16991
12:43:16,616 --> 12:43:18,000
you're gonna actually I should return
16992
12:43:18,000 --> 12:43:19,916
minus on here
16993
12:43:19,916 --> 12:43:22,980
because uh if at the end of our looping
16994
12:43:22,980 --> 12:43:27,720
and after we checked our all of our list
16995
12:43:27,720 --> 12:43:29,160
and we didn't find the number then we
16996
12:43:29,160 --> 12:43:30,540
determine this one meaning that we
16997
12:43:30,540 --> 12:43:32,276
didn't find the element
16998
12:43:32,276 --> 12:43:35,479
so now if you run this again
16999
12:43:41,096 --> 12:43:43,640
we get
17000
12:43:43,800 --> 12:43:47,096
search number one two three and then we
17001
12:43:47,096 --> 12:43:49,740
get 27 and as you can see this is
17002
12:43:49,740 --> 12:43:52,500
significantly different from our linear
17003
12:43:52,500 --> 12:43:54,900
search because now we only took three
17004
12:43:54,900 --> 12:43:57,840
searched and then I found the he found
17005
12:43:57,840 --> 12:44:00,116
our element previously we had to search
17006
12:44:00,116 --> 12:44:02,520
through all of our elements
17007
12:44:02,520 --> 12:44:06,240
through our elements until uh the number
17008
12:44:06,240 --> 12:44:09,116
27 to to get the number seven but now we
17009
12:44:09,116 --> 12:44:10,560
only get the
17010
12:44:10,560 --> 12:44:13,560
three iteration and we get the number 27
17011
12:44:13,560 --> 12:44:16,020
and how is that possible this is because
17012
12:44:16,020 --> 12:44:17,580
now we're using what is called the
17013
12:44:17,580 --> 12:44:20,160
binary search and how this binary search
17014
12:44:20,160 --> 12:44:23,580
works I'm going to explain it now so now
17015
12:44:23,580 --> 12:44:25,380
let's see how the code works
17016
12:44:25,380 --> 12:44:27,900
so first we have a variable declare code
17017
12:44:27,900 --> 12:44:30,596
load which assign a value of 0. then we
17018
12:44:30,596 --> 12:44:31,980
have another variable called height
17019
12:44:31,980 --> 12:44:34,200
which assign number the PSI minus one so
17020
12:44:34,200 --> 12:44:36,240
we assign the size of the array to our
17021
12:44:36,240 --> 12:44:37,020
high
17022
12:44:37,020 --> 12:44:39,116
then we have a variable called I to
17023
12:44:39,116 --> 12:44:42,060
which you assign 0. then we have a while
17024
12:44:42,060 --> 12:44:44,160
loop and we start looping by this
17025
12:44:44,160 --> 12:44:46,200
condition while low is less than equal
17026
12:44:46,200 --> 12:44:47,340
to high
17027
12:44:47,340 --> 12:44:50,400
then we increment our I to keep track of
17028
12:44:50,400 --> 12:44:52,500
how many iteration it takes before we
17029
12:44:52,500 --> 12:44:55,140
find the element and without any console
17030
12:44:55,140 --> 12:44:57,416
search element and the number at that
17031
12:44:57,416 --> 12:44:58,680
iteration
17032
12:44:58,680 --> 12:45:01,256
then we have a variable called mid and
17033
12:45:01,256 --> 12:45:02,756
this is going to give us the middle
17034
12:45:02,756 --> 12:45:05,160
element and to get the middle element we
17035
12:45:05,160 --> 12:45:08,220
add the lower lowest element plus the
17036
12:45:08,220 --> 12:45:10,980
highest element so yeah we'll add here 0
17037
12:45:10,980 --> 12:45:13,380
plus 30 divided by 2 which is 15. so
17038
12:45:13,380 --> 12:45:14,756
this is the
17039
12:45:14,756 --> 12:45:16,560
middle element
17040
12:45:16,560 --> 12:45:19,020
then we declare another variable called
17041
12:45:19,020 --> 12:45:22,800
comparison and we type here numbers that
17042
12:45:22,800 --> 12:45:24,416
get the middle element which is 15
17043
12:45:24,416 --> 12:45:28,200
compared to search element and if the
17044
12:45:28,200 --> 12:45:32,276
middle element is less than the search
17045
12:45:32,276 --> 12:45:35,520
element that means that the search
17046
12:45:35,520 --> 12:45:38,340
element is on the right of the numbers
17047
12:45:38,340 --> 12:45:41,220
which come after the 15 and because of
17048
12:45:41,220 --> 12:45:43,860
that this is gonna return because our
17049
12:45:43,860 --> 12:45:46,256
middle element is less than the search
17050
12:45:46,256 --> 12:45:47,936
element is going to return a negative
17051
12:45:47,936 --> 12:45:50,520
number so this is going to be true
17052
12:45:50,520 --> 12:45:52,800
comparison is going to be less than zero
17053
12:45:52,800 --> 12:45:55,980
and then I'm gonna assign low equals to
17054
12:45:55,980 --> 12:45:58,820
Mid Plus on so now
17055
12:45:58,820 --> 12:46:04,200
the low variable has the value 16 all
17056
12:46:04,200 --> 12:46:07,860
right now it will loop again it will uh
17057
12:46:07,860 --> 12:46:10,860
it will uh do it will have the I
17058
12:46:10,860 --> 12:46:13,020
incrementally it will
17059
12:46:13,020 --> 12:46:15,000
do the while loop it will increment the
17060
12:46:15,000 --> 12:46:17,460
I again and it will output into I in the
17061
12:46:17,460 --> 12:46:21,840
console now here now it will calculate
17062
12:46:21,840 --> 12:46:25,080
again the middle element and it will it
17063
12:46:25,080 --> 12:46:27,660
will add now 16.
17064
12:46:27,660 --> 12:46:28,916
Plus
17065
12:46:28,916 --> 12:46:32,160
30 divided by 2 and that is going to
17066
12:46:32,160 --> 12:46:34,616
give give us 23.
17067
12:46:34,616 --> 12:46:35,756
all right
17068
12:46:35,756 --> 12:46:38,400
now that is the middle element now and
17069
12:46:38,400 --> 12:46:42,000
what we do now here is we again uh I
17070
12:46:42,000 --> 12:46:44,416
have our variable comparison and we get
17071
12:46:44,416 --> 12:46:48,960
numbers that get 23 so we get the 23
17072
12:46:48,960 --> 12:46:50,756
element and we compile with our search
17073
12:46:50,756 --> 12:46:53,400
element which is 27.
17074
12:46:53,400 --> 12:46:57,720
and again because our middle element
17075
12:46:57,720 --> 12:47:00,900
is less than the search element this is
17076
12:47:00,900 --> 12:47:02,756
going to return on any negative number
17077
12:47:02,756 --> 12:47:04,616
so this shift check is going to be true
17078
12:47:04,616 --> 12:47:08,400
again now what is gonna
17079
12:47:08,400 --> 12:47:11,580
happen is that now low
17080
12:47:11,580 --> 12:47:15,000
is going to be equal to Mid
17081
12:47:15,000 --> 12:47:18,300
plus 1 and the low previously was equal
17082
12:47:18,300 --> 12:47:21,776
to 23 so now I'm going to have 23 plus 1
17083
12:47:21,776 --> 12:47:24,416
which is 24 all right
17084
12:47:24,416 --> 12:47:27,116
now this will loop again
17085
12:47:27,116 --> 12:47:29,700
or loop again so
17086
12:47:29,700 --> 12:47:32,640
as you can see this basically breaks the
17087
12:47:32,640 --> 12:47:36,660
the list in half at each iteration and
17088
12:47:36,660 --> 12:47:38,040
now this will loop again it will
17089
12:47:38,040 --> 12:47:40,200
increment the eye to Output I and now it
17090
12:47:40,200 --> 12:47:42,000
will calculate again the middle element
17091
12:47:42,000 --> 12:47:45,240
now the middle element is 24.
17092
12:47:45,240 --> 12:47:47,880
and the high element is 30 and we have
17093
12:47:47,880 --> 12:47:49,980
24 plus 30
17094
12:47:49,980 --> 12:47:54,256
divided by 2. and that is 27 the exact
17095
12:47:54,256 --> 12:47:57,596
number that we are searching for so this
17096
12:47:57,596 --> 12:47:59,936
is how binary search works it will
17097
12:47:59,936 --> 12:48:01,680
compare the middle element with the
17098
12:48:01,680 --> 12:48:04,200
search element if the middle element is
17099
12:48:04,200 --> 12:48:05,820
less than the search element that means
17100
12:48:05,820 --> 12:48:08,400
that your element is on the right side
17101
12:48:08,400 --> 12:48:10,616
of the element which come after the
17102
12:48:10,616 --> 12:48:12,900
middle element so all the elements from
17103
12:48:12,900 --> 12:48:14,340
the left are going to be excluded and
17104
12:48:14,340 --> 12:48:16,616
it's going to search only on the on the
17105
12:48:16,616 --> 12:48:17,520
right side
17106
12:48:17,520 --> 12:48:20,220
then it will split the list again in
17107
12:48:20,220 --> 12:48:22,680
half if the middle element again is less
17108
12:48:22,680 --> 12:48:24,240
than the search element it will go to
17109
12:48:24,240 --> 12:48:26,276
the right again it will speed the least
17110
12:48:26,276 --> 12:48:29,096
in half I need to sooner or later it
17111
12:48:29,096 --> 12:48:32,040
will find the element to to to the right
17112
12:48:32,040 --> 12:48:33,480
or to the left
17113
12:48:33,480 --> 12:48:35,700
so this is how one research version is
17114
12:48:35,700 --> 12:48:42,560
very powerful because for a very big uh
17115
12:48:42,660 --> 12:48:45,480
list you can use binary search and it's
17116
12:48:45,480 --> 12:48:48,000
more effective that uh only the linear
17117
12:48:48,000 --> 12:48:49,560
search
17118
12:48:49,560 --> 12:48:53,040
and uh this is the code that I type but
17119
12:48:53,040 --> 12:48:54,596
it doesn't you don't need to use this
17120
12:48:54,596 --> 12:48:57,060
code because uh I only type this code to
17121
12:48:57,060 --> 12:48:58,980
show you how the binary search works but
17122
12:48:58,980 --> 12:49:01,200
usually you'll use the binary search
17123
12:49:01,200 --> 12:49:03,416
function provided by The Collection so
17124
12:49:03,416 --> 12:49:06,740
you will just type here return
17125
12:49:07,020 --> 12:49:08,580
numbers
17126
12:49:08,580 --> 12:49:11,096
we're gonna put square brackets and I'm
17127
12:49:11,096 --> 12:49:13,200
gonna type binary I'm going to type here
17128
12:49:13,200 --> 12:49:17,756
numbers again binary dot binary search
17129
12:49:17,756 --> 12:49:20,400
because the binary search function
17130
12:49:20,400 --> 12:49:22,740
provided by The Collection only return
17131
12:49:22,740 --> 12:49:26,240
the returns the index so if I
17132
12:49:26,240 --> 12:49:30,500
search for the 27 element
17133
12:49:31,800 --> 12:49:34,800
that is going to return the index 26th
17134
12:49:34,800 --> 12:49:38,096
and the index 26 then it's gonna be the
17135
12:49:38,096 --> 12:49:41,220
number 27 from our numbers list so now
17136
12:49:41,220 --> 12:49:43,680
if you run this you'll see 27 output in
17137
12:49:43,680 --> 12:49:46,160
the console
17138
12:49:47,640 --> 12:49:51,540
so get 27 and we did this one with one
17139
12:49:51,540 --> 12:49:53,640
line of code you can even remove this
17140
12:49:53,640 --> 12:49:56,276
and just have a
17141
12:49:56,276 --> 12:49:58,500
single expression function so we can put
17142
12:49:58,500 --> 12:49:59,700
here
17143
12:49:59,700 --> 12:50:02,460
equals
17144
12:50:02,460 --> 12:50:04,560
so just by that it will do the same
17145
12:50:04,560 --> 12:50:06,300
thing that we did previously with all
17146
12:50:06,300 --> 12:50:08,520
the code that I typed
17147
12:50:08,520 --> 12:50:11,160
so this is our discussion about binary
17148
12:50:11,160 --> 12:50:14,520
search and see you in the next video but
17149
12:50:14,520 --> 12:50:16,320
before I end the video I should say that
17150
12:50:16,320 --> 12:50:19,436
binary search only works with the order
17151
12:50:19,436 --> 12:50:21,900
collection of elements so your elements
17152
12:50:21,900 --> 12:50:24,240
need to be sorted in order to for the
17153
12:50:24,240 --> 12:50:27,596
binary search to work and in our case we
17154
12:50:27,596 --> 12:50:32,096
look we used integer numbers and it was
17155
12:50:32,096 --> 12:50:35,276
easy because we typed elements in a in a
17156
12:50:35,276 --> 12:50:36,900
sorted order
17157
12:50:36,900 --> 12:50:39,660
but uh if the elements are not sorted by
17158
12:50:39,660 --> 12:50:42,480
by research is not going to work also if
17159
12:50:42,480 --> 12:50:44,400
you have your own class so you have your
17160
12:50:44,400 --> 12:50:46,740
own objects and you search with using
17161
12:50:46,740 --> 12:50:51,000
one research uh through a list for a
17162
12:50:51,000 --> 12:50:54,480
specific element in that case your class
17163
12:50:54,480 --> 12:50:56,276
has to implement the comparable
17164
12:50:56,276 --> 12:51:01,320
interface or it has to to to to use the
17165
12:51:01,320 --> 12:51:03,000
comparator interface and I'm going to
17166
12:51:03,000 --> 12:51:04,500
show you how to do that in the next
17167
12:51:04,500 --> 12:51:06,900
video so see you in the next video
17168
12:51:06,900 --> 12:51:08,580
so now it's time to start a discussion
17169
12:51:08,580 --> 12:51:10,400
about genetics so what are genetics
17170
12:51:10,400 --> 12:51:13,200
genetics allows us to give to the
17171
12:51:13,200 --> 12:51:15,776
compiler some hints about what kind of
17172
12:51:15,776 --> 12:51:18,300
objects you are dealing with and this
17173
12:51:18,300 --> 12:51:20,400
helps the compiler to figure out some
17174
12:51:20,400 --> 12:51:23,820
errors at compile time and it also helps
17175
12:51:23,820 --> 12:51:26,400
us to write more consistent and better
17176
12:51:26,400 --> 12:51:29,820
code and we already use genetics when we
17177
12:51:29,820 --> 12:51:32,096
look at collections so every time you
17178
12:51:32,096 --> 12:51:34,500
use an array and you you specified in
17179
12:51:34,500 --> 12:51:38,096
the angle brackets an INT or a double or
17180
12:51:38,096 --> 12:51:42,180
a class that you created an object what
17181
12:51:42,180 --> 12:51:44,400
you actually did there is that you use
17182
12:51:44,400 --> 12:51:47,640
generic you set the compiler that that
17183
12:51:47,640 --> 12:51:50,340
is the kind of data that specific
17184
12:51:50,340 --> 12:51:52,916
collection is gonna store so if you have
17185
12:51:52,916 --> 12:51:56,040
something like this while let's say
17186
12:51:56,040 --> 12:51:58,320
numbers
17187
12:51:58,320 --> 12:52:01,020
and
17188
12:52:01,020 --> 12:52:03,720
we put equals is equals here and type
17189
12:52:03,720 --> 12:52:06,256
array of
17190
12:52:10,740 --> 12:52:15,079
here you can put angle brackets
17191
12:52:17,640 --> 12:52:19,860
and you can specify a type here let's
17192
12:52:19,860 --> 12:52:21,360
say in because
17193
12:52:21,360 --> 12:52:23,820
I'm going to put numbers here and now
17194
12:52:23,820 --> 12:52:27,360
this helps the compiler to figure out if
17195
12:52:27,360 --> 12:52:30,116
let's say we want to add here let's say
17196
12:52:30,116 --> 12:52:33,980
a string so if I type here numbers
17197
12:52:34,520 --> 12:52:37,256
equals let's say your
17198
12:52:37,256 --> 12:52:41,160
some text let's put in quotation marks
17199
12:52:41,160 --> 12:52:43,800
some text as you can see you have those
17200
12:52:43,800 --> 12:52:46,560
underlines red which says if you hover
17201
12:52:46,560 --> 12:52:47,880
over here
17202
12:52:47,880 --> 12:52:49,980
first it says that Val cannot be resent
17203
12:52:49,980 --> 12:52:52,560
so let's change this to VAR to get the
17204
12:52:52,560 --> 12:52:55,276
error that I want so let's put VAR here
17205
12:52:55,276 --> 12:52:58,916
so we get here type mismatch required
17206
12:52:58,916 --> 12:53:02,340
and found string so here we are using a
17207
12:53:02,340 --> 12:53:04,980
generic type and the generic type is an
17208
12:53:04,980 --> 12:53:08,520
integer and because we said explicitly
17209
12:53:08,520 --> 12:53:10,436
here that this array is only going to
17210
12:53:10,436 --> 12:53:13,680
store integers we cannot go down here
17211
12:53:13,680 --> 12:53:18,000
and assign some string but
17212
12:53:18,000 --> 12:53:20,040
as I said at the beginning
17213
12:53:20,040 --> 12:53:23,040
genetics are only a compile timing
17214
12:53:23,040 --> 12:53:24,660
feature they don't make it through the
17215
12:53:24,660 --> 12:53:26,640
runtime and again we're going to come
17216
12:53:26,640 --> 12:53:28,616
back to this later because this has some
17217
12:53:28,616 --> 12:53:30,180
implications
17218
12:53:30,180 --> 12:53:33,180
now let's uh delete this and let's
17219
12:53:33,180 --> 12:53:36,540
create our own generic class to see how
17220
12:53:36,540 --> 12:53:40,200
it works because here we're passing in
17221
12:53:40,200 --> 12:53:44,776
but if you click on this array of
17222
12:53:44,936 --> 12:53:48,540
you see that you don't have here any an
17223
12:53:48,540 --> 12:53:50,700
intest the type that we SP we
17224
12:53:50,700 --> 12:53:53,096
specifically decided to pass to this
17225
12:53:53,096 --> 12:53:56,180
array but if you look here inside this
17226
12:53:56,180 --> 12:53:59,400
library.kt and if you go to array here
17227
12:53:59,400 --> 12:54:01,320
you see you see that we have VAR Arc
17228
12:54:01,320 --> 12:54:03,416
elements and we have this t
17229
12:54:03,416 --> 12:54:07,436
and T is a genetic type and the specific
17230
12:54:07,436 --> 12:54:10,800
type that is going to be passed here is
17231
12:54:10,800 --> 12:54:13,140
the type that we pass when we create an
17232
12:54:13,140 --> 12:54:14,936
arrays when we want to store some
17233
12:54:14,936 --> 12:54:18,480
numbers or some strings and now now
17234
12:54:18,480 --> 12:54:21,000
let's see how we can create a similar
17235
12:54:21,000 --> 12:54:23,520
thing that we have here with our with
17236
12:54:23,520 --> 12:54:26,756
our own code so I'm gonna delete this so
17237
12:54:26,756 --> 12:54:29,096
again generics are there to
17238
12:54:29,096 --> 12:54:31,800
to to help the compiler to figure out
17239
12:54:31,800 --> 12:54:35,540
errors and it also they are there for
17240
12:54:35,540 --> 12:54:37,680
helping us to write better and
17241
12:54:37,680 --> 12:54:39,900
consistent calls so that you you don't
17242
12:54:39,900 --> 12:54:44,160
add let's say uh strings to to an array
17243
12:54:44,160 --> 12:54:47,936
which is meant to store only numbers so
17244
12:54:47,936 --> 12:54:50,220
first I'm going to create a class and
17245
12:54:50,220 --> 12:54:53,220
it's going to be an Open Class
17246
12:54:53,220 --> 12:54:56,580
and it's going to be called player
17247
12:54:56,580 --> 12:54:58,500
and it's going to have a property called
17248
12:54:58,500 --> 12:55:00,720
name so it's going to be a Val name
17249
12:55:00,720 --> 12:55:02,220
we're going to Define this in the
17250
12:55:02,220 --> 12:55:03,776
primary Constructor it's going to be a
17251
12:55:03,776 --> 12:55:05,096
string
17252
12:55:05,096 --> 12:55:08,360
now I'm gonna create
17253
12:55:08,580 --> 12:55:11,840
open class here
17254
12:55:14,640 --> 12:55:16,256
and down here I'm going to create two
17255
12:55:16,256 --> 12:55:18,660
classes first is going to be called
17256
12:55:18,660 --> 12:55:23,060
football player so football
17257
12:55:24,596 --> 12:55:26,096
player
17258
12:55:26,096 --> 12:55:28,500
and this is gonna have a name and it's
17259
12:55:28,500 --> 12:55:30,180
gonna be a property because you're gonna
17260
12:55:30,180 --> 12:55:34,080
gonna extend from our player class we're
17261
12:55:34,080 --> 12:55:35,580
gonna derive from our operator class I'm
17262
12:55:35,580 --> 12:55:37,680
gonna pass this property to the dot
17263
12:55:37,680 --> 12:55:41,040
parameter name to our property name from
17264
12:55:41,040 --> 12:55:43,380
the superclass so if we type here player
17265
12:55:43,380 --> 12:55:46,380
because we were in inheriting from it
17266
12:55:46,380 --> 12:55:49,200
and here first name
17267
12:55:49,200 --> 12:55:52,140
so you pass your name
17268
12:55:52,140 --> 12:55:54,060
next I'm going to create another class
17269
12:55:54,060 --> 12:55:56,400
called baseball player so we type here
17270
12:55:56,400 --> 12:55:58,616
baseball
17271
12:55:58,616 --> 12:56:00,900
player
17272
12:56:00,900 --> 12:56:03,256
and this is also going to have a name
17273
12:56:03,256 --> 12:56:06,660
also only a parameter because the
17274
12:56:06,660 --> 12:56:07,860
property is going to be ultimately
17275
12:56:07,860 --> 12:56:09,480
defined by the
17276
12:56:09,480 --> 12:56:11,400
superclass by the class from which we
17277
12:56:11,400 --> 12:56:13,320
are inheriting from so here we're gonna
17278
12:56:13,320 --> 12:56:15,540
inherit put the colon to inherit from
17279
12:56:15,540 --> 12:56:19,436
our player player class and here we pass
17280
12:56:19,436 --> 12:56:22,436
our name parameter that we defined here
17281
12:56:22,436 --> 12:56:24,180
we pass it here to the primary
17282
12:56:24,180 --> 12:56:26,880
Constructor of the base class from which
17283
12:56:26,880 --> 12:56:28,800
we are inheriting from
17284
12:56:28,800 --> 12:56:32,220
all right so now I'm gonna go up here
17285
12:56:32,220 --> 12:56:36,000
and now I'm gonna create a class called
17286
12:56:36,000 --> 12:56:37,200
team
17287
12:56:37,200 --> 12:56:39,596
and this class is going to represent a
17288
12:56:39,596 --> 12:56:41,160
team and this thing can be a football
17289
12:56:41,160 --> 12:56:43,740
team or a baseball team and here we're
17290
12:56:43,740 --> 12:56:45,300
going to create the class called team so
17291
12:56:45,300 --> 12:56:47,700
we type the keyword class I'm gonna call
17292
12:56:47,700 --> 12:56:48,840
it team
17293
12:56:48,840 --> 12:56:51,596
and now here we put angle brackets and
17294
12:56:51,596 --> 12:56:53,340
inside the angle brackets we're gonna
17295
12:56:53,340 --> 12:56:55,560
Define the generic type t
17296
12:56:55,560 --> 12:56:57,720
and you can use a different letter here
17297
12:56:57,720 --> 12:57:00,180
if you want but usually and generally
17298
12:57:00,180 --> 12:57:03,660
use the T because it stands for the for
17299
12:57:03,660 --> 12:57:05,160
type parameter
17300
12:57:05,160 --> 12:57:08,040
next to put parenthesis to define the
17301
12:57:08,040 --> 12:57:09,960
primary Constructor and for the primary
17302
12:57:09,960 --> 12:57:11,776
Constructor I'm going to define
17303
12:57:11,776 --> 12:57:14,580
a Val name and this is going to
17304
12:57:14,580 --> 12:57:16,380
represent the name of the theme and it's
17305
12:57:16,380 --> 12:57:18,060
going to be a string and next I'm going
17306
12:57:18,060 --> 12:57:20,460
to Define also valuable this is going to
17307
12:57:20,460 --> 12:57:22,980
be called players and it's going to be a
17308
12:57:22,980 --> 12:57:24,776
multiple list
17309
12:57:24,776 --> 12:57:28,256
of also of type T so we type here
17310
12:57:28,256 --> 12:57:30,480
Multiple List
17311
12:57:30,480 --> 12:57:32,700
and we put here t
17312
12:57:32,700 --> 12:57:34,080
right
17313
12:57:34,080 --> 12:57:36,480
now I'm going to put curly braces
17314
12:57:36,480 --> 12:57:38,040
because I'm going to define a function
17315
12:57:38,040 --> 12:57:41,640
inside our class team so here we type
17316
12:57:41,640 --> 12:57:44,460
fun and I'm going to call this function
17317
12:57:44,460 --> 12:57:45,540
add
17318
12:57:45,540 --> 12:57:47,400
players so this function is going to add
17319
12:57:47,400 --> 12:57:49,380
players to the team
17320
12:57:49,380 --> 12:57:51,540
we put parentheses and here we Define a
17321
12:57:51,540 --> 12:57:54,000
parameter called called player and it's
17322
12:57:54,000 --> 12:57:56,640
going to be of type T so it's going to
17323
12:57:56,640 --> 12:57:58,080
be
17324
12:57:58,080 --> 12:58:00,740
should put
17325
12:58:00,776 --> 12:58:02,640
a colon here
17326
12:58:02,640 --> 12:58:06,540
and we put curly braces
17327
12:58:06,540 --> 12:58:08,400
and this is going to receive a parameter
17328
12:58:08,400 --> 12:58:10,560
of type T and this type T that we Define
17329
12:58:10,560 --> 12:58:13,500
here and here and here is going to be
17330
12:58:13,500 --> 12:58:16,680
the type the specific type that we gonna
17331
12:58:16,680 --> 12:58:18,900
pass when we create an instance with our
17332
12:58:18,900 --> 12:58:21,480
class team so
17333
12:58:21,480 --> 12:58:24,000
first here we're gonna check if
17334
12:58:24,000 --> 12:58:26,276
the
17335
12:58:26,276 --> 12:58:27,960
players
17336
12:58:27,960 --> 12:58:31,800
so our list of players contains this
17337
12:58:31,800 --> 12:58:33,960
player that is passed our ad player
17338
12:58:33,960 --> 12:58:36,776
function because if or you already have
17339
12:58:36,776 --> 12:58:39,180
that player in our list We're Not Gonna
17340
12:58:39,180 --> 12:58:41,880
add it to our list so if player and to
17341
12:58:41,880 --> 12:58:44,160
check if that player is in the list we
17342
12:58:44,160 --> 12:58:48,300
call the contains function on our uh on
17343
12:58:48,300 --> 12:58:50,480
on our uh
17344
12:58:50,480 --> 12:58:54,300
Multiple List so here we we type players
17345
12:58:54,300 --> 12:58:58,500
that contains player the argument
17346
12:58:58,500 --> 12:59:01,800
and if that is true I'm gonna put curly
17347
12:59:01,800 --> 12:59:03,000
braces
17348
12:59:03,000 --> 12:59:04,980
and here we're gonna output something
17349
12:59:04,980 --> 12:59:06,840
because to the console so we type here
17350
12:59:06,840 --> 12:59:08,276
print line
17351
12:59:08,276 --> 12:59:12,480
quotation marks and we type player
17352
12:59:12,480 --> 12:59:16,080
colon and then we put dollar sign curly
17353
12:59:16,080 --> 12:59:17,460
braces
17354
12:59:17,460 --> 12:59:20,580
and here we need to do something which
17355
12:59:20,580 --> 12:59:24,596
is called casting and casting casting is
17356
12:59:24,596 --> 12:59:28,580
used when you want a variable to be
17357
12:59:28,580 --> 12:59:32,640
treated as a certain type of variable so
17358
12:59:32,640 --> 12:59:35,160
when you want a variable to be treated
17359
12:59:35,160 --> 12:59:37,800
by the component by the compiler as a
17360
12:59:37,800 --> 12:59:42,416
certain type of object or or a class so
17361
12:59:42,416 --> 12:59:45,596
we type here player
17362
12:59:45,596 --> 12:59:48,360
because let's let's let me show you why
17363
12:59:48,360 --> 12:59:50,700
we need to do that because if I type
17364
12:59:50,700 --> 12:59:53,580
here player now I want to get the name
17365
12:59:53,580 --> 12:59:56,936
and if I put here dot name as you can
17366
12:59:56,936 --> 13:00:00,116
see we don't have any name here because
17367
13:00:00,116 --> 13:00:03,300
because this type because this T and
17368
13:00:03,300 --> 13:00:06,000
this T that we have here is generic the
17369
13:00:06,000 --> 13:00:09,180
compiler has no idea if this T that is
17370
13:00:09,180 --> 13:00:11,220
going to be passed here so this piece so
17371
13:00:11,220 --> 13:00:12,660
if you pass here
17372
13:00:12,660 --> 13:00:15,360
a baseball player or football player it
17373
13:00:15,360 --> 13:00:18,540
has no idea if at this moment now if
17374
13:00:18,540 --> 13:00:21,116
that is gonna have uh is gonna have a
17375
13:00:21,116 --> 13:00:24,180
name property on it this is why when you
17376
13:00:24,180 --> 13:00:25,800
type here that's name we don't get
17377
13:00:25,800 --> 13:00:29,040
nothing and to solve that we can say to
17378
13:00:29,040 --> 13:00:31,740
the compiler for them to the compiler to
17379
13:00:31,740 --> 13:00:34,980
treat this parameter here so this is
17380
13:00:34,980 --> 13:00:36,840
going to be a value pass to this
17381
13:00:36,840 --> 13:00:41,520
parameter as a player and in that sense
17382
13:00:41,520 --> 13:00:43,320
in that in that way you can get access
17383
13:00:43,320 --> 13:00:45,900
to the name property so we type here
17384
13:00:45,900 --> 13:00:47,400
player and
17385
13:00:47,400 --> 13:00:51,540
to cast to cast a variable to a certain
17386
13:00:51,540 --> 13:00:54,300
type we put as so we put
17387
13:00:54,300 --> 13:00:58,560
the keyword as and the type player
17388
13:00:58,560 --> 13:01:01,500
and we put this in parentheses so we
17389
13:01:01,500 --> 13:01:04,140
surround our expression in parentheses
17390
13:01:04,140 --> 13:01:07,580
inside the curly braces
17391
13:01:08,040 --> 13:01:11,820
and now here at the end of of the of the
17392
13:01:11,820 --> 13:01:13,916
end of the enclosing parentheses you put
17393
13:01:13,916 --> 13:01:16,200
that and now we have access to name
17394
13:01:16,200 --> 13:01:19,080
because we settle the compiler hey treat
17395
13:01:19,080 --> 13:01:21,360
this player variable I don't know
17396
13:01:21,360 --> 13:01:23,700
because I don't know if this is going to
17397
13:01:23,700 --> 13:01:27,180
be it's going to be a player
17398
13:01:27,180 --> 13:01:29,460
a player object passed here or a
17399
13:01:29,460 --> 13:01:32,160
baseball player or or a football player
17400
13:01:32,160 --> 13:01:34,800
treat this player variable that is
17401
13:01:34,800 --> 13:01:37,020
passed here as because here we have
17402
13:01:37,020 --> 13:01:39,060
teeth so we don't know what it is three
17403
13:01:39,060 --> 13:01:41,276
display to display a variable as a
17404
13:01:41,276 --> 13:01:43,500
player so you can use the name of a
17405
13:01:43,500 --> 13:01:48,596
variable and we're gonna say is already
17406
13:01:48,596 --> 13:01:52,680
in the TM and we can put here
17407
13:01:52,680 --> 13:01:55,080
the team and we can type here dollar
17408
13:01:55,080 --> 13:02:00,480
sign this Dot name so the name of the TM
17409
13:02:00,480 --> 13:02:04,560
else if if our player is not in the team
17410
13:02:04,560 --> 13:02:06,720
we put else
17411
13:02:06,720 --> 13:02:08,820
we're going to press enter to add the
17412
13:02:08,820 --> 13:02:11,700
right curly brace we're gonna add this
17413
13:02:11,700 --> 13:02:14,040
player to the list because if the if
17414
13:02:14,040 --> 13:02:16,276
fails that means that the player is not
17415
13:02:16,276 --> 13:02:19,140
present in our list so we type here
17416
13:02:19,140 --> 13:02:21,060
players
17417
13:02:21,060 --> 13:02:22,616
our
17418
13:02:22,616 --> 13:02:26,820
Multiple List dot add this player that
17419
13:02:26,820 --> 13:02:30,540
is passed player and we're also going to
17420
13:02:30,540 --> 13:02:32,160
output something to go to the console so
17421
13:02:32,160 --> 13:02:34,320
you add here print line
17422
13:02:34,320 --> 13:02:36,300
and uh
17423
13:02:36,300 --> 13:02:38,880
I'm gonna copy this because I'm just
17424
13:02:38,880 --> 13:02:41,756
gonna change the text a little bit
17425
13:02:41,756 --> 13:02:44,160
so let's delete this
17426
13:02:44,160 --> 13:02:49,820
player and so here is we're gonna say
17427
13:02:50,040 --> 13:02:52,140
instead of it's already
17428
13:02:52,140 --> 13:02:53,880
I'm gonna say it was
17429
13:02:53,880 --> 13:02:55,980
added
17430
13:02:55,980 --> 13:02:58,820
in the team
17431
13:03:01,020 --> 13:03:04,080
now let's create some objects with those
17432
13:03:04,080 --> 13:03:06,240
classes that we have here so first I'm
17433
13:03:06,240 --> 13:03:09,596
going to create some some players so I'm
17434
13:03:09,596 --> 13:03:10,500
going to create first the football
17435
13:03:10,500 --> 13:03:12,660
player so type here
17436
13:03:12,660 --> 13:03:16,560
Val for the ball player equals football
17437
13:03:16,560 --> 13:03:18,840
player
17438
13:03:18,840 --> 13:03:20,700
and
17439
13:03:20,700 --> 13:03:22,800
for the name I'm going to use a generic
17440
13:03:22,800 --> 13:03:26,720
name I'm gonna type just football
17441
13:03:36,240 --> 13:03:39,079
player
17442
13:03:39,180 --> 13:03:41,160
one
17443
13:03:41,160 --> 13:03:43,380
and I'm gonna press Ctrl D to create
17444
13:03:43,380 --> 13:03:46,020
another football player so football
17445
13:03:46,020 --> 13:03:49,820
player two is going to be called
17446
13:03:50,640 --> 13:03:52,320
and I'm gonna create two baseball
17447
13:03:52,320 --> 13:03:54,900
players so we type here Val
17448
13:03:54,900 --> 13:03:57,900
baseball player equals
17449
13:03:57,900 --> 13:04:00,960
baseball player
17450
13:04:00,960 --> 13:04:03,720
and we type here
17451
13:04:03,720 --> 13:04:06,256
baseball
17452
13:04:06,960 --> 13:04:10,616
player one and I'm gonna call Press Ctrl
17453
13:04:10,616 --> 13:04:14,300
D here also I'm gonna
17454
13:04:15,180 --> 13:04:17,880
call this baseball player 2 and I'm
17455
13:04:17,880 --> 13:04:21,079
gonna change this to 2.
17456
13:04:21,840 --> 13:04:24,000
now I'm going to create an instance of
17457
13:04:24,000 --> 13:04:26,220
our team class so we cut we type here
17458
13:04:26,220 --> 13:04:29,040
Vault team and this is going to be a
17459
13:04:29,040 --> 13:04:31,140
team of football players so we type Val
17460
13:04:31,140 --> 13:04:32,400
team
17461
13:04:32,400 --> 13:04:33,776
equals
17462
13:04:33,776 --> 13:04:37,080
and hero type team
17463
13:04:37,080 --> 13:04:39,416
and we put now because as you can see
17464
13:04:39,416 --> 13:04:42,596
it's expecting a type here and the type
17465
13:04:42,596 --> 13:04:44,220
is going to be
17466
13:04:44,220 --> 13:04:47,340
football player so it's gonna be a team
17467
13:04:47,340 --> 13:04:50,340
with what it's gonna be a team with the
17468
13:04:50,340 --> 13:04:52,980
football players and you're gonna Define
17469
13:04:52,980 --> 13:04:54,116
a name
17470
13:04:54,116 --> 13:04:58,040
also generic name football
17471
13:04:58,680 --> 13:05:01,140
team is going to be called
17472
13:05:01,140 --> 13:05:03,060
and they also need to pass an array
17473
13:05:03,060 --> 13:05:04,680
there
17474
13:05:04,680 --> 13:05:07,200
and for this array I'm gonna type just
17475
13:05:07,200 --> 13:05:08,820
uh
17476
13:05:08,820 --> 13:05:12,616
mod multiple array of
17477
13:05:13,560 --> 13:05:15,900
we can't
17478
13:05:15,900 --> 13:05:18,900
foreign
17479
13:05:22,380 --> 13:05:25,020
list of
17480
13:05:25,020 --> 13:05:28,820
and here I'm gonna pass our
17481
13:05:29,880 --> 13:05:32,580
football player I'm gonna pass only our
17482
13:05:32,580 --> 13:05:35,900
first football player here
17483
13:05:37,800 --> 13:05:41,700
now I'm going to create another team Val
17484
13:05:41,700 --> 13:05:45,240
team uh let's actually call this first
17485
13:05:45,240 --> 13:05:50,160
team football team so let's type here
17486
13:05:50,160 --> 13:05:54,080
food bowl
17487
13:05:58,560 --> 13:06:00,000
football team
17488
13:06:00,000 --> 13:06:03,616
now if you run this code
17489
13:06:10,256 --> 13:06:12,660
we don't get it on any output because
17490
13:06:12,660 --> 13:06:15,300
our ad player's function wasn't called
17491
13:06:15,300 --> 13:06:17,640
but if I
17492
13:06:17,640 --> 13:06:21,320
type here a football team
17493
13:06:26,040 --> 13:06:26,756
foreign
17494
13:06:26,756 --> 13:06:30,060
actually here football team
17495
13:06:30,060 --> 13:06:32,220
that
17496
13:06:32,220 --> 13:06:34,560
add players and I'm gonna add football
17497
13:06:34,560 --> 13:06:37,340
player too
17498
13:06:39,240 --> 13:06:42,615
now if I run this code
17499
13:06:47,340 --> 13:06:50,880
we get football player two was actually
17500
13:06:50,880 --> 13:06:54,255
here should be added
17501
13:06:56,040 --> 13:06:58,616
football player 2 was added in the team
17502
13:06:58,616 --> 13:07:01,200
football team so our code works well and
17503
13:07:01,200 --> 13:07:04,560
we use generic types for our classes
17504
13:07:04,560 --> 13:07:07,980
here we specified only the T and this T
17505
13:07:07,980 --> 13:07:10,080
that we have here was replaced with
17506
13:07:10,080 --> 13:07:13,680
football player that we Define here and
17507
13:07:13,680 --> 13:07:14,520
um
17508
13:07:14,520 --> 13:07:16,740
uh we're gonna do the same thing we're
17509
13:07:16,740 --> 13:07:18,416
gonna create another theme for the
17510
13:07:18,416 --> 13:07:21,540
baseball player and we're also going to
17511
13:07:21,540 --> 13:07:22,500
look at
17512
13:07:22,500 --> 13:07:25,616
how we can restrict the types that are
17513
13:07:25,616 --> 13:07:27,776
going to be passed to our generic type
17514
13:07:27,776 --> 13:07:29,040
here
17515
13:07:29,040 --> 13:07:30,540
so now I'm going to create a baseball
17516
13:07:30,540 --> 13:07:32,936
team so I'm going to type here Val
17517
13:07:32,936 --> 13:07:36,020
baseball team
17518
13:07:37,500 --> 13:07:41,540
put equals then you put Here theme
17519
13:07:44,520 --> 13:07:47,160
and here we type for the generic type T
17520
13:07:47,160 --> 13:07:49,740
baseball player because this is gonna
17521
13:07:49,740 --> 13:07:52,560
store baseball players and we Define a
17522
13:07:52,560 --> 13:07:53,756
name here the name is going to be
17523
13:07:53,756 --> 13:07:57,436
baseball team or generic name
17524
13:07:58,340 --> 13:08:02,820
and uh also you need to type to pass a
17525
13:08:02,820 --> 13:08:06,116
multiple list so type Multiple List
17526
13:08:06,116 --> 13:08:11,480
and we pass here a baseball player
17527
13:08:13,200 --> 13:08:15,540
but as you can see
17528
13:08:15,540 --> 13:08:18,116
here for the type
17529
13:08:18,116 --> 13:08:20,820
the type here is great oh that is
17530
13:08:20,820 --> 13:08:21,900
because
17531
13:08:21,900 --> 13:08:25,616
here we pass the football player
17532
13:08:25,616 --> 13:08:28,140
uh object that we create then here we
17533
13:08:28,140 --> 13:08:29,700
pass the baseball player
17534
13:08:29,700 --> 13:08:32,580
because kotlin has Type inference it can
17535
13:08:32,580 --> 13:08:36,116
infer the type as being a team of
17536
13:08:36,116 --> 13:08:37,800
football players so we don't need to put
17537
13:08:37,800 --> 13:08:40,560
here for the player we can delete this
17538
13:08:40,560 --> 13:08:43,880
you can delete all of this
17539
13:08:44,400 --> 13:08:46,256
and now our code still works and you can
17540
13:08:46,256 --> 13:08:48,060
see we have this type here which says
17541
13:08:48,060 --> 13:08:50,756
team football player so it's a team of
17542
13:08:50,756 --> 13:08:53,580
football players and it's uh inferred
17543
13:08:53,580 --> 13:08:55,740
it's inferred automatically by quoting
17544
13:08:55,740 --> 13:08:58,320
for us and you can do the same thing if
17545
13:08:58,320 --> 13:09:00,480
here for a baseball player so I'm gonna
17546
13:09:00,480 --> 13:09:01,860
delete this
17547
13:09:01,860 --> 13:09:03,840
as you can see now we also have the hint
17548
13:09:03,840 --> 13:09:07,380
team baseball player now let's add to
17549
13:09:07,380 --> 13:09:09,416
the baseball player
17550
13:09:09,416 --> 13:09:12,660
let's call the baseball player function
17551
13:09:12,660 --> 13:09:15,300
let's put here a
17552
13:09:15,300 --> 13:09:19,020
so we type here base bullet
17553
13:09:19,020 --> 13:09:22,140
team let's call the function dot add
17554
13:09:22,140 --> 13:09:25,500
players and hero type baseball player 2
17555
13:09:25,500 --> 13:09:28,320
because first one was added there
17556
13:09:28,320 --> 13:09:30,776
so now if you run the squad
17557
13:09:30,776 --> 13:09:35,115
not debug so run this code
17558
13:09:37,500 --> 13:09:40,580
let's increase this
17559
13:09:41,340 --> 13:09:44,040
we get let's actually decrease this so
17560
13:09:44,040 --> 13:09:46,860
let's put here we get player football
17561
13:09:46,860 --> 13:09:49,140
player 2 was added to the team football
17562
13:09:49,140 --> 13:09:52,860
team so we have this code here
17563
13:09:52,860 --> 13:09:56,220
which is called and it says that the
17564
13:09:56,220 --> 13:09:58,560
the football player was added to the
17565
13:09:58,560 --> 13:10:00,000
team and the name of the football team
17566
13:10:00,000 --> 13:10:02,400
and then we have player and we have
17567
13:10:02,400 --> 13:10:05,276
baseball player player two
17568
13:10:05,276 --> 13:10:06,840
so this
17569
13:10:06,840 --> 13:10:08,880
this one
17570
13:10:08,880 --> 13:10:10,500
that we have here
17571
13:10:10,500 --> 13:10:12,416
that we pass here
17572
13:10:12,416 --> 13:10:14,640
baseball team that was this to the
17573
13:10:14,640 --> 13:10:16,380
baseball team as you can see it says
17574
13:10:16,380 --> 13:10:18,540
player baseball player 2 was added to
17575
13:10:18,540 --> 13:10:19,860
the baseball team
17576
13:10:19,860 --> 13:10:22,500
and what generics helps out to do
17577
13:10:22,500 --> 13:10:24,840
because I said they can help the
17578
13:10:24,840 --> 13:10:27,360
compiler to figure out errors if you try
17579
13:10:27,360 --> 13:10:28,916
to
17580
13:10:28,916 --> 13:10:32,700
pass here baseball TM Dot
17581
13:10:32,700 --> 13:10:35,700
add football player so we type here
17582
13:10:35,700 --> 13:10:37,616
football player
17583
13:10:37,616 --> 13:10:40,560
now you get an error because
17584
13:10:40,560 --> 13:10:45,660
the generic type enforces the type on
17585
13:10:45,660 --> 13:10:48,116
the the
17586
13:10:48,116 --> 13:10:50,340
on the class because here we said
17587
13:10:50,340 --> 13:10:53,220
explicitly that this is because this was
17588
13:10:53,220 --> 13:10:55,020
inferred to be as a baseball player if
17589
13:10:55,020 --> 13:10:56,936
you cannot pass here a football player
17590
13:10:56,936 --> 13:11:00,240
in our baseball team because this is in
17591
13:11:00,240 --> 13:11:02,040
first because you said Siri here
17592
13:11:02,040 --> 13:11:05,040
explicitly explicitly because first
17593
13:11:05,040 --> 13:11:07,740
because this was inferred from here so
17594
13:11:07,740 --> 13:11:10,020
we have team baseball player so we
17595
13:11:10,020 --> 13:11:11,936
cannot add here a football brain inside
17596
13:11:11,936 --> 13:11:13,860
our team football player and this is
17597
13:11:13,860 --> 13:11:15,596
very very
17598
13:11:15,596 --> 13:11:18,180
powerful for us because in this way we
17599
13:11:18,180 --> 13:11:21,000
don't do things which doesn't make sense
17600
13:11:21,000 --> 13:11:24,416
and the the same is true about our
17601
13:11:24,416 --> 13:11:28,560
football team so if on our football team
17602
13:11:28,560 --> 13:11:30,240
so if enough of working with a football
17603
13:11:30,240 --> 13:11:32,220
team
17604
13:11:32,220 --> 13:11:34,560
dot add
17605
13:11:34,560 --> 13:11:38,479
and here to put baseball player
17606
13:11:39,180 --> 13:11:42,360
we get an error because we specified we
17607
13:11:42,360 --> 13:11:45,060
specified that because the generic type
17608
13:11:45,060 --> 13:11:46,740
was
17609
13:11:46,740 --> 13:11:49,380
in fair to be a football player so we
17610
13:11:49,380 --> 13:11:51,300
cannot put a baseball player in our team
17611
13:11:51,300 --> 13:11:54,240
of football players so this is what
17612
13:11:54,240 --> 13:11:57,596
generics is doing is figure out those
17613
13:11:57,596 --> 13:12:00,000
errors for us at compile time and this
17614
13:12:00,000 --> 13:12:02,340
is very powerful because we cannot we
17615
13:12:02,340 --> 13:12:04,740
can run our code and we don't get in
17616
13:12:04,740 --> 13:12:06,616
those some
17617
13:12:06,616 --> 13:12:09,840
scenarios where we have a bay of
17618
13:12:09,840 --> 13:12:12,300
baseball player added to a baseball team
17619
13:12:12,300 --> 13:12:15,840
or maybe other examples there so I'm
17620
13:12:15,840 --> 13:12:17,700
gonna delete this
17621
13:12:17,700 --> 13:12:19,916
next we're gonna look at upper bounds
17622
13:12:19,916 --> 13:12:21,480
because
17623
13:12:21,480 --> 13:12:25,500
our upper bounds basically allows us to
17624
13:12:25,500 --> 13:12:29,040
restrict what kind of type is passed to
17625
13:12:29,040 --> 13:12:31,400
our generic type here so
17626
13:12:31,400 --> 13:12:34,256
and particularly gonna see how this is
17627
13:12:34,256 --> 13:12:36,000
gonna affect this code that we use here
17628
13:12:36,000 --> 13:12:41,040
to cast so to to add an upper bound so
17629
13:12:41,040 --> 13:12:43,680
to restrict what kind of type is passed
17630
13:12:43,680 --> 13:12:46,140
to this type we can put
17631
13:12:46,140 --> 13:12:51,060
colon so extends and here you can we can
17632
13:12:51,060 --> 13:12:52,860
type player
17633
13:12:52,860 --> 13:12:56,520
so what we are saying here is that
17634
13:12:56,520 --> 13:12:58,380
every
17635
13:12:58,380 --> 13:13:00,416
type that is going to be passed here
17636
13:13:00,416 --> 13:13:03,060
when you create an instance like
17637
13:13:03,060 --> 13:13:07,020
we did here so but only that this was in
17638
13:13:07,020 --> 13:13:10,020
Fair here this type that is going to be
17639
13:13:10,020 --> 13:13:13,320
passed here needs needs to inherit from
17640
13:13:13,320 --> 13:13:16,256
the player class otherwise this type is
17641
13:13:16,256 --> 13:13:19,200
not going to work so because of that
17642
13:13:19,200 --> 13:13:22,616
because the compiler now knows that and
17643
13:13:22,616 --> 13:13:25,256
enforces this that this type that is
17644
13:13:25,256 --> 13:13:27,360
going to be passed here is is going to
17645
13:13:27,360 --> 13:13:29,900
be is going to inherit from the player
17646
13:13:29,900 --> 13:13:34,380
class we we can't delete this cast
17647
13:13:34,380 --> 13:13:37,616
because now it knows that that type is
17648
13:13:37,616 --> 13:13:40,080
gonna have uh it's gonna be it's gonna
17649
13:13:40,080 --> 13:13:41,820
inherit from the player class so it's
17650
13:13:41,820 --> 13:13:43,916
gonna have the property name so if I
17651
13:13:43,916 --> 13:13:46,399
delete this
17652
13:13:46,680 --> 13:13:49,800
and if I delete this so let's put name
17653
13:13:49,800 --> 13:13:52,040
here
17654
13:13:53,400 --> 13:13:56,000
now
17655
13:13:57,116 --> 13:14:00,500
so let's delete these two
17656
13:14:02,936 --> 13:14:04,680
should be player here
17657
13:14:04,680 --> 13:14:06,660
player
17658
13:14:06,660 --> 13:14:08,700
so now we have player the name because
17659
13:14:08,700 --> 13:14:11,160
it knows that this type so this type
17660
13:14:11,160 --> 13:14:12,660
that is going to be passed series is
17661
13:14:12,660 --> 13:14:14,756
going to inherit from the player cards
17662
13:14:14,756 --> 13:14:16,860
from the player class so it's gonna have
17663
13:14:16,860 --> 13:14:19,080
the property name so you don't need to
17664
13:14:19,080 --> 13:14:24,360
do this the weird this word cast is
17665
13:14:24,360 --> 13:14:27,720
overcasting so I'm gonna delete this
17666
13:14:27,720 --> 13:14:29,820
and now as you can see we don't have uh
17667
13:14:29,820 --> 13:14:32,276
any arrows so now we have the name here
17668
13:14:32,276 --> 13:14:34,800
because now it knows that whatever type
17669
13:14:34,800 --> 13:14:37,500
you pass here it has to inherit from the
17670
13:14:37,500 --> 13:14:40,680
player class so if I go down here and I
17671
13:14:40,680 --> 13:14:43,160
create another class called gamer
17672
13:14:43,160 --> 13:14:46,520
player so
17673
13:14:46,680 --> 13:14:50,000
we type here class
17674
13:14:50,220 --> 13:14:54,000
let's call it games player
17675
13:14:54,000 --> 13:14:57,180
and it's gonna have a Val
17676
13:14:57,180 --> 13:14:59,276
is going to be
17677
13:14:59,276 --> 13:15:01,916
actually let's define a parameter only
17678
13:15:01,916 --> 13:15:03,180
name
17679
13:15:03,180 --> 13:15:05,400
is going to be a string
17680
13:15:05,400 --> 13:15:08,640
and we will not inherit from our player
17681
13:15:08,640 --> 13:15:12,116
class let's see what happens so
17682
13:15:12,116 --> 13:15:14,340
if I um
17683
13:15:14,340 --> 13:15:17,520
if I uh create here another theme let's
17684
13:15:17,520 --> 13:15:19,700
call it Val
17685
13:15:19,700 --> 13:15:25,020
let's call it the game Steam
17686
13:15:25,020 --> 13:15:27,900
so I'm gonna
17687
13:15:27,900 --> 13:15:32,040
type here team and we're gonna specify
17688
13:15:32,040 --> 13:15:33,000
here
17689
13:15:33,000 --> 13:15:35,880
games player
17690
13:15:35,880 --> 13:15:38,160
now if if I put now the primary
17691
13:15:38,160 --> 13:15:40,020
Constructor here here we need to define
17692
13:15:40,020 --> 13:15:42,560
a name
17693
13:15:42,596 --> 13:15:44,936
let's call it
17694
13:15:44,936 --> 13:15:46,500
games
17695
13:15:46,500 --> 13:15:48,000
team
17696
13:15:48,000 --> 13:15:50,160
and
17697
13:15:50,160 --> 13:15:53,960
multiple list of
17698
13:15:56,580 --> 13:15:58,616
now
17699
13:15:58,616 --> 13:16:00,596
here we have an error and if you look it
17700
13:16:00,596 --> 13:16:01,520
says
17701
13:16:01,520 --> 13:16:05,820
expected player found games player and
17702
13:16:05,820 --> 13:16:09,416
this happens because our games player
17703
13:16:09,416 --> 13:16:11,820
that we have here is not inheriting from
17704
13:16:11,820 --> 13:16:14,040
the player class and because we said
17705
13:16:14,040 --> 13:16:16,860
here that the upper bound is this T that
17706
13:16:16,860 --> 13:16:19,740
is passed here so this T is this here
17707
13:16:19,740 --> 13:16:23,340
has to inherit from the player class
17708
13:16:23,340 --> 13:16:26,936
otherwise if you if you don't uh inherit
17709
13:16:26,936 --> 13:16:29,276
from the player class it will not work
17710
13:16:29,276 --> 13:16:32,340
because you said here that it it must
17711
13:16:32,340 --> 13:16:35,160
inherit from the player class now if I
17712
13:16:35,160 --> 13:16:36,660
change this to
17713
13:16:36,660 --> 13:16:38,820
if I change this down here to inherit
17714
13:16:38,820 --> 13:16:40,380
from the player class so it I hear
17715
13:16:40,380 --> 13:16:42,860
player
17716
13:16:43,680 --> 13:16:46,380
we type here name
17717
13:16:46,380 --> 13:16:48,540
now as you can see the error disappears
17718
13:16:48,540 --> 13:16:51,596
because now it's respecting the upper
17719
13:16:51,596 --> 13:16:54,776
bound that we set we set up here which
17720
13:16:54,776 --> 13:16:56,400
is T
17721
13:16:56,400 --> 13:16:59,340
meaning games player has to inherit from
17722
13:16:59,340 --> 13:17:01,860
the player class and down here it's
17723
13:17:01,860 --> 13:17:05,040
inheriting so this is our discussion
17724
13:17:05,040 --> 13:17:08,700
about how we can use generics and how we
17725
13:17:08,700 --> 13:17:10,680
can use how you can Define upper bounds
17726
13:17:10,680 --> 13:17:13,200
and in the next videos you're gonna look
17727
13:17:13,200 --> 13:17:13,980
at
17728
13:17:13,980 --> 13:17:14,820
um
17729
13:17:14,820 --> 13:17:19,140
type erasures and you're going to lose
17730
13:17:19,140 --> 13:17:21,360
what reified
17731
13:17:21,360 --> 13:17:24,360
a keyword and how you can inline our
17732
13:17:24,360 --> 13:17:26,220
function with because you need to inline
17733
13:17:26,220 --> 13:17:29,096
your function when the use refine the
17734
13:17:29,096 --> 13:17:32,400
keyword so see you in the next video
17735
13:17:32,400 --> 13:17:34,680
well actually before I end the video I
17736
13:17:34,680 --> 13:17:36,960
should say why this uh genetics is
17737
13:17:36,960 --> 13:17:38,640
important why
17738
13:17:38,640 --> 13:17:42,240
because if you think about because here
17739
13:17:42,240 --> 13:17:44,460
you can type we can define a generic
17740
13:17:44,460 --> 13:17:46,740
type we don't need to create
17741
13:17:46,740 --> 13:17:51,180
um a separate class for for every team
17742
13:17:51,180 --> 13:17:54,840
that that we want to use so we don't
17743
13:17:54,840 --> 13:17:57,116
need to create a football team we don't
17744
13:17:57,116 --> 13:18:00,060
need to create a separate baseball team
17745
13:18:00,060 --> 13:18:02,300
we don't need to create a
17746
13:18:02,300 --> 13:18:07,500
separate games team we we just create a
17747
13:18:07,500 --> 13:18:10,020
team we Define the generic type T and
17748
13:18:10,020 --> 13:18:14,756
here we pass our we pass our
17749
13:18:14,756 --> 13:18:17,700
our type and in this way we can use only
17750
13:18:17,700 --> 13:18:18,960
one class
17751
13:18:18,960 --> 13:18:22,500
and we can we can have all of those uh
17752
13:18:22,500 --> 13:18:26,640
those uh baseball team we can have the
17753
13:18:26,640 --> 13:18:28,916
football team we can have just by having
17754
13:18:28,916 --> 13:18:31,200
the the type here which is generic
17755
13:18:31,200 --> 13:18:33,596
otherwise we'll have to create separate
17756
13:18:33,596 --> 13:18:35,660
classes for each of those
17757
13:18:35,660 --> 13:18:38,096
teams so
17758
13:18:38,096 --> 13:18:39,776
see you in the next video and next video
17759
13:18:39,776 --> 13:18:41,160
as I said we're gonna look at type
17760
13:18:41,160 --> 13:18:44,700
erasers and refine the verified the
17761
13:18:44,700 --> 13:18:48,300
keyword and the inline functions and in
17762
13:18:48,300 --> 13:18:50,040
the next videos we're going to look also
17763
13:18:50,040 --> 13:18:52,560
at covariance and counter variance so
17764
13:18:52,560 --> 13:18:55,399
see in the next video
1277308
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.