Java 11 Features

1. πŸ”ΉΒ New HttpClient API (Standard)

Upgraded from incubator in Java 9 β†’ Now stable and production-ready.

Example:

import java.net.http.*;
import java.net.URI;

public class HttpExample {
    public static void main(String[] args) {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://jsonplaceholder.typicode.com/posts/1"))
            .build();

        client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
              .thenApply(HttpResponse::body)
              .thenAccept(System.out::println)
              .join();
    }
}

2. πŸ”Ή Local Variable Syntax for Lambda Parameters

Now you can use var in lambda parameters, useful for annotations or readability.

Example:

List<String> names = List.of("Java", "Python", "Go");
names.forEach((var name) -> System.out.println(name));

3. πŸ”Ή String API Enhancements

New useful methods added to the String class:

String str = " Java ";
System.out.println(str.isBlank());              // true
System.out.println("Hello\nWorld".lines().count()); // 2
System.out.println(str.strip());                // "Java" (trims Unicode)
System.out.println(str.repeat(3));              // " Java  Java  Java "

4. πŸ”Ή Files.readString() and Files.writeString()

Simplifies reading/writing text files.

Example:

import java.nio.file.*;

public class FileReadWrite {
    public static void main(String[] args) throws Exception {
        Path path = Path.of("example.txt");

        Files.writeString(path, "Hello, Java 11!");
        String content = Files.readString(path);
        System.out.println(content);
    }
}

5. πŸ”Ή Collection.toArray(IntFunction)

New way to convert collections to arrays:

Example:

List<String> list = List.of("A", "B", "C");
String[] array = list.toArray(String[]::new); // No need for size hint

6. πŸ”Ή Optional.isEmpty() Method

Adds a convenient method to check if an Optional is empty.

Example:

Optional<String> emptyOpt = Optional.empty();
System.out.println(emptyOpt.isEmpty()); // true

7. πŸ”Ή Launch Single File Programs Without Compilation

You can now run .java files directly without compiling them manually:

java HelloWorld.java

This is great for scripting or small utilities.


8. πŸ”Ή Z Garbage Collector (Experimental)

ZGC is a low-latency, scalable garbage collector that works well with very large heaps (100GB+). It’s designed to have sub-millisecond pause times.


9. πŸ”Ή Epsilon GC (No-Op GC)

A do-nothing GC that only allocates memory and never reclaims it. Useful for performance testing without GC overhead.


10. πŸ”Ή Removed Java EE and CORBA Modules

Java 11 removed the following deprecated modules:

  • java.xml.ws, java.activation, java.xml.bind, java.corba, etc.

Use external libraries if you still need these.


πŸ”š Summary Table

Feature Description
HttpClient API Standardized, modern HTTP client
Lambda var var keyword allowed in lambdas
String methods isBlank(), lines(), repeat(), etc.
File methods readString(), writeString()
Optional isEmpty() for cleaner code
Collection.toArray() Cleaner syntax for converting to arrays
Run .java directly One-liner Java script execution
ZGC / Epsilon GC Experimental GCs for special use cases
Removed APIs Cleaned up deprecated EE/CORBA modules


Back to blog

Leave a comment

Please note, comments need to be approved before they are published.