Back to Java 11 Features
Q1Question 1 of 5

What are the key Java 11 features?

Key Concepts
  • var in lambda parameters enables annotations on lambda params: (@NonNull var x) -> x.process()
  • New String methods: isBlank(), strip() (Unicode-aware trim), lines(), repeat(n)
  • Predicate.not() provides clean static negation: filter(Predicate.not(String::isBlank))
  • Standardized HttpClient replaces HttpURLConnection with HTTP/2 support and async sendAsync()
  • Files.readString(path) and Files.writeString(path, text) simplify file I/O to one-liners
  • Single-file source launch: java File.java runs without explicit javac step

Definition

Java 11 (LTS) introduced var in lambdas for annotations, new String methods (isBlank, strip, lines, repeat), Predicate.not(), a standardized HttpClient with HTTP/2, and single-file source launch.
DIAGRAM
- **var in lambda parameters**: `(var x, var y) -> x + y` enables annotations on lambda params like `(@NonNull var x) -> x.process()`. Lambdas already infer types, so var does not add inference but enables annotation syntax.
- **New String methods**: `isBlank()` checks empty or whitespace-only, `strip()`/`stripLeading()`/`stripTrailing()` are Unicode-aware (unlike `trim()`), `repeat(n)` duplicates the string, `lines()` returns a `Stream<String>` of lines.
- **Predicate.not()**: static method for clean negation: `filter(Predicate.not(String::isBlank))` instead of `filter(s -> !s.isBlank())`.
- **HttpClient**: `java.net.http.HttpClient` replaces `HttpURLConnection` with HTTP/2, async via `sendAsync()`, and a builder API.
- **Files.readString/writeString**: one-liner file I/O replacing verbose reader/writer boilerplate.
- **Single-file launch**: `java File.java` compiles and runs in one step.

Java 11 is like National Bank of India upgrading its toolkit. var in lambdas is annotated forms. isBlank/strip is better string cleaning for KYC data. Predicate.not is a cleaner "NOT this" stamp. HttpClient is a modern API caller for payment gateways. Single-file run is quick scripts without a compile step.

Understanding the Concepts

What is var in lambda parameters?

Java 10 introduced var for local variables. Java 11 extends it to lambda parameters. This does not add type inference (lambdas already infer types) but enables annotations on parameters.

JAVA
// Without var: cannot annotate lambda params
(x, y) -> x + y

// With var: can annotate
(@NonNull var x, @NonNull var y) -> x + y

You must use var for ALL parameters or NONE. Mixing var and explicit types is not allowed.

What are the new String methods?

JAVA
package com.bank.util;

public class Java11StringDemo {
    public static void main(String[] args) {
        // isBlank(): true for empty or whitespace-only
        System.out.println("".isBlank());       // true
        System.out.println("   ".isBlank());    // true
        System.out.println("Rajesh".isBlank()); // false

        // strip(): Unicode-aware trim
        String padded = "  Rajesh  ";
        System.out.println(padded.strip());          // "Rajesh"
        System.out.println(padded.stripLeading());   // "Rajesh  "
        System.out.println(padded.stripTrailing());  // "  Rajesh"

        // repeat(n): duplicate string
        System.out.println("=-".repeat(20));  // =-=-=-...

        // lines(): split into Stream<String>
        String multiline = "Line1\nLine2\nLine3";
        multiline.lines().forEach(System.out::println);
    }
}

How is strip() different from trim()?

DIAGRAM
| Method | Handles ASCII whitespace | Handles Unicode whitespace |
|--------|------------------------|---------------------------|
| `trim()` | Yes (chars <= 32) | No (misses `\u00A0` etc.) |
| `strip()` | Yes | Yes (uses Character.isWhitespace) |
JAVA
// Non-breaking space: trim misses it, strip catches it
String nbs = "\u00A0Rajesh\u00A0";
System.out.println(nbs.trim().length());   // 8 (still has \u00A0)
System.out.println(nbs.strip().length());  // 6 (\u00A0 removed)

What is Predicate.not()?

JAVA
import java.util.function.Predicate;

// Before Java 11: lambda negation
lines.stream().filter(s -> !s.isBlank()).toList();

// Java 11: Predicate.not()
lines.stream().filter(Predicate.not(String::isBlank)).toList();

Predicate.not() is cleaner than .negate() which requires casting: ((Predicate) String::isBlank).negate().

What about Files.readString and writeString?

JAVA
import java.nio.file.*;

// One-liner file read
String content = Files.readString(Path.of("statement.txt"));

// One-liner file write
Files.writeString(Path.of("output.txt"), content);

These replace the verbose pattern of new String(Files.readAllBytes(path)) and BufferedWriter boilerplate.

What are common mistakes?

WRONG: Using trim() when Unicode whitespace is possible.

WRONG: Using isEmpty() when whitespace-only strings should be caught.

What should a senior engineer know?

  • Java 11 also removed Java EE modules (JAXB, JAX-WS) which is the main migration pain from Java 8.
  • Files.readString loads the entire file into memory. For large files, use Files.lines(path) which is lazy and must be closed in a try-with-resources.
  • Collection.toArray(IntFunction) was added: list.toArray(String[]::new) produces a typed array.
  • The Epsilon GC (no-op collector) was added for performance testing and ultra-short-lived applications.

Visual Overview

DIAGRAM
  Java 11 Key Features
  ┌────────────────────────────────────────────┐
  │ var in lambdas     (@NonNull var x) -> ... │
  │ String methods     isBlank, strip, repeat  │
  │ Predicate.not()    filter(not(isBlank))     │
  │ HttpClient         HTTP/2, sync + async    │
  │ Files.readString   one-liner file I/O      │
  │ Single-file run    java File.java          │
  │ Epsilon GC         no-op collector         │
  └────────────────────────────────────────────┘

Key Takeaway

Java 11 introduced developer-friendly features that reduce boilerplate. The var keyword in lambda parameters enables annotations like @NonNull on lambda params. New String methods: isBlank() catches whitespace-only strings that isEmpty() misses, strip() is a Unicode-aware replacement for trim(), and lines() splits into a Stream. Predicate.not() provides clean negation for method references.

The standardized HttpClient replaces HttpURLConnection with HTTP/2, async operations, and a clean builder API. Files.readString/writeString reduce file I/O to one-liners.

At National Bank of India, parsing settlement files becomes Files.readString(path).lines().filter(Predicate.not(String::isBlank)).map(String::strip).toList() in one fluent pipeline.

Remember one sentence: Java 11 is the LTS that gave us isBlank, strip, Predicate.not, HttpClient, and Files.readString as everyday productivity improvements.

---

Share
Use ← → arrow keys to navigate