Text blocks in Java
Multi-line strings that keep their shape.
// a text block keeps its line breaks and strips the common indent
var report = """
typestar build
tests: passing
seeds: 1604
""";
System.out.println(report);
// formatted() is printf aimed at a string
var line = "%-8s %5.1f%%".formatted("java", 99.5);
System.out.println(line);
System.out.println(report.lines().count());
How it works
- Triple quotes open a block; the common indent is stripped.
formattedis printf aimed at a string instead of the console.lines()streams the block line by line.
Keywords and builtins used here
var
The run, in numbers
- Lines
- 12
- Characters to type
- 323
- Tokens
- 62
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 41 seconds.
Step 2 of 4 in Strings, step 5 of 29 in Language basics.