Nearby lessons

117 of 125

Java 12 and 13 Features

📌 What You Will Learn
  • 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.

In simple words: `indent(n)` shifts every line of the string right or left. A positive number adds leading spaces to every line; a negative number removes spaces that are already there.
Example01
JCode Cell
1class IndentDemo {
2 public static void main(String[] args) {
3 String text = "Hello\nWorld";
4 
5 System.out.println(text.indent(4)); // add 4 spaces to each line
6 // Output:
7 // Hello
8 // World
9 }
10}
Output
Hello World

String.transform()

transform() lets you apply a chain of operations to a String in one line:

In simple words: `transform()` applies one function to the whole String and returns the result. It is a helper step you can add at the end of a chain, so the whole pipeline stays in one line.
Example02
JCode Cell
1class TransformDemo {
2 public static void main(String[] args) {
3 var result = " java "
4 .trim() // "java"
5 .toUpperCase() // "JAVA"
6 .transform(s -> s + " Rocks!"); // "JAVA Rocks!"
7 
8 System.out.println(result);
9 }
10}
Output
JAVA Rocks!

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:

In simple words: `teeing()` runs two collectors on the same stream in one pass and merges their results. When you need two answers from one stream — like the min and the max — it does both in a single collect() call.
Example03
JCode Cell
1import java.util.*;
2import java.util.stream.*;
3 
4class TeeingDemo {
5 public static void main(String[] args) {
6 var numbers = List.of(45, 12, 78, 33, 90);
7 
8 var result = numbers.stream().collect(
9 Collectors.teeing(
10 Collectors.minBy(Integer::compareTo), // first job
11 Collectors.maxBy(Integer::compareTo), // second job
12 (min, max) -> min.get() + " to " + max.get() // merge
13 ));
14 
15 System.out.println("Range: " + result);
16 }
17}
Output
Range: 12 to 90

Compact Number Formatting

Java 12 lets us format big numbers the human-friendly way — 1.2K, 3.5M:

Example04
JCode Cell
1import java.text.NumberFormat;
2import java.util.Locale;
3 
4class CompactDemo {
5 public static void main(String[] args) {
6 NumberFormat fmt = NumberFormat.getCompactNumberInstance(
7 Locale.ENGLISH, NumberFormat.Style.SHORT);
8 
9 System.out.println(fmt.format(1200)); // 1.2K
10 System.out.println(fmt.format(3500000)); // 3.5M
11 }
12}
Output
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).

Trainer's Note: Remember: preview != stable. In exams and interviews, what matters is the final form of these features, which we study in the Java 13 and 14 chapters.

String.formatted()

Text blocks made formatting easy too. formatted() works just like String.format() but is attached to the string:

In simple words: `formatted()` fills the `%s` and `%d` placeholders right inside the text block. Instead of calling String.format(...) separately, you attach .formatted(...) to the text itself.
Example06
JCode Cell
1class FormatDemo13 {
2 public static void main(String[] args) {
3 String msg = """
4 Welcome, %s!
5 Your marks: %d
6 """.formatted("Rahul", 92);
7 
8 System.out.println(msg);
9 }
10}
Output
Welcome, Rahul! Your marks: 92

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.

In simple words: A preview is Java's way of testing a feature in public before making it permanent. You compile with --enable-preview to try it, and the feature may change until it becomes final.
FeaturePreview inFinal in
Switch expressionsJava 12 (first), Java 13 (yield added)Java 14
Text blocksJava 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.
Example08
JCode Cell
1long pos = Files.mismatch(path1, path2); // -1 if files are identical
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4