Hi everyone.
Welcome back.
In this lecture we will code and learn about the very first feature named Local Variable Type Inference.
Or it's also called Lbti using where historically Java was labeled as a verbose language, the local
variable type inference is specifically introduced to address the verbosity concern.
Let me quickly show you an example of this to understand what this feature actually is.
So in this case, we are defining a list of strings, right?
In order for this code to compile, we always need to make sure the actual type is mentioned in the
left side of the code.
Otherwise the compiler will not compile this code.
But with the introduction of lbti or local variable type inference, you don't need to do that.
Instead, we use a reserved type named var and give a variable name, and the type will be inferred
automatically for you.
That's one of the reason why it is called local variable type inference.
The idea behind this the feature is that since we already know what the type is on the right side,
it is redundant to define the type again manually on the left hand side.
Instead, have the compiler automatically infer it for you.
So what does var mean in the Java language?
VAR is a reserved type name.
This means you can still use var as a variable name because it's not a reserved keyword.
So this means this code is actually valid.
In this case we can define var war equal to Java.
The var type in here is going to be string, and the compiler won't complain.
Any issues about using var as a variable name.
Let me show you another example where using var might be helpful.
So in this case we have a map of integer keys in which each key holds another map.
Probably some of you might have defined a data type like this.
But in this kind of scenarios you have to define the whole type on the left hand side, but with the
introduction of var, we can simply use var on the left and have the compiler infer the type for you.
When using Ides like IntelliJ, it has the ability to infer the type and display it as inlay hints.
I will show you how to enable this in IntelliJ when we get to the coding part.
So this marks the end of our theory part.
Now it's time to jump into the code and explore the local variable type inference, or LTI in action.
I'm back in IntelliJ here for this specific section.
What we are going to do is we are going to open the Explore Java latest Features project.
So in this case if you go to the main folder you will notice the package named var.
So in this case you'll notice a file named var type example.
So there you have a empty var type example class with an empty main function.
And there is this function transform which we will use in the later part of the course.
But for now let's go ahead and start exploring the var or local variable type inference in action.
So in this case what I'm going to do I'm going to define a list here.
So in this case list stuff is a factory function using which we can create a list.
So in this case what I'm going to do I'm going to create a list of names.
In this case I'm going to name this one as list of name as Adam.
And then the other name that I'm going to give is the leap.
So prior to leap to what we normally do is we define the list of string on the left right.
So you don't have to do it.
So in this case we can define var and then provide a variable name.
The compiler won't complain anything about it.
The compiler will compile fine.
But in here if you still want to see what this actual infotype is, there is a way to enable that setting
in IntelliJ.
So in this case, click on the IntelliJ over here and go to the settings.
And in the settings you need to go to the editor.
And then you will find something called Inlay Hints.
Click on this one.
In this case you have to expand the type here and then select Java over here.
And then click apply and click okay.
So as soon as you enable that inlay hints you'll be able to see the info type displayed over here.
Right.
So this is kind of a nice visual representation of what that inferred type is.
Okay.
So now what we're going to do we are going to print this name.
I'm going to do a soft V and let's execute this program.
There you go.
The program is getting executed.
And then I'm able to see the list being printed as Adam and the leap.
There you go.
Our very first modern Java feature, which is lbti works as expected.
Now let's go ahead and define some additional different types.
Okay.
So the next thing is I'm going to be defining a map.
So in this case we can type map dot.
You have a function named of entries.
So in this case we can do off entries.
So basically using this off entries we can create an immutable map okay.
And again with the list off what it creates is an immutable list collection.
So all these are factory functions that got introduced after Java eight.
Basically these got introduced in Java nine.
So in this case what we can do is map dot entry.
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.
So in this case we can do list dot off.
We can call this one as Adam.
And then we can call this one as Alex.
So you can think of this as a map with a key value as string.
But the actual values are going to be a list.
So in this case if we define a var over here.
So we can define this one as map.
And this should still work as expected.
Okay.
So we can actually print the value and then check.
So in this case it's going to be map.
And then let's execute this code.
So as you can see the map got printed as okay.
So now there is another option where let's say if you want to iterate the collection right.
Can we use var in those kind of scenarios.
So let's say in this case it's going to be for var name equal to list right?
So is this allowed?
The answer is yes.
This is still allowed.
VAR can also be used as a local variable for iterating over the collection in a for loop.
So in this case if I do this this should still work as expected.
And the same thing can be done over here.
So in this case we can do map dot for each.
So in this case what we can do is we can use var over here too.
So if we do a for each you're going to have a key and value right.
Ideally the types are automatically inferred in the for each block.
So you can still use var over here.
So this is still allowed.
So this code should still compile.
So in this case in this case we're going to do this.
So we're going to print the s over here.
And if you execute this program we are going to be seeing the name in the console.
And we are going to be seeing the S variable also in the console.
There you go.
As you can see the name got printed as expected and the value has got printed as expected.
But in reality if you are using a for each, ideally all these war is not even needed.
This is automatically inferred for you.
So let's say you have a function, right?
So in this function, if you're going to be returning a string right.
Can we use type inference over there.
Let's go ahead and quickly check.
So in this case let's call the transform function and then pass the name over here.
So in this case for this kind of scenarios can we use a var.
The answer is yes.
We can give it a name.
It's going to be transform name.
There you go.
And the type inference still works without any issues in this case.
There you go.
Transform name.
Let's go ahead and run this program.
We should be able to see the transform name in the console.
So in this case the transform name is a uppercase T.
So this is our very first feature which is local variable type inference using var.
And this is how it works.
In the next lecture I will show you some limitations of using var.
I hope you all were able to successfully execute this and understand the importance of using var.
This marks the end of this lecture.
Thank you for watching.