New features in Java 11
Posted By : Simran Ahuja | 30-Jun-2021
Oracle has released version Java 11 in September 2018, only 6 months after its precursor, version 10.
Oracle has also stopped supporting Java 8 since January 2019. As a consequence, a lot of us will now have to upgrade java version to Java 11.
New String Methods
Java 11 has added some new methods to the String class: isBlank, strip, stripLeading, stripTrailing, repeat and lines .
Let's check how we can use the following new methods to extract non-blank, stripped lines from a multiline string:
String multiString = "Baeldung \n \n developers \n explore Java.";
List<String> lines = multiString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());
assertThat(lines).containsExactly("Baeldung ", "developers", "explore Java.");
In the case of the strip methods it provide exactly functionality to the more familiar trim method with finer control and Unicode support.
New File Methods
It's now much more easier to read and write Strings from a file.
We can use the new methods readString and writeString static from the Files class to write and read:
Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text");
String fileContent = Files.readString(filePath);
assertThat(fileContent).isEqualTo("Sample text");
Collection to an Array
The java.util.Collection interface contains a new default called toArray method which takes an IntFunction as an argument.
This makes it easier to create an array of the correct type from a collection:
List sampleList = Arrays.asList("Java", "Kotlin");
String[] sampleArray = sampleList.toArray(String[]::new);
assertThat(sampleArray).containsExactly("Java", "Kotlin");
The Not Predicate Method
A static not method has also been added to the interface Predicate. We can also use it to negate an already existing predicate, much like the negate method:
List<String> sampleList = Arrays.asList("Java", "\n \n", "Kottlin", " ");
List withoutBlanks = sampleList.stream()
.filter(Predicate.not(String::isBlank))
.collect(Collectors.toList());
assertThat(withoutBlanks).containsExactly("Java", "Kottlin");
Local-Variable Syntax for Lambda
In Java 11 the Support for using the local variable syntax (var keyword) in lambda parameters was added.
We can use such feature to apply modifiers to our local variables, like defining a type annotation:
List<String> sampleList = Arrays.asList("Java", "Kottlin");
String resultString = sampleList.stream()
.map((@Nonnull var x) -> x.toUpperCase())
.collect(Collectors.joining(", "));
assertThat(resultString).isEqualTo("JAVA, KOTTLIN");
HTTP Client
The new HTTP client from the java.net.http package was launched in version Java 9. It has now become one of a prominent feature in Java 11.
The new HTTP API improves overall performance and efficiency of the code and provides support for both HTTP/1.1 and HTTP/2:
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(20))
.build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:" + port))
.build();
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
assertThat(httpResponse.body()).isEqualTo("Hello from the server side!");
Running Java Files
A major change in this version is that we now don'thave to further need to compile the Java source files with javac explicitly anymore:
$ javac HelloWorldd.java
$ java HelloWorldd
Hello Java 11!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Simran Ahuja
Simran is a bright Java Developer,with good knowledge of core java and advanced java, always ready to face challenges and explore new places.