Check out a free preview of the full Java Fundamentals course

The "Text Blocks" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses working with multi-line strings using text blocks and demonstrates how to define them with three quotation marks. Student questions regarding what data type text blocks are and the return type of the function containing StringBuilder are also covered in this segment.

Preview
Close

Transcript from the "Text Blocks" Lesson

[00:00:00]
>> Let's talk about text blocks, which are basically used to work with multi-line strings, all right? And this is also something that's relatively new to the Java language, maybe a year or two old. But yeah, text can be pretty ugly to work with in Java, especially when it's multi-line.

[00:00:23]
So this was life before text blocks. Let's say you had a JSON response that you wanted to represent in the string. I would have to, one, add all of these new line characters, so that \n is a new line character, after each line. And then these ugly plus signs, I would need to escape any quotes, so you know JSON has quotes, for the key part.

[00:00:52]
And so, each one of those, I would have to put a slash in front of it. It's absolute nightmare to deal with, all right? So text blocks allows you to cleanly define multi-line strings by using three quotation marks to open and to close. Now, I don't have the new line characters, I don't have the plus signs, I don't have to escape any quotes.

[00:01:17]
It's just nice and clean, and I can actually read this JSON now [LAUGH]. So that's text blocks, they work pretty much like strings, other than how they're defined here. Some rules, you cannot open and close a text block all on the same line, right? They're really supposed to be for multi-line text anyway.

[00:01:41]
So this doesn't really make sense to have this one line in a text block versus just a regular string. But yeah, you cannot have the opening and closing on the same line. So I move the text to a new line, this is legal, but this is also not the preferred way.

[00:02:00]
So the preferred convention is start the three lines, add any text you need in the three quotes on its own new line, right? All right, so that is it for text, so we talked about strings. We talked about string builder, and then we also talked about text blocks.

[00:02:30]
Any questions on strings, yeah.
>> Is text blocks, are they similar to strings, do they have the same split and all that kind of stuff?
>> Yeah, so you can utilize them. You can even, when you define the type, you would even define that as string. So I can say string message = this hey y'all, and then I do whatever I want.

[00:02:53]
So the data type is a string. Good question, yes.
>> When you're working with StringBuilder, would the return type of the function be StringBuilder?
>> Which function is it? Are they referring to-
>> I think that, yeah, they're referring to the-
>> The constructor. So this, I put var here.

[00:03:23]
So this would actually be a StringBuilder. That would be the type, not string.
>> And I think they're asking, if they were to be returning from a function with StringBuilder, StringBuilder would be the return type.
>> StringBuilder is the type, so StringBuilder is not the same as string, right?

[00:03:46]
StringBuilder is a mutable version of string. So yeah, the datatype would be different. Let's see what happens if I put string here. Yeah, doesn't like it.
>> You declared it as a new StringBuilder, so. The modified type-
>> I'll show you that doesn't always matter in a later lesson.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now