Notable features of Java 11 to 17
Published in
6 min readJun 20, 2023
Here are some notable features introduced in Java versions 11 to 17, along with example code:
Java 11:
- Local-Variable Syntax for Lambda Parameters:
- In Java 11, the ‘var’ keyword was introduced to allow the use of type inference for lambda parameters.
- The example demonstrates how you can use the ‘var’ keyword to declare lambda parameters without explicitly specifying the type.
// Before Java 11
Function<Integer, String> oldLambda = (Integer x) -> "The number is: " + x;
// With Java 11
Function<Integer, String> newLambda = (var x) -> "The number is: " + x;
2. HTTP Client (Standard):
- Java 11 introduced a new built-in HTTP client API that provides a more modern and flexible way to interact with HTTP services.
- The example demonstrates how to create an HTTP client, send a GET request to a specified URI, and retrieve the response body.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/users"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());