Hi everyone.
Welcome back.
In this lecture we are going to explore unnamed variables in Java in detail.
I want to explain the concepts first and then we will explore them in action by actually coding it.
In Java 25 there is a new special identifier underscore.
It's a signal to other developers that the variable is intentionally ignored and not needed or used
anywhere.
Here is an example of how this can be used.
In this case, we are parsing an integer from a string.
Let's say if the parsing fails, we just catch the exception and then we ignore the exception.
Basically, we do not do anything with the exception, we just return null over here.
In this kind of scenarios, there is no need to add a variable.
We can actually add this underscore and the code will compile without any issues.
What is the benefit of this one?
Actually, this fundamentally improves the readability of the code.
As a developer, you do not need to pay attention to that variable.
Common places where this can be used are in the catch block, like the example above, or in the case
of lambdas, where only some parameters matter.
We can actually use this underscore in the case of pattern matching where the variable is not used or
needed, then we can actually add the underscore over there to.
Another important point to remember is that if you try to read that variable, then it will throw the
compile time error.
Now let's go ahead and explore this in action.
Let's go back to the IntelliJ.
So in here we are going to be working on the package named Unnamed Variables.
And then you will see a class named Unnamed Variables over here.
So this class is basically a very simple main function that we have over here.
And then we have this get integer function which takes in a string.
And then it's going to do the parsing from the string to an integer.
Here if there is an exception we are actually catching that exception right.
Now what we will do is let's go ahead and write a function that's going to be invoking this particular
function, and then see what the behavior is.
If there is an exception that happened over here.
Okay.
So the exception is something that is being caught with a variable.
But this is not used at all.
So now if we change this to underscore there is no compilation issue over here.
This was not the case before.
Now this is a valid syntax.
In order to make things easy for us, instead of writing the code from scratch, I have created something
called a markdown file.
You'll be able to see markdown file over here.
Let's go ahead and split this to the right.
So this markdown file have two different prompts.
So if you can take a look at it the first prompt is basically the lambda parameters with unnamed variables.
A prompt is something you can think of an input to the large language model.
In this case, it's an AI that we are going to leverage to write the code for us.
So I have one for lambda parameters with unnamed variables.
Other one is exception handling with unnamed variables.
What we are looking at is the exception handling.
So I'm going to be just go to this one.
And then here what this one says is that create a function that demonstrates using the unnamed variables
and exception handling combined with stream operations for parsing the data.
So what I'm doing here is that I'm going to be passing the list of strings, which is going to be an
an invalid integer and a valid integer.
And this is going to call the get integer function.
And this is going to be printing the value which is the.
These are the parsed integer.
So we are going to be converting a list of string into a list of integer.
And those integers are going to be stored in this int list.
So what I'm going to do I'm going to copy this whole prompt.
Go to the assistant.
And you can see that it is pointing to the unnamed variables.
But the one that we want to make the changes to is going to be unnamed variables.
Dot Java okay, so now I'm going to have this over here and then give the prompt and then press send
over here.
So now what this is going to do is that this is going to go ahead and edit the function for us, or
create the function for us in this particular class.
And then you will be able to see the code for yourself.
So now it made the code change.
Let's go ahead and validate the code change.
So in here for the demonstrate exception handling it's going to be talking about multiple unnamed variables.
So it took the list of string right which is 123.
Divide is invalid integer and ten is a valid integer.
So we have a list of string we stream to that one.
And then we call the map function for each and every string that's over here we're calling the get integer.
So in the get integer it's going to be parsing the string to an integer.
If it's a valid one then it is going to return the valid integer.
If it's not this is going to return null.
So now I'm going to accept the change accept all.
And then I'm going to be running this one.
There you go.
We got the result as expected.
So there is an exception, right.
You see that fail to parse divide.
So in this case if you search for this one that code is over here.
So what is a feature we are exploring.
We are exploring the unnamed variables.
In this case this syntax is valid.
And we are also printing what string that failed this particular functionality.
So the string is divide.
So the final list is we have 123 which is a valid integer.
Divide is an invalid.
That's why we have the null over here.
And number ten is basically a valid integer.
So as a result we have this output.
So the beauty here is that we were able to write all these code by just providing this prompt okay.
So this is one of the beauty of using the AI assistant.
You don't have to hand code everything.
As far as you know, what the logic that you are going to be thinking in your head, you just have to
provide that logic as a prompt, and the AI will generate the code for you and write the code for you.
So here I have a very good structured prompt.
You do not have to be like this all the time.
You can actually provide your thoughts in a plain English in a way that you can communicate with the
AA model, similar to how you communicate with ChatGPT, you should be able to communicate with it.
The reason why I have it as a structured prompt with this task, expected function, signature and requirements
is that I want to replicate the code that can actually help with explaining these concepts to you,
but in your case, you don't have to have this exact structure.
You can actually give a prompt as like you interact with your colleague or your fellow developers,
it should be able to generate a reasonable code for you.
But one thing here is that you need to have a clear intent.
So we explored the prompt two, which is using the exception handling with unnamed variables.
And I'm going to be exploring this particular one.
So in this case if you take a look at it we are asking it to create something called demonstrate lambda
with unnamed function, and it's going to take a map of string and integer, but while printing it,
I'm going to be just printing the key and ignoring the value.
So that's why we have this underscore.
This is again to explore the unnamed variables concept.
So let's go ahead and copy this.
Print it over here.
So now I expect the changes to be made in the unnamed variables dot Java file.
So in this course I'm actually going to be using a assistant for some lectures and hand code them for
some lectures.
It's going to be depending on how big of a feature that I'm explaining.
Okay.
So in this case it wrote the function for me.
Let's go ahead and explore this function.
So here you can see that it created the map for me as like I expected it to do.
But in this case for each entry of the map, I'm just going to be taking care of the key and I don't
use the value at all.
So I'm assigning the underscore over here.
Actually, when the code is being written in your source code, you can actually execute this, but
still you'll be able to see the result without even accepting it.
So you have the ability to accept that.
So let's say I'm going to discard it.
Took away all the changes actually.
So now what we can do is that we can copy the same thing, put it here and press enter.
It should be able to write the code for you again.
There you go.
It made the code change for you again.
So this is the beauty of these a assistant.
You have the ability to take away code and ask it to write a new code.
It's all way cheaper to write code using this assistant.
So now I'm going to accept the change.
So we are going to be executing it one more time.
There we go.
So we have just the key over here.
And then we are completely ignoring the value aspect which is underscore.
I hope you all have a pretty good idea about how unnamed variables can be beneficial in expressing the
intent to the developers when they are reading the code.
If there is a underscore that you see in the code, that means that is a variable that is completely
ignored.
You don't have to pay attention to it.
Well, this marks the end of this lecture.
Thank you for watching.