WEBVTT

00:00.140 --> 00:00.830
Hi everyone.

00:00.830 --> 00:01.520
Welcome back.

00:01.520 --> 00:06.410
In this lecture we will code and learn about the next exciting feature that is part of the modern Java

00:06.410 --> 00:08.150
which is text blocks.

00:08.180 --> 00:12.260
I'll start with some theory concepts first and then we will jump into the coding part.

00:12.290 --> 00:15.620
This feature got released in Java 15.

00:15.650 --> 00:21.980
Let me start with a quick background about why this feature was added to the Java language in the first

00:21.980 --> 00:22.520
place.

00:22.550 --> 00:29.270
Java's string type, which was considered really primitive compared to the other programming languages.

00:29.270 --> 00:34.790
For example, if you have a string that holds double quotes, then you need to add an escape character

00:34.790 --> 00:39.680
like a backslash for the compiler to compile and treat this value as a string.

00:39.710 --> 00:42.800
Another example where you need a multi-line string.

00:42.800 --> 00:48.590
Then we need to add slash n at the end of each line in order to introduce new lines.

00:48.590 --> 00:52.010
This kind of works actually, but still this looks ugly.

00:52.010 --> 00:54.830
So this is where text blocks comes to the rescue.

00:54.830 --> 01:00.550
And it's primarily introduced in Java to work with strings in a better and easy way.

01:00.580 --> 01:03.340
So now it's time to introduce you to text blocks.

01:03.370 --> 01:06.670
Here is an example of how to define a text block.

01:06.700 --> 01:10.030
The text block begins and ends with a triple quotes.

01:10.030 --> 01:11.440
So that's what you have over here.

01:11.470 --> 01:17.830
Your new line is a must for the compiler to be happy, and which is also the beginning of the text block,

01:17.830 --> 01:24.880
where we can start providing the text and the indentation of the whole text block is based on the closing

01:24.910 --> 01:25.840
triple quotes.

01:25.870 --> 01:31.450
I'll show you examples where the ending triple quotes drive the indentation of the whole string when

01:31.450 --> 01:32.740
we get to the coding part.

01:32.770 --> 01:37.750
The benefit with this approach is that the code looks much cleaner and more readable.

01:37.750 --> 01:43.030
Some of the real time examples where our application can benefit from text blocks is that we can use

01:43.060 --> 01:45.040
text blocks to write a SQL queries.

01:45.070 --> 01:50.320
Actually, because it is a very common pattern for Java applications to interact with the DB.

01:50.350 --> 01:55.810
If you are using something like Springs, JDBC template, or some other SQL frameworks where you write

01:55.810 --> 02:01.740
a SQL query in those kind of scenarios, using triple quotes will be very beneficial, because the code

02:01.740 --> 02:06.630
that you're going to write is going to be very similar to the code that you write in a SQL client.

02:06.660 --> 02:12.390
Another example where text blocks can be handy is that we can use text blocks to represent JSON in a

02:12.390 --> 02:16.260
much cleaner approach compared to the regular string representation.

02:16.290 --> 02:18.510
I hope you all have a pretty good idea about text blocks.

02:18.540 --> 02:22.200
Now it's time to jump into the code and explore this feature in action.

02:22.230 --> 02:22.710
There you go.

02:22.740 --> 02:23.730
I'm back in IntelliJ.

02:23.820 --> 02:27.540
So we are still going to be working on the Explore latest Java Features project.

02:27.540 --> 02:30.510
And in here you will notice a package named Text Blocks.

02:30.510 --> 02:31.920
So let's open the text blocks.

02:31.920 --> 02:35.040
So in here I have an example already available.

02:35.040 --> 02:37.860
But this is using the regular string approach.

02:37.860 --> 02:39.930
And this is a multi-line string.

02:39.960 --> 02:42.900
Let's execute this program and then look at the result in the console.

02:44.340 --> 02:44.880
There you go.

02:44.880 --> 02:47.280
So this is the string that is printed in the console.

02:47.310 --> 02:52.020
Now what I'm going to do is I'm going to represent the same multi-line string using triple quotes.

02:52.020 --> 02:55.590
So what we will do, we'll copy this code and then put it over here.

02:55.590 --> 02:58.380
and here I'm going to name this one as V two.

02:58.410 --> 02:58.770
Okay.

02:58.800 --> 03:02.640
And then we are going to change this approach to a triple quoted string.

03:02.670 --> 03:07.050
So in this case what I'm going to do I'm going to define the triple quoted string.

03:07.080 --> 03:08.130
There you go.

03:08.220 --> 03:12.930
And then what I'm going to do is I'm going to copy the content from here.

03:12.930 --> 03:14.820
So in this case let's do this.

03:14.820 --> 03:19.080
And then you can pretty much copy the content that we have over there.

03:19.080 --> 03:21.150
And then we can actually copy this one too.

03:21.180 --> 03:25.650
So this is how you represent a multi-line string using text blocks.

03:25.650 --> 03:30.210
And one of the thing that I mentioned is that the ending triple quotes drive the indentation.

03:30.240 --> 03:30.510
Right.

03:30.540 --> 03:33.690
So in this case you see this actual green line over here.

03:33.690 --> 03:36.300
So this is the indentation for this particular string.

03:36.300 --> 03:38.340
What we'll do is let's print this one first.

03:38.340 --> 03:40.770
And then I'll show you how to play with this indentation.

03:42.210 --> 03:44.700
So I'm going to copy and paste this over here.

03:44.700 --> 03:47.970
So I'm going to actually call this multi-line string v two.

03:48.000 --> 03:49.410
Multi-line string v two.

03:49.440 --> 03:51.990
Let's execute this particular main function.

03:51.990 --> 03:54.050
So this is going to print the value over here.

03:54.050 --> 03:57.800
So in here, as you can see the data is printed in the console.

03:57.800 --> 04:00.170
So the code still works as expected.

04:00.170 --> 04:05.240
But if you take a look at the code comparing with the previous version, this code looks much cleaner

04:05.240 --> 04:06.260
compared to this one.

04:06.260 --> 04:09.470
And another thing is that this line drives the indentation, right?

04:09.470 --> 04:12.950
So let's say if I want to introduce a space in the second line.

04:12.980 --> 04:13.190
Right.

04:13.220 --> 04:14.810
So what you can do is you can do this.

04:14.810 --> 04:18.080
This will automatically introduce a space in the second line.

04:18.080 --> 04:23.810
So now if you go and execute the function here so you will be noticing a space before this second line.

04:25.880 --> 04:26.300
There you go.

04:26.330 --> 04:27.530
You see the space over here.

04:27.530 --> 04:31.520
So this space is added by our code that we added over here.

04:31.520 --> 04:37.880
So in this case let's say I want to have a indentation for a space for each and every line that I have

04:37.910 --> 04:38.390
over here.

04:38.390 --> 04:43.310
So in this case what you need to do is you just move the triple quoted string towards your left.

04:43.310 --> 04:46.820
As you can see now the indentation, this is a starting point of the line.

04:46.820 --> 04:50.120
And we have a space introduced for each and every line.

04:50.120 --> 04:53.380
So in this case if we execute this program you will see a different result.

04:53.560 --> 04:54.820
Let's execute this program.

04:54.820 --> 04:59.380
And in here, as you can see, there is a space that is being added for each line over here.

04:59.380 --> 05:04.150
So this is how you define a multi-line string in a modern Java using text blocks.

05:04.180 --> 05:09.580
Now let's say we want to have some dynamic code being added before we print the value into the console.

05:09.580 --> 05:11.530
So the way that I'm thinking here is that.

05:11.530 --> 05:16.810
So let's say we are going to be introducing this, and then we are going to be adding a name over here

05:16.930 --> 05:17.380
okay.

05:17.410 --> 05:20.020
And in here I want to print hello name.

05:20.020 --> 05:21.670
And then this particular string.

05:21.670 --> 05:23.710
So in this case what we can do is hello.

05:23.710 --> 05:26.770
And we can do percentage s which is the format function.

05:26.770 --> 05:31.240
And then we can still use all these functions as like we normally work with a string.

05:31.240 --> 05:35.500
So in this case we can call dot formatted and then pass the name.

05:35.500 --> 05:40.240
So this will automatically actually open the name to this one and then print that value in the console.

05:40.240 --> 05:43.300
So I'm going to name this one as string with format.

05:43.300 --> 05:47.710
So now what we are going to do we are going to actually call that function from our main function.

05:48.040 --> 05:51.240
So in this case we'll put this over here.

05:51.270 --> 05:54.900
We'll put this over here, and then we'll pass the name as any names.

05:54.900 --> 05:57.210
In this case, I'm going to pass the name as Dileep.

05:57.240 --> 05:59.310
Now if I go ahead and execute this function.

05:59.310 --> 06:01.560
So this is going to print the value in the console.

06:01.560 --> 06:05.610
But in the place of percentages that's what we are doing over here.

06:05.610 --> 06:10.050
We are actually replace the value that you pass to the formatted function.

06:10.050 --> 06:14.730
So this particular functionality still works with the text blocks.

06:14.730 --> 06:16.410
There are no changes to that one.

06:16.410 --> 06:18.750
Now let's write some SQL query right.

06:18.780 --> 06:22.620
So in this case what I'm going to do I'm going to create a function similar to this one.

06:24.120 --> 06:26.730
And in here I'm going to name this one as SQL.

06:26.760 --> 06:29.430
We don't need to pass any function over here.

06:29.430 --> 06:32.400
So in this case what we can do is we can write a SQL right.

06:32.400 --> 06:34.320
So let's remove all these things.

06:34.320 --> 06:39.570
So we can write function like select star from employee.

06:39.570 --> 06:47.370
And then you can actually pass where first underscore name equal to delete and last underscore name

06:47.370 --> 06:50.880
equal to Sundar Raj, so this will still work as expected.

06:50.880 --> 06:53.370
So this is where the text blocks come in handy.

06:53.370 --> 06:57.090
If you try to print this value in the console, this should work without any issues.

06:57.120 --> 07:04.650
In this case, let's go ahead and call this function SQL and SQL.

07:05.220 --> 07:06.810
It doesn't accept any argument.

07:06.810 --> 07:08.100
Let's go ahead and remove that.

07:08.100 --> 07:11.130
And then now what we are going to do we are going to execute this function.

07:12.270 --> 07:13.890
So in here in the console you see right.

07:13.890 --> 07:16.050
The SQL is printed very nicely.

07:16.050 --> 07:18.240
And the similar concept works for JSON too.

07:18.270 --> 07:24.420
So what we can do is we can actually create another function and then call this one as JSON.

07:24.450 --> 07:29.160
So let's remove everything over here and let's create a sample JSON over here.

07:29.160 --> 07:34.230
So in this case you can actually define let's say we wanted to create a JSON for an order.

07:34.260 --> 07:38.700
So in this case it could be order underscore id equal to 123456.

07:38.700 --> 07:43.050
And then you can actually provide some additional parameters like status.

07:43.170 --> 07:45.330
Let's say this is an online food order.

07:45.330 --> 07:50.840
So we can have a status like delivered next property could be final amount or final charge.

07:50.840 --> 07:57.140
And then we can have the value represented as 99.99 or whatever value you want.

07:57.170 --> 08:02.300
But the fundamental idea is you can write JSON as like you would want to see it actually.

08:02.300 --> 08:03.740
So that's again another benefit.

08:03.740 --> 08:09.020
So what we will do, we will actually go ahead and call this one.

08:09.020 --> 08:11.630
So in here let's pass the JSON over here.

08:11.660 --> 08:12.200
There you go.

08:12.230 --> 08:14.510
Now let's go ahead and execute this particular function.

08:14.510 --> 08:17.240
So the JSON should printed as expected.

08:17.270 --> 08:17.690
There you go.

08:17.720 --> 08:19.370
The JSON is printed as expected.

08:19.370 --> 08:25.610
So the point that I'm trying to explain is that with text blocks, working with strings are going to

08:25.610 --> 08:29.870
be very much easy and efficient compared to the previous approaches.

08:29.870 --> 08:31.910
So this is all about text blocks.

08:31.910 --> 08:37.520
So in your code, anytime you have a use case to use a multi-line string, or if you are writing SQL

08:37.520 --> 08:43.040
queries, I highly recommend you to start using the triple quoted string or the text blocks.

08:43.070 --> 08:44.780
Well, this marks the end of this lecture.

08:44.780 --> 08:45.800
Thank you for watching.
