-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jdk15.java
69 lines (63 loc) · 2.59 KB
/
Jdk15.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package samples.jdk15;
public class Jdk15 {
public static void main(String[] args) {
//stringConcatenation();
noStringConcatenationWithTextBlock();
formattedTextBlock();
}
/**
* Shows how to use a Text Block.
* See that we don't have to worry about String concatenation,
* neither manually insert line breaks (\n).
* Line breaks are inserted automatically when you press ENTER inside
* the Text Block.
*
* @see <a href="https://openjdk.org/jeps/378">JEP 378</a>
*/
private static void noStringConcatenationWithTextBlock() {
final String html =
"""
<table>
<tr><th>Site</th><th>Domain</th></tr>
<tr><td>Instagram</td><td>https://instagram.com/manoelcampos</td></tr>
<tr><td>GitHub</td><td>https://github.com/manoelcampos</td></tr>
<tr><td>Twitter</td><td>https://twitter.com/manoelcampos</td></tr>
<tr><td>YouTube</td><td>https://youtube.com/manoelcamposfh</td></tr>
</table>
""";
System.out.println(html);
}
/**
* Show how you can create formatted
* Strings that are easier to ready,
* providing a more clean code.
*/
private static void formattedTextBlock() {
final String city = "Palmas", state = "Tocantins";
final double areaKm2 = 2.219;
final String cityStr =
"""
City: %s
State: %s
Area: %.2f KM²
""".formatted(city, state, areaKm2);
System.out.println(cityStr);
/*
//The same code using the traditional String.format method.
//The code is less readable and the dev needs to manually include line breaks.
final String cityStr2 = String.format("City: %s\nState: %s\nArea: %.2f KM²", city, state, areaKm2);
System.out.println(cityStr2);
*/
}
private static void stringConcatenation() {
final String html =
"<table>\n" +
" <tr><th>Site</th><th>Domain</th></tr>\n" +
" <tr><td>Instagram</td><td>https://instagram.com/manoelcampos</td></tr>\n" +
" <tr><td>GitHub</td><td>https://github.com/manoelcampos</td></tr>\n" +
" <tr><td>Twitter</td><td>https://twitter.com/manoelcampos</td></tr>\n" +
" <tr><td>YouTube</td><td>https://youtube.com/manoelcamposfh</td></tr>\n" +
"</table>\n";
System.out.println(html);
}
}