Java 11 introduced several features and enhancements. Here's a list of some notable features along with examples:
Local-Variable Syntax for Lambda Parameters:
- In Java 11, you can use
var
in lambda expressions.
- In Java 11, you can use
// Before Java 11
BiFunction<Integer, Integer, Integer> add = (Integer a, Integer b) -> a + b;
// With Java 11
BiFunction<Integer, Integer, Integer> add = (var a, var b) -> a + b;
HTTP Client Updates:
- A modern HTTP Client API was introduced.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
public class HttpClientExample {
public static void main(String[] args) {
// Create an instance of the HTTP client
HttpClient httpClient = HttpClient.newHttpClient();
// Define the URI for the GET request
URI uri = URI.create("https://jsonplaceholder.typicode.com/posts/1");
// Build the GET request
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
// Send the GET request asynchronously and handle the response
CompletableFuture<HttpResponse<String>> responseFuture = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
// Use CompletableFuture to handle the response when it is available
responseFuture.thenAccept(response -> {
// Check if the request was successful (HTTP status code 200)
if (response.statusCode() == 200) {
// Print the response body
System.out.println("Response Body:");
System.out.println(response.body());
} else {
System.out.println("Request failed with status code: " + response.statusCode());
}
}).join(); // Wait for the CompletableFuture to complete
}
}
String Methods:
- New methods were added to the
String
class.
- New methods were added to the
String str = " Hello, Java 11! ";
System.out.println(str.isBlank()); // Output: false
System.out.println(str.strip()); // Output: Hello, Java 11!
System.out.println(str.lines().count()); // Output: 1
Nashorn JavaScript Engine Deprecated:
- The Nashorn JavaScript Engine was deprecated.
Epsilon: A No-Op Garbage Collector:
- The Epsilon garbage collector is a no-op garbage collector, useful for performance testing and memory-limited environments.
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
Launch Single-File Source-Code Programs:
- You can launch a single-file Java source-code program directly, without compiling it explicitly.
// Before Java 11
// javac HelloWorld.java
// java HelloWorld
// With Java 11
// java HelloWorld.java
ZGC: A Scalable Low-Latency Garbage Collector:
- The Z Garbage Collector (ZGC) was introduced as an experimental feature for low-latency applications.
-XX:+UnlockExperimentalVMOptions -XX:+UseZGC
Flight Recorder:
- Java Flight Recorder (JFR) was open-sourced, allowing developers to use it without a commercial license.
// Enable Flight Recorder
-XX:StartFlightRecording
TLS 1.3:
- Java 11 supports TLS 1.3 for secure communication.
// Enable TLS 1.3
-Djdk.tls.client.protocols=TLSv1.3
These are some of the notable features introduced in Java 11. Keep in mind that this is not an exhaustive list, and Java continues to evolve with each new release.