WEBVTT

00:00.640 --> 00:01.210
Hi everyone.

00:01.210 --> 00:02.530
Welcome back to this tutorial.

00:02.530 --> 00:08.860
In this tutorial we will code and explore the is present and if present method that is part of the optional

00:08.860 --> 00:11.650
class in here, what we are going to do.

00:11.680 --> 00:15.010
I'm going to create a class in the optional package.

00:15.010 --> 00:26.260
The class name is going to be optional present example and then I'm going to have the class executable

00:26.440 --> 00:29.320
by adding the public static void main method.

00:30.380 --> 00:32.240
Let me put this in presentation mode.

00:33.710 --> 00:39.470
So the method that we are going to explore is going to be is present.

00:39.590 --> 00:44.900
We have seen some examples of is present already, but still I would like to cover this in this tutorial

00:44.900 --> 00:46.250
and if present.

00:48.350 --> 00:51.200
First, let's take a look at the Is present method.

00:51.660 --> 00:56.030
So I'm going to create an optional object of type string.

00:56.060 --> 00:58.940
I'm going to give it a name optional.

01:01.330 --> 01:06.160
And then I'm going to use the off nullable method, which is part of the optional to create an optional

01:06.160 --> 01:09.760
object optional dot of nullable.

01:09.880 --> 01:18.220
I'm going to give the string as hello optional and in here what I'm going to do, I'm going to use the

01:18.220 --> 01:22.960
Is present method and then show you what is that it is going to give you as an output.

01:23.140 --> 01:25.750
The optional dot is present.

01:26.500 --> 01:28.720
Let's run this example and then check the result.

01:30.150 --> 01:33.870
But on this example that is going to give me the value as true.

01:33.900 --> 01:39.690
Basically, it checks whether this optional object has some value in it or not.

01:39.720 --> 01:42.060
Let's say I'm going to pass null here.

01:45.030 --> 01:52.230
Those kind of scenarios is going to give you the value as false because this option doesn't have any

01:52.230 --> 01:53.850
value inside this.

01:54.850 --> 02:01.690
Basically, if you want to get to the value that it encapsulates what we do, we do optional Dot is

02:01.690 --> 02:02.020
present.

02:02.020 --> 02:06.070
This might this content might be a repeated content from the previous tutorial.

02:06.070 --> 02:11.230
But if you are if you're already aware of all this content, you can go ahead and move to the next topic,

02:11.230 --> 02:13.540
which I'm going to explain in a bit.

02:15.160 --> 02:22.990
And then this is going to be optional dot get, which will give you access to the actual value that

02:22.990 --> 02:24.310
it encapsulates.

02:25.690 --> 02:26.150
Here you go.

02:26.170 --> 02:27.650
It printed the Hello optional.

02:27.670 --> 02:31.060
Now let's go ahead and explore the if present method.

02:31.090 --> 02:38.230
So if present method is again almost similar to the is present method, but it gives you one additional

02:38.650 --> 02:39.820
logic.

02:39.850 --> 02:41.290
Let's explore that.

02:41.530 --> 02:45.540
So I'm going to do the optional dot if present.

02:45.550 --> 02:51.010
If you take a look at it, if present takes in an input as a consumer, we all know consumer takes in

02:51.010 --> 02:54.460
an input and it doesn't return any output, right?

02:54.460 --> 03:00.280
So what I'm going to do as soon as I type in s, IntelliJ is going to give me the recommendation to

03:00.280 --> 03:04.270
implement it and then I'm going to just print that string.

03:07.690 --> 03:09.100
So what it does.

03:10.530 --> 03:11.970
It does two things.

03:12.000 --> 03:19.560
First thing, it checks whether the optional has a value, and after that it it gives you the access

03:19.560 --> 03:20.880
to the actual value.

03:20.880 --> 03:23.130
And you can do any kind of operation you want.

03:23.160 --> 03:24.180
Here I'm just printing it.

03:24.180 --> 03:26.580
If you want some other operation, you can still do it.

03:26.580 --> 03:29.640
Let's say you want to call some other method or some other service.

03:29.640 --> 03:33.570
If that object is having any value, then you can go ahead and do that too.

03:33.780 --> 03:40.080
So the only difference is that is present will just tell you whether the value is present or not.

03:40.230 --> 03:48.570
But if present will tell you there is a value present or not, and then it gives you an additional operation

03:48.810 --> 03:52.050
that you can perform on top of that optional object.

03:52.970 --> 03:58.460
So this is all about the present and if present that are part of the optional class.

03:58.490 --> 04:00.480
With this, we came to the end of this tutorial.

04:00.500 --> 04:01.670
Thank you for watching.
