WEBVTT

00:00.260 --> 00:00.830
Hi everyone!

00:00.830 --> 00:02.300
Welcome back to this tutorial.

00:02.300 --> 00:08.210
In this tutorial we will implement the runnable interface using the legacy way, the way that is created

00:08.210 --> 00:14.180
by using the anonymous classes and then the modern Java way that is using Lambda.

00:14.210 --> 00:20.660
After that we will compare both and discuss the advantages of one over the other.

00:20.660 --> 00:24.680
Let's go ahead and code this one time to set up our base project.

00:24.680 --> 00:27.230
So in this case I have the IntelliJ open here.

00:27.260 --> 00:28.880
At the time of recording the lecture.

00:28.910 --> 00:34.310
The IntelliJ version that I'm using right now is 2024 .3.1.

00:34.310 --> 00:40.910
So you need to have the latest version of IntelliJ in order for 21 Java version to be supported.

00:40.910 --> 00:47.840
If you have IntelliJ versions like 2022 or something like that, or 2023 for that matter, because some

00:47.840 --> 00:52.220
of the versions of 2023 still doesn't support Java 21.

00:52.220 --> 00:55.070
So please ensure you have the latest version of IntelliJ.

00:55.100 --> 00:57.710
The next step is I'm going to click on the new project.

00:57.710 --> 01:02.330
So in here I'm going to give the project name as Java eight, okay?

01:02.330 --> 01:06.740
Because for the first part of the course, we will be exploring all the Java eight features.

01:06.740 --> 01:08.930
And Java eight is a significant release.

01:08.930 --> 01:12.350
So I'm going to be creating the project name as Java eight.

01:12.350 --> 01:18.800
And in here we have the whole Java eight project sitting under a project named Modern Java two.

01:18.830 --> 01:21.440
In your case, you can give any folder you want.

01:21.440 --> 01:24.860
And now the next step is we need to select the right version of Java.

01:24.860 --> 01:27.920
So in this case I have selected the Java 21.

01:27.920 --> 01:29.930
And then click on create over here.

01:29.930 --> 01:32.750
So this is going to be creating the project in your machine.

01:32.780 --> 01:35.000
As you can see the project is getting loaded.

01:35.150 --> 01:38.240
So it is still indexing the configuration metadata.

01:38.240 --> 01:42.260
Once it is completed then this project is set up and ready to use.

01:42.290 --> 01:43.010
There you go.

01:43.970 --> 01:45.590
We are going to create a package here.

01:45.590 --> 01:48.530
So what are we going to implement as part of this project.

01:48.530 --> 01:51.290
We are going to implement the runnable interface.

01:51.320 --> 01:56.930
The first step in the process is that we are going to explore the lambdas, which got introduced as

01:56.930 --> 01:58.670
part of JDK 1.8.

01:58.670 --> 02:00.800
I am going to create a package called.

02:02.020 --> 02:02.830
lambdas.

02:04.630 --> 02:05.110
Good.

02:05.140 --> 02:09.730
The next step in the process is that we are going to implement a runnable interface as part of this

02:09.730 --> 02:10.450
tutorial.

02:10.450 --> 02:13.210
So I'm going to create a class called.

02:16.660 --> 02:21.610
Runnable example okay.

02:21.940 --> 02:25.090
Otherwise I can name it as runnable Lambda example.

02:27.490 --> 02:32.080
Runnable lambda example okay.

02:32.110 --> 02:37.990
Before we go ahead and implement the runnable interface, let's talk about runnable interface first.

02:38.020 --> 02:41.590
What is that is going to do and what is that.

02:42.100 --> 02:46.690
What is a method that it has as part of this functional interface?

02:46.720 --> 02:53.440
Most of the experienced developers would have worked on this interface at some point in their life.

02:53.560 --> 02:56.890
But still, I'm going to give you a quick explanation about it.

02:56.920 --> 03:01.780
First step in the process is that I'm going to open the runnable interface in IntelliJ.

03:01.810 --> 03:04.180
I'm going to press the shift key twice.

03:04.420 --> 03:09.490
It's going to give me this search everywhere option and then type in runnable.

03:10.630 --> 03:11.410
Runnable.

03:11.440 --> 03:16.930
If you take a look at it the second one here which stands for I here which is an interface I'm going

03:16.960 --> 03:17.980
to select this one.

03:18.280 --> 03:24.880
If you go and take a look at it, this runnable interface has only one method which is called run.

03:24.910 --> 03:28.570
This method doesn't take any input, does not return any output.

03:28.570 --> 03:32.800
So the idea of this interface is to work with the thread class.

03:32.800 --> 03:38.440
And the next thing if you notice there is a new annotation called at Functional interface Annotation,

03:38.440 --> 03:42.460
which is introduced as part of JDK 1.8.

03:43.060 --> 03:45.160
This annotation is an optional annotation.

03:45.160 --> 03:50.440
If you are going to create any functional interface of your own, it is not mandatory that you have

03:50.440 --> 03:56.230
to provide this annotation, but this is an optional and annotation that helps to determine whether

03:56.230 --> 03:59.770
that interface is a functional interface or not.

04:00.420 --> 04:01.020
Okay.

04:01.050 --> 04:04.290
Let's go ahead and implement this runnable interface.

04:05.400 --> 04:12.330
First step in the process is that we are going to make this class an executable class.

04:12.570 --> 04:18.450
If you type in PS VM in IntelliJ it's going to give you the public static void main.

04:18.480 --> 04:21.960
That is the shortcut to generate this method.

04:22.170 --> 04:27.330
The next step in the process is that I'm going to create the runnable interface.

04:27.390 --> 04:33.330
Prior to Java eight I'm going to give some comments over there.

04:33.510 --> 04:34.800
Prior Java eight.

04:35.910 --> 04:40.320
First step is runnable runnable equal to.

04:41.340 --> 04:44.100
We are basically creating an anonymous class.

04:44.100 --> 04:49.260
That's the way we normally implement and functional interface prior to Java eight.

04:50.550 --> 04:51.270
Runnable.

04:51.480 --> 04:52.470
There we go.

04:52.500 --> 04:54.300
I'm going to keep this really simple.

04:54.300 --> 04:56.880
I'm just going to print something inside this one.

04:57.780 --> 04:59.910
Inside runnable one.

05:02.310 --> 05:02.880
Awesome.

05:02.910 --> 05:06.000
Right now we have the instance of runnable ready.

05:06.000 --> 05:12.720
The next step is we will pass this runnable to the thread and start the thread, which is going to run

05:12.720 --> 05:16.020
the method whatever we have implemented here.

05:17.070 --> 05:22.980
So the way to create the instance of thread is new thread and then pass the runnable interface.

05:23.010 --> 05:23.670
Right.

05:23.970 --> 05:24.870
So that's the next step.

05:24.900 --> 05:30.480
Pass the runnable interface dot start as soon as I run this.

05:30.480 --> 05:33.810
Basically this thread is going to start this runnable.

05:33.810 --> 05:39.810
And then it is going to run the whatever implementation that we have inside this run method.

05:42.600 --> 05:44.670
Could not find or load the main class.

05:44.700 --> 05:46.140
This sometimes happens.

05:46.170 --> 05:52.980
The way to fix this issue is that click on this build and then build module Java eight.

05:53.730 --> 05:57.870
Or you can select build project can use either one of those options.

05:57.870 --> 05:59.010
I'm going to run it again.

06:00.240 --> 06:01.760
So we have the same problem?

06:01.760 --> 06:02.690
And then let's do this.

06:02.690 --> 06:03.680
Recompile.

06:05.300 --> 06:07.640
Go implemented something.

06:08.450 --> 06:09.860
Go ahead and run this.

06:11.750 --> 06:12.320
There we go.

06:12.350 --> 06:13.790
The program finally worked.

06:13.790 --> 06:17.600
So here we have the inside runnable implementation.

06:17.600 --> 06:22.700
Whatever we have implemented is called and it got printed in the console.

06:24.320 --> 06:25.100
What is the next step.

06:25.100 --> 06:26.840
The next step is that we know this right.

06:26.870 --> 06:33.080
We have seen this over have worked on this runnable interface would have seen this implementation already.

06:33.080 --> 06:34.940
There is nothing new at this point.

06:34.970 --> 06:40.370
Let's go ahead and implement this using the Java eight Lambda syntax.

06:40.700 --> 06:42.290
Java eight Lambda.

06:43.670 --> 06:46.700
So here let's review the syntax again.

06:46.790 --> 06:50.870
The concept is this is a input followed by that there is an arrow.

06:51.320 --> 06:55.100
And open and close braces end with semicolon.

06:55.100 --> 06:57.020
This is a syntax of lambda.

06:57.170 --> 07:01.820
Let's go ahead and implement this using Lambdas.

07:01.880 --> 07:05.330
So we are going to implement the runnable interface using Lambda.

07:05.420 --> 07:08.990
I'm going to give a name Runnable Lambda.

07:08.990 --> 07:12.470
And then what does this method take as an input.

07:12.500 --> 07:13.910
Nothing right.

07:14.750 --> 07:15.830
So no input.

07:16.430 --> 07:20.930
And then the arrow followed by that we need the.

07:25.310 --> 07:26.180
Curly braces.

07:26.210 --> 07:33.590
In here we are going to write the implementation of it.

07:33.620 --> 07:39.440
The implementation here is that I'm going to name it as inside runnable two to differentiate between

07:39.440 --> 07:41.720
this implementation and that implementation.

07:41.750 --> 07:42.260
Okay.

07:42.260 --> 07:48.920
If you take a look at the syntax, the syntax is really really simple with the lambda implementation

07:48.920 --> 07:51.110
of the same runnable interface.

07:51.350 --> 07:53.000
It is concise.

07:53.030 --> 07:54.590
It looks really simple.

07:54.620 --> 08:04.940
Now let's go ahead and run this one new Read the runnable lambda dot start.

08:06.230 --> 08:07.850
Okay, I'm going to run this program.

08:09.560 --> 08:10.010
There you go.

08:10.010 --> 08:16.970
We have both of those implementation working one with the Lambda way, the other with the legacy way.

08:17.540 --> 08:23.210
What is the difference if you take a look at it, there is a lot of boilerplate code that is avoided

08:23.240 --> 08:25.520
with the Lambda implementation.

08:25.520 --> 08:31.700
And we can make this implementation of Lambda much simpler than whatever we have.

08:31.850 --> 08:34.820
Go ahead and write that in a more simpler way.

08:34.820 --> 08:41.120
So I'm going to have runnable I'm going to name it as Runnable Lambda one.

08:42.590 --> 08:44.810
It doesn't take any input this.

08:44.810 --> 08:47.390
So here this is a single line of statement right.

08:47.390 --> 08:52.700
If you are having if you are writing any lambda expression that is just going to execute a single line

08:52.730 --> 08:57.440
of statement, those kind of scenarios, you don't even need the curly braces.

08:58.190 --> 08:59.360
This is enough.

09:01.450 --> 09:05.710
So you have implemented the runnable interface in a single line.

09:05.740 --> 09:06.820
We take a look at it.

09:06.850 --> 09:13.540
If you compare this one with the prior Java wave, which is a legacy way of creating anonymous classes.

09:13.570 --> 09:15.310
Compare the number of lines here.

09:15.310 --> 09:20.890
The number of lines that we have is one, two, three, four, five, six.

09:20.890 --> 09:28.180
Here just one single line and it is more readable compared to the legacy way.

09:28.600 --> 09:29.920
Let's go ahead and run this.

09:32.080 --> 09:38.590
New thread runnable Lambda one dot start.

09:40.510 --> 09:41.020
Okay.

09:41.080 --> 09:41.560
Okay.

09:41.560 --> 09:42.730
Let's run this.

09:45.970 --> 09:52.210
It worked as expected, but if we take a look at it, we are creating this lambda expression and then

09:52.210 --> 09:54.280
assigning that to a variable.

09:54.280 --> 09:57.910
And then we are passing that variable to the thread instance.

09:58.150 --> 09:59.740
Let's say you don't want to do that.

09:59.740 --> 10:06.700
You don't want to create a runnable variable and assign a lambda expression to it.

10:06.730 --> 10:08.710
You can do that like this too.

10:08.740 --> 10:19.840
So new thread and then pass in the lambda expression directly as an parameter to the thread.

10:22.480 --> 10:23.110
Code.

10:25.960 --> 10:30.070
So double for and then start the thread.

10:31.360 --> 10:32.410
I'm going to run this.

10:35.410 --> 10:35.890
There you go.

10:35.920 --> 10:38.410
It printed the value in the console.

10:38.440 --> 10:43.480
If you have to do this in the legacy way, let's go ahead and take a look at it and implement that.

10:43.720 --> 10:45.520
The new thread.

10:47.650 --> 10:48.910
New runnable.

10:52.690 --> 10:59.050
System dot out dot println inside runnable 3.1.

11:02.550 --> 11:06.660
Look what the implementation and check the difference between these two here.

11:07.140 --> 11:08.310
It looks really clumsy.

11:08.310 --> 11:09.720
The code is not clear at all.

11:09.720 --> 11:13.590
If some other developer have to look at the code, they have to break their head.

11:13.620 --> 11:20.700
So with the Lambda, the implementation is really, really simple and it is easy.

11:20.730 --> 11:27.750
It might be really tough in the initial stages of adapting to the lambda expressions, but once you

11:27.750 --> 11:31.620
get into it, you will love it, I guarantee you that.

11:32.070 --> 11:37.740
I would like to cover one more scenario before we wind up the runnable implementation using Lambda.

11:37.980 --> 11:43.380
So here I mentioned for single statements you don't need the curly braces.

11:43.500 --> 11:50.160
Let's say you have two statements, meaning you are going to print twice in your code.

11:50.220 --> 11:53.460
In those kind of scenarios this is not valid.

11:53.460 --> 11:57.780
You cannot give multiple statements without curly braces.

11:57.780 --> 12:05.940
So for these kind of scenarios you need to have curly braces here now to make this statement a valid

12:05.940 --> 12:06.570
one.

12:07.620 --> 12:11.310
So if you want to make this valid, you have to go like this.

12:12.570 --> 12:15.600
Then we will provide the semicolon here.

12:15.600 --> 12:21.840
So you whenever you have multiple statements inside the lambda body then in those kind of scenarios

12:21.840 --> 12:25.050
you will have to use curly braces.

12:25.050 --> 12:27.960
Otherwise your compiler will complain.

12:27.990 --> 12:30.060
Hey there is a syntax here error.

12:30.270 --> 12:32.220
Please go ahead and fix it.

12:32.250 --> 12:36.210
I'm going to run this and I have inside A33 twice.

12:36.360 --> 12:38.130
You see, it got printed twice.

12:39.360 --> 12:40.290
What I'm going to do.

12:40.320 --> 12:43.770
Let's go ahead and do a quick summary of whatever we discussed.

12:44.370 --> 12:48.000
So these are the certain things which you have to keep in your mind.

12:48.030 --> 12:53.490
So if you are using Lambda and then you have just single statement or expression as part of the lambda

12:53.490 --> 12:56.610
body, then curly braces are not needed.

12:56.640 --> 13:03.050
If you are using multiple statements, meaning if your lambda body is gonna execute multiple statements,

13:03.050 --> 13:05.510
then curly braces are mandatory.

13:05.510 --> 13:09.140
Otherwise your compiler is going to complain about it.

13:10.490 --> 13:11.330
I know we did this.

13:11.360 --> 13:17.180
We discussed about it, but still, I want to stress this again and then give you a overview or a summary

13:17.180 --> 13:19.700
of whatever we have done in the legacy way.

13:19.700 --> 13:26.960
If you take a look at it, there is a lot of boilerplate code which is involved in order to create an

13:26.960 --> 13:32.390
instance of a functional interface, but that is not the case with Lambda.

13:32.420 --> 13:36.890
With Lambda, it is really, really simple and it is more readable.

13:37.610 --> 13:43.940
Okay, so the next thing, no need of creating anonymous classes with lambda.

13:44.300 --> 13:51.860
With lambda, it is evident that it lets you write more readable and concise code.

13:51.920 --> 13:54.110
With this, we came to the end of this tutorial.

13:54.110 --> 13:58.610
Let's discuss about the comparator interface in the next tutorial.

13:59.000 --> 14:00.140
Thank you for watching.
