WEBVTT

00:01.080 --> 00:01.620
Hi everyone.

00:01.620 --> 00:02.790
Welcome back to this tutorial.

00:02.790 --> 00:09.150
In this tutorial, we'll code and explore the of nullable of an empty method that is part of the optional

00:09.150 --> 00:09.830
class.

00:09.840 --> 00:16.530
The first step is I'm going to create a class called Optional of empty.

00:18.130 --> 00:20.740
Nullable example.

00:21.460 --> 00:25.390
Let's make this class executable by adding public static void main.

00:29.230 --> 00:34.270
The first method that we are going to explore is a off label method, which is part of the optional

00:34.270 --> 00:34.900
object.

00:35.680 --> 00:42.040
And the method that I'm going to create is going to return optional of type string.

00:42.400 --> 00:44.440
So I'm going to keep this example really simple.

00:44.440 --> 00:51.130
That's the reason why I'm going to explore the different methods using the optional of type string.

00:53.220 --> 00:53.940
Public.

00:54.960 --> 00:57.930
Static optional of string.

00:57.930 --> 01:00.000
This is what it is going to return.

01:00.390 --> 01:05.220
And we are going to explore the of nullable.

01:08.000 --> 01:10.400
How do we create the.

01:11.840 --> 01:12.270
Optional.

01:12.290 --> 01:16.760
How do we make this method return the optional of type string?

01:17.090 --> 01:20.270
So I'm going to use the optional class dot.

01:20.450 --> 01:21.730
So you get all these methods, right?

01:21.740 --> 01:25.730
We are going to explore off nullable and then it accepts an input.

01:25.730 --> 01:29.900
So since we are going to return optional of type string, I'm going to pass.

01:30.840 --> 01:33.870
Optional of hollow the return type of this method.

01:33.870 --> 01:38.790
If you go to this method, the return type is going to be optional of type that you are passing.

01:38.790 --> 01:41.280
The type that we are passing is string.

01:41.370 --> 01:46.320
So this is going to return optional of string.

01:47.160 --> 01:52.050
Let's give it a name string optional and then return it.

01:53.290 --> 01:54.260
Button string optional.

01:57.180 --> 02:00.090
So let's go ahead and run this.

02:05.550 --> 02:05.940
Doubt.

02:08.820 --> 02:09.780
And then.

02:11.380 --> 02:11.830
Condition.

02:11.830 --> 02:12.760
Check the result.

02:14.000 --> 02:19.350
The result is optional and the hollow string is wrapped inside this optional.

02:19.380 --> 02:21.450
How do we get access to that value?

02:21.480 --> 02:24.960
The way we get access to that value is optional.

02:24.990 --> 02:26.040
Dot get.

02:27.670 --> 02:33.040
That's that way we can actually get the ring that it encapsulates.

02:33.070 --> 02:37.720
The next question is under what scenarios we would use optional of nullable.

02:37.840 --> 02:41.020
Let's go to this method and check this method.

02:41.170 --> 02:43.000
Check this first condition.

02:43.000 --> 02:47.350
If the value equal to equal to null, then it is going to return an empty optional.

02:47.380 --> 02:51.100
Otherwise it is going to give you the off value.

02:51.310 --> 02:57.550
So if you are not certain this value is going to be a valid string all the time, then you would use

02:57.550 --> 02:59.470
optional dot of nullable.

02:59.500 --> 03:02.290
What will happen if I pass null here?

03:02.800 --> 03:05.320
So this one is going to give us.

03:06.010 --> 03:08.050
An empty, optional object.

03:09.720 --> 03:13.470
How do we check whether a value is present in an optional object or not?

03:13.500 --> 03:19.110
We have seen that in the previous tutorial is present is a method which will tell you whether that optional

03:19.110 --> 03:21.690
has a valid value inside.

03:22.730 --> 03:24.710
That optional object or not.

03:25.250 --> 03:27.170
So this is all about optional.

03:27.200 --> 03:29.690
Let's go ahead and explore the next method.

03:32.110 --> 03:36.950
We are going to explore about the of method, which is part of the optional.

03:37.990 --> 03:41.080
Instead of the off nullable we are going to use off.

03:41.970 --> 03:42.510
Here.

03:42.510 --> 03:44.820
I'm going to give hello as an.

03:46.580 --> 03:47.180
Input.

03:48.190 --> 03:50.950
And then we're going to call the off method.

03:53.560 --> 03:53.950
Of.

03:57.580 --> 03:59.590
Let's run this example and check.

04:00.190 --> 04:06.730
Let me give some string here so that it will be able to differentiate between this and the other one.

04:08.230 --> 04:09.840
Is going to be off.

04:14.490 --> 04:15.330
Run this.

04:16.390 --> 04:17.620
If you run this.

04:18.720 --> 04:20.550
Here we are doing the.

04:21.620 --> 04:22.250
Is present.

04:22.250 --> 04:23.960
I'm going to just remove this.

04:23.970 --> 04:25.370
I'm going to make this.

04:26.460 --> 04:28.320
Accept a valid string.

04:29.010 --> 04:31.800
I'm going to run this again and then let's check the result.

04:32.580 --> 04:38.280
If both are having the valid string, then if you take a look at it, both are returning you the same

04:38.280 --> 04:38.670
result.

04:38.700 --> 04:42.180
What would happen if I pass the value as null here?

04:44.250 --> 04:46.620
Let's go ahead and run this and check the result.

04:47.280 --> 04:50.250
We passed the value as null.

04:50.670 --> 04:58.020
It gives you an exception called null pointer exception because this method always expects you to send

04:58.020 --> 05:02.280
a valid input as a valid value, as an input to this method.

05:02.310 --> 05:03.600
That is not the case here.

05:03.600 --> 05:05.220
If you pass null, what will happen?

05:05.220 --> 05:08.430
It is going to return you the empty optional object.

05:08.430 --> 05:13.290
So if you are not, if you are certain all the time, you're going to have a valid string.

05:13.290 --> 05:16.380
In those kind of scenarios, we would use the off method.

05:16.410 --> 05:19.140
Otherwise you would use the off nullable method.

05:19.170 --> 05:23.880
I'm going to return it back to its original state and run this and check the result.

05:26.010 --> 05:26.610
There you go.

05:27.300 --> 05:28.480
The next condition.

05:28.500 --> 05:31.320
The next method that we are going to explore is.

05:32.750 --> 05:36.800
The empty method, which is part of the optional class itself.

05:37.580 --> 05:37.970
Empty.

05:40.420 --> 05:41.980
I'm going to just return.

05:43.090 --> 05:44.440
Optional dot.

05:46.260 --> 05:46.590
Empty.

05:49.440 --> 05:51.870
Then will be a written statement.

05:51.870 --> 05:52.440
Right?

05:52.980 --> 05:54.270
Optional dot empty.

05:54.300 --> 05:58.830
So this one is going to give you the empty object as a result.

06:02.970 --> 06:03.330
Empty.

06:07.300 --> 06:08.770
But condition check the result.

06:10.520 --> 06:13.100
Now this will give you an empty optional object.

06:13.270 --> 06:15.620
So under what scenarios you would use empty.

06:15.650 --> 06:17.960
Let's say you have a method that returns this.

06:17.960 --> 06:18.460
Right?

06:18.470 --> 06:25.910
And those that returns an type of optional In those kind of scenarios, you can have empty in the exception

06:25.910 --> 06:26.510
block.

06:26.630 --> 06:33.770
So in that case, you add her to the actual method return type to always return optional as an output

06:33.770 --> 06:34.790
of that method.

06:34.820 --> 06:38.220
So this is all about of nullable of an empty method.

06:38.240 --> 06:44.210
Let's explore some more use cases and some more methods that are part of the optional in the next tutorial.

06:44.240 --> 06:46.220
With this we came to the end of this tutorial.

06:46.250 --> 06:47.390
Thank you for watching.
