Overview of New Features in Java 11

Overview of New Features in Java 11

Java 11 introduced several new features and enhancements that improve the language's functionality, performance, and ease of use. This article summarizes the key features that developers should be aware of.

Key Features of Java 11

1. New String Methods

  • strip(): Removes leading and trailing whitespace.
  • repeat(int): Repeats a string a specified number of times.

lines(): Returns a stream of lines from a string.

String multiline = "Line 1\nLine 2";
multiline.lines().forEach(System.out::println);

isBlank(): Checks if a string is empty or contains only whitespace.

String str = "   ";
System.out.println(str.isBlank()); // true

2. Local-Variable Syntax for Lambda Parameters

You can use var for lambda parameters.

var list = List.of("a", "b", "c");
list.forEach((var item) -> System.out.println(item));

3. New File Methods

  • Files.readString(Path): Reads the content of a file as a string.
  • Files.writeString(Path, String): Writes a string to a file.

4. HTTP Client

A new standard HTTP client that supports HTTP/2 and WebSocket.

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://example.com"))
        .build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
      .thenApply(HttpResponse::body)
      .thenAccept(System.out::println);

5. Removal of Deprecated Features

  • Java 11 removed several deprecated features, including:
  • Java EE and CORBA modules.
  • Applet API.

6. Flight Recorder

  • A profiling tool integrated into the JVM that collects data about the application at runtime, helping to analyze performance issues.

7. Improvements in Garbage Collection

  • Introduction of the Z Garbage Collector (ZGC) for low-latency applications, which can handle large heaps with minimal pause times.

Conclusion

Java 11 brings significant improvements and new features that enhance developers' productivity and application performance. These features aim to make Java more modern and efficient while maintaining backward compatibility with previous versions.