WEBVTT

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

00:00.680 --> 00:01.250
Welcome back.

00:01.250 --> 00:05.810
In this lecture we will code and learn about the new enhanced switch in Java.

00:05.840 --> 00:08.390
I'll start with some theory first as like the other lectures.

00:08.390 --> 00:10.520
And then we'll get to the coding part.

00:10.640 --> 00:13.640
Switch statements have been part of the language since the beginning.

00:13.640 --> 00:19.340
Enhanced switch is an enhancement to the existing switch statement, which got released as part of Java

00:19.340 --> 00:19.820
14.

00:19.850 --> 00:24.860
The new enhanced switch is an expression which means it returns a value.

00:24.860 --> 00:30.080
Basically, it's not a switch statement anymore, it is an expression.

00:30.080 --> 00:32.660
Let me explain this using a simple example.

00:32.660 --> 00:37.250
Here is a function that returns a number of days based on the month and year.

00:37.280 --> 00:39.710
Now let's quickly look into the implementation.

00:39.710 --> 00:45.230
Since this function returns a value, you need to define an integer, because the return type of this

00:45.230 --> 00:51.320
particular function is an integer, and then we default that value to be zero, and then we have a switch

00:51.320 --> 00:55.430
statement for all the months that fall under 30 days bucket.

00:55.430 --> 00:58.790
And then we update the number of days variable to 30.

00:58.820 --> 01:05.680
If the passed in month to the function is April, June, September or November and then we break the

01:05.680 --> 01:10.000
case statement, and then we return the number of days in the case of February.

01:10.030 --> 01:12.040
We have a specific case statement for that.

01:12.040 --> 01:17.740
And then we have the default block for the rest of the months, which have the number of days as 31.

01:17.740 --> 01:23.200
And then we have the default block for the rest of the months, which has the number of days as 31.

01:23.200 --> 01:26.980
So this is an old way of implementing a switch statement.

01:26.980 --> 01:32.530
This one works really well, and most of you might have done a similar kind of implementation in your

01:32.530 --> 01:33.220
experience.

01:33.250 --> 01:37.330
Now let's look at the same implementation using the enhanced switch.

01:37.360 --> 01:42.970
The first thing that comes into my mind when I look at this implementation is that it looks very concise

01:42.970 --> 01:45.250
and looks much cleaner to me.

01:45.250 --> 01:51.550
So now let's dive into the implementation and understand what this implementation mean and how it works.

01:51.550 --> 01:55.030
The key thing to notice here is that switch is an expression.

01:55.030 --> 01:59.470
So we place the return keyword on the switch expression itself.

01:59.470 --> 02:05.050
And the next thing is that we have multiple case labels allowed in the enhanced switch.

02:05.050 --> 02:11.910
So in this case we have September April June and November have the same case label, and number three

02:11.910 --> 02:16.320
is that the brake statement is replaced by the arrow on the value.

02:16.350 --> 02:17.790
The switch is going to return.

02:17.790 --> 02:21.270
So basically we don't have to deal with the brake statement anymore.

02:21.270 --> 02:24.000
And the same is applicable for the following case statement.

02:24.000 --> 02:29.640
And then we have a default block that takes care of handling the scenario where it needs to handle the

02:29.640 --> 02:30.930
remaining conditions.

02:30.930 --> 02:37.830
This makes the enhanced switch looks elegant, and it is also can be used in advanced use cases like

02:37.830 --> 02:41.040
pattern matching, which I'll cover in the future part of the course.

02:41.040 --> 02:45.270
I hope you have a good understanding of how to implement the enhanced switch.

02:45.300 --> 02:48.090
Now it's time to explore the enhanced switch in action.

02:48.090 --> 02:51.600
For that, we are going to switch back to IntelliJ and then start exploring it.

02:51.600 --> 02:53.160
So I'm back in IntelliJ here.

02:53.160 --> 02:53.670
And in here.

02:53.670 --> 02:54.720
What we are going to do.

02:54.750 --> 02:57.630
We are going to be opening the enhanced switch package.

02:57.630 --> 03:02.430
So in the enhanced switch package you will have the implementation already present over here.

03:02.430 --> 03:06.390
And this is a older way of implementing the switch statement okay.

03:06.420 --> 03:11.940
And if you take a look at the input to this function month is an enum which represents all the months.

03:11.940 --> 03:17.250
It is part of the Java.time package itself, and then the year is something which is being passed.

03:17.250 --> 03:20.700
So based on the year, we are going to use the leap year function.

03:20.700 --> 03:22.260
That's part of the year class.

03:22.260 --> 03:24.840
This is also part of the Java.time package, I believe.

03:24.870 --> 03:29.130
There you go to determine whether the current year is a leap year or not.

03:29.130 --> 03:34.620
Because if it's a leap year, we need to return the value of the number of days to be 29.

03:34.650 --> 03:37.410
Otherwise, we need to return the value as 28.

03:37.410 --> 03:40.410
For rest of the condition, we are returning the value as 30.

03:40.440 --> 03:42.420
So there is a test case already available.

03:42.420 --> 03:43.950
Let's go ahead and take a look at that one.

03:45.000 --> 03:47.010
So there is a days and month test.

03:47.040 --> 03:48.870
Let me put this in side by side view.

03:48.900 --> 03:53.550
So this one actually if you take a look at it it uses something called parameterized test.

03:53.550 --> 03:58.410
If you are hearing this term for the very first time, it is very simple and easy to upskill yourself

03:58.410 --> 03:58.950
on this one.

03:58.950 --> 04:03.660
So you can think of this as a test where the function parameters are.

04:03.660 --> 04:06.930
The input parameters are returned from a different function.

04:06.930 --> 04:08.760
So in this case I'm using the method source.

04:08.760 --> 04:10.740
And then the function name is input.

04:10.740 --> 04:13.860
So as soon as I highlight this one the reference goes here.

04:13.860 --> 04:20.270
So what this does is that here we are passing the arguments as February September, and we are passing

04:20.270 --> 04:23.750
the expected number of days to be returned as part of this call.

04:23.750 --> 04:29.630
So these two values, the month and expected number of days, is being returned by this function as

04:29.660 --> 04:30.920
a stream of arguments.

04:30.920 --> 04:36.290
So when we execute this function, it is going to be getting executed three times because that's what

04:36.290 --> 04:37.490
this input function returns.

04:37.490 --> 04:39.740
It returns a stream of three arguments.

04:39.740 --> 04:43.760
The first argument is February and the expected number of days is 28.

04:43.760 --> 04:47.750
And the second argument is September and the expected number of days is 30.

04:47.780 --> 04:53.120
And the third argument is January, and the expected number of days is going to be 31.

04:53.120 --> 04:56.750
Let's execute this test case and then look into the results over here.

04:57.800 --> 04:58.160
There you go.

04:58.160 --> 04:58.400
You see.

04:58.430 --> 04:58.730
Right.

04:58.730 --> 05:00.920
All these three conditions are executed.

05:00.920 --> 05:06.920
And the three conditions basically the input to the test cases is passed by this input function.

05:06.920 --> 05:12.590
So now what we are going to do is we are going to be implementing the same functionality using the enhanced

05:12.590 --> 05:13.010
switch.

05:13.010 --> 05:15.830
So what we are going to do we are going to create another function.

05:15.830 --> 05:19.520
So that's going to accept something similar okay.

05:19.520 --> 05:22.570
But I'm going to name this function as get days this v2.

05:24.940 --> 05:25.330
Okay.

05:25.360 --> 05:30.010
And in here, what we are going to do, we are going to use the enhanced switch.

05:30.010 --> 05:32.380
So enhanced switch is an expression right.

05:32.380 --> 05:35.380
So in this case let's do a return and then pass the switch.

05:35.380 --> 05:38.050
And then you pass a month over here okay.

05:38.080 --> 05:41.290
And then what we are going to do let's implement the first condition.

05:41.290 --> 05:45.340
The first condition is a case for all these different months.

05:45.340 --> 05:47.350
And the return value is going to be 30.

05:47.380 --> 05:47.950
Right.

05:47.950 --> 05:53.740
So what we can do we can do a case and then you can add September.

05:55.780 --> 05:57.280
April June and November.

05:57.310 --> 05:57.850
Right.

05:57.850 --> 06:03.190
So it can be April June and November.

06:03.190 --> 06:06.070
And the value that we want to return is 30 days.

06:06.100 --> 06:08.920
That's what we want to do right for February.

06:08.950 --> 06:12.340
So in this case it's going to be February case February.

06:12.340 --> 06:17.410
And if the year is a leap year then we need to return the value as 29.

06:17.410 --> 06:21.790
If the year is not a leap year, then we need to return the value as 28.

06:21.820 --> 06:26.290
I am recording this lecture where the year is 2023, which is not a leap year.

06:26.290 --> 06:28.770
So in this case I'm going to have a check year.

06:28.770 --> 06:31.950
Dot is leap year and then pass this value.

06:31.980 --> 06:33.930
So in this case pass the year over here.

06:33.930 --> 06:37.770
And if the year is leap year it's going to return the boolean as true.

06:37.770 --> 06:40.710
So we will have the number of days as 29.

06:40.740 --> 06:43.260
Otherwise the value is going to be 28.

06:43.350 --> 06:46.530
Okay I believe here we need to add the semicolon.

06:46.530 --> 06:48.390
And the next one is the default case.

06:48.390 --> 06:52.050
So default case we want to return the value as 31 okay.

06:52.080 --> 06:57.000
So we have our very first enhanced switch statement implemented.

06:57.000 --> 07:01.230
And if you take a look at the number of lines just for the sake of comparing the number of lines, the

07:01.230 --> 07:04.890
number of lines is just three lines for the same implementation.

07:04.890 --> 07:10.710
And we don't have to define this redundant variable and then mutate that value in order to determine

07:10.710 --> 07:11.640
the result.

07:11.640 --> 07:15.990
So in here we basically have the return value represented in the lambda.

07:15.990 --> 07:19.260
So what we will do how do we make sure the functionality works as expected.

07:19.260 --> 07:21.810
So now we are going to write a similar test.

07:21.840 --> 07:24.090
We'll leverage the same code.

07:24.090 --> 07:27.720
But in this case the function name is going to be get this v2.

07:27.720 --> 07:32.970
And we are going to be invoking the function name as get is this v2 and the method input source is still

07:32.970 --> 07:37.470
going to be the same as long as this test case succeeds without any issue.

07:37.500 --> 07:40.020
Our implementation works as expected.

07:40.020 --> 07:42.480
So in this case, let's go ahead and execute this test case.

07:43.170 --> 07:43.620
There you go.

07:43.650 --> 07:46.170
Our execution of the test case is all green.

07:46.200 --> 07:52.770
That means our enhanced switch works very similar to the older implementation of the same functionality.

07:52.800 --> 07:55.860
Now let's go ahead and execute this whole test.

07:55.860 --> 08:00.330
So this is going to execute both the get days and get days v2 function.

08:00.330 --> 08:05.280
And we have implemented our very first enhanced switch in our modern Java.

08:05.310 --> 08:11.280
Some of the quick things to recollect is that in modern Java, with the usage of enhanced switch return

08:11.280 --> 08:15.690
keyword placed on the switch statement, the reason being switch is an expression.

08:15.690 --> 08:21.630
You can basically return the whole switch and the condition gets evaluated and the appropriate value

08:21.660 --> 08:23.730
that's after the lambda gets returned.

08:23.760 --> 08:27.180
I hope you all have a good understanding about how enhanced switch works.

08:27.180 --> 08:32.130
There are a couple of advanced topics let's try to enhance switch, which I'll be covering in the following

08:32.160 --> 08:32.760
lectures.

08:32.760 --> 08:34.080
This marks the end of this lecture.

08:34.080 --> 08:35.100
Thank you for watching.
