Java 11: The New LTS Baseline
Java 11 became the new long-term support baseline, replacing Java 8 for most enterprises. It standardised the HTTP Client, added powerful String and Files utility methods, allowed var in lambda parameters, and introduced the experimental Z Garbage Collector.
Java Evolution — Part 4
Java 11, released on September 25, 2018, is the first LTS release under the new cadence. For many organisations, it replaced Java 8 as the production baseline. It delivered a standardised HTTP Client, a suite of new String and Files methods, var in lambda parameters, and the experimental Z Garbage Collector.
The Standardised HTTP Client
The HttpClient API, previewed in Java 9 as an incubating module, was finalised in Java 11 (JEP 321) and moved to java.net.http. It is a modern, fluent API that supports:
- HTTP/1.1 and HTTP/2
- Synchronous and asynchronous request/response
- WebSockets
- Request body publishers and response body handlers
Synchronous GET Request
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.example.com/users/1")) .header("Accept", "application/json") .GET() .build();
HttpResponse<String> response = client.send( request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode()); // 200System.out.println(response.body()); // JSON stringAsynchronous POST Request
String json = """ {"name": "Alice", "email": "alice@example.com"} """;
HttpClient client = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(10)) .build();
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.example.com/users")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(json)) .build();
CompletableFuture<HttpResponse<String>> future = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
future.thenAccept(response -> { System.out.println("Status: " + response.statusCode()); System.out.println("Body: " + response.body());});HTTP/2 and Connection Reuse
The HttpClient is designed to be reused across multiple requests. It maintains a connection pool and automatically upgrades to HTTP/2 when the server supports it.
// Create once, reuse many timesHttpClient client = HttpClient.newBuilder() .version(HttpClient.Version.HTTP_2) .followRedirects(HttpClient.Redirect.NORMAL) .authenticator(Authenticator.getDefault()) .build();New String Methods
Java 11 added several highly useful methods to String:
// isBlank() — true if empty or contains only whitespace"".isBlank(); // true" ".isBlank(); // true"hello".isBlank(); // false
// strip() — Unicode-aware whitespace removal (unlike trim())" hello ".strip(); // "hello"" hello ".stripLeading(); // "hello "" hello ".stripTrailing();// " hello"
// lines() — splits on line terminators, returns a Stream<String>"line1\nline2\nline3".lines() .forEach(System.out::println);// line1// line2// line3
// repeat(n) — repeats the string n times"ha".repeat(3); // "hahaha""-".repeat(40); // "----------------------------------------"The distinction between strip() and trim() matters for internationalised applications. trim() only removes ASCII whitespace (characters ≤ \u0020), while strip() uses Unicode’s definition of whitespace, correctly handling characters like the non-breaking space (\u00A0) and various Unicode space characters.
New Files Methods
Java 11 added convenience methods to java.nio.file.Files for reading and writing strings directly:
// Write a string to a filePath path = Files.writeString( Path.of("/tmp/hello.txt"), "Hello, Java 11!", StandardCharsets.UTF_8);
// Read a file as a stringString content = Files.readString(path, StandardCharsets.UTF_8);System.out.println(content); // Hello, Java 11!Before Java 11, reading a file into a string required either Files.readAllBytes() + new String(...) or a third-party library. These methods make the common case trivial.
var in Lambda Parameters
Java 11 extended var (introduced in Java 10) to work in lambda parameter lists. This may seem redundant — lambda parameters already have inferred types — but it enables one important use case: annotating lambda parameters.
// Without var — cannot annotate lambda parameterslist.stream() .map(s -> s.toUpperCase()) .collect(Collectors.toList());
// With var — annotations are now possiblelist.stream() .map((@NonNull var s) -> s.toUpperCase()) .collect(Collectors.toList());You must use var for all parameters or none — mixing var and explicit types, or var and unannotated parameters, is not allowed.
Running Java Files Directly
Java 11 allows you to run a single-file Java program without compiling it first:
# Before Java 11javac HelloWorld.javajava HelloWorld
# Java 11 — compile and run in one stepjava HelloWorld.javaThe source file is compiled in memory and executed immediately. This is particularly useful for scripts, quick experiments, and teaching. The file must contain a single public class with a main method.
The Z Garbage Collector (Experimental)
Java 11 introduced ZGC (JEP 333) as an experimental garbage collector. ZGC is designed for applications that require:
- Very low pause times — pauses are consistently under 10ms, regardless of heap size
- Large heaps — scales from a few hundred megabytes to multi-terabyte heaps
- Concurrent operation — most GC work happens concurrently with the application
# Enable ZGCjava -XX:+UseZGC -Xmx16g MyApplicationZGC achieves low pauses by doing almost all its work concurrently with the running application. It uses coloured pointers and load barriers to track object references and perform relocation without stopping the world for more than a millisecond.
ZGC became production-ready in Java 15. We will revisit it there.
Removed and Deprecated Features
Java 11 also cleaned house:
- Java EE and CORBA modules removed —
javax.xml.bind(JAXB),javax.activation,javax.transaction,javax.xml.ws(JAX-WS), and CORBA were removed from the JDK. Projects using these must now add them as explicit Maven/Gradle dependencies. - Nashorn JavaScript Engine deprecated — The Nashorn engine (introduced in Java 8) was deprecated and later removed in Java 15.
- JavaFX removed — JavaFX was decoupled from the JDK and is now distributed separately via OpenJFX.
Summary
Java 11 is the LTS release that most organisations adopted as their Java 8 replacement. Its practical improvements — the HTTP Client, new String/Files methods, and direct file execution — made everyday Java development noticeably more pleasant.
| Feature | Key Benefit |
|---|---|
| Standardised HTTP Client | Modern HTTP/1.1 and HTTP/2 support built into the JDK |
| New String Methods | isBlank, strip, lines, repeat — common string operations |
| New Files Methods | readString and writeString — trivial file I/O |
var in Lambda Parameters | Enables annotations on lambda parameters |
| Run Source Files Directly | java MyScript.java — no explicit compilation step |
| ZGC (Experimental) | Sub-10ms GC pauses at any heap size |