![]() Now you know the reason why text blocks were introduced into Java, Text blocks started out as a preview feature, first in Java 13 and the second iteration in Java 14, and after feedback and improving the feature, it’s now officially part of the Java 15 language. You might be wondering about whitespace handling and indentation for this text block, and this is something that we’ll dive into a bit later. Just by looking at this text block, it’s fully clear that we’re dealing with JSON, and there’s no syntactic noise to obscure this fact. However, I hope you’ll agree with me that this looks much, much nicer. In the end, a text block will result in a regular string.Īt runtime there’s absolutely no difference between this string being created as a text block or using regular strings in concatenation, as we saw before. Inside a text block, a double quote can be freely used. Also note that we don’t have to escape the quotes around the keys and the values in the JSON anymore. The new lines that are introduced here inside of the text block are preserved into the resulting string. In fact, the text block you see here leads to the exact same string that we saw in the previous examples. #JAVA 13 TEXT BLOCKS CODE#Then you can enter as many lines of text as you want in the source code until you close the text block again using three double quotes. A text block always starts with three double quotes and a new line. It allows you to freely express multiple lines of text using the new syntax shown here. The text blocks feature solves all of these issues. And the visual noise of the escape sequences makes it pretty hard to see the real intention of these multiple lines of text. We can only express multiple lines of text using multiple string literals and concatenating them. Some people prefer to use the string builder in this case to do the concatenation, but the point still stands. This is somewhat better, but you still have the explicit new line escape sequences and the escapes for the double quotes, so unfortunately, it still doesn’t really look like JSON. Usually we try to alleviate this by splitting the string literal into multiple string literals and then concatenating them using plus. All of this noise in the string makes it very hard to see what is actually going on. You have to use escape sequences to indicate new lines in the string.Īnd to top it off, you have to escape the quotes that are part of the JSON using a backslash. First of all, this leads to pretty long lines, which isn’t really great in your source code, but more importantly, the multiline nature of the string is obscured. One way would be to create a single string literal. Let’s say we want to create a string containing JSON in a pretty printed format using new lines. Whenever you wanted to have a string literal in source code that spans multiple lines, you had several options, but none of them were great. Up until now, there was no real way to have multiline string literals in source code. Text blocks actually address a long‑standing issue in the Java language. Now let’s explore why text blocks were added and what they can do. Since it’s now an official part of the language, you can start using it in production code, and the feature is here to stay. Text blocks started out as a preview feature in the Java language, but it now has graduated to be part of the Java language specification. Let’s dive into the text blocks language feature. Hope you gone thought the Java 15 Introduction ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |