Back to Java 11 Features
Q3Question 3 of 5

What String and Optional improvements came in Java 11?

Key Concepts
  • isBlank() returns true for empty OR whitespace-only strings, unlike isEmpty() which only checks length == 0
  • strip() is Unicode-aware and removes non-breaking spaces that trim() misses
  • lines() returns a lazy Stream<String> split by line terminators
  • repeat(n) duplicates a string n times without manual StringBuilder loops
  • Files.readString(path) and writeString(path, text) replace verbose byte-array patterns
  • Optional.isEmpty() is the inverse of isPresent() for cleaner negation

Definition

Java 11 added isBlank (empty or whitespace), strip (Unicode-aware trim), lines (lazy stream), repeat (duplicate), Files.readString/writeString (one-liner I/O), and Optional.isEmpty (inverse of isPresent).
  • isBlank() returns true for empty strings AND whitespace-only strings. " ".isBlank() is true. " ".isEmpty() is false.
  • strip() removes leading and trailing whitespace using Character.isWhitespace(), which handles Unicode whitespace like \u00A0. trim() only handles ASCII characters with code point <= 32.
  • lines() returns a lazy Stream split on line terminators (\n, \r, \r\n).
  • repeat(n) returns the string repeated n times: "=-".repeat(20) produces a separator line.
  • Files.readString(Path) reads an entire file to a String. Files.writeString(Path, String) writes a String to a file.
  • Optional.isEmpty() returns true when the Optional is empty: cleaner than !opt.isPresent().

strip() vs trim() is like two levels of cleaning at National Bank of India. trim() is basic cleaning (removes common dirt). strip() is professional cleaning (removes exotic dirt too, like Unicode non-breaking spaces from imported data). isBlank() is the inspector who catches both empty rooms and rooms with only invisible furniture (whitespace).

Understanding the Concepts

How is isBlank different from isEmpty?

JAVA
System.out.println("".isEmpty());       // true
System.out.println("".isBlank());       // true
System.out.println("   ".isEmpty());    // false -- has 3 spaces
System.out.println("   ".isBlank());    // true  -- only whitespace
System.out.println("Rajesh".isBlank()); // false

isBlank() is strictly better than isEmpty() for input validation because it catches whitespace-only strings.

How is strip different from trim?

JAVA
// Non-breaking space (\u00A0): common in imported data
String imported = "\u00A0Rajesh\u00A0";
System.out.println(imported.trim());   // "\u00A0Rajesh\u00A0" -- not removed!
System.out.println(imported.strip());  // "Rajesh" -- removed!
DIAGRAM
| Method | Removes | Unicode-aware |
|--------|---------|---------------|
| `trim()` | chars <= 32 (ASCII) | No |
| `strip()` | `Character.isWhitespace()` | Yes |
| `stripLeading()` | Leading whitespace only | Yes |
| `stripTrailing()` | Trailing whitespace only | Yes |

How does lines() work?

JAVA
String statement = "Date,Amount,Type\n2024-01-15,50000,NEFT\n2024-01-16,30000,UPI";

statement.lines()
    .skip(1)  // skip header
    .map(line -> line.split(",")[1])
    .forEach(amount -> System.out.println("Amount: " + amount));
TEXT
Amount: 50000
Amount: 30000

lines() is lazy and handles \n, \r, and \r\n line terminators.

How does repeat work?

JAVA
System.out.println("=-".repeat(30));  // separator line
System.out.println("*".repeat(5));    // *****
System.out.println("abc".repeat(0));  // "" (empty)

How do Files.readString and writeString simplify I/O?

JAVA
import java.nio.file.*;

// Before Java 11
String old = new String(Files.readAllBytes(Path.of("template.txt")));

// Java 11
String modern = Files.readString(Path.of("template.txt"));
Files.writeString(Path.of("output.txt"), modern);

How is Optional.isEmpty useful?

JAVA
import java.util.Optional;

Optional<String> account = Optional.empty();

// Before Java 11
if (!account.isPresent()) { /* handle empty */ }

// Java 11
if (account.isEmpty()) { /* handle empty */ }

What are common mistakes?

WRONG: Using trim() on Unicode data and missing non-breaking spaces.

WRONG: Using isEmpty() for validation and missing whitespace-only input.

WRONG: Using Files.readString on huge files that should be streamed.

JAVA
// WRONG: loads entire 2GB file into memory
String huge = Files.readString(Path.of("transactions.csv"));

// CORRECT: lazy streaming for large files
try (var lines = Files.lines(Path.of("transactions.csv"))) {
    lines.filter(l -> l.contains("NEFT")).forEach(System.out::println);
}

What should a senior engineer know?

  • Files.readString loads the entire file into memory. For files larger than available heap, use Files.lines(path) which is lazy and must be closed.
  • strip() uses Character.isWhitespace(int) which considers Unicode whitespace categories. trim() uses a simple c <= ' ' check.
  • lines() does not include trailing empty strings from the final line terminator.
  • repeat(n) throws OutOfMemoryError if n is very large. It uses Arrays.copyOf internally for efficiency.

Visual Overview

DIAGRAM
  trim() vs strip():

  Input: "\u00A0 Rajesh \u00A0"
         ^                   ^
         non-breaking spaces

  trim():  "\u00A0 Rajesh \u00A0"  (unchanged!)
  strip(): "Rajesh"               (cleaned!)

  isEmpty() vs isBlank():

  ""    → isEmpty=true,  isBlank=true
  "   " → isEmpty=false, isBlank=true
  "abc" → isEmpty=false, isBlank=false

Key Takeaway

Java 11 added convenience methods that eliminate boilerplate. isBlank() catches whitespace-only strings that isEmpty() misses. strip() handles Unicode whitespace that trim() ignores. lines() lazily splits a string into a Stream. repeat(n) duplicates without manual loops. Files.readString/writeString replace verbose byte-array patterns. Optional.isEmpty() is the cleaner inverse of isPresent().

At National Bank of India, the statement processor reads a template with Files.readString(path), splits rows with .lines(), trims imported Unicode data with .strip(), and writes the rendered output with Files.writeString(out, rendered) in a clean pipeline.

Remember one sentence: isBlank catches whitespace, strip handles Unicode, lines gives a Stream, and readString/writeString make file I/O one-liners.

---

Share
Use ← → arrow keys to navigate