Hi everyone.
Welcome back.
In this lecture we are going to learn about the exhaustive nature of the enhanced switch.
So what is the exhaustive nature means?
Is that any switch expression that we write must handle every possible scenario for your input type.
So in this case for the month, we have September, April, June, November and February.
And then we have default here.
Right.
So in this case if I remove default the code will start to complain which means we are missing out some
scenarios.
So this is the exhaustive nature of switch expression.
And we can actually avoid using default if we provide all the matching conditions that is possible for
this switch expression.
That is exactly what I am going to explain in this specific lecture.
So in here what we are going to do I'm going to create another function.
And the function name is going to be v3.
So in here what I'm going to do I'm going to remove the default.
And then instead of default we can provide all the different possible months.
So in this case it's going to be January March, May, July, August, October and December.
And then if we provide this value as 31, then in this case, the compiler is not going to complain.
What this means is that for the input that we are using in our switch expression, we have covered the
all possible scenarios.
This is one of the nicest thing about the switch expression and the exhaustive nature of the enhanced
switch expression.
So if all the possible enum constants are present in a switch expression for this specific example,
and the match is total, and it is not necessary for the default case to be included for the compiler
to be happy.
If you cover all the scenarios, the compiler will be happy to compile the code without the use of the
default block.
I hope the explanation is clear.
Now what we will do.
We will quickly go ahead and write the test that's going to invoke the get days v3 function.
So in this case we can pretty much use the same test block, just that we need to change the function
to the get days is v three and get this v three over here.
Now if I go ahead and execute this function, as long as it returns me the green signal, that means
our code is still working as expected.
Now let's go ahead and execute the whole test covering all the different test cases that we have.
There we go.
We got a green check mark for all the test cases that are part of this test.
So well this is the exhaustive nature of the enhanced switch block.
You don't need to provide the default.
If you cover all the possible inputs that can be passed to this switch expression.
So a quick recap here.
We learned about the exhaustive nature.
And then we also learned about how to provide a lambda body for a case which covers multiple code statements.
And as a whole, we have learned about how to use switch as an expression using the enhanced switch
block.
Well, this wraps the enhanced switch feature that's part of the modern Java.
I hope you all had fun coding this specific feature and learning this specific feature.
This marks the end of this lecture.
Thank you for watching.