Hi everyone.
Welcome back.
In this lecture we will code and learn about pattern matching.
I will start with some theory first and then we'll get to the coding part.
Let's start with why pattern matching in the first place.
Let me explain this using a simple example.
You all might have seen a code like this or wrote one in your experience.
So in this code, what we are doing here is that we check the type, and if the type matches, then
we apply a cost and create a variable and then we act on that variable.
So in this case what we are doing is that if the type is integer, we are building a string with the
cost variable that we created at step number two, and then returning that value.
And the code logic is similar for the next if block, which is if the instance is of type string, then
we cast and create a variable, and then we return a string that represents what is a length of that
string.
And if the type is not string or integer, then we return a result stating not a string or integer.
So this is a very simple and naive example to easily understand what I am trying to convey here.
The one thing that you notice here is that this code is verbose.
Okay.
And another thing is that step one and step two can be combined into one single step.
Because once you match a type, you already know what the type is.
But still in this kind of code, we need to cast it to the type of integer and then create a variable
which is actually redundant.
So this is where pattern matching comes to the rescue to write concise and elegant code.
Let's look at the same example using pattern matching in place.
It is the same example on step one.
What we do is we check the type.
If there is a match, we cast and create a binding variable.
You see in here, if the type is a match then we create a binding variable I over here itself okay.
Which means we can directly act on that variable.
So this is what pattern matching is all about.
It helps the developers to write a more concise and clean code.
It removes that redundant step of again, applying the cast and creating a variable to act on it.
This is what pattern matching is all about.
It helps the developers to write more concise and clean code.
Pattern matching using Instanceof is available from Java 16.
So in this case we are using Instanceof that applies a type check and then it creates a binding variable
to okay.
So this specific type of pattern matching is called a type pattern because we are applying the pattern
on the type of the variable and see if there is a match.
There are other patterns such as record patterns and guarded patterns.
These are introduced as part of Java 21, which we'll talk about in the following lectures.
But in Java 21, they have introduced a new way of applying pattern matching for the type pattern that
we are talking about.
So in this case, for the same logic it can be written in a more concise way.
So let me show you one of my favorite way of applying pattern matching which is pattern matching using
switch.
So in this context, each case statement applies a pattern to match.
Right.
So each case label is going to apply a pattern match.
So in this case if the string matches to the object O that is being passed, then it's going to apply
the pattern.
And then in this case block gets executed.
Okay.
Another nice thing is that we are using lambdas for the case body.
So which is going to simplify your code instead of having this multiple if checks.
So this is basically an enhanced switch.
If we have covered the enhanced switch section already you might be able to understand this code really
well.
Another important thing is that you have a case label to handle null.
So you can have case null as a case label, which is a really nice addition to avoid dealing with the
null pointer exception.
I hope I have provided the necessary information about pattern matching.
Let's explore this in action.
This may be a trivial example, but I have a separate section in which we will build a real time example
using all the concepts that we have learned so far.
With this knowledge in place, Let's go ahead and explore the pattern matching in action.
There you go.
We are back in IntelliJ.
So in here in the Explore latest Java Features project you will notice the pattern matching package.
And in the pattern matching package you will find a class named Pattern Matching example.
Let's go ahead and open this one.
So in this case what we are doing here is that we have a pattern function.
So this is similar to what we saw in the slides.
Right.
So what we are going to do we are going to implement the pattern matching using Instanceof.
So this Instanceof is available from Java 16.
So if you're running in Java 17 you can still use pattern matching using the approach that I'm going
to show you first.
And then we will implement the enhanced switch way of implementing pattern matching.
So in this case let's copy this and put it over here.
So before I go ahead and provide the implementation let's go ahead and rename this one.
It's going to be using Instanceof.
And in here there is a test case already present.
Let's go ahead and open that.
So there you go.
This is a test case.
So in this test case we are still using the parameterized test option.
So what this function does is that it's going to pass the object value.
So in this case the object value is going to be the number one here.
And then we are passing null.
So if the value is null then we are going to expect this value as a result.
Right.
So that's the second argument.
In this case that's the expected result okay.
And in the case of the leap the value is going to be a string of length colon five.
So that's what this particular code logic does.
So in this case string of length which is returning the length okay.
So the next thing is if the type is integer the value that it is going to return is integer colon one.
That's what we have over here.
So now what we will do.
We'll go ahead and write the implementation using Instanceof.
So if the object is of type instanceof then what we can do we can assign a variable over here.
We can remove this okay.
That's the first thing and the same thing over here.
So we can give this value as yes.
And then we can add this value over here.
So basically using Instanceof we have removed one additional step compared to this okay.
So that's the benefit with using pattern matching using Instanceof.
So it creates this binding variable automatically for you.
So now what we will do.
We'll go ahead and write a test case for this one.
Split and move right over here.
So this is where we have the actual code in here we are going to write the test.
So in this case let's go ahead and create a similar test.
Because the input and output is going to be very much the same.
So in this case copy this over here and pass this over here.
So now if I execute this test case, as long as it gives me a green check mark.
There you go.
That's what we got.
So this code works as expected with a pattern matching using Instanceof.
Now the next code is we are going to implement the similar functionality using switch.
Probably this is my favorite way of implementing pattern matching.
So in this case let's copy this and put this over here.
And then I'm going to be naming this function as pattern matching using switch okay.
So let's remove all this code.
So what we are going to do is that we're going to start with the return statement.
Because we know switch is an expression by now.
So in this case we pass it over here, and then what we are going to do is we are going to write case
labels.
So the first thing is we will write a case for string.
So in this case string s if the value is this then we are going to be returning this value.
There you go.
Now let's go ahead and cover the remaining scenarios.
In this case if it's an integer then I'm going to have the binding variable as I.
And then the code logic is going to be integer I.
So in this case let's go ahead and add this one.
And in the case of default we are going to return a different one.
So in this case you see right as soon as I type in the case you have different options available.
So you have a case label that's going to allow you to handle null also, which is a really nice addition
to handling null.
So in this case you do this.
And in the case of this one what we are going to do, we are going to return this value as not a string
or not an integer.
So in this case let's go ahead and add this semicolon over here.
So this is how we implement the pattern matching using the enhanced switch.
So now what we will do.
We'll go ahead and write test cases to test this logic using the enhanced switch.
So in this case I'm going to pretty much use the same test case.
But we are going to be referencing to the new function that we created.
So in this case pattern using switch.
Put it over here and call this one.
So now I'm going to execute the test case.
As long as it gives me a green check.
That means this is working as expected.
Okay let's go ahead and execute this.
There you go.
So we got the green check mark as expected.
That means we have successfully implemented the pattern matching using the enhanced switch.
Okay.
So as far as this tutorial goes or this lecture goes, we have learned about implementing pattern matching
using the instance of and pattern matching using switch.
So in a nutshell this is what pattern matching is.
So this is going to enhance the code readability.
At the same time it is going to allow you to write the concise code compared to what we used to write
over here.
This marks the end of this lecture.
Thank you for watching.