Hi everyone.
Welcome back.
In this lecture we will code and learn about the next exciting feature that is part of the modern Java
which is text blocks.
I'll start with some theory concepts first and then we will jump into the coding part.
This feature got released in Java 15.
Let me start with a quick background about why this feature was added to the Java language in the first
place.
Java's string type, which was considered really primitive compared to the other programming languages.
For example, if you have a string that holds double quotes, then you need to add an escape character
like a backslash for the compiler to compile and treat this value as a string.
Another example where you need a multi-line string.
Then we need to add slash n at the end of each line in order to introduce new lines.
This kind of works actually, but still this looks ugly.
So this is where text blocks comes to the rescue.
And it's primarily introduced in Java to work with strings in a better and easy way.
So now it's time to introduce you to text blocks.
Here is an example of how to define a text block.
The text block begins and ends with a triple quotes.
So that's what you have over here.
Your new line is a must for the compiler to be happy, and which is also the beginning of the text block,
where we can start providing the text and the indentation of the whole text block is based on the closing
triple quotes.
I'll show you examples where the ending triple quotes drive the indentation of the whole string when
we get to the coding part.
The benefit with this approach is that the code looks much cleaner and more readable.
Some of the real time examples where our application can benefit from text blocks is that we can use
text blocks to write a SQL queries.
Actually, because it is a very common pattern for Java applications to interact with the DB.
If you are using something like Springs, JDBC template, or some other SQL frameworks where you write
a SQL query in those kind of scenarios, using triple quotes will be very beneficial, because the code
that you're going to write is going to be very similar to the code that you write in a SQL client.
Another example where text blocks can be handy is that we can use text blocks to represent JSON in a
much cleaner approach compared to the regular string representation.
I hope you all have a pretty good idea about text blocks.
Now it's time to jump into the code and explore this feature in action.
There you go.
I'm back in IntelliJ.
So we are still going to be working on the Explore latest Java Features project.
And in here you will notice a package named Text Blocks.
So let's open the text blocks.
So in here I have an example already available.
But this is using the regular string approach.
And this is a multi-line string.
Let's execute this program and then look at the result in the console.
There you go.
So this is the string that is printed in the console.
Now what I'm going to do is I'm going to represent the same multi-line string using triple quotes.
So what we will do, we'll copy this code and then put it over here.
and here I'm going to name this one as V two.
Okay.
And then we are going to change this approach to a triple quoted string.
So in this case what I'm going to do I'm going to define the triple quoted string.
There you go.
And then what I'm going to do is I'm going to copy the content from here.
So in this case let's do this.
And then you can pretty much copy the content that we have over there.
And then we can actually copy this one too.
So this is how you represent a multi-line string using text blocks.
And one of the thing that I mentioned is that the ending triple quotes drive the indentation.
Right.
So in this case you see this actual green line over here.
So this is the indentation for this particular string.
What we'll do is let's print this one first.
And then I'll show you how to play with this indentation.
So I'm going to copy and paste this over here.
So I'm going to actually call this multi-line string v two.
Multi-line string v two.
Let's execute this particular main function.
So this is going to print the value over here.
So in here, as you can see the data is printed in the console.
So the code still works as expected.
But if you take a look at the code comparing with the previous version, this code looks much cleaner
compared to this one.
And another thing is that this line drives the indentation, right?
So let's say if I want to introduce a space in the second line.
Right.
So what you can do is you can do this.
This will automatically introduce a space in the second line.
So now if you go and execute the function here so you will be noticing a space before this second line.
There you go.
You see the space over here.
So this space is added by our code that we added over here.
So in this case let's say I want to have a indentation for a space for each and every line that I have
over here.
So in this case what you need to do is you just move the triple quoted string towards your left.
As you can see now the indentation, this is a starting point of the line.
And we have a space introduced for each and every line.
So in this case if we execute this program you will see a different result.
Let's execute this program.
And in here, as you can see, there is a space that is being added for each line over here.
So this is how you define a multi-line string in a modern Java using text blocks.
Now let's say we want to have some dynamic code being added before we print the value into the console.
So the way that I'm thinking here is that.
So let's say we are going to be introducing this, and then we are going to be adding a name over here
okay.
And in here I want to print hello name.
And then this particular string.
So in this case what we can do is hello.
And we can do percentage s which is the format function.
And then we can still use all these functions as like we normally work with a string.
So in this case we can call dot formatted and then pass the name.
So this will automatically actually open the name to this one and then print that value in the console.
So I'm going to name this one as string with format.
So now what we are going to do we are going to actually call that function from our main function.
So in this case we'll put this over here.
We'll put this over here, and then we'll pass the name as any names.
In this case, I'm going to pass the name as Dileep.
Now if I go ahead and execute this function.
So this is going to print the value in the console.
But in the place of percentages that's what we are doing over here.
We are actually replace the value that you pass to the formatted function.
So this particular functionality still works with the text blocks.
There are no changes to that one.
Now let's write some SQL query right.
So in this case what I'm going to do I'm going to create a function similar to this one.
And in here I'm going to name this one as SQL.
We don't need to pass any function over here.
So in this case what we can do is we can write a SQL right.
So let's remove all these things.
So we can write function like select star from employee.
And then you can actually pass where first underscore name equal to delete and last underscore name
equal to Sundar Raj, so this will still work as expected.
So this is where the text blocks come in handy.
If you try to print this value in the console, this should work without any issues.
In this case, let's go ahead and call this function SQL and SQL.
It doesn't accept any argument.
Let's go ahead and remove that.
And then now what we are going to do we are going to execute this function.
So in here in the console you see right.
The SQL is printed very nicely.
And the similar concept works for JSON too.
So what we can do is we can actually create another function and then call this one as JSON.
So let's remove everything over here and let's create a sample JSON over here.
So in this case you can actually define let's say we wanted to create a JSON for an order.
So in this case it could be order underscore id equal to 123456.
And then you can actually provide some additional parameters like status.
Let's say this is an online food order.
So we can have a status like delivered next property could be final amount or final charge.
And then we can have the value represented as 99.99 or whatever value you want.
But the fundamental idea is you can write JSON as like you would want to see it actually.
So that's again another benefit.
So what we will do, we will actually go ahead and call this one.
So in here let's pass the JSON over here.
There you go.
Now let's go ahead and execute this particular function.
So the JSON should printed as expected.
There you go.
The JSON is printed as expected.
So the point that I'm trying to explain is that with text blocks, working with strings are going to
be very much easy and efficient compared to the previous approaches.
So this is all about text blocks.
So in your code, anytime you have a use case to use a multi-line string, or if you are writing SQL
queries, I highly recommend you to start using the triple quoted string or the text blocks.
Well, this marks the end of this lecture.
Thank you for watching.