WEBVTT

00:00.080 --> 00:00.590
Hi everyone.

00:00.590 --> 00:01.160
Welcome back.

00:01.160 --> 00:06.050
In this lecture we are going to learn about the exhaustive nature of the enhanced switch.

00:06.050 --> 00:08.240
So what is the exhaustive nature means?

00:08.240 --> 00:14.690
Is that any switch expression that we write must handle every possible scenario for your input type.

00:14.690 --> 00:21.020
So in this case for the month, we have September, April, June, November and February.

00:21.020 --> 00:22.580
And then we have default here.

00:22.580 --> 00:22.910
Right.

00:22.910 --> 00:28.190
So in this case if I remove default the code will start to complain which means we are missing out some

00:28.190 --> 00:28.790
scenarios.

00:28.790 --> 00:32.330
So this is the exhaustive nature of switch expression.

00:32.330 --> 00:39.080
And we can actually avoid using default if we provide all the matching conditions that is possible for

00:39.080 --> 00:40.490
this switch expression.

00:40.490 --> 00:43.790
That is exactly what I am going to explain in this specific lecture.

00:43.790 --> 00:47.600
So in here what we are going to do I'm going to create another function.

00:47.600 --> 00:49.460
And the function name is going to be v3.

00:49.460 --> 00:52.430
So in here what I'm going to do I'm going to remove the default.

00:52.430 --> 00:56.810
And then instead of default we can provide all the different possible months.

00:56.810 --> 01:04.960
So in this case it's going to be January March, May, July, August, October and December.

01:04.960 --> 01:10.420
And then if we provide this value as 31, then in this case, the compiler is not going to complain.

01:10.450 --> 01:16.330
What this means is that for the input that we are using in our switch expression, we have covered the

01:16.330 --> 01:18.040
all possible scenarios.

01:18.040 --> 01:24.130
This is one of the nicest thing about the switch expression and the exhaustive nature of the enhanced

01:24.130 --> 01:25.090
switch expression.

01:25.090 --> 01:31.480
So if all the possible enum constants are present in a switch expression for this specific example,

01:31.480 --> 01:37.450
and the match is total, and it is not necessary for the default case to be included for the compiler

01:37.450 --> 01:38.350
to be happy.

01:38.350 --> 01:43.330
If you cover all the scenarios, the compiler will be happy to compile the code without the use of the

01:43.330 --> 01:44.260
default block.

01:44.290 --> 01:45.700
I hope the explanation is clear.

01:45.730 --> 01:46.600
Now what we will do.

01:46.630 --> 01:51.820
We will quickly go ahead and write the test that's going to invoke the get days v3 function.

01:51.850 --> 01:56.860
So in this case we can pretty much use the same test block, just that we need to change the function

01:56.860 --> 02:00.830
to the get days is v three and get this v three over here.

02:00.860 --> 02:05.810
Now if I go ahead and execute this function, as long as it returns me the green signal, that means

02:05.810 --> 02:08.120
our code is still working as expected.

02:08.150 --> 02:13.400
Now let's go ahead and execute the whole test covering all the different test cases that we have.

02:13.430 --> 02:13.970
There we go.

02:14.000 --> 02:18.350
We got a green check mark for all the test cases that are part of this test.

02:18.350 --> 02:22.700
So well this is the exhaustive nature of the enhanced switch block.

02:22.730 --> 02:24.230
You don't need to provide the default.

02:24.230 --> 02:28.940
If you cover all the possible inputs that can be passed to this switch expression.

02:28.970 --> 02:30.110
So a quick recap here.

02:30.140 --> 02:32.420
We learned about the exhaustive nature.

02:32.420 --> 02:38.750
And then we also learned about how to provide a lambda body for a case which covers multiple code statements.

02:38.750 --> 02:45.260
And as a whole, we have learned about how to use switch as an expression using the enhanced switch

02:45.260 --> 02:45.650
block.

02:45.950 --> 02:49.940
Well, this wraps the enhanced switch feature that's part of the modern Java.

02:49.970 --> 02:54.770
I hope you all had fun coding this specific feature and learning this specific feature.

02:54.800 --> 02:56.150
This marks the end of this lecture.

02:56.180 --> 02:57.380
Thank you for watching.
