25 Aralık 2020 Cuma

Java 13 Text Blocks

Giriş
Java 13 ile geliyor. Çok satırlı (multi-line) string imkanı tanıyor. Text Block """ ile başlar ve """ ile biter. Açıklaması şöyle
The opening delimiter is defined by a sequence of three double quotes, followed by zero or more spaces and a line terminator. The content of the text block starts from the first character after the line terminator. Therefore, any white spaces between the three quotation marks and the line terminator are not taken into consideration.

The closing delimiter, on the other hand, is defined only by three double quotes sequence. The content of the text block ends with the character preceding the first double quotes in the sequence, of the closing delimiter.
Derleme Aşamaları
Açıklaması şöyle
There are three phases that are performed during compile-time:

- Normalization of line terminators.
- Removal of white spaces that were introduced to align the text block with the Java code.
- Interpretation of escape characters.
1. Normalization of Line Terminators
Açıklaması şöyle. Yani Line Terminator olarak \n karakteri kullanılır
Normalization for text blocks always transforms all line terminators into LF, regardless of the platform on which it runs. 
2. Removing Superfluous White Spaces
Açıklaması şöyle
After the normalization process, a text block will clearly consist of one or more lines. The algorithm for removing superfluous white spaces, (i.e., the spaces introduced to align the text block code with the Java code) includes:

- The removal of all of the white spaces that are at the end of each line.
- The removal of all of the white spaces that are at the beginning of each line, common to all lines.
Açıklaması şöyle
The algorithm described in this section is implemented through the use of the static method of the String class introduced with Java 13 stripIndent.
Örnek
Şöyle yaparız. Burada en son kapanma bölümünü satır başına aldığımı için "minimum number of common initial white spaces" 0 oluyor, çıktı da sağa doğru kayar
String textBlock = """
    This is a text block!
""";
3. Interpretation of escape characters
Açıklaması şöyle. Yani \" yapmaya gerek yok
Technically it is also possible to use the escape characters, and \", but it is useless and therefore not recommended.


Hiç yorum yok:

Yorum Gönder