-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTemplates.java
More file actions
69 lines (57 loc) · 2.64 KB
/
Copy pathStringTemplates.java
File metadata and controls
69 lines (57 loc) · 2.64 KB
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
package java22.stringtemplates;
/**
* Java 22 String Templates (Second Preview) Demonstrates string interpolation with template processors
* NOTE: String Templates were withdrawn in Java 23+ due to design concerns. This code demonstrates the concept using traditional string formatting.
* For Java 22, String Templates were available with FormatProcessor. For Java 23+, use traditional string concatenation or String.format().
*/
public class StringTemplates
{
public static void main(String[] args)
{
System.out.println("=== Java 22 String Templates (Second Preview) ===\n");
String name = "John";
int age = 30;
System.out.println("NOTE: String Templates were withdrawn in Java 23+");
System.out.println("Using traditional string formatting instead:\n");
// 1. Basic string interpolation (traditional approach)
System.out.println("1. Basic String Interpolation");
System.out.println("-----------------------------");
String message = "Hello, " + name + "! You are " + age + " years old.";
System.out.println(message);
// 2. Formatted template (using String.format)
System.out.println("\n2. Formatted Templates");
System.out.println("---------------------");
String formatted = String.format("Age: %03d", age);
System.out.println(formatted); // Age: 030
// 3. Complex expressions
System.out.println("\n3. Complex Expressions");
System.out.println("---------------------");
int x = 10;
int y = 20;
String calculation = "Sum: " + x + " + " + y + " = " + (x + y);
System.out.println(calculation);
// 4. Multi-line template
System.out.println("\n4. Multi-line Template");
System.out.println("---------------------");
String info = """
Name: %s
Age: %d
Status: Active
""".formatted(name, age);
System.out.println(info);
// 5. Comparison with old approach
System.out.println("\n5. Comparison");
System.out.println("-------------");
// Traditional way
String oldWay = "Hello, " + name + "! You are " + age + " years old.";
// Using String.format (more readable for complex formatting)
String formattedWay = String.format("Hello, %s! You are %d years old.", name, age);
System.out.println("Concatenation: " + oldWay);
System.out.println("String.format: " + formattedWay);
System.out.println("\nNote: String Templates (STR.\"text{var}\") were available in Java 21-22");
System.out.println("but were withdrawn in Java 23+ due to design concerns.");
System.out.println("\nExample of what String Templates looked like (Java 22):");
System.out.println(" String message = STR.\"Hello, \\{name}! You are \\{age} years old.\";");
System.out.println(" String formatted = FMT.\"Age: %03d\\{age}\";");
}
}