- New methods to the String class: isBlank, lines, strip, stripLeading, stripTrailing, and repeat.
- readString and writeString static methods from the Files class
Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text"); String fileContent = Files.readString(filePath); assertThat(fileContent).isEqualTo("Sample text");
- java.util.Collection interface contains a new default toArray method
List sampleList = Arrays.asList("Java", "Kotlin"); String[] sampleArray = sampleList.toArray(String[]::new); assertThat(sampleArray).containsExactly("Java", "Kotlin");
- A static not method has been added to the Predicate interface. Predicate.not(String::isBlank)
- The new HTTP client from the java.net.http package was introduced in Java 9. It has now become a standard feature in Java 11.The new HTTP API improves overall performance 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!");
- A major change in this version is that we don’t need to compile the Java source files with javac explicitly anymore:
Before Java 11Version$ javac HelloWorld.java $ java HelloWorld Hello Java 8!
New Version
$ java HelloWorld.java Hello Java 11!