Java 11 is a major Long-Term Support (LTS) release that introduced the modern HTTP Client API, Flight Recorder for performance monitoring, and experimental garbage collectors. It marked a significant milestone in Java's evolution with enhanced string manipulation, improved collection APIs, and better developer productivity features.
September 2018
Java 11 is a major Long-Term Support (LTS) release that introduced the modern HTTP Client API, Flight Recorder for performance monitoring, and experimental garbage collectors. It marked a significant milestone in Java's evolution with enhanced string manipulation, improved collection APIs, and better developer productivity features.
Java 11 introduces a modern, fully-featured HTTP client that supports HTTP/2 and WebSocket protocols. This new API provides both synchronous and asynchronous request capabilities, making it easier to build web applications and microservices with improved performance and modern web standards.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
// Create HTTP client
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
// Create HTTP request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/users"))
.timeout(Duration.ofSeconds(5))
.header("Accept", "application/json")
.GET()
.build();
// Send request synchronously
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status: " + response.statusCode());
System.out.println("Body: " + response.body());
// Send request asynchronously
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();Java 11 enhances the String API with new utility methods for better string manipulation and processing. These additions include Unicode-aware whitespace handling, string repetition, line processing, and improved null checking with Optional.isEmpty().
// New String methods in Java 11
String text = " Hello World ";
// Check if string is blank (empty or whitespace only)
boolean isBlank = text.isBlank(); // false
boolean isBlankEmpty = " ".isBlank(); // true
// Strip whitespace (Unicode-aware)
String stripped = text.strip(); // "Hello World"
String leftStripped = text.stripLeading(); // "Hello World "
String rightStripped = text.stripTrailing(); // " Hello World"
// Repeat string
String repeated = "Java ".repeat(3); // "Java Java Java "
// Process lines
String multiline = "Line1\nLine2\nLine3";
multiline.lines()
.map(String::toUpperCase)
.forEach(System.out::println);
// Optional isEmpty()
Optional<String> optional = Optional.empty();
if (optional.isEmpty()) {
System.out.println("Optional is empty");
}Java 11 introduces enhanced collection factory methods and improvements to existing collection APIs. These changes provide better performance, more convenient collection operations, and improved type safety when converting collections to arrays.
// Collection.toArray() with IntFunction parameter
List<String> list = List.of("A", "B", "C");
String[] array = list.toArray(String[]::new);
// Collection.toArray() without parameter
Object[] objArray = list.toArray();Java 11 introduces the ability to use the var keyword in lambda parameters, enabling type inference for lambda expressions. This feature provides more flexibility in lambda parameter declarations and improves code readability while maintaining type safety.
// Traditional lambda with explicit types
BinaryOperator<Integer> add = (Integer a, Integer b) -> a + b;
// Lambda with var keyword (Java 11+)
BinaryOperator<Integer> addVar = (var a, var b) -> a + b;Java 11 introduces Flight Recorder as a production-ready profiling and monitoring tool. This feature provides low-overhead performance monitoring capabilities, allowing developers to collect detailed runtime information about JVM and application performance without significant performance impact.
// Flight Recorder is primarily used via JVM flags and tools
// Start application with Flight Recorder enabled
// java -XX:+FlightRecorder -XX:StartFlightRecording=duration=60s,filename=recording.jfr MyApp
// Using Flight Recorder programmatically
import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingFile;
public class FlightRecorderExample {
public static void main(String[] args) throws Exception {
// Start a recording
Recording recording = new Recording();
recording.start();
// Your application code here
performWork();
// Stop recording and save to file
recording.stop();
recording.dump(Paths.get("recording.jfr"));
recording.close();
// Read recording file
try (RecordingFile recordingFile = new RecordingFile(Paths.get("recording.jfr"))) {
while (recordingFile.hasMoreEvents()) {
RecordedEvent event = recordingFile.readEvent();
System.out.println("Event: " + event.getEventType().getName());
}
}
}
private static void performWork() {
// Simulate some work
for (int i = 0; i < 1000000; i++) {
Math.sqrt(i);
}
}
}