Hi everyone.
Welcome back.
In this lecture we will code and learn about the new enhanced switch in Java.
I'll start with some theory first as like the other lectures.
And then we'll get to the coding part.
Switch statements have been part of the language since the beginning.
Enhanced switch is an enhancement to the existing switch statement, which got released as part of Java
14.
The new enhanced switch is an expression which means it returns a value.
Basically, it's not a switch statement anymore, it is an expression.
Let me explain this using a simple example.
Here is a function that returns a number of days based on the month and year.
Now let's quickly look into the implementation.
Since this function returns a value, you need to define an integer, because the return type of this
particular function is an integer, and then we default that value to be zero, and then we have a switch
statement for all the months that fall under 30 days bucket.
And then we update the number of days variable to 30.
If the passed in month to the function is April, June, September or November and then we break the
case statement, and then we return the number of days in the case of February.
We have a specific case statement for that.
And then we have the default block for the rest of the months, which have the number of days as 31.
And then we have the default block for the rest of the months, which has the number of days as 31.
So this is an old way of implementing a switch statement.
This one works really well, and most of you might have done a similar kind of implementation in your
experience.
Now let's look at the same implementation using the enhanced switch.
The first thing that comes into my mind when I look at this implementation is that it looks very concise
and looks much cleaner to me.
So now let's dive into the implementation and understand what this implementation mean and how it works.
The key thing to notice here is that switch is an expression.
So we place the return keyword on the switch expression itself.
And the next thing is that we have multiple case labels allowed in the enhanced switch.
So in this case we have September April June and November have the same case label, and number three
is that the brake statement is replaced by the arrow on the value.
The switch is going to return.
So basically we don't have to deal with the brake statement anymore.
And the same is applicable for the following case statement.
And then we have a default block that takes care of handling the scenario where it needs to handle the
remaining conditions.
This makes the enhanced switch looks elegant, and it is also can be used in advanced use cases like
pattern matching, which I'll cover in the future part of the course.
I hope you have a good understanding of how to implement the enhanced switch.
Now it's time to explore the enhanced switch in action.
For that, we are going to switch back to IntelliJ and then start exploring it.
So I'm back in IntelliJ here.
And in here.
What we are going to do.
We are going to be opening the enhanced switch package.
So in the enhanced switch package you will have the implementation already present over here.
And this is a older way of implementing the switch statement okay.
And if you take a look at the input to this function month is an enum which represents all the months.
It is part of the Java.time package itself, and then the year is something which is being passed.
So based on the year, we are going to use the leap year function.
That's part of the year class.
This is also part of the Java.time package, I believe.
There you go to determine whether the current year is a leap year or not.
Because if it's a leap year, we need to return the value of the number of days to be 29.
Otherwise, we need to return the value as 28.
For rest of the condition, we are returning the value as 30.
So there is a test case already available.
Let's go ahead and take a look at that one.
So there is a days and month test.
Let me put this in side by side view.
So this one actually if you take a look at it it uses something called parameterized test.
If you are hearing this term for the very first time, it is very simple and easy to upskill yourself
on this one.
So you can think of this as a test where the function parameters are.
The input parameters are returned from a different function.
So in this case I'm using the method source.
And then the function name is input.
So as soon as I highlight this one the reference goes here.
So what this does is that here we are passing the arguments as February September, and we are passing
the expected number of days to be returned as part of this call.
So these two values, the month and expected number of days, is being returned by this function as
a stream of arguments.
So when we execute this function, it is going to be getting executed three times because that's what
this input function returns.
It returns a stream of three arguments.
The first argument is February and the expected number of days is 28.
And the second argument is September and the expected number of days is 30.
And the third argument is January, and the expected number of days is going to be 31.
Let's execute this test case and then look into the results over here.
There you go.
You see.
Right.
All these three conditions are executed.
And the three conditions basically the input to the test cases is passed by this input function.
So now what we are going to do is we are going to be implementing the same functionality using the enhanced
switch.
So what we are going to do we are going to create another function.
So that's going to accept something similar okay.
But I'm going to name this function as get days this v2.
Okay.
And in here, what we are going to do, we are going to use the enhanced switch.
So enhanced switch is an expression right.
So in this case let's do a return and then pass the switch.
And then you pass a month over here okay.
And then what we are going to do let's implement the first condition.
The first condition is a case for all these different months.
And the return value is going to be 30.
Right.
So what we can do we can do a case and then you can add September.
April June and November.
Right.
So it can be April June and November.
And the value that we want to return is 30 days.
That's what we want to do right for February.
So in this case it's going to be February case February.
And if the year is a leap year then we need to return the value as 29.
If the year is not a leap year, then we need to return the value as 28.
I am recording this lecture where the year is 2023, which is not a leap year.
So in this case I'm going to have a check year.
Dot is leap year and then pass this value.
So in this case pass the year over here.
And if the year is leap year it's going to return the boolean as true.
So we will have the number of days as 29.
Otherwise the value is going to be 28.
Okay I believe here we need to add the semicolon.
And the next one is the default case.
So default case we want to return the value as 31 okay.
So we have our very first enhanced switch statement implemented.
And if you take a look at the number of lines just for the sake of comparing the number of lines, the
number of lines is just three lines for the same implementation.
And we don't have to define this redundant variable and then mutate that value in order to determine
the result.
So in here we basically have the return value represented in the lambda.
So what we will do how do we make sure the functionality works as expected.
So now we are going to write a similar test.
We'll leverage the same code.
But in this case the function name is going to be get this v2.
And we are going to be invoking the function name as get is this v2 and the method input source is still
going to be the same as long as this test case succeeds without any issue.
Our implementation works as expected.
So in this case, let's go ahead and execute this test case.
There you go.
Our execution of the test case is all green.
That means our enhanced switch works very similar to the older implementation of the same functionality.
Now let's go ahead and execute this whole test.
So this is going to execute both the get days and get days v2 function.
And we have implemented our very first enhanced switch in our modern Java.
Some of the quick things to recollect is that in modern Java, with the usage of enhanced switch return
keyword placed on the switch statement, the reason being switch is an expression.
You can basically return the whole switch and the condition gets evaluated and the appropriate value
that's after the lambda gets returned.
I hope you all have a good understanding about how enhanced switch works.
There are a couple of advanced topics let's try to enhance switch, which I'll be covering in the following
lectures.
This marks the end of this lecture.
Thank you for watching.