Skip to content

Java 11

Neelu edited this page Jul 26, 2026 · 1 revision

☕ Java 11: The Modern LTS Baseline

Release Date: September 25, 2018 | Type: LTS | Status: ✅ Active (widely deployed in production)


📋 Table of Contents

  1. Overview
  2. Core Features
  3. Best Practices
  4. Migration Notes

🎯 Overview

Java 11 is the first LTS after Java 8 and, for many companies, the version they finally migrated to from Java 8. It ships a standard HTTP Client, useful String/Files methods, and removes several Java EE modules that used to ship with the JDK by default.

flowchart TD
    J8["Java 8 LTS\n2014"] -->|"4 years, most\nenterprise apps stayed here"| J11["Java 11 LTS\n2018"]
    J11 --> A["New HTTP Client"]
    J11 --> B["String/Files QoL methods"]
    J11 --> C["var in lambdas"]
    J11 --> D["Java EE modules removed"]
Loading

🚀 Core Features

1️⃣ Standard HTTP Client (java.net.http)

What: A modern, fluent HTTP/2-capable client — finally replacing HttpURLConnection for real-world use.

HttpClient client = HttpClient.newBuilder()
    .version(HttpClient.Version.HTTP_2)
    .connectTimeout(Duration.ofSeconds(10))
    .build();

// Synchronous
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.example.com/users"))
    .header("Accept", "application/json")
    .GET()
    .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());

// Asynchronous
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
      .thenApply(HttpResponse::body)
      .thenAccept(System.out::println)
      .join();
sequenceDiagram
    participant App
    participant HttpClient
    participant Server
    App->>HttpClient: sendAsync(request)
    HttpClient->>Server: HTTP/2 request
    Server-->>HttpClient: response
    HttpClient-->>App: CompletableFuture&lt;HttpResponse&gt;
    App->>App: thenApply / thenAccept
Loading

2️⃣ New String Methods

"  hello  ".isBlank();          // false
"   ".isBlank();                 // true

"line1\nline2\nline3".lines()   // Stream<String>
    .forEach(System.out::println);

"ab".repeat(3);                  // "ababab"

"  padded  ".strip();            // "padded" (Unicode-aware, unlike trim())
"  padded  ".stripLeading();     // "padded  "
"  padded  ".stripTrailing();    // "  padded"

strip() vs trim(): trim() only removes characters <= U+0020. strip() uses Character.isWhitespace(), correctly handling Unicode whitespace.


3️⃣ Files.readString() / Files.writeString()

// Before: multiple lines with streams/readers
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

// Java 11
String content = Files.readString(Path.of("data.txt"));
Files.writeString(Path.of("output.txt"), "Hello, Java 11!");

4️⃣ var in Lambda Parameters

// Allows applying annotations to lambda parameters, which was previously impossible
list.stream()
    .map((@NonNull var x) -> x.trim())
    .collect(Collectors.toList());

// Mostly used for consistency, since annotations are the real motivator
BiFunction<Integer, Integer, Integer> add = (var a, var b) -> a + b;

5️⃣ Running Single-File Source Programs Directly

# No need to compile first!
java HelloWorld.java

# Even works with shebang scripts on Unix
#!/usr/bin/java --source 11
public class Script {
    public static void main(String[] args) {
        System.out.println("Run directly!");
    }
}

6️⃣ Flight Recorder (JFR) — now open source

# Built into the JDK, low-overhead production profiling
java -XX:StartFlightRecording=duration=60s,filename=recording.jfr -jar app.jar

💡 Best Practices

✅ Migrate from HttpURLConnection/Apache HttpClient to java.net.http.HttpClient for new code
✅ Use Files.readString()/writeString() for small text files instead of stream boilerplate
✅ Use .strip() instead of .trim() going forward — better Unicode correctness
❌ Don't assume Java EE libraries (JAXB, JAX-WS, etc.) are still on the classpath — add them explicitly as dependencies


🔄 Migration Notes (Java 8 → 11)

Removed / Changed What to do
java.xml.bind (JAXB) Add jakarta.xml.bind:jakarta.xml.bind-api + an impl (e.g., org.glassfish.jaxb)
java.xml.ws (JAX-WS) Add com.sun.xml.ws:rt or migrate off SOAP
java.corba Rare in modern apps; migrate off CORBA
java.se.ee aggregator module No longer implicitly available
-XX:+UseConcMarkSweepGC deprecated Move to G1 (default) or ZGC
Nashorn JS engine Deprecated (removed in 15) — use GraalVM if you need embedded JS
flowchart LR
    A[Java 8 App] -->|Add explicit deps for\nremoved EE modules| B[Compiles on 11]
    B -->|Run full test suite| C[Validate on 11]
    C -->|Update CI/CD JDK| D[Deployed on 11]
Loading

Clone this wiki locally