WEBVTT

00:00.240 --> 00:00.920
Hi everyone.

00:00.920 --> 00:01.480
Welcome back.

00:01.480 --> 00:06.280
In this lecture we are going to explore unnamed variables in Java in detail.

00:06.320 --> 00:11.320
I want to explain the concepts first and then we will explore them in action by actually coding it.

00:11.480 --> 00:15.800
In Java 25 there is a new special identifier underscore.

00:15.840 --> 00:23.360
It's a signal to other developers that the variable is intentionally ignored and not needed or used

00:23.360 --> 00:24.000
anywhere.

00:24.320 --> 00:27.120
Here is an example of how this can be used.

00:27.120 --> 00:30.640
In this case, we are parsing an integer from a string.

00:30.640 --> 00:36.360
Let's say if the parsing fails, we just catch the exception and then we ignore the exception.

00:36.360 --> 00:41.320
Basically, we do not do anything with the exception, we just return null over here.

00:41.600 --> 00:45.120
In this kind of scenarios, there is no need to add a variable.

00:45.120 --> 00:50.440
We can actually add this underscore and the code will compile without any issues.

00:50.880 --> 00:52.200
What is the benefit of this one?

00:52.240 --> 00:56.560
Actually, this fundamentally improves the readability of the code.

00:56.560 --> 01:00.070
As a developer, you do not need to pay attention to that variable.

01:00.510 --> 01:06.750
Common places where this can be used are in the catch block, like the example above, or in the case

01:06.750 --> 01:09.510
of lambdas, where only some parameters matter.

01:09.550 --> 01:15.030
We can actually use this underscore in the case of pattern matching where the variable is not used or

01:15.070 --> 01:19.430
needed, then we can actually add the underscore over there to.

01:19.470 --> 01:24.550
Another important point to remember is that if you try to read that variable, then it will throw the

01:24.550 --> 01:25.830
compile time error.

01:26.350 --> 01:28.670
Now let's go ahead and explore this in action.

01:28.710 --> 01:30.190
Let's go back to the IntelliJ.

01:30.430 --> 01:34.390
So in here we are going to be working on the package named Unnamed Variables.

01:34.390 --> 01:38.390
And then you will see a class named Unnamed Variables over here.

01:38.670 --> 01:43.190
So this class is basically a very simple main function that we have over here.

01:43.350 --> 01:47.270
And then we have this get integer function which takes in a string.

01:47.270 --> 01:51.590
And then it's going to do the parsing from the string to an integer.

01:51.710 --> 01:55.790
Here if there is an exception we are actually catching that exception right.

01:55.830 --> 02:01.380
Now what we will do is let's go ahead and write a function that's going to be invoking this particular

02:01.380 --> 02:04.500
function, and then see what the behavior is.

02:04.700 --> 02:07.020
If there is an exception that happened over here.

02:07.060 --> 02:07.460
Okay.

02:07.580 --> 02:12.140
So the exception is something that is being caught with a variable.

02:12.340 --> 02:14.180
But this is not used at all.

02:14.180 --> 02:18.860
So now if we change this to underscore there is no compilation issue over here.

02:18.860 --> 02:20.260
This was not the case before.

02:20.500 --> 02:23.060
Now this is a valid syntax.

02:23.300 --> 02:29.100
In order to make things easy for us, instead of writing the code from scratch, I have created something

02:29.100 --> 02:30.300
called a markdown file.

02:30.540 --> 02:32.780
You'll be able to see markdown file over here.

02:33.460 --> 02:35.980
Let's go ahead and split this to the right.

02:36.380 --> 02:39.620
So this markdown file have two different prompts.

02:39.620 --> 02:45.820
So if you can take a look at it the first prompt is basically the lambda parameters with unnamed variables.

02:45.860 --> 02:50.500
A prompt is something you can think of an input to the large language model.

02:50.500 --> 02:55.220
In this case, it's an AI that we are going to leverage to write the code for us.

02:55.220 --> 02:58.780
So I have one for lambda parameters with unnamed variables.

02:58.780 --> 03:01.740
Other one is exception handling with unnamed variables.

03:01.980 --> 03:04.220
What we are looking at is the exception handling.

03:04.220 --> 03:06.460
So I'm going to be just go to this one.

03:06.700 --> 03:12.580
And then here what this one says is that create a function that demonstrates using the unnamed variables

03:12.580 --> 03:17.340
and exception handling combined with stream operations for parsing the data.

03:17.340 --> 03:22.220
So what I'm doing here is that I'm going to be passing the list of strings, which is going to be an

03:22.220 --> 03:25.260
an invalid integer and a valid integer.

03:25.260 --> 03:27.820
And this is going to call the get integer function.

03:28.140 --> 03:30.980
And this is going to be printing the value which is the.

03:31.020 --> 03:32.620
These are the parsed integer.

03:32.620 --> 03:37.860
So we are going to be converting a list of string into a list of integer.

03:37.860 --> 03:41.020
And those integers are going to be stored in this int list.

03:41.020 --> 03:43.620
So what I'm going to do I'm going to copy this whole prompt.

03:44.260 --> 03:45.980
Go to the assistant.

03:45.980 --> 03:49.100
And you can see that it is pointing to the unnamed variables.

03:49.100 --> 03:53.140
But the one that we want to make the changes to is going to be unnamed variables.

03:53.140 --> 03:59.490
Dot Java okay, so now I'm going to have this over here and then give the prompt and then press send

03:59.490 --> 04:00.170
over here.

04:00.290 --> 04:05.370
So now what this is going to do is that this is going to go ahead and edit the function for us, or

04:05.370 --> 04:07.770
create the function for us in this particular class.

04:07.770 --> 04:11.010
And then you will be able to see the code for yourself.

04:11.130 --> 04:12.450
So now it made the code change.

04:12.450 --> 04:15.290
Let's go ahead and validate the code change.

04:15.450 --> 04:21.410
So in here for the demonstrate exception handling it's going to be talking about multiple unnamed variables.

04:21.410 --> 04:25.210
So it took the list of string right which is 123.

04:25.250 --> 04:28.970
Divide is invalid integer and ten is a valid integer.

04:28.970 --> 04:32.050
So we have a list of string we stream to that one.

04:32.050 --> 04:37.730
And then we call the map function for each and every string that's over here we're calling the get integer.

04:38.010 --> 04:42.130
So in the get integer it's going to be parsing the string to an integer.

04:42.170 --> 04:45.770
If it's a valid one then it is going to return the valid integer.

04:45.770 --> 04:48.050
If it's not this is going to return null.

04:48.330 --> 04:51.450
So now I'm going to accept the change accept all.

04:51.610 --> 04:53.840
And then I'm going to be running this one.

04:54.720 --> 04:55.120
There you go.

04:55.160 --> 04:57.160
We got the result as expected.

04:57.160 --> 04:59.080
So there is an exception, right.

04:59.080 --> 05:01.560
You see that fail to parse divide.

05:01.880 --> 05:04.880
So in this case if you search for this one that code is over here.

05:05.240 --> 05:07.280
So what is a feature we are exploring.

05:07.280 --> 05:09.160
We are exploring the unnamed variables.

05:09.160 --> 05:11.280
In this case this syntax is valid.

05:11.440 --> 05:16.400
And we are also printing what string that failed this particular functionality.

05:16.400 --> 05:18.000
So the string is divide.

05:18.000 --> 05:22.480
So the final list is we have 123 which is a valid integer.

05:22.520 --> 05:23.720
Divide is an invalid.

05:23.720 --> 05:25.280
That's why we have the null over here.

05:25.280 --> 05:28.120
And number ten is basically a valid integer.

05:28.120 --> 05:30.160
So as a result we have this output.

05:30.160 --> 05:37.000
So the beauty here is that we were able to write all these code by just providing this prompt okay.

05:37.040 --> 05:40.520
So this is one of the beauty of using the AI assistant.

05:40.520 --> 05:42.360
You don't have to hand code everything.

05:42.400 --> 05:47.080
As far as you know, what the logic that you are going to be thinking in your head, you just have to

05:47.120 --> 05:52.990
provide that logic as a prompt, and the AI will generate the code for you and write the code for you.

05:52.990 --> 05:55.910
So here I have a very good structured prompt.

05:55.950 --> 05:58.030
You do not have to be like this all the time.

05:58.030 --> 06:04.630
You can actually provide your thoughts in a plain English in a way that you can communicate with the

06:04.670 --> 06:10.270
AA model, similar to how you communicate with ChatGPT, you should be able to communicate with it.

06:10.270 --> 06:16.150
The reason why I have it as a structured prompt with this task, expected function, signature and requirements

06:16.150 --> 06:21.310
is that I want to replicate the code that can actually help with explaining these concepts to you,

06:21.310 --> 06:24.630
but in your case, you don't have to have this exact structure.

06:24.630 --> 06:30.550
You can actually give a prompt as like you interact with your colleague or your fellow developers,

06:30.710 --> 06:33.550
it should be able to generate a reasonable code for you.

06:33.550 --> 06:36.750
But one thing here is that you need to have a clear intent.

06:36.750 --> 06:41.670
So we explored the prompt two, which is using the exception handling with unnamed variables.

06:41.670 --> 06:44.270
And I'm going to be exploring this particular one.

06:44.270 --> 06:49.830
So in this case if you take a look at it we are asking it to create something called demonstrate lambda

06:49.830 --> 06:56.470
with unnamed function, and it's going to take a map of string and integer, but while printing it,

06:56.470 --> 06:59.630
I'm going to be just printing the key and ignoring the value.

06:59.630 --> 07:01.390
So that's why we have this underscore.

07:01.550 --> 07:04.670
This is again to explore the unnamed variables concept.

07:04.670 --> 07:06.350
So let's go ahead and copy this.

07:07.230 --> 07:08.350
Print it over here.

07:08.350 --> 07:12.390
So now I expect the changes to be made in the unnamed variables dot Java file.

07:12.390 --> 07:19.030
So in this course I'm actually going to be using a assistant for some lectures and hand code them for

07:19.030 --> 07:19.630
some lectures.

07:19.630 --> 07:23.110
It's going to be depending on how big of a feature that I'm explaining.

07:23.110 --> 07:23.470
Okay.

07:23.510 --> 07:25.550
So in this case it wrote the function for me.

07:25.550 --> 07:27.430
Let's go ahead and explore this function.

07:27.430 --> 07:31.950
So here you can see that it created the map for me as like I expected it to do.

07:32.230 --> 07:39.350
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

07:39.350 --> 07:40.510
use the value at all.

07:40.510 --> 07:42.670
So I'm assigning the underscore over here.

07:42.710 --> 07:47.750
Actually, when the code is being written in your source code, you can actually execute this, but

07:47.750 --> 07:51.500
still you'll be able to see the result without even accepting it.

07:51.500 --> 07:53.300
So you have the ability to accept that.

07:53.300 --> 07:55.100
So let's say I'm going to discard it.

07:55.100 --> 07:56.540
Took away all the changes actually.

07:56.540 --> 08:01.460
So now what we can do is that we can copy the same thing, put it here and press enter.

08:01.580 --> 08:04.300
It should be able to write the code for you again.

08:05.260 --> 08:05.740
There you go.

08:05.780 --> 08:07.900
It made the code change for you again.

08:08.180 --> 08:10.460
So this is the beauty of these a assistant.

08:10.500 --> 08:14.340
You have the ability to take away code and ask it to write a new code.

08:14.700 --> 08:18.860
It's all way cheaper to write code using this assistant.

08:18.860 --> 08:20.700
So now I'm going to accept the change.

08:20.940 --> 08:23.460
So we are going to be executing it one more time.

08:23.460 --> 08:23.860
There we go.

08:23.860 --> 08:25.620
So we have just the key over here.

08:25.620 --> 08:29.420
And then we are completely ignoring the value aspect which is underscore.

08:29.460 --> 08:35.540
I hope you all have a pretty good idea about how unnamed variables can be beneficial in expressing the

08:35.540 --> 08:38.140
intent to the developers when they are reading the code.

08:38.180 --> 08:43.380
If there is a underscore that you see in the code, that means that is a variable that is completely

08:43.380 --> 08:43.860
ignored.

08:43.860 --> 08:45.660
You don't have to pay attention to it.

08:45.700 --> 08:47.260
Well, this marks the end of this lecture.

08:47.260 --> 08:48.260
Thank you for watching.
