Nearby lessons
117 of 125Java 12 and 13 Features
- Switch expressions (first preview)
- String indent() and transform()
- Collectors.teeing
- Compact number formatting
Java 12 and 13 Features is a core concept of the Java language. This lesson explains String.indent(), String.transform() and Collectors.teeing() with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
String.indent()
indent(n) adds or removes spaces at the start of every line. A positive number adds spaces; a negative number removes them.
String.transform()
transform() lets you apply a chain of operations to a String in one line:
Collectors.teeing()
teeing() is an advanced Stream collector that processes the stream two ways at once and merges both results. Famous example — getting the min and max together:
Compact Number Formatting
Java 12 lets us format big numbers the human-friendly way — 1.2K, 3.5M:
Preview Features — What Does That Mean?
Java 12 called switch expressions a preview feature. A preview means: we are showing you this feature to try and give feedback; it may change before it becomes final. You had to compile with --enable-preview to use it. Preview features become final in a later version (switch expressions became final in Java 14).
String.formatted()
Text blocks made formatting easy too. formatted() works just like String.format() but is attached to the string:
The Preview Journey — A Good Lesson
Text blocks in Java 13 were a second preview (improved from 13's first look). They became final in Java 15. This shows the careful way Java grows: an idea appears as a preview, gets feedback and changes, then becomes permanent.
| Feature | Preview in | Final in |
|---|---|---|
| Switch expressions | Java 12 (first), Java 13 (yield added) | Java 14 |
| Text blocks | Java 13 (first), Java 14 (second) | Java 15 |
Other Java 13 Additions
- Dynamic CDS Archives — faster startup by sharing class data (advanced).
- ZGC enhancements — the garbage collector could return unused memory to the OS.
- `Files.mismatch()` — finds the first byte where two files differ.
- Java 12 introduced switch expressions as a preview — no break, arrows, returns a value.
- indent(n) adds/removes leading spaces on every line.
- transform() chains String operations in one line.
- Collectors.teeing() runs two collectors at once and merges results.
- CompactNumberFormat shows big numbers as 1.2K, 3.5M.
- Preview features are experiments that become final later.