WEBVTT

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

00:00.650 --> 00:01.400
Welcome back.

00:01.400 --> 00:07.040
In this lecture we will code and learn about the very first feature named Local Variable Type Inference.

00:07.070 --> 00:14.690
Or it's also called Lbti using where historically Java was labeled as a verbose language, the local

00:14.690 --> 00:19.970
variable type inference is specifically introduced to address the verbosity concern.

00:19.970 --> 00:25.160
Let me quickly show you an example of this to understand what this feature actually is.

00:25.160 --> 00:29.270
So in this case, we are defining a list of strings, right?

00:29.300 --> 00:35.420
In order for this code to compile, we always need to make sure the actual type is mentioned in the

00:35.420 --> 00:36.890
left side of the code.

00:36.920 --> 00:39.920
Otherwise the compiler will not compile this code.

00:39.920 --> 00:45.590
But with the introduction of lbti or local variable type inference, you don't need to do that.

00:45.590 --> 00:53.090
Instead, we use a reserved type named var and give a variable name, and the type will be inferred

00:53.090 --> 00:54.290
automatically for you.

00:54.290 --> 00:58.490
That's one of the reason why it is called local variable type inference.

00:58.490 --> 01:04.080
The idea behind this the feature is that since we already know what the type is on the right side,

01:04.080 --> 01:09.030
it is redundant to define the type again manually on the left hand side.

01:09.030 --> 01:12.840
Instead, have the compiler automatically infer it for you.

01:12.870 --> 01:16.080
So what does var mean in the Java language?

01:16.110 --> 01:18.540
VAR is a reserved type name.

01:18.540 --> 01:24.570
This means you can still use var as a variable name because it's not a reserved keyword.

01:24.570 --> 01:27.930
So this means this code is actually valid.

01:27.960 --> 01:32.010
In this case we can define var war equal to Java.

01:32.040 --> 01:37.170
The var type in here is going to be string, and the compiler won't complain.

01:37.170 --> 01:40.410
Any issues about using var as a variable name.

01:40.410 --> 01:44.400
Let me show you another example where using var might be helpful.

01:44.400 --> 01:51.030
So in this case we have a map of integer keys in which each key holds another map.

01:51.030 --> 01:54.630
Probably some of you might have defined a data type like this.

01:54.630 --> 02:01.090
But in this kind of scenarios you have to define the whole type on the left hand side, but with the

02:01.090 --> 02:08.140
introduction of var, we can simply use var on the left and have the compiler infer the type for you.

02:08.260 --> 02:15.280
When using Ides like IntelliJ, it has the ability to infer the type and display it as inlay hints.

02:15.310 --> 02:19.390
I will show you how to enable this in IntelliJ when we get to the coding part.

02:19.420 --> 02:21.280
So this marks the end of our theory part.

02:21.310 --> 02:28.180
Now it's time to jump into the code and explore the local variable type inference, or LTI in action.

02:28.210 --> 02:31.210
I'm back in IntelliJ here for this specific section.

02:31.210 --> 02:36.340
What we are going to do is we are going to open the Explore Java latest Features project.

02:36.340 --> 02:41.230
So in this case if you go to the main folder you will notice the package named var.

02:41.230 --> 02:44.950
So in this case you'll notice a file named var type example.

02:44.950 --> 02:49.540
So there you have a empty var type example class with an empty main function.

02:49.540 --> 02:53.830
And there is this function transform which we will use in the later part of the course.

02:53.830 --> 02:59.640
But for now let's go ahead and start exploring the var or local variable type inference in action.

02:59.640 --> 03:03.060
So in this case what I'm going to do I'm going to define a list here.

03:03.060 --> 03:07.860
So in this case list stuff is a factory function using which we can create a list.

03:07.860 --> 03:11.040
So in this case what I'm going to do I'm going to create a list of names.

03:11.040 --> 03:14.220
In this case I'm going to name this one as list of name as Adam.

03:14.220 --> 03:16.920
And then the other name that I'm going to give is the leap.

03:16.920 --> 03:22.170
So prior to leap to what we normally do is we define the list of string on the left right.

03:22.200 --> 03:23.280
So you don't have to do it.

03:23.280 --> 03:27.690
So in this case we can define var and then provide a variable name.

03:27.690 --> 03:30.750
The compiler won't complain anything about it.

03:30.750 --> 03:32.670
The compiler will compile fine.

03:32.700 --> 03:38.460
But in here if you still want to see what this actual infotype is, there is a way to enable that setting

03:38.460 --> 03:39.390
in IntelliJ.

03:39.420 --> 03:43.200
So in this case, click on the IntelliJ over here and go to the settings.

03:43.200 --> 03:46.080
And in the settings you need to go to the editor.

03:46.230 --> 03:49.770
And then you will find something called Inlay Hints.

03:49.800 --> 03:50.790
Click on this one.

03:50.790 --> 03:55.980
In this case you have to expand the type here and then select Java over here.

03:55.990 --> 03:58.330
And then click apply and click okay.

03:58.330 --> 04:03.940
So as soon as you enable that inlay hints you'll be able to see the info type displayed over here.

04:03.970 --> 04:04.330
Right.

04:04.360 --> 04:09.400
So this is kind of a nice visual representation of what that inferred type is.

04:09.700 --> 04:10.000
Okay.

04:10.030 --> 04:12.460
So now what we're going to do we are going to print this name.

04:12.460 --> 04:17.020
I'm going to do a soft V and let's execute this program.

04:17.020 --> 04:17.500
There you go.

04:17.500 --> 04:18.940
The program is getting executed.

04:18.940 --> 04:23.230
And then I'm able to see the list being printed as Adam and the leap.

04:23.260 --> 04:24.070
There you go.

04:24.070 --> 04:28.690
Our very first modern Java feature, which is lbti works as expected.

04:28.690 --> 04:32.350
Now let's go ahead and define some additional different types.

04:32.380 --> 04:32.710
Okay.

04:32.740 --> 04:34.870
So the next thing is I'm going to be defining a map.

04:34.870 --> 04:36.910
So in this case we can type map dot.

04:36.940 --> 04:39.520
You have a function named of entries.

04:39.520 --> 04:42.070
So in this case we can do off entries.

04:42.070 --> 04:46.900
So basically using this off entries we can create an immutable map okay.

04:46.930 --> 04:52.000
And again with the list off what it creates is an immutable list collection.

04:52.570 --> 04:56.510
So all these are factory functions that got introduced after Java eight.

04:56.540 --> 04:59.300
Basically these got introduced in Java nine.

04:59.300 --> 05:02.660
So in this case what we can do is map dot entry.

05:02.660 --> 05:08.090
So I'm going to have a key as a and then I'm going to have a list of names which starts with a.

05:08.150 --> 05:10.910
So in this case we can do list dot off.

05:10.940 --> 05:12.740
We can call this one as Adam.

05:13.310 --> 05:15.140
And then we can call this one as Alex.

05:15.140 --> 05:18.800
So you can think of this as a map with a key value as string.

05:18.800 --> 05:22.370
But the actual values are going to be a list.

05:22.370 --> 05:25.310
So in this case if we define a var over here.

05:25.310 --> 05:26.810
So we can define this one as map.

05:26.810 --> 05:29.180
And this should still work as expected.

05:29.210 --> 05:29.450
Okay.

05:29.480 --> 05:31.340
So we can actually print the value and then check.

05:31.370 --> 05:34.040
So in this case it's going to be map.

05:34.070 --> 05:35.990
And then let's execute this code.

05:37.460 --> 05:40.250
So as you can see the map got printed as okay.

05:40.280 --> 05:45.020
So now there is another option where let's say if you want to iterate the collection right.

05:45.050 --> 05:47.960
Can we use var in those kind of scenarios.

05:47.990 --> 05:53.760
So let's say in this case it's going to be for var name equal to list right?

05:53.790 --> 05:55.050
So is this allowed?

05:55.080 --> 05:56.130
The answer is yes.

05:56.130 --> 05:57.240
This is still allowed.

05:57.270 --> 06:03.000
VAR can also be used as a local variable for iterating over the collection in a for loop.

06:03.030 --> 06:07.020
So in this case if I do this this should still work as expected.

06:07.050 --> 06:09.120
And the same thing can be done over here.

06:09.150 --> 06:13.290
So in this case we can do map dot for each.

06:13.290 --> 06:16.710
So in this case what we can do is we can use var over here too.

06:16.740 --> 06:19.920
So if we do a for each you're going to have a key and value right.

06:19.950 --> 06:23.310
Ideally the types are automatically inferred in the for each block.

06:23.310 --> 06:25.470
So you can still use var over here.

06:25.500 --> 06:27.090
So this is still allowed.

06:27.570 --> 06:29.760
So this code should still compile.

06:29.790 --> 06:32.550
So in this case in this case we're going to do this.

06:32.550 --> 06:34.440
So we're going to print the s over here.

06:34.440 --> 06:37.950
And if you execute this program we are going to be seeing the name in the console.

06:37.950 --> 06:41.100
And we are going to be seeing the S variable also in the console.

06:41.910 --> 06:42.270
There you go.

06:42.300 --> 06:47.100
As you can see the name got printed as expected and the value has got printed as expected.

06:47.100 --> 06:53.780
But in reality if you are using a for each, ideally all these war is not even needed.

06:53.780 --> 06:55.880
This is automatically inferred for you.

06:55.880 --> 06:57.320
So let's say you have a function, right?

06:57.350 --> 07:01.190
So in this function, if you're going to be returning a string right.

07:01.220 --> 07:03.830
Can we use type inference over there.

07:03.860 --> 07:05.090
Let's go ahead and quickly check.

07:05.090 --> 07:09.140
So in this case let's call the transform function and then pass the name over here.

07:09.140 --> 07:13.010
So in this case for this kind of scenarios can we use a var.

07:13.040 --> 07:14.390
The answer is yes.

07:14.570 --> 07:15.590
We can give it a name.

07:15.620 --> 07:18.230
It's going to be transform name.

07:18.230 --> 07:19.010
There you go.

07:19.010 --> 07:23.180
And the type inference still works without any issues in this case.

07:23.210 --> 07:24.230
There you go.

07:24.560 --> 07:25.700
Transform name.

07:26.210 --> 07:27.590
Let's go ahead and run this program.

07:27.590 --> 07:30.440
We should be able to see the transform name in the console.

07:30.470 --> 07:33.530
So in this case the transform name is a uppercase T.

07:33.530 --> 07:38.930
So this is our very first feature which is local variable type inference using var.

07:38.930 --> 07:40.550
And this is how it works.

07:40.550 --> 07:43.880
In the next lecture I will show you some limitations of using var.

07:43.910 --> 07:49.220
I hope you all were able to successfully execute this and understand the importance of using var.

07:49.250 --> 07:50.600
This marks the end of this lecture.

07:50.600 --> 07:51.500
Thank you for watching.
