Sunday, February 12, 2023

Java 11 features

 Java 11 is a long-term support (LTS) version of the Java SE platform, which was released on September 25, 2018. It includes several new features, improvements, and bug fixes over the previous version of Java. In this article, we will take a closer look at some of the most important new features in Java 11.

  1. Local-Variable Syntax for Lambda Parameters

Java 11 introduced a new syntax for declaring variables in a more concise way, known as local-variable syntax for lambda parameters. Prior to Java 11, lambda parameters were required to be declared as final and had to be declared as separate variables. With this new syntax, lambda parameters can be declared directly in the parameter list without the need for separate variables.

Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); numbers.forEach((var number) -> System.out.println(number));
  1. HTTP Client

Java 11 introduced a new HTTP client in the standard library, which provides an easy-to-use and asynchronous API for sending HTTP requests. The new HTTP client supports both HTTP/1.1 and HTTP/2 protocols and offers improved performance, security, and ease of use over the previous HttpURLConnection API.

Example:

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

Java 11 introduced a new representation for strings called compact strings. Compact strings are a memory-efficient representation of strings that use one byte per character for characters in the ASCII range and two bytes per character for other characters. This new representation provides improved memory usage and performance compared to the previous representation.

Example:

String str = "Hello, World!"; System.out.println(str.length()); System.out.println(str.charAt(0));
  1. Launch Single-File Source-Code Programs

Java 11 introduced a new feature that allows you to launch single-file source-code programs directly from the command line, without the need to compile the code first. This makes it easier and faster to write, run, and share small Java programs.

Example:

java HelloWorld.java
  1. Improved Unicode Support

Java 11 introduced improved Unicode support, with support for Unicode 10 and improved handling of supplementary characters. This makes it easier to work with text in different languages and character sets.

Example:

String str = "Hello, 世界!"; System.out.println(str);

These are just a few examples of the new features in Java 11. With its improved performance, security, and ease of use, Java 11 is an important release for developers and a significant step forward for the Java SE platform.

No comments:

Post a Comment

Concurrent Hashmap in java

  A ConcurrentHashMap is a thread-safe implementation of the Map interface in Java. Unlike a HashMap , which is not thread-safe and can ca...