Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet34/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   30   31   32   33   34   35   36   37   ...   245
Bog'liq
Java 17 Recipes

2-3. Writing a Text Block
 Problem
You want to write a string in multiple lines in a smart way.
 Solution
A smart form for writing a multiline string in a simple text block was introduced in
Java 15 (JEP 378), though first proposed in Java 13.
The following is an example.
public class MultipleLineStringEx {
public static void main (String[] args) {
String writeTextStandard = "\n" +
"
Ciao, hello
\n" +
"\n";
Chapter 2 enhanCements from Java 9 through Java 17 


54
String writeTextSmart = """

Ciao, hello

""";
System.out.println(writeTextStandard);
System.out.println(writeTextSmart);
}
}
The output is the same.
 How It Works
Java 13 introduced an enhancement to make multiline strings more readable. So, text 
block efficiently eliminates the concatenation of multiple strings together. The need was 
to choose a way to visually text blocks and string literals: the delimiter """ was chosen. 
Moreover, indentation was removed and replaced with whitespace characters.
For more information on text blocks, see the documentation at 
https://openjdk 
.java.net/jeps/378
.
2-4. The Enhancement of NullPointerException
 Problem
You want to know which variable is null when a NullPointerException occurs.
 Solution
Java 14 improved the NullPointerException functionalities generated when an 
error occurs.
The following is an example.
Line 1:package org.java17recipes.chapter02.recipe02_04;
Line 2:
Line 3:public class NullPointerExample {
Chapter 2 enhanCements from Java 9 through Java 17 


55
Line 4: public static void main (String[] args) {
Line 5: String professor= null;

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling