Unlock Java 11: Powerful New Features You Need to Know

This article introduces Java 11's most useful enhancements—including the var keyword, new String utilities, collection factory methods, Stream API additions, InputStream.transferTo, and the revamped HttpClient—showing how each feature simplifies code and improves productivity for modern Java developers.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Unlock Java 11: Powerful New Features You Need to Know

Many developers are still mastering Java 8, but Java 11 brings several handy new features that can simplify code and boost productivity.

var keyword

String convenience methods

Collection factory methods

Stream API enhancements

InputStream.transferTo

HttpClient API

var keyword

Java 10 introduced the var keyword, allowing the compiler to infer the type of local variables declared inside methods.

Before Java 10: String text = "Hello Java 9"; With var: var text = "Hello Java 10"; Note that var does not make the variable dynamically typed; the inferred type is still static and cannot be changed. The following examples will not compile:

var text = "Hello Java 11";
text = 23; // compilation error

var a; // missing initializer, compilation error
var nothing = null; // ambiguous type, compilation error
var lambda = () -> System.out.println("Pity!"); // ambiguous target type, compilation error
var method = this::someMethod; // ambiguous, compilation error

The main benefit of var is reduced boilerplate, for example:

var myList = new ArrayList<Map<String, List<Integer>>>();
for (var current : myList) {
    System.out.println(current);
}
// current's type is Map<String, List<Integer>>

String convenience methods

" ".isBlank();                     // true
" Foo Bar ".strip();               // "Foo Bar"
" Foo Bar ".stripTrailing();      // " Foo Bar"
" Foo Bar ".stripLeading();       // "Foo Bar "
"Java".repeat(3);                // "JavaJavaJava"
"A
B
C".lines().count();       // 3

Collection convenience methods

Factory methods for immutable collections and copy utilities simplify list, set, and map creation.

var list = List.of("A", "B", "C");
var copy = List.copyOf(list);
System.out.println(list == copy); // true (same immutable instance)

var mutableList = new ArrayList<String>();
var mutableCopy = List.copyOf(mutableList);
System.out.println(mutableList == mutableCopy); // false (different instances)

var map = Map.of("A", 1, "B", 2);
System.out.println(map); // {A=1, B=2}

Stream API enhancements

Java 11 adds three new Stream methods: ofNullable, dropWhile, and takeWhile.

Stream.ofNullable(null).count(); // 0
Stream.of(1,2,3,2,1).dropWhile(n -> n < 3).toList(); // [3,2,1]
Stream.of(1,2,3,2,1).takeWhile(n -> n < 3).toList(); // [1,2]

InputStream.transferTo

The new transferTo method copies all bytes from an InputStream directly to an OutputStream:

var classLoader = ClassLoader.getSystemClassLoader();
var inputStream = classLoader.getResourceAsStream("myFile.txt");
var tempFile = File.createTempFile("myFileCopy", "txt");
try (var outputStream = new FileOutputStream(tempFile)) {
    inputStream.transferTo(outputStream);
}

HttpClient API

Originally introduced in Java 9, the HttpClient became standard in Java 11, supporting both synchronous and asynchronous requests.

Synchronous GET request example:

var request = HttpRequest.newBuilder()
    .uri(URI.create("https://winterbe.com"))
    .GET()
    .build();
var client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Asynchronous GET request example:

var request = HttpRequest.newBuilder()
    .uri(URI.create("https://winterbe.com"))
    .GET()
    .build();
var client = HttpClient.newHttpClient();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
      .thenApply(HttpResponse::body)
      .thenAccept(System.out::println);

POST request example:

var request = HttpRequest.newBuilder()
    .uri(URI.create("https://postman-echo.com/post"))
    .header("Content-Type", "text/plain")
    .POST(HttpRequest.BodyPublishers.ofString("Hi there!"))
    .build();
var client = HttpClient.newHttpClient();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode()); // 200

Basic authentication example:

var request = HttpRequest.newBuilder()
    .uri(URI.create("https://postman-echo.com/basic-auth"))
    .build();
var client = HttpClient.newBuilder()
    .authenticator(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("postman", "password".toCharArray());
        }
    })
    .build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode()); // 200

Conclusion

The article compiles and translates key Java 11 features—var, String methods, collection factories, Stream enhancements, InputStream.transferTo, and the new HttpClient—providing concise examples that demonstrate their practical use.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CollectionsStreamsHttpClientJava 11InputStreamString Methodsvar keyword
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.