WEBVTT

00:00.710 --> 00:01.340
Hi everyone.

00:01.340 --> 00:02.620
Welcome back to this tutorial.

00:02.630 --> 00:06.410
In this tutorial, I'll give you all an introduction about optional.

00:06.800 --> 00:14.510
Optional is introduced as part of Java eight and this is used to represent a non null value in Java.

00:15.270 --> 00:21.000
The main purpose of this one is to provide a way to avoid the most common exception, which is the null

00:21.000 --> 00:26.160
pointer exception and avoid having too many null checks in your code.

00:26.190 --> 00:32.730
Any experienced developer would have come across a null pointer exception at some point in their life.

00:35.020 --> 00:42.730
The concept of optional is inspired from the other new languages such as Scala, Groovy and Eccetera.

00:42.760 --> 00:45.940
Let's go ahead and take a look at an example.

00:46.880 --> 00:48.230
Apparently English open.

00:48.230 --> 00:53.450
What I'm going to do, I'm going to create a package called optional.

00:55.790 --> 00:58.430
Package all optional.

00:58.430 --> 01:02.750
And then I'm going to name a class called Optional Example.

01:05.210 --> 01:06.990
Okay, that's good.

01:07.010 --> 01:11.900
Then I'm going to make this class executable by adding public static void main method.

01:16.040 --> 01:21.050
The use case that we are going to code is that I'm going to create a method that is going to return

01:21.050 --> 01:26.750
me a string, which is going to be the name of the student, and then I'm going to print the length

01:26.750 --> 01:28.220
of that name.

01:29.920 --> 01:31.420
So I'm going to create a method here.

01:31.430 --> 01:41.720
The method name is going to be public static, and this is going to return the name of the student and

01:41.720 --> 01:47.090
the return type is going to be string, and the name of the method is going to be get student name.

01:48.520 --> 01:51.470
So I'm going to use the student database class.

01:53.520 --> 01:53.960
Yard.

01:53.970 --> 01:57.240
There is a student supplier which just supplies a student.

01:57.420 --> 01:58.090
Take a look at it.

01:58.110 --> 02:01.430
We are receiving a student called Adam as part of this method.

02:01.440 --> 02:08.010
But this supplier is going to supply the student object and I'm going to call the get method.

02:08.340 --> 02:11.190
So let's consider this one as like a database call.

02:11.190 --> 02:12.690
You're making a database call here.

02:12.690 --> 02:16.230
But actually, behind the scenes, it is just a method which is running the student.

02:16.230 --> 02:20.790
But let's assume this is going to make the database call and then returns the student.

02:20.880 --> 02:21.750
Right.

02:23.250 --> 02:25.260
And let's give it a name.

02:25.920 --> 02:30.060
So if the student is not found, this is going to return null.

02:30.060 --> 02:33.030
So what I'm going to do, I'm going to have the null check here.

02:33.030 --> 02:39.840
If the student is not equal to null, then return.

02:42.100 --> 02:44.890
Student dot getName.

02:45.790 --> 02:46.470
Okay.

02:46.480 --> 02:47.440
Else.

02:48.290 --> 02:49.970
I'm going to return null.

02:50.450 --> 02:53.930
Otherwise, instead of else, I'm going to put the return statement below.

02:55.190 --> 02:56.300
Return null.

02:57.620 --> 03:02.270
Okay, so let's call this method and print the length of the student name.

03:02.300 --> 03:04.430
That's the actual use case, right?

03:04.430 --> 03:05.960
Student Not the student.

03:05.960 --> 03:10.190
It's going to be string name equal to get student name.

03:11.290 --> 03:12.040
Sounds good.

03:12.040 --> 03:16.930
And then what I'm going to do, then I'm going to print the length of the student.

03:17.890 --> 03:21.730
Length of the student name.

03:23.800 --> 03:28.790
In here, what we are going to do, we are going to do name dot length.

03:28.810 --> 03:31.690
This is going to print the length of the student name.

03:31.780 --> 03:33.280
I'm going to run this example.

03:33.430 --> 03:34.150
There you go.

03:34.150 --> 03:35.740
It printed the length as four.

03:35.770 --> 03:41.650
If you go to the student supplier method, this is returning the student name as Adam, and the length

03:41.650 --> 03:43.510
of the name is equal to four.

03:44.580 --> 03:49.770
So what is going to happen is the student is not available in the database, right?

03:49.770 --> 03:52.350
So in that case, we are going to pass null.

03:53.340 --> 03:54.900
And then I'm going to run this program.

03:54.900 --> 03:56.220
Let's see what's going to happen.

03:57.210 --> 04:02.340
So this is going to give me the null pointer exception because I don't have the null check here.

04:02.610 --> 04:08.040
And what I'm going to do, I'm going to add the name not equal to null.

04:08.070 --> 04:09.720
Basically, I'm adding a null check.

04:10.150 --> 04:14.340
So if the name is not equal to null, get the length of the student.

04:14.370 --> 04:20.280
Else you can give name not found.

04:22.050 --> 04:24.000
Name not found.

04:25.110 --> 04:30.960
So now if you take a look at the student, we are having multiple null checks in many places.

04:30.990 --> 04:34.520
Let's say you want to get into the student activities.

04:34.530 --> 04:40.770
Then again, we have to check the length of that student array list and then print the student activities.

04:40.820 --> 04:47.040
So if this object is having many properties and if you want to access each and every property, then

04:47.040 --> 04:53.460
you might have to perform the null check of each and every property and it is going to bloat your code

04:53.460 --> 04:55.110
with all these null checks.

04:55.840 --> 05:00.630
Now we will handle the same scenario using optional and then we'll compare.

05:00.640 --> 05:03.250
What is the difference between these two methods?

05:03.280 --> 05:07.900
These two approaches and what are the advantages of one over the other?

05:08.440 --> 05:13.240
We want to create a method in here.

05:13.840 --> 05:19.120
It's going to be get student name optional.

05:19.330 --> 05:26.050
And in here, what I'm going to do, I'm going to call the optional class dot.

05:26.080 --> 05:31.390
There are many different ways of creating an optional object, but in here I'm going to use the off

05:31.390 --> 05:32.470
nullable method.

05:32.500 --> 05:37.180
We will be covering all these different methods that are part of the option in the future tutorial.

05:37.180 --> 05:41.830
But for now I'm going to use the optional nullable and then I'm going to call this.

05:41.860 --> 05:48.850
Basically what we are doing here is that we are wrapping the actual student object inside the optional

05:48.880 --> 05:49.540
right?

05:49.540 --> 05:55.790
So it's going to be optional of student and then let's give it a name.

05:55.790 --> 05:56.660
Student Optional.

05:56.660 --> 06:03.650
So if you take a look at the code, this code is basically taking this student object actually, and

06:03.650 --> 06:05.540
then wraps it with the optional object.

06:05.540 --> 06:06.650
That's what is happening.

06:08.290 --> 06:08.740
Right.

06:08.740 --> 06:14.470
So how do we check whether this option is having a value or not?

06:14.500 --> 06:21.310
How do we check in this case, whether this object has a optional has a student inside that optional

06:21.310 --> 06:22.090
object or not?

06:22.120 --> 06:28.030
There is a handy method which is available, which is called student optional Dot is present.

06:28.540 --> 06:35.620
This is to check whether is this optional object has any value inside that object or not.

06:35.650 --> 06:40.480
If it is there, what we are going to do, we are going to return the student name.

06:40.480 --> 06:40.710
Right?

06:40.720 --> 06:41.410
That's a use case.

06:41.410 --> 06:42.450
We are coding here.

06:42.460 --> 06:43.930
It is going to return.

06:43.930 --> 06:46.300
Student optional dot.

06:47.090 --> 06:47.570
Map.

06:47.570 --> 06:48.840
There is a handy method called map.

06:48.860 --> 06:51.520
If you take a look at it, there are many methods which are available.

06:51.530 --> 06:57.160
I'll be covering all these methods in the future tutorials, but for now we are going to use a map method.

06:57.170 --> 06:58.160
What do you want to map?

06:58.160 --> 06:59.360
We want to return the name.

06:59.360 --> 06:59.870
Right.

06:59.900 --> 07:04.220
Let's use the method reference syntax and then do the get name.

07:06.160 --> 07:09.220
So this is going to return the.

07:11.730 --> 07:12.750
So the name.

07:12.750 --> 07:20.340
But again, this is going to return the student name inside this optional so optional of type string.

07:20.340 --> 07:24.300
So here the return type is going to be optional of string.

07:24.390 --> 07:30.060
Instead of returning the plain old string, we are going to return the optional of string, which is

07:30.060 --> 07:32.150
going to actually hold the value.

07:32.160 --> 07:35.580
So in here we return the null right in here.

07:35.580 --> 07:40.710
What we are going to do, we are going to return optional of empty.

07:41.680 --> 07:44.110
Basically option of empty represents

07:46.780 --> 07:48.430
an optional object.

07:51.020 --> 07:51.980
But no value.

07:53.210 --> 07:54.620
That's what it represents.

07:55.430 --> 07:58.190
Okay, now I'm going to comment this code.

07:59.950 --> 08:01.060
Let's run this one.

08:02.230 --> 08:08.520
So it's going to return optional of string and let's give it a name.

08:08.530 --> 08:14.770
It's going to be string optional equal to get student name optional.

08:14.860 --> 08:17.410
So that's the method which we are going to call.

08:20.510 --> 08:29.300
So like how we perform the not null check we are going to perform string optional dot is present.

08:29.810 --> 08:32.030
If it is present, then get the name.

08:32.030 --> 08:33.800
So how do we get the actual value?

08:33.800 --> 08:39.250
The way to get it is by using string optional dot get.

08:39.260 --> 08:43.340
So in here we use a map method to get to the actual name of it.

08:43.340 --> 08:48.550
If you want to just get the student object itself, then you can call the get method.

08:48.560 --> 08:50.090
This will return the.

08:50.870 --> 08:51.680
Didn't object.

08:52.340 --> 08:58.490
Then Here, this will return the string, which is student name.

08:59.900 --> 09:00.350
Okay.

09:00.350 --> 09:01.910
And then what do you want to do?

09:01.910 --> 09:04.550
We want to calculate the length of the student.

09:04.550 --> 09:05.120
Right.

09:05.690 --> 09:09.680
Let's copy this code here and then put it here.

09:10.460 --> 09:13.580
So here we are, printing the length of the student name.

09:13.580 --> 09:16.430
Let's run this code and then check the result.

09:17.810 --> 09:21.020
If you take a look at it, the length of the student name is four.

09:22.570 --> 09:23.910
If it is not present.

09:23.910 --> 09:27.990
What we want, what we want to do in the case of else, we will provide.

09:29.510 --> 09:30.530
Name not found.

09:32.750 --> 09:33.230
Right.

09:33.230 --> 09:34.430
Let's run this and check.

09:35.700 --> 09:39.550
You run it, it's going to give you the length of the student of the name.

09:39.580 --> 09:41.760
Length of the student name, actually.

09:41.760 --> 09:46.530
And then what I'm going to do here, let's change this.

09:46.860 --> 09:52.140
Let's change this code to return the option of nullable.

09:53.660 --> 09:54.380
What are the values?

09:54.600 --> 09:55.920
What this is going to do.

09:55.920 --> 09:59.700
This is going to return optional dot empty.

10:00.510 --> 10:07.350
So when this code runs, when this is present, check is run, basically this is going to return false

10:07.350 --> 10:11.730
and then we are going to return the actual object.

10:11.730 --> 10:13.440
So instead of returning this, we can do this.

10:13.440 --> 10:19.080
Also, we can return the student optional or if we need the optional dot string, right?

10:19.080 --> 10:21.030
So this represents optional of student.

10:21.030 --> 10:28.710
So in here, when we pass null what it is going to return, it is going to return optional dot empty

10:28.980 --> 10:33.600
when this check runs for the empty optional object this is going to.

10:34.630 --> 10:38.960
Evaluate to false and then it is going to return optional empty.

10:39.010 --> 10:40.780
And in here, what is going to happen?

10:40.780 --> 10:48.520
This is going to this is going to evaluate to false and then it is going to print name not found.

10:48.640 --> 10:51.460
Let's run this and check if we take a look at it.

10:51.460 --> 10:53.280
That's the result we got.

10:53.290 --> 10:55.450
So you might be wondering what's the difference here?

10:55.450 --> 10:56.050
Right.

10:56.290 --> 11:00.760
Let me move away from the presentation mode so that I can show you the whole code.

11:00.790 --> 11:07.840
So in here, if you take a look at it, this method is returning null and this method is returning option

11:07.840 --> 11:08.620
dot empty.

11:08.650 --> 11:11.230
So we replaced not null.

11:11.230 --> 11:13.180
Check with the is present check.

11:13.300 --> 11:13.840
Right.

11:13.840 --> 11:15.130
That might be running in your mind.

11:15.130 --> 11:16.260
What is the difference here?

11:16.270 --> 11:23.740
The difference between the null and optional here is that you can still execute a method call on an

11:23.740 --> 11:25.000
empty optional object.

11:25.000 --> 11:31.600
So in here, even though this get student name optional returns empty, you will still be able to check

11:31.600 --> 11:39.560
whether that value inside that optional object is present or not on that empty optional object and the

11:39.560 --> 11:44.990
return type of this method here, if you take a look at it, the return type is here we are returning

11:44.990 --> 11:47.420
null null doesn't belong to any type.

11:47.420 --> 11:51.500
Anything can be null, a string can be null, the student object can be null.

11:51.650 --> 11:59.180
But in the case of this method, it is always going to return the optional type optional as a support

11:59.180 --> 12:03.680
for a lot of additional methods which can be used for variety of use cases.

12:03.680 --> 12:10.280
We will be exploring all those methods in the following tutorials, but for now this is all about optional.

12:10.460 --> 12:13.280
Let's talk about more different scenarios in the future.

12:13.280 --> 12:14.090
Tutorials.

12:14.090 --> 12:16.130
With this we came to the end of this tutorial.

12:16.160 --> 12:17.270
Thank you for watching.
